Here’s a brief tutorial that shows you how to create a virtual host or server block on Nginx web server. Virtual Host is a term used with Apache2 to host multiple websites on a single web server.
Nginx on the other hand calls it Server Block. So to Nginx it’s called Server Block and Apache2, it’s called Virtual Host. So instead of running a single website on a single web server, virtual hosting allows for one web server to host multiple websites with different domain names in separate containers.
That’s what this short guide is going to show you. Websites hosted in virtual environments will need separate root directories to host each website content. Each website also will have its own configurations files which control how the website functions and that’s the beauty of implementing virtual hosting with web servers.
To get started with implementing virtual server blocks on Nginx, continue below.
- Install Nginx on Ubuntu 14.04
First install Nginx web server. To do that on Ubuntu 14.04, run the commands one
sudo apt-get update && sudo apt-get install nginx
- Creating Virtual Directory
The next step is to create separate virtual directories for each website. Since Nginx default path is at /var/www/, we’re going to be creating our directories in there.
Create a virtual directory for a website called myblog.com
sudo mkdir -p /var/www/html/myblog.com/vhost
Content for myblog.com domain will live in the /var/www/html/myblog.com/vhost directory. You can create as many you like, just keep them separate.
The next thing is granting the appropriate ownership to Nginx webserver. To change the ownership of the directory to Nginx, run the commands below.
sudo chown -R www-data:www-data /var/www/html/myblog.com/vhost
Next, change the permissions on the directory so Nginx can function correctly.
sudo chmod -R 755 /var/www/html
- Creating a test page
Now you can create a test index page and place it in the vhost folder of myblog.com to verify if the virtual host is working since there’s nothing there. Copy and page the code below into a new file called index.html
<head><title>My Test Page</title>
<p>This is my test page</p>
- Configuring the virtual host
The next step is defining the virtual host perimeter in its configuration file. Virtual host configuration files are used to control how the virtual webserver functions and operates.
In this file is where you define the document root directory, control access rights, define the server name, admin email address and more. The configuration file is very important.
When you install Nginx on Ubuntu, a default configuration file with the basic settings is created. This file is there to verify that Nginx is working after you browse to the host.
So, we’re going to make a copy of the default configuration file to create the myblog.com domain configuration file. To do that, run the commands below.
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/myblog.com
Next, open the new configuration file for myblog.com and define the configurations for the myblog.com virtual site.
sudo vi /etc/nginx/sites-available/myblog.com
Configure the file
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
index index.html index.htm;
This is the basic configuration just to confirm that the virtual host is up. For more detailed configuration of Nginx, do a search on this site for Nginx configuration.
Save the file.
Finally, run the commands below to enable the site by creating a symbolic link to the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/myblog.com /etc/nginx/sites-enabled/myblog.com
Run the commands below to remove the default enabled site to prevent duplicate entry for the default IP/Port
sudo rm /etc/nginx/sites-enabled/default
Restart Nginx and test the site
sudo service nginx restart
If you’re running this test locally, create a host entry on your local computer for myblog.com with IP address of the server.
Do this for as many virtual websites as you wish, just make sure to define the unit IP addresses.