Learn how to add a virtual host to an Apache web server. Virtual hosts allow you to host multiple separate websites on the same server, with a separate set of di­rec­to­ries for each website.

Apache is a popular and powerful web server for Linux platforms, and is installed and running by default on Cloud Servers running CentOS 7 and Ubuntu 14.04. You can add as many Virtual Hosts to Apache as your server can handle, based on the traffic to all of the websites being hosted by that server.

Re­quire­ments

  • A Cloud Server running CentOS 7 or Ubuntu 16.04.
  • Apache web server installed and running.
Free Cloud Server Trial
En­ter­prise-grade virtual private servers
  • KVM based dev servers for de­vel­op­ers
  • Scalable to en­ter­prise cloud level
  • Pay-as-you-go, per-minute billing

To check the status of Apache on your server, use the command:

  • CentOS 7:sudo systemctl status httpd
  • Ubuntu 16.04:sudo systemctl status apache2

If Apache is installed and running, you will see output similar to:

user@localhost:~# systemctl status apache2
● apache2.service - LSB: Apache2 web server
    Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
    Drop-In: /lib/systemd/system/apache2.service.d
                     └─apache2-systemd.conf
     Active: active (running) since Mon 2016-11-28 22:18:46 UTC; 1 weeks 1 days ago
         Docs: man:systemd-sysv-generator(8)
    Process: 2893 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
    Process: 50598 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
    Process: 2916 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
     CGroup: /system.slice/apache2.service
             ├─ 2934 /usr/sbin/apache2 -k start
             ├─50628 /usr/sbin/apache2 -k start
             ├─50629 /usr/sbin/apache2 -k start
             ├─50630 /usr/sbin/apache2 -k start
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

Note the line that reads:

Active: active (running) since Mon 2016-11-28 22:18:46 UTC; 1 weeks 1 days ago

This means that the server is installed and running.

Restart Apache

Whenever you make changes to the Apache con­fig­u­ra­tion files, you must restart Apache for the changes to take effect:

  • CentOS 7:sudo systemctl restart httpd
  • Ubuntu 16.04:sudo systemctl restart apache2

Every Apache web server has a default site. Unless you have added files to the default site, this will be the standard "Apache Default Page."

One of the most common symptoms of an Apache problem is that all URLs on the server are pointing to this default site. This can happen if there is an error in the con­fig­u­ra­tions, or if Apache simply needs to be restarted.

Create the Directory Structure

It is important to create the di­rec­to­ries first. If Apache expects di­rec­to­ries and cannot find them, it can cause the Apache web server to break, as described in the section above.

Although you can create the di­rec­to­ries anywhere, by common con­ven­tion the di­rec­to­ries for each site are located in the /var/www directory, and begin with a directory named after the site's URL.

To create the di­rec­to­ries for example.com the commands are:

sudo mkdir /var/www/example.com
sudo mkdir /var/www/example.com/html
Note

The directory /var/www/example.com/html is where your website's web pages will be placed. This directory is known as the "document root."

Next, change the ownership of these di­rec­to­ries to the Apache user.

  • CentOS 7:sudo chown -R apache:apache /var/www/example.com
  • Ubuntu 16.04:sudo chown -R www-data:www-data /var/www/example.com

In order to upload website files via FTP, the html directory will need to be owned by the FTP user.

For example, if your user logs in via FTP with the username jdoe the command to change the owner to jdoe is:

  • CentOS 7:sudo chown jdoe:jdoe /var/www/example.com/html
  • Ubuntu 16.04:sudo chown jdoe:jdoe /var/www/example.com/html

Add an Index File

Create a test file with the name index.html in the website's document root:

sudo nano /var/www/example.com/html/index.html

Put the following content into this file:

<html>
    <head>
        <title>Welcome to your new website!</title>
    </head>
    <body>
        <p>Hello, your virtual host is set up correctly.</p>
    </body>
</html>

Save and exit the file.

Create the Apache Con­fig­u­ra­tion File

The Apache con­fig­u­ra­tion file will contain all di­rec­tives for your virtual host. There are many con­fig­u­ra­tions which you can put into this file, based on your specific needs. To begin with, let's create a simple file with just the basic con­fig­u­ra­tions:

  • CentOS 7:sudo nano /etc/httpd/conf.d/example.com.conf
  • Ubuntu 16.04:sudo nano /etc/apache2/sites-available/example.com.conf

Put the following content into this file:

<VirtualHost *:80>
 ServerName example.com
 ServerAlias www.example.com
 DocumentRoot /var/www/example.com/html
</VirtualHost>

Save and exit the file.

Ubuntu 16.04 only: Create a symlink to this file with the command sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/example.com.conf

Restart Apache for the changes to take effect:

  • CentOS 7:sudo systemctl restart httpd
  • Ubuntu 16.04:sudo systemctl restart apache2

Once Apache has finished the restart, visit the website in a browser. You will see the sample index page.

Go to Main Menu