Sometimes it may be wise to run multiple websites or blogs on a single server. For example, running one website or blog per server may not be the best way to utilize your server resources. One server, one website? No way.. just not efficient.

That’s why there’s a feature called Virtual Hosts on Apache2 and Server Blocks on Nginx. Virtual hosts or server blocks are feature on these web servers that allow multiple websites and blogs to be hosted on a single server. If a server has enough resources, why wastes those resources by only hosting a single website?

This brief tutorial is going to show you how to enable server blocks on Nginx web server to host multiple blogs.

We started yesterday with Virtual hosting on Apache2 web server. We discussed how to host multiple websites using Apache2 on Ubuntu 15.04. To read that post click here or the link below.

https://www.liberiangeek.net/2015/07/how-to-enable-and-run-multiple-websites-using-apache2-on-ubuntu-15-04/

This post shows you how to do the same with Nginx using its server blocks feature.

For this tutorial, we’re going to be hosting two websites called example.com and myexample.com on a single server assigned IP address 192.168.20.1

To get started, sign on to Ubuntu and update your system. To do that run the commands below.

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

After updating and rebooting your server, run the command below to install Nginx web server.

sudo apt-get install nginx

The above commands install a more stable version of Nginx, but not the latest. If you wish to install the latest version of Nginx on Ubuntu, please click here.

After installing Nginx, go and create two root directories. Each website will need a unique directory to store its content. Run the commands below to create two unique root directories for the websites.

sudo mkdir -p /var/www/html/example.com/public_html
sudo mkdir -p /var/www/html/myexample.com/public_html

For each website, we will create a test page to identify the site. First, run the commands below to create a sample webpage for example.com domain.

sudo vi /var/www/html/example.com/public_html/index.html

Then type these HTML tags and save the file.

<html>
<head>
<title>This is the example.com domain</title>
</head>
<body>
<p>This is example.com</p>
</body>
</html>

Then do the same for myexample.com domain. Run the commands below to create a sample webpage.

sudo vi /var/www/html/myexample.com/public_html/index.html

Then type these HTML tags and save the file.

<html>
<head>
<title>This is the myexample.com domain</title>
</head>
<body>
<p>This is myexample.com</p>
</body>
</html>

After that, change the permissions on those directories so that the webserver can serve them correctly.

sudo chown www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

When you’re done, go and create two websites from the default site configurations file. To do that run these commands

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/myexample.com

Next run the commands below to edit example.com website configurations

sudo vi /etc/nginx/sites-available/example.com

Then make the change to look like what’s below and save the file

[.....]
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html/example.com/public_html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
[.....]

Do the same to myexample.com configurations

sudo vi /etc/nginx/sites-available/myexample.com

Then make change to look liek what’s below and save the file.

[....]
server {
listen 80;
listen [::]:80;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html/myexample.com/public_html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name myexample.com www.myexample.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
[....]

nginx Enable Site

For the myexample.com domain, remove the default_server directive as highlighted above. You can’t have two default servers on the same hosts.

Next, run the commands below to disable the default site configurations and use the nginx enable site function on our two new sites (example.com and myexample.com) by running the commands below.

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/myexample.com /etc/nginx/sites-enabled/

Restart Nginx web server using the commands below.

sudo systemctl reload nginx

The final thing to do is change the local host file to reference the website IP to test.

192.168.20.1    example.com
192.168.20.1    myexample.com

Restart Nginx webserver and browse to the websites to test and you should see the two websites being hosted on Nginx

Enjoy!