How to Manage and Use Apache virtual hosts in Ubuntu

Posted on July 20th, 2019

In this informative piece of content, I will explain how to use virtual hosts in Apache in Ubuntu Operating system. We will also learn the directory structure of the Apache webserver. By the end of this guide, you will know everything you need to know about Apache virtual hosts.

You will be able to perform the following tasks very easily.

  • Host multiple applications on a single VPS/VM.
  • Read virtual host files very easily.
  • Manage log files for each application very easily.
  • Understand important directives like DocumentRoot, ServerName, ServerAlias and many more.

So, we can finally start with the actual tutorial. However, If you want to follow this guide practically, you can configure a new virtual private server within minutes using InterServer VPS plans.

Finally, I think we are ready and we can start by installing Apache on our brand new virtual private server.

Installing Apache on VPS

It is very easy to install Apache on Virtual Private Servers. In most cases, you just have to execute a single command on your server to install Apache. Here is how you can do it on Ubuntu, one of the most popular operating systems right now.

$ sudo apt-get install apache2 -y

If your server is brand new, you might face issues installing apache on your server. Most probably, it’s because your source list is not updated yet. Execute the following command to update source list.

$ sudo apt-get update

Verify the installation by visiting the IP address of your server in your browser. You should see the default Apache page just like the following image.

Apache default page

If you can see this page, you can move on to the most interesting part of this guide, understanding Apache directory structure and how the virtual hosts work in Apache.

Apache Virtual Host Directory Structure

Execute the following command to go to the directory where all the Apache configuration files are stored.

$ cd /etc/apache2

Once you are in, run the ls command and you will see a few files and directories. Here are some important directories and files you will see in the /etc/apache2 directory.

  1. apache2.conf is Apache’s main configuration file.
  2. conf-available holds all the extra configuration files.
  3. conf-enabled contains symlinks of the configuration files that are enabled.
  4. The envvars file contains environment variables.
  5. mods-available contains all the available Apache modules.
  6. mods-enabled contains symlinks of the modules that are enabled.
  7. ports.conf contain the port configuration (On which port you want to listen for requests?).
  8. sites-available contains all the virtual host files we create.
  9. sites-enabled contains symlinks of all the virtual hosts that we want to enable.

From all these files and directories, we just have to focus on sites-available and sites-enabled directories. Because these directories will hold all our Virtual Host configuration files.

So, why there are two directories to store apache virtual host files? There is a very logical reason behind it. Let’s discuss sites-available and sites-enabled directories in detail.

Apache sites-available directory

The sites-available directory contains all the virtual host files we want to manage on our server. For example, Let’s say we have two domain names called domain1.com and domain2.com. It means that we have to create two virtual host files in the sites-available directory with names domain1.conf and domain2.conf.

Both of these virtual host files will have contents specific to the specific domain name. However, the file name can be anything, all that matters is the directives inside these files and the .conf extension of the virtual host files.

You can create as many virtual host files as you want. But, these virtual host files are not yet active. It means that merely creating virtual host configuration files inside the sites-available directory won’t take our sites live on the server.

The sites-available directory is just a directory to store all your active and non-active virtual host files. And we have to explicitly enable the virtual host files to take our domains live on the server.

You can execute the following command to activate VirtualHost files.

$ sudo a2ensite {name_of_virtual_host}

The a2ensite command comes with the Apache webserver. It’s just a small script that creates a symlink of your main virtual host configuration file stored in sites-available to the sites-enabled directory.

Apache sites-enabled directory

The sites-enabled directory contains symlinks of the virtual host files stored in the sites-available directory. If you check the main apache configuration file located at /etc/apache2/apache2.conf, you will see that all the configuration files stored in the sites-enabled directory are included in the runtime.

It means that you can store as many virtual host files as you want but you can enable them as per your needs. For example, you can create a Virtual Host file for the maintenance mode that points to some other DocumentRoot and you can enable the maintenance virtual host file whenever you want to put your site on maintenance.

You can disable the virtual host configuration by using the following command.

$ sudo a2dissite {name_of_virtual_host}

Or, as it’s just a symlink, you can just delete the link from the sites-enabled directory and the Virtual Host configuration will be disabled.

Note that the commands a2ensite and a2dissite will enable and disable the virtual host configurations but the effects are not realtime. We have to restart or reload the Apache service to apply the changes. You can execute the following command to reload the main Apache configuration.

$ sudo service apache2 reload

Now, As we know how the virtual host configuration files work in Apache, Let’s do some practical work. We will create two Virtual Hosts for two different domain names and we will see how it really works.

Create Apache Virtual Hosts

Finally, we are going to create virtual host files for our two domain names domain1.com and domain2.com. You can just replace these domain names with your own domain names and everything should work as expected.

So, execute the following command to create the first virtual host file in sites-available directory.

$ sudo nano /etc/apache2/sites-available/domain1.conf

Now, Paste the following contents in the virtual host file. I will explain these directives in detail later in this guide.

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName domain1.com
    ServerAlias www.domain1.com

    DocumentRoot /var/www/domain1.com/public_html

    ErrorLog /var/www/domain1.com/logs/error.log
    CustomLog /var/www/domain1.com/logs/access.log combined
</VirtualHost>

Do not forget to replace domain1.com with your domain name. Let’s first understand the directives used in this Virtual Host file.

  1. ServerAdmin: The E-mail you provide in this directive will be displayed if the user encounters Internal server error. So that they can contact you directly via E-mail.
  2. ServerName: It’s the kind of a primary domain of your application. Here, I am using domain1.com. You can replace it with whatever domain name you want to host on your server.
  3. ServerAlias: This directive holds all the extra domains/subdomains you want to use to access the site. You can add multiple hostnames here separated by space.
  4. DocumentRoot: In this directive, you have to define the directory in which you want to route the requests coming for this virtual host file, if it is enabled. It means that the requests coming on domain1.com will be routed to /var/www/domain1.com/public_html directory.
  5. ErrorLog: In this directive, you have to define the path of the error log file. All the errors encountered in runtime will be logged in the file mentioned here.
  6. CustomLog: This log file path you mention here will hold all the access logs.

And the <VirtualHost *:80> ... </VirtualHost> is the Virtual Host block. You can create multiple such blocks in a single domain.conf file for multiple domains and it will still work!

Now, It’s time to enable our virtual host file so that we can access our site.

NOTE: Apache will check if the directories mentioned in the Virtual Host file are present. If any of the directories mentioned in the Virtual Host file is missing, whole apache server might go down until you fix the issue.

Let’s create the directories for our example site first. Then, we will enable the apache virtual host configuration file. Execute the following command to create the domain1.com, public_html and logs directories for our site.

$ sudo mkdir -p /var/www/domain1.com/{public_html,logs}

Once you have created the required directories, you can enable the virtual host file by executing the following command.

$ sudo a2ensite domain1 # OR sudo a2ensite domain1.conf

As I mentioned above, Enabling the virtual host won’t make the changes in real-time. We have to reload the main apache configuration to apply the changes. Execute the following command to reload the Apache configuration.

$ sudo service apache2 reload

If you don’t see any error message while reloading the configuration, your virtual host is live and the files will be served to the web browser from the directory mentioned in the DocumentRoot directive.

You can create as many virtual host files as you want. And you can even add extra Apache configuration in the virtual host files as per your requirements.

 

Conclusion: Apache Virtual Hosts are very powerful configuration files that allows you to host multiple domains with different configurations in the same machine. There are hundreds of different Directives you can use to define different things specific to the Virtual Host. So, there are endless possibilities! It might take some time to understand how exactly Apache works, and you might face some issues while getting started, but you can always use the comment section given below to contact us directly for help!

One Response to “How to Manage and Use Apache virtual hosts in Ubuntu”

  1. Anoop D says:

    I have a newly setup Ubuntu20.04 and have apache2 installed . I have a wordpress install in /var/www/html directory like /var/www/html/wordpress and i can access it through localhost/wordpress . I tried creating a .conf file as you instructed with document root /var/www/html/wordpress and servername wordpress1.dev but it is showing DNS_PROBE_FINISHED_NXDOMAIN error . What could have gone wrong ?

Leave a Reply