wave
Uncategorized

How To Set Up Nginx Virtual Hosts on Ubuntu.

Mohammed Naser

Mohammed Naser

If we have a single server that is used as web server and it has allocated one IP address only, then in order to have more than one website or domain running on the server, we use Virtual Hosts.

This means that all websites/domains on that server will share the same web server, its configuration and the server’s resources. Virtual Hosts are widely used and popular in the shared web hosting, where the users get hosting for their website at significantly lower costs.

In order to setup virtual hosts, we need to make sure that Nginx is already installed on our Ubuntu server. If it is not, the command to install it is:

# apt-get install nginx

Once it is installed it will create one configuration file for the default host and respective directory for it. In order to create new virtual host, the first step is to create its root directory. Let’s assume that we are creating new virtual hosts for the www.example.com domain. We will create its root directory as follows:

# mkdir -p /var/www/www.example.com/

We can use any name for the new directory. It doesn’t have to be exactly same as the domain. In this case it is done for easier distinguishing later. Once the directory is created we need to set the proper permissions to it:

# chown -R www-data:www-data /var/www/www.example.com/

In order to make sure that the content for our new virtual host is served from the correct directory, we can create simple index.html file for testing containing the following line:

<h1>This is a test page for www.example.com domain</h1>

The file should be saved inside /var/www/www.example.com/ directory. The next thing is to create configuration file that will instruct Nginx to serve files for our new domain from the directory that we have just created.

Nginx for Ubuntu comes with some sample configuration files inside /etc/nginx/sites-available directory. Nginx is threating each file in /etc/nginx/sites-enabled directory as separate configuration file and is processing all these files on startup. To create new configuration file for our domain we can copy the default configuration file.

# cp /etc/nginx/sites-available/default /etc/nginx/sites-available/www.example.com

Next, we need to open that file using our favorite text editor and edit as follows:

  • Set listen variable to 80. This will make the server to listen on the default port for this host.
  • Set server_name variable to www.example.com.
  • Set the root variable to /var/www/www.example.com/

After we are done editing in order to activate the new virtual host we can either create symbolic links to this file and put it in /etc/nginx/sites-enabled directory or copy/move the actual file to /etc/nginx/sites-enabled directory. Creating symbolic link is something that is most common.

# ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.com

After we have created at least one virtual host, we can delete the default configuration file from /etc/nginx/sites-enabled/ directory

# rm /etc/nginx/sites-enabled/default

After all these changes are done, in order to apply them, we need to restart Nginx:

# service nginx restart

In order to test if everything works we need to have actual DNS entry for the www.example.com pointing to the public IP address on our server or we can override the DNS by inserting entry on our local hosts file

#Virtual Host test for www.example.com
aaa.bbb.ccc.ddd    www.example.com

After that, if we simply open our browser and go to www.example.com, we should get the content from the test index.html page that we have created earlier.

Would you like to know about Zuul, a CI/CD project gating tool? Download our white paper and get reading!

How to up your DevOps game with Project Gating

Share on Social Media:

OpenStack

Cluster API driver for OpenStack Magnum

Mohammed Naser

Public Cloud

9000 MTUs (jumbo frames) in all Public Cloud Regions

Mohammed Naser

Uncategorized

OpenInfra Summit Berlin 2022 VEXXHOST Recap

Mohammed Naser

Go to Top