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 example.com on our server. Create the public directory of your website:

sudo mkdir -p /var/www/example.com/public_html

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

Make a index.html file:

sudo nano /var/www/example.com/public_html/index.html

Write a sample HTML code:

<html>
  <head>
    <title>Welcome</title>
  </head>
  <body>
    <h1>Welcome to example.com</h1>
  </body>
</html>

Step 2 : Create Log Files

Create a log folder to store log data:

sudo mkdir -p /var/www/example.com/log

Create two log files:

sudo touch /var/www/example.com/log/error.log
sudo touch /var/www/example.com/log/access.log

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/example.com/public_html

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/example.com.conf

Now paste this configuration block:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

	<Directory /var/www/example.com/public_html>
		Options Indexes FollowSymLinks
		AllowOverride All
		Require all granted
	</Directory>

    ErrorLog /var/www/example.com/log/error.log
    CustomLog /var/www/example.com/log/access.log combined
</VirtualHost>

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

sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf

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.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.