Setup Domains/Subdomains on VPS in Ubuntu
So in this article, I will be discussing how to set up Nginx Server with multiple Domains or Subdomains.
- Setup a VPS of your requirements. In this tutorial, I will be using Azure to set up a VPS with 1GB RAM, 1vcpu, and Ubuntu 18.04.
- Connect to your newly created server using an ssh client like Putty.
- Must have a Domain and have access to it. I will be using Namecheap for the Domain.
Okay, so now all the things are setup. We will start with the Actual Process.
Step 1- Connect your VPS with Domain
Connect your domain with the server so that you don't have to access your server by typing IP Addresses.
First of all, find your public IP Address from the VPS dashboard that your provider has given you. Then head over to the Domain Dashboard settings( In my case, it is Namecheap). In your domain settings, head over to the DNS section and point an ‘A’ record to your public IP Address. It would look something like this below.
I have added 3 DNS, the host @ and www denotes the default domain, and the host ‘API’ denotes the subdomain that I will be using in this tutorial.
So the two domains that I will be using here as a demonstration is
After this step, your domain and subdomain are pointed to your server’s IP Address.
Step 2- Set up Nginx Server
So, after all the setup is over, I will be connecting to my server using the Public IP or my domain name along with my username and password in any ssh client.
Now Comes the Interesting part
Now your Nginx server is basically installed, You can check it at by typing your default domain in your web browser address bar. I will show you that Nginx Server is successfully installed. Now we just need to adjust some firewall settings.
Adjusting More Settings
The above command shows us different firewall options available. After that just follow the commands below
If everything goes well, You could type ‘sudo ufw status’ and see the status of your settings.
More Web Server Settings
Now that the server is installed, you can now check the status of your server by typing the code below.
To stop, start or restart your server, you can use the below commands respectively
Step 3- Create Domain Directory
Now that our server is installed, we can now create our directory for our domain routes.
Now assign Ownership to the directory we just made to the current logged in user.
Now we will give permission to the web roots by:
Step 4- Creating a Sample Webpage
We have successfully created the directory for our domain routes. Now let us just make a sample page for each website so that we will have something to display.
After the above command, an editor will open up. We will write a simple HTML code.
To save this file, press (CTRL + O) and it will prompt a confirmation, Just press enter
To exit, press (CTRL + X).
Now your HTML code is written in Domain 1(jugaadapp.me) Directory. We will perform the same steps to change our Domain 2(api.jugaadapp.me).
Then write the same HTML code we used earlier and save it.
Step 5- Configure Server Block
Till now, we have created the directory and the HTML files for both the directory to show. But now we will have to create and configure the server block files for each Domain.
The default server block is called ‘default’. We will use that server block file to copy and rename and make some changes to make the block for our domain
Server Block for Domain 1(jugaadapp.me)
Now our server block file for Domain 1(jugaadapp.me) is opened. If we ignore the comments, the file will look similar to this:
First of all, we will remove the default_server word from the first two lines as there can be only one default_server and that is present is the default server block.
Secondly, we will change the root location /var/www/html to our custom made domain directory /var/www/jugaadapp.me/html .
Third, we will put a server name that is similar to our domain that we are going to use, it will look something like this server_name jugaadapp.me www.jugaadapp.me;
The final file will look something like this:
Server Block for Domain 2(api.jugaadapp.me)
Now our domain 1 configuration is done, we will copy that and make some changes to match the second Domain.
Again repeat the previous steps and change the root and the server_name relevant to this Domain. The final file will look something like this.
Step 6- Finishing Steps
Now, all that left is to enable the server blocks that we just created and restart our server.
Enable the server blocks by typing the command below:
We have now in a total of 3 server blocks, the two that we created will respond to the respective domains and the remaining default block will execute when the request doesn't match from the above two.
The only step is left to add additional server names to avoid a possible hash bucket memory problem. We can do this by:
And uncomment the Line server_names_hash_bucket_size 64;
Save the file and exit from it.
Now test if there are any no errors on your Nginx server by typing the commands below:
If you reached up to this line then you are ready to go. Now all that is left to restart your Nginx server by typing the command below:
Your server is now ready and you can check your websites in any web browser.
Nginx subdomain configuration
I have nginx acting as a reverse proxy to apache. I now need to add a new subdomain that will serve files from another directory, but at the same time I want all location and proxy_pass directives that I have for the default host to apply to the subdomain also.
I know that if I copy the rules from the default host to the new subdomain it will work, but is there a way for the subdomain to inherit the rules? Below is a sample configuration
1 Answer 1
You could move the common parts to another configuration file and include from both server contexts. This should work:
How to setup subdomain or host multiple domains using nginx in linux server
Well if you don’t know how to do that reading this tutorial will setup you two domains and one subdomain both pointing to the same ip address and host on the same server. Here’s the general assumption for this setup:
IP Address: 184.108.40.206
Domain names: example.com, blog.example.com, fakenews.com
Before starting the tutorial, first thing you’ve to do is to point all your domains and subdomains to the single ip address via your DNS provider (edit A, CNAME). However if you want to test this locally, then edit the /etc/hosts configuration file and add the following:
And when you ping these domains locally on the server, you’ll get ok (200) response.
Now we’ll follow the steps to setup these domain names:
1. Install and start nginx
Use the following command to install nginx on ubuntu
2. Test the nginx
Check any of the domains or ip address in your browser to make sure nginx works correctly. The browser will output a default nginx page.
3. Setup the test directories for each domains
Up until now, all the domains have set up correctly but there is one huge problem, all pointing to same page. We need to separate these domains to point to their own pages. For this, I will setup test directories and html pages.
Creating directories for each domains and subdomain
Creating simple html pages for each
Lastly put some different content in each index.html files
4. Creating server blocks for each domains and subdomain
Nginx provide default server block in /etc/nginx/sites-available. We will copy that server block for each domains and do modifications for each. Also we will create symbolic link of new file
Now after modification, the new file will look like this for example.com domain
Here default_server means if none of the other domains resolve, the last resort is to resolve this server block.
NOTE: There is only one default server block in nginx with same port.
How To Serve NGINX Subdomains or Multiple Domains
Boost your career with the AWS Certified Solutions Architect certification. Get a FREE prep guide: deep dive into exam domains, preparation tips, and valuable resources. Download.
Table of Contents
Do you have a server with a single public IP address, but need to host multiple domains or subdomains? What would you do? This scenario can be confusing if you are a beginner. But no worries, this tutorial has got you covered, so you can serve an NGINX subdomain and multiple domains.
In this tutorial, you’ll learn how to effectively serve NGINX subdomains or multiple domains on one server with a single IP address!
If you’re ready, time to get down to it!
This tutorial comes with hands-on demos but doesn’t require many prerequisites and will assume you have an Ubuntu Server LTS with SSH enabled and NGINX installed. The demos in this tutorial are on Ubuntu Server LTS 20.04.1.
Setting Up DNS Records
Before you can serve NGINX subdomains or multiple domains, you will need to add an A record in a DNS control panel. The A record binds and points all domains and subdomains to a single IP address to let web browsers find your website.
1. Launch your favorite web browser, and log in to your DNS control panel.
2. Click on Add Record to start adding A records. Your browser redirects to a page where you’ll configure DNS settings for your domain and subdomains.
Adding an A Record
Now configure the DNS settings with the following:
Select A record in the Type field.
Enter @ in the Host field, and your server IP in the Value field.
Select your desired value in the TTL field, then click on the Confirm button to save the settings.
Repeat the same process for other subdomains.
Configuring DNS settings for Domain and Subdomains The final DNS Settings page looks like the one below.
The final DNS Settings page looks like the one below.
Viewing DNS Settings Page
Your DNS record control panel may vary in functionality and design, but the same principles will apply to all.
Setting Up Web Directories for NGINX Domain and Subdomain
Now that you’ve added A records for your domain and subdomains, it’s time to set up their web directories. NGINX comes with a default virtual host file and is configured to serve a web directory located at /usr/share/nginx/html.
You’ll create a separate web directory for each domain inside the NGINX default document root (/var/www/html).
1. First, open your terminal and run the commands below to create web directories for all domains and subdomains. Doing so separates, organizes, and isolates files for each website.
2. Next, run the chown command to recursively ( -R ) change the ownership of each directory you created in step one to www-data user and group. You’re changing ownership of each directory to www-data user and group since NGINX runs as a www-data user.
3. Create a file named index.html in your preferred code editor and then copy/paste the code below to the index.html file. Save the file inside your domains’ primary web directory (/var/www/html/awstutorial.net).
The HTML code below displays a message that says “Congratulations! The awstutorial.net website is working!” when the index.html file is opened in a web browser.
4. Finally, create the same index.html in the /var/www/html/web1.awstutorial.net and /var/www/html/web2.awstutorial.net directories. But replace awstutorial.net with web1.awstutorial.net , and web2.awstutorial.net in the code of each index.html accordingly.
Setting Up Virtual Host for NGINX Domain and Subdomains
You already have an index.html page for your domain and subdomains to serve through an NGINX web server. The next step is you’ll create an NGINX virtual host configuration file for each domain to serve the HTML pages.
1. Create an NGINX virtual host configuration file named awstutorial.net in your preferred code editor, and then copy/paste the code below to that file. Save the file in the /etc/nginx/sites-available/ directory.
The code below controls the behavior of your server, such as the server name and index (home) page when a user tries to access your domain.
2. Next, run the following nginx command to check ( -t ) the NGINX configuration file for any syntax error.
If no syntax error is found in the NGINX configuration file, you will get the following output.
Verifying NGINX configuration file
3. Next, run the below command to create a symbolic link ( ln -s ) from the /etc/nginx/sites-available to the /etc/nginx/sites-enabled/ directory. This command enables the awstutorial.net virtual host configuration file.
The sites-available and sites-enabled format are standard within an NGINX Ubuntu installation but other distributions may use a different standard.
4. Repeat steps one to three to create NGINX virtual host configuration files named web1.awstutorial.net and web2.awstutorial.net.
Change the following lines in each NGINX virtual host configuration file:
- Replace the line root /var/www/html/awstutorial.net with the webroot directory of each subdomain ( root /var/www/html/web1.awstutorial.net and root /var/www/html/web2.awstutorial.net ).
- Replace the line server_name awstutorial.net with the name of each subdomain ( server_name web1.awstutorial.net and server_name web2.awstutorial.net ).
5. Now run the sudo ln commands below as you did in step three to enable the virtual host configuration files.
6. Run the below systemctl command to restart the NGINX ( restart nginx ) service to apply all configuration changes.
7. Finally, navigate to the domain and subdomains’ URLs on your browser to test if the websites work fine.
If the domain and subdomains load up, you’ll see a message like the one below.
Verifying websites are loading through the web browser.
Setting Up HTTPS on NGINX Domain and Subdomain
You’ve successfully set up and tested an NGINX domain and subdomains on HTTP protocol, which is great. But you must secure your domain and subdomains’ connection by enabling HTTPS. How? With a certificate that you’ll get from a Certificate Authority like Let’s Encrypt SSL.
1. First, run the command below to install the Certbot software package ( apt-get install certbot ). Certbot lets you download an SSL certificate for your domain and subdomains.
2. Next, run the certbot command below to download an SSL certificate ( certonly ) for your domain ( -d awstutorial.net ). Notice that you agree to the term of service ( —agree-tos ) with your email address ( —email ).
3. Enter ‘1’ to select the NGINX Web Server plugin (nginx) to authenticate with the ACME CA, as shown below.
Selecting Plugin to Authenticate with ACME CA
After selecting the NGINX Web Server plugin, you’ll see the download progress of the SSL certificate for your domain (awstutorial.net).
By default, Let’s Encrypt’s SSL certificates are stored in the /etc/letsencrypt/live/ directory.
Showing Download Progress of the SSL Certificate
4. Now run each command below as you did in step two to download SSL certificates for the remaining subdomains ( web1.awstutorial.net and web2.awstutorial.net ).
5. Finally, run the ls command below to list all SSL certificates in the /etc/letsencrypt/live/ directory that contain ( * ) the awstutorial.net in their names. Doing so lets you verify that the SSL certificates exist.
Below, you’ll see the SSL certificates for your domain and subdomains.
Showing SSL certificate for all domains.
Configuring NGINX Virtual Host to Use SSL Certificates
At this point, you already have SSL certificates at your disposal. But how do you use them to secure your domain and subdomains? You’ll define the certificates’ path in the server block of each NGINX virtual host configuration file.
1. Open the awstutorial.net NGINX virtual host configuration file sitting at the /etc/nginx/sites-available/ directory in your preferred code editor.
2. Replace the content of the file with the code below, where you define your domain’s SSL certificate’s path, and SSL protocol (under Path of the SSL certificate ).
3. Repeat the same process (steps one to two) for the remaining virtual host configuration files (web1.awstutorial.net and web2.awstutorial.net). But be sure to replace the SSL certificate’s path with your subdomain’s certificate’s path (under Path of the SSL certificate ).
4. Now rerun the systemctl command below to restart the NGINX service to apply the configuration changes.
5. Finally, navigate to your domain and subdomain’s URLs on your web browser. But this time, instead of the HTTP protocol, use HTTPS to see if they work.
Below, you can see a padlock icon at the address bar, which indicates the website is secure with your SSL certificate.
Verifying Domain is Secure
In this tutorial, you’ve learned how to serve an NGINX subdomain or multiple domains by configuring a virtual host configuration file. You’ve also touched on securing your domains with an SSL certificate that you also define in the virtual host configuration file.
Now, why not use this setup in a production environment, such as to host multiple apps on a single server, and provide affordable web hosting?
Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.
More from ATA Learning & Partners
Recommended Resources for Training, Information Security, Automation, and more!
Get Paid to Write!
ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?
ATA Learning Guidebooks
ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!