Add an Apache Virtual Host on CentOS 7 and Ubuntu 16.04
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 directories 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.
Requirements
- A Cloud Server running CentOS 7 or Ubuntu 16.04.
- Apache web server installed and running.
Thanks to free starting credit, you can test the IONOS cloud server for 1 month free of charge (or until the credit is used up) and experience the perfect combination of performance and security!
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 configuration 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 configurations, or if Apache simply needs to be restarted.
Create the Directory Structure
It is important to create the directories first. If Apache expects directories and cannot find them, it can cause the Apache web server to break, as described in the section above.
Although you can create the directories anywhere, by common convention the directories for each site are located in the /var/www directory, and begin with a directory named after the site's URL.
To create the directories for example.com the commands are:
sudo mkdir /var/www/example.com
sudo mkdir /var/www/example.com/html
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 directories 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 Configuration File
The Apache configuration file will contain all directives for your virtual host. There are many configurations 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 configurations:
- 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.