Using Apache web server, you can host more than one domain on the same server using virtual hosts. Virtual hosting can host multiple websites on a single web server. You can even specify the site's Root Document (the directory containing the website's files), create a different security policy for each site on the virtual server, use SSL certificates for each site, and more. Stay tuned to Radib Group for more information. Earn more.
In this tutorial, we will get to know the configuration steps of Apache virtual host configuration in CentOS 7.
Introduction:
Apache is one of the most common free and open source web server software used to run websites/applications. Approximately 40% of the world's websites use the Apache web server.
Prerequisites:
- You must have a domain name that is connected to your public server IP. Here we use testdomain.com.
- Log in to your SSH server as a user with sudo permission.
- You have already installed Apache.
Apache web service configuration steps on CentOS7 virtual server
The first step is to create a DocumentRoot. DocumentRoot is a directory where all website files for a domain name are stored and displays the website in response to requests. You can set the DocumentRoot to the location you need.
After creating the directory structure, you need to create the virtual host file.
It is enough to see the virtual server plans in Radib Click on this link
Create virtual hosts file
There are different ways through which you can set up a virtual host. You can even create a separate file for each virtual host directive, or you can include all virtual host directives in one file. It is also suggested to create separate files for each domain due to its maintainability.
By default, Apache is configured to load all configurations ending in .conf from the /etc/httpd/conf.d/ directory.
Now, to create a virtual host for a specific website, open your editor of choice and create the main virtual host configuration file below.
nano /etc/httpd/conf.d/testdomain.com.conf
<VirtualHost *:80>
ServerName testdomain.com
ServerAlias www.testdomain.com
ServerAdmin radib@testdomain.com
DocumentRoot /var/www/testdomain.com/public_html
<Directory /var/www/testdomain.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/log/httpd/testdomain.com-error.log
CustomLog /var/log/httpd/testdomain.com-access.log combined
</VirtualHost>
ServerName: This should be your domain name and should match your virtual host configuration.
ServerAlias: All other domains or subdomains must match the www subdomain in addition to the virtual host.
DocumentRoot: is the directory from which Apache serves domain files.
Options: This directive controls which server features are available in a particular directory.
- Indexes: prevents directories from being indexed.
- FollowSymLinks: Tells your web server to follow symbolic links.
AllowOverride: Specifies whether directives declared in the htaccess file. Can override configuration devices.
ErrorLog, CustomLog: Specifies the location of log files.
It is very important that the name of the configuration file ends with .conf. Any name can be given to your configuration file, but it is recommended to use the domain name as the name of the virtual host configuration file.
Now do the initial check by typing the following command:
$ httpd -t
If there are no errors, it will give you the following output.
Output
Syntax OK
Now restart the Apache service to activate the newly created virtual host:
sudo systemctl restart httpd
You can open http://testdomain.com in your web browser