When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. In this step, you will set up a domain called example.com
, but you should replace this with your own domain name.
Apache on CentOS 7 has one server block enabled by default that is configured to serve documents from the /var/www/html
directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html
, you will create a directory structure within /var/www
for the example.com
site, leaving /var/www/html
in place as the default directory to be served if a client request doesn’t match any other sites.
Create the html
directory for example.com
as follows, using the -p
flag to create any necessary parent directories:
sudo mkdir -p /var/www/example.com/html
Create an additional directory to store log files for the site:
sudo mkdir -p /var/www/example.com/log
Next, assign ownership of the html
directory with the $USER
environmental variable:
sudo chown -R $USER:$USER /var/www/example.com/html
Make sure that your web root has the default permissions set:
sudo chmod -R 755 /var/www
Next, create a sample index.html
page using vi
or your favorite editor:
sudo vi /var/www/example.com/html/index.html
Press i
to switch to INSERT
mode and add the following sample HTML to the file:/var/www/example.com/html/index.html
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>
Save and close the file by pressing ESC
, typing :wq
, and pressing ENTER
.
With your site directory and sample index file in place, you are almost ready to create the virtual host files. Virtual host files specify the configuration of your separate sites and tell the Apache web server how to respond to various domain requests.
Before you create your virtual hosts, you will need to create a sites-available
directory to store them in. You will also create the sites-enabled
directory that tells Apache that a virtual host is ready to serve to visitors. The sites-enabled
directory will hold symbolic links to virtual hosts that we want to publish. Create both directories with the following command:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Next, you will tell Apache to look for virtual hosts in the sites-enabled
directory. To accomplish this, edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files:
sudo vi /etc/httpd/conf/httpd.conf
Add this line to the end of the file:
IncludeOptional sites-enabled/*.conf
Save and close the file when you are done adding that line. Now that you have your virtual host directories in place, you will create your virtual host file.
Start by creating a new file in the sites-available
directory:
sudo vi /etc/httpd/sites-available/example.com.conf
Add in the following configuration block, and change the example.com
domain to your domain name:/etc/httpd/sites-available/example.com.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/example.com/html
ErrorLog /var/www/example.com/log/error.log
CustomLog /var/www/example.com/log/requests.log combined
</VirtualHost>
This will tell Apache where to find the root directly that holds the publicly accessible web documents. It also tells Apache where to store error and request logs for this particular site.
Save and close the file when you are finished.
Now that you have created the virtual host files, you will enable them so that Apache knows to serve them to visitors. To do this, create a symbolic link for each virtual host in the sites-enabled
directory:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
Your virtual host is now configured and ready to serve content. Before restarting the Apache service, let’s make sure that SELinux has the correct policies in place for your virtual hosts.