Setup Apache Virtual Host on CentOS 7 Step by Step

In this guide, you are going to set up a virtual host on CentOS 7 server. To install Apache on your server, you can read this article Install Apache on CentOS 7. Let’s set up a virtual host.

Table of Contents

  1. Create Public Directory
  2. Create Log Files
  3. Assign Ownership
  4. Configure Virtual Host
  5. Adjust SELinux Permissions
  6. Test the Virtual Host

Step 1 : Create Public Directory

We are going to host on our server. Create the public directory of your website:

sudo mkdir -p /var/www/

We have used -p flag to create necessary parent directories.

Make a index.html file:

sudo nano /var/www/

Write a sample HTML code:

    <h1>Welcome to</h1>

Step 2 : Create Log Files

Create a log folder to store log data:

sudo mkdir -p /var/www/

Create two log files:

sudo touch /var/www/
sudo touch /var/www/

Step 3 : Assign Ownership

Assign ownership of the public_html directory. I’m going to provide ownership to apache user. You can provide to any user you want.

sudo chown -R apache:apache /var/www/

Ensure default permissions for root user:

sudo chmod -R 755 /var/www

Step 4 : Configure Virtual Host

We have to create two directories called sites-available and sites-enabled. Apache store website in sites-available folder and Apache server website from sites-enabled folder. Let’s make these folders:

sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled

Open httpd.conf file using this command:

sudo nano /etc/httpd/conf/httpd.conf

Add this line to the end of the file:

IncludeOptional sites-enabled/*.conf

After that let’s make a configuration file in the sites-available directory:

sudo nano /etc/httpd/sites-available/

Now paste this configuration block:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/

	<Directory /var/www/>
		Options Indexes FollowSymLinks
		AllowOverride All
		Require all granted

    ErrorLog /var/www/
    CustomLog /var/www/ combined

We have to create a symbolic link for each virtual host in the sites-enableddirectory:

sudo ln -s /etc/httpd/sites-available/ /etc/httpd/sites-enabled/

Our virtual host is configured and ready to serve.

Step 5 : Adjust SELinux Permissions

It’s recommended to adjust SELinux permissions. Run this command to set universal permissions:

sudo setsebool -P httpd_unified 1

Step 6 : Test the Virtual Host

Restart the web server and visit the website to see the welcome message.

sudo systemctl restart httpd

The article is over. Thanks for reading.