
When using Apache web server, you can use virtual hosts to host more than one domain in a single server. Apache will break its functionality and components into individual units so you can customize independently. The basic unit that describes an individual site or domain is called a virtual hosts on CentOS server.
Create Apache Virtual Hosts On CentOS 7
In this tutorial, we will provide a step by step instructions about how to set up Apache Virtual Hosts on a CentOS 7 server.
Prerequisites
- A CentOS 7 server with a non-root user with sudo privileges.
- Apache should installed and configured, as shown in How to Install Apache on CentOS 7 Server.
- A domain name should pointing to your server IP address.
Creating the Directory Structure
At first, we will create a directory where website files for a domain will store and serve response to visitors. Generally, it called DocumentRoot. You can set the document root to any location that you want but it’s best practice to set in directory structure. Generally in all /var/www
.
/var/www/
├── example1.com
│ └── public_html
├── example2.com
│ └── public_html
Here, we need to create separate directory inside /var/www
directory for each domain which we want to host on our server.
sudo mkdir -p /var/www/example1.com/public_html
For testing purpose we will create a index.html
file inside the domain document root directory. This page will be show by default when visitors will visit your site.
Creating a new index.html
file using your favorite text editor by typing :
sudo vi /var/www/example1.com/public_html/index.html
Add the below lines into it:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome!!</title>
</head>
<body>
<h1>Success! example1.com set up completed!</h1>
</body>
</html>
Since all commands are executed as sudo user, so newly created files and directories are owned by the root user. We will change document root directories ownership to avoid permission issue for our regular user. Thus, regular user can modify files in our web directories without any issues.
sudo chown -R apache: /var/www/example1.com
Creating Virtual Host Files
There are multiple ways to set up a virtual host. You can make separate file for each Virtual Host Directive or you can add all Virtual Host Directives in a single file. It’s recommended to make separate file for each domain because it’s maintainability.
On CentOS, Apache will load all .conf files from /etc/httpd/conf.d/
directory because of it’s default configuration. So now we will create a separate virtual host.
Create a new file using your choice text editor by typing :
sudo vi /etc/httpd/conf.d/example1.com.conf
<VirtualHost *:80>
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/public_html
<Directory /var/www/example1.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/log/httpd/example1.com-error.log
CustomLog /var/log/httpd/example1.com-access.log combined
</VirtualHost>
- ServerName: This should be your domain name and match with virtual host configuration.
- ServerAlias: All other domains or subdomains that should match for this virtual host as well, usually the www subdomain.
- DocumentRoot: Path of virtual host directory that from which Apache will serve the domain files.
- Options: This directive controls which server features are available in a specific directory.
- -Indexes: It will prevent directory listings.
- FollowSymLinks: Apache will follow the symbolic links if this option is enabled.
- AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
- ErrorLog, CustomLog: Specifies the location for log files.
You can give any names to your configuration file but it’s recommended to give file name same as domain name.
Now check the syntax by type :
sudo apachectl configtest
It will show below output if there are no errors:
Output
Syntax OK
You must restart apache2 service to make active newly created virtual hosts :
sudo systemctl restart httpd
You can verify by opening your http://example1.com
to your web browser and it will show you as following :

Conclusion
Finally, in this tutorial you learned how to create and set up Apache virtual hosts. You repeat same procedure for multiple domain.
Leave a Reply