How to Add New Websites on Nginx Ubuntu 16.04 Server

Sunday, June 26th 2016. | how to

#howto ,#tutorial #VPS – How to Add New Websites on Nginx Ubuntu 16.04 Server – This tutorial explains step by step guide (with pics) about How to Add New Websites on Nginx Ubuntu 16.04 Server

Let’s say you have installed full LEMP stack (Linux, Nginx, MySQL/MariaDB, and PHP) but how can you add new websites or domains on it? The answer is by creating Virtual Hosts or Server Blocks. The main point is to create a Virtual Block file for each of your website / domain. It means you have to create another Server Block for another website you want to add. Going back to basic, an Nginx Server Block is configuration file holding all necessary configuration for websites you want it to be made accessible through and served by Nginx. Basically you can put all configurations of all websites in a single Server Block file but we believe it is better to have separate Server Block for each website.

Requirements

  1. A SSH client like Bitvise or Putty and basic knowledge about how to use it.
  2. Basic knowledge about common Unix command to SSH to your server.
  3. A server or VPS with at least 256MB of RAM (OpenVZ) but 512MB is recommended if you are using KVM or Xen.
  4. Ubuntu 16.04 either 32-bit or 64-bit.
  5. About 30 minutes of your time.
  6. a cup of coffee or tea.

Confused on choosing which VPS provider to setup an Apache based hosting server? Read my recommended VPS providers or this list of top low end cloud server providers. Quick recommendation: Digital Ocean, Atlantic.net or Ramnode.

In this guide I am using a 512MB RAM Cloud VPS from Atlantic running Ubuntu 16.04 x64.

Prerequisites

  1. Provision the Ubuntu server.
  2. Install full LAMP on it.
  3. Do not forget to always replace domain.tld with your own domain name and replace x.x.x.x with your server IP address.
  4. In this tutorial I simply login as root. However, it is advised to login as another user with sudo privilege. Read : Basic Ubuntu Setup to build a production server. Therefore, please add sudo in every and each command.

I’ve explained how to provision a cloud server on Atlantic.net and how to install Nginx, PHP7, and MySQL on my previous post.

Therefore, I assumed at this point you have an Ubuntu server with Nginx, PHP7, and MySQL installed. Let’s get going..

Objective

We will add two websites in the LEMP server: Servermom.net and Servermom.co.id, for example. You should replace those domain names with your own.

Do not have one? If you have a plan to register a domain name but you have not done it yet, simply configure / create the Server Block first and you can register it later. You can then test the configuration by modifying your local hosts file.

Create New Web Directory

Web directory is a directory / folder in your server of where all your website’s files (and folders) like scripts, images, etc., are stored / hosted. First, login to your server.

Step 1 – Login to your server via Terminal, Putty, Bitvise, etc.

Login to Ubuntu Server

Step 2 – Now create the directory. The command below creates two directories for two websites. Issue only once if you want to add only one website

mkdir -p /var/www/domain1.tld/html
mkdir -p /var/www/domain2.tld/html

in this example I created web directories for servermom.net and servermom.co.id

create nginx web directory

Step 3 – Now assign ownership of these directories to www-data

chown -R www-data:www-data /var/www/domain1.tld/html
chown -R www-data:www-data /var/www/domain2.tld/html

assign permission wwwdata

Step 4 – Now, recursively change directory permissions to 755.

chmod -R 755 /var/www

change permission 755

Step 5 – Now create a index.html test file.

For the first website (domain1.tld):

nano /var/www/domain1.tld/html/index.html

and put these text in there :

<html>
    <head>
        <title>Homepage for Servermom.net</title>
    </head>
    <body>
        <h1>Success!  The servermom.net Nginx server block is working!</h1>
    </body>
</html>

Save and exit the editor by pressing Control+O then Control+X.

First Server Block

For the first website (domain2.tld):

nano /var/www/domain2.tld/html/index.html

and put these text in there :

<html>
    <head>
        <title>Homepage for Servermom.co.id</title>
    </head>
    <body>
        <h1>Success!  The servermom.co.id Nginx server block is working!</h1>
    </body>
</html>

Save and exit the editor by pressing Control+O then Control+X.

Second Server Block

Create Server Blocks

Step 1 – Nginx server block files should be stored inside /etc/nginx/sites-available. Therefore, let’s create new ones there :

nano /etc/nginx/sites-available/domain1.tld

Create Server Block First Domain

and then put these configuration there :

server {
    listen x.x.x.x:80;
    listen [::]:80 ipv6only=on;
 
    root /var/www/domain1.tld/html;
    index index.php index.html index.htm;
 
    server_name domain1.tld;
 
    location / {
        try_files $uri $uri/ =404;
    }
 
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
 
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
 
    location ~ /.ht {
        deny all;
    }
}

Again, replace x.x.x.x with your own IPv4 and replace [::] with your own IPv6 address. This Nginx configuration activates Nginx dual-stack: IPv4 and IPv6. In the example below I deactivate IPv6 configuration line since Atlantic.net doesn’t have IPv6 yet.

nginx serverblock config

Save and exit the editor by pressing Control+O then Control+X. Now create for another website :

nano /etc/nginx/sites-available/domain2.tld

Put similar configuration in there :

server {
    listen x.x.x.x:80;
    listen [::]:80 ipv6only=on;
 
    root /var/www/domain2.tld/html;
    index index.php index.html index.htm index.nginx-debian.html;
 
    server_name domain2.tld;
 
    location / {
        try_files $uri $uri/ =404;
    }
 
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
 
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
 
    location ~ /.ht {
        deny all;
    }
}

Here’s the example :

nginx serverblock config 2

Again, do not forget to save and exit the editor.

editing nginx server block command

Step 2 – Now can enable each of Server Block by issuing this command :

ln -s /etc/nginx/sites-available/domain1.tld /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/domain2.tld /etc/nginx/sites-enabled/

enable server block

Step 3 – For security purpose, edit nginx.conf file and enable “server names hash bucket size” option by removing the comment symbol (#):

nano /etc/nginx/nginx.conf

enable server hash nginx

Press Control+O to save and Control+X to exit the editor.

Step 4 – Now test Nginx configuration and make sure you get the “Ok” message:

nginx -t

Step 5 – Finally, reload or restart Nginx service.

service nginx reload

Edit Ngin Conf

Step 6 – Now configure your domain’s DNS record or you can edit your local hosts file (in your computer) to test the configuration. I suggest you to use app like HostsMan for Windows 8, 8.1, or 10.

Edit Local Hosts File

Edit Local Hosts File Editor

Step 7 – Launch your favorite web browser up and try accessing your vps through your domain name :

try nginx config

Congratulation! Your Nginx Ubuntu server is now ready to host your websites. You can start uploading your website’s files / scripts. Coming up next: How to install WordPress on Nginx Ubuntu Server. Stay tuned or follow me on twitter to get faster update.

Please do not hesitate to drop comment below..

This post How to Add New Websites on Nginx Ubuntu 16.04 Server is part of ServerMom.

Source link