With Apache web server, you can serve web content on the Internet. Basically, 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 host.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Apache queries.
In this context, we shall look into how to set up Apache Virtual Hosts on a CentOS 7 server.
How to Create Apache Virtual Hosts On CentOS ?
Before proceeding with this procedure, you need to ensure that the following prerequisites are met:
- A CentOS 7 server with a non-root user with sudo privileges.
- Apache should be installed and configured, as mentioned in How to Install Apache on CentOS 7 Server.
- A domain name should pointing to your server IP address.
1. Create the Directory Structure
To begin, 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:
│ └── public_html
│ └── 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
To test this, 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.
To create a new index.html file using your favorite text editor, execute the command:
$ sudo vi /var/www/example1.com/public_html/index.html
Then, Add the below lines into it:
<h1>Success! example1.com set up completed!</h1>
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
2. Create 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.
Now, Create a new file using your choice text editor by running:
$ sudo vi /etc/httpd/conf.d/example1.com.conf
Options -Indexes +FollowSymLinks
CustomLog /var/log/httpd/example1.com-access.log combined
Note the following:
- 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.
Here, 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
You will see an Output such as this:
You must restart apache2 service to make active newly created virtual hosts :
$ sudo systemctl restart httpd
Finally, you can verify by accessing your domain (http://example1.com) on a web browser and it will see something like this:
Success! example1.com set up completed!
[Need urgent assistance in fixing Apache configuration errors? We can help you. ]