You can host multiple domains on a single server using the Nginx server block.
Since many domains are using the same server, the resource requirement for hosting the server will be reduced.
You have to create a separate server block for Nginx multiple domain configurations.
We will be doing the following tasks on the Nginx server to host multiple domains.
- Creating new root directories
- Creating sample pages for testing
- Creating server block for each domain
You should have a sudo non-root user configured according to this ubuntu 16.04 initial server setup guide.
Nginx installed on your Ubuntu 16.04 server.
Install LEMP stack Ubuntu 16.04 server (Follow this guide, if you want to use MySQL and PHP with Nginx ).
We are going to use two domains for the testing purpose. Those two domains are example.com and test.com.
You have to use your own domain names for this purpose. Make them point to the server.
Creating New Document Root Directories
Nginx server has a server block by default which will serve the website from /var/www/html.
For single domain, this directory is enough. But for multiple domains, you have to create multiple directories to store website and serving it for the corresponding domain request.
We will create the new directory under /var/www for all the sites.
We will also place the web content inside the html. It will let us create the sibling directory to the html directory if we require that one.
Use the below code to create the directories under /var/www.
$ sudo mkdir -p /var/www/example.com/html $ sudo mkdir -p /var/www/test.com/html
Once you create the directory, you have to assign the ownership to the normal user. This will help us in writing in those directories without sudo.
Sometimes you need to change the permission of the directory to make it accessible to the www-data user. It is required for dynamic sites since they need to access the file.
You should fix the permission level based on the configuration and technology you are using.
Here, we will be using $USER to give permission to the user which we logged in currently.
$ sudo chown -R $USER:$USER /var/www/example.com/html $ sudo chown -R $USER:$USER /var/www/test.com/html
The Webroot permissions will be correct if you did not change the unmask value.
$ sudo chmod -R 755 /var/www
The next steps should be creating the sample web page for each domain.
Sample Page for Domains
Here, we are going to create the sample website for each domain. First, create the index.html file in the corresponding domain.
$ nano /var/www/example.com/html/index.html
Paste the below code in the file.
/var/www/example.com/html/index.html <html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>Success! The example.com server block is working!</h1> </body> </html>
Then save and close the file. After that, we will be going for the second website creation.
Simply copy the existing website to another file.
$ cp /var/www/example.com/html/index.html /var/www/test.com/html/
We will make some modifications in the second file to use it to create second sample site.
Modify the code as mentioned below.
/var/www/test.com/html/index.html <html> <head> <title>Welcome to Test.com!</title> </head> <body> <h1>Success! The test.com server block is working! </h1> </body>