Configure Apache Virtual Hosts on CentOS 7

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 LinuxAPT, 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:

/var/www/
 ├── example1.com
 │   └── public_html
 ├── linuxapt.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

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:

<!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


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
<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>

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:

Output
Syntax OK

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. ]

This article covers method to easily configure Apache virtual hosts. You repeat same procedure for multiple domain.

You can use yum to install Apache through CentOS's default software repositories:

$ sudo yum -y install httpd

Next, enable Apache as a CentOS service so that it will automatically start after a reboot:

$ sudo systemctl enable httpd.service


How to Set Up Local Hosts File ?

If you are on a Mac or Linux computer, edit your local hosts file with administrative privileges by typing:

$ sudo nano /etc/hosts

The details that you need to add are the public IP address of your VPS followed by the domain that you want to use to reach that VPS:

127.0.0.1   localhost
127.0.1.1   guest-desktop
server_ip_address example.com
server_ip_address example2.com

This will direct any requests for example.com and example2.com on our local computer and send them to our server at server_ip_address.

Related Posts