Easy Ways to Set NGINX for Multiple Domains

Often we find the need to run several websites on one server. This need can be met by setting the Directory Structure and Server Block on NGINX, or better known as Virtual Host on Apache.

This article will explain how easy it is to setup NGINX for multiple domains. In this explanation, we will create Server Blocks for web1.com and web2.com. In practice, you can change web1.com and web2.com according to your actual domain. Here are the steps:

1. Create Directory and Set Permissions:

– Create a directory under the /var/www/ directory for our two domains above:

$ sudo mkdir -p /var/www/web1.com/public_html
$ sudo mkdir -p /var/www/web2.com/public_html

– If we want non-root users to be able to change files under that directory, then we have to change the ownership of the directory:

$ sudo chown -R $(whoami):$(whoami) /var/www/web1.com/public_html
$ sudo chown -R $(whoami):$(whoami) /var/www/web2.com/public_html

Note: $(whoami) is a variable that displays the currently logged in user.

– Change the permissions of the /var/www directory to 755 :

$ sudo chmod -R 755 /var/www

2. Create a Web Page:

$ sudo echo “Welcome to web1.com!” > /var/www/web1.com/public_html/index.html
$ sudo echo “Welcome to web2.com!” > /var/www/web2.com/public_html/index.html

3. Create First Server Block:

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/web1.com
$ sudo nano /etc/nginx/sites-available/web1.com

Press CTRL + W and search for: default_server;

listen 80 default_server;
listen [::]:80 default_server;

Remove default_server to be like this:

listen 80;
listen [::]:80;

Press CTRL + W and search for: root /var/www/html;
Change this root path to the domain directory that was created earlier ( /var/www/web1.com/public_html ).

root /var/www/web1.com/public_html;

Press CTRL + W and search for: server_name _;
Adjust to your domain, in this example is web1.com and http://www.web1.com .

server_name web1.com www.web1.com;

Save and exit (press CTRL + X , press Y , and press ENTER ).

Make sure the NGINX configuration file is valid by:

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4. Create a Second Server Block:

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/web2.com
$ sudo nano /etc/nginx/sites-available/web2.com

Press CTRL + W and search for: default_server;

listen 80 default_server;
listen [::]:80 default_server;

Remove default_server to be like this:

listen 80;
listen [::]:80;

Press CTRL + W and search for: root /var/www/html;
Change this root path to the domain directory that was created earlier ( /var/www/web2.com/public_html ).

root /var/www/web2.com/public_html;

Press CTRL + W and search for: server_name _;
Adjust to your domain, in this example is web2.com and http://www.web2.com .

server_name web2.com www.web2.com;

Save and exit (press CTRL + X , press Y , and press ENTER ).

Make sure the NGINX configuration file is valid by:

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5. Create a Symbolic Link:

The next step is to create symbolic links from the sites-available directory to the sites-enabled directory, which NGINX will read at startup.

$ sudo ln -s /etc/nginx/sites-available/web1.com /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/web2.com /etc/nginx/sites- enabled/

– Restart NGINX
$ sudo service nginx restart

6. Edit /etc/hosts (Optional)

If you don’t have a registered domain, you can still try this tutorial by modifying the hosts file to test the configuration results. Here are the steps:

$ sudo nano /etc/hosts

Add the following two lines:

xxxx web1.com
xxxx web2.com

Replace xxxx with the IP of your web server .
Save (press CTRL + X , press Y, and press ENTER ).

7. Test NGINX

Open browser and call web1.com then go to web2.com

These are the steps that need to be taken to setup NGINX for multiple domains. Thank you for visiting, I hope this article is useful.

Leave a Reply

Your email address will not be published.