NGINX is a web server every network administrator should try
Not only is it powerful, but it’s flexible and lightweight as well. But don’t think that small footprint strips it of the features you need; in fact, you’d be hard-pressed to find a feature in Apache that isn’t in NGINX. One such feature is virtual hosts.
If you’re not sure what a virtual host is, let me explain. The idea behind this feature is to allow you to run multiple websites (or domains) from a single server. Most web admins know how to set up virtual hosts with the Apache server, but possibly not with NGINX. To that end, let’s walk through the process of creating a virtual host on this outstanding web server.
I assume you already have NGINX installed and running on your Linux server. I’ll demonstrate this on a Ubuntu 16.04 release.
Step 1
The first thing you must do is create a new directory that will house the virtual host. For the sake of simplicity, I’ll create a virtual host called testing.
To create the necessary directory, issue the command
mkdir /var/www/testing
Next, change the ownership to the new folder with the command
chown -R www-data:www-data /var/www/testing
We’ll change the permissions of the folder with the command
chmod 755 /var/www/testing
Step 2
Let’s create a sample page that will show up for our virtual host. Create the file /var/www/testing/index.html with the following contents.
<html>
<head>
<title>SAMPLE</title>
</head>
<body>
<h1>Hello, TechRepublic</h1>
</body>
</html>
Save and close that file.
Step 3
Now we must create the virtual host file. NGINX makes this easy (as it copies a bit from Apache2). Issue the command sudo touch /etc/nginx/sites-available/testing and then add the following contents to that file.
server {
listen 80;
listen [::]:80;
server_name TESTING;
root /var/www/testing;
index index.html;
autoindex on;
location / {
try_files $uri $uri/ =404;
}
}
Save and close that file.
Step 4
You must create a symbolic link from sites-available to sites-enabled with the command
sudo ln -s /etc/nginx/sites-available/testing /etc/nginx/sites-enabled/testing
Restart NGINX with the command
service nginx restart
Step 5
This is optional if you have already pointed a domain name to your server IP address; otherwise, you’ll want to do this so you can test out your virtual hosts. We need to create a host file entry to make the virtual hosts visible to your local machine. Open the file /etc/hosts and add the following line (IP_ADDRESS is the address of your NGINX server).
IP_ADDRESS testing
Save that file and then point your browser to your server’s IP address. You should see the output of the /var/www/testing/index.html created earlier.
That’s it – your virtual host is up and running. You can create as many virtual hosts on your machine as you need using this process.
Keep it going
NGINX is a really powerful, lightweight web server. Although the setup varies slightly from what you have used with Apache, it shouldn’t take you any time to get up to speed. For more information on NGINX, look at the official NGINX Wiki.