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
- Create Public Directory
- Create Log Files
- Assign Ownership
- Configure Virtual Host
- Adjust SELinux Permissions
- 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.
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-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
httpd.conf file using this command:
sudo nano /etc/httpd/conf/httpd.conf
Add this line to the end of the file:
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
Our virtual host is configured and ready to serve.
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
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.
The article is over. Thanks for reading. ?
sudo systemctl restart httpd
Preview may take a few seconds to load.
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Bold **double asterisks**
Three back ticks and then enter your code blocks here.
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
> type a greater than sign and start typing your quote.
You can add links by adding text inside of  and the link inside of (), like so:
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of , and the image URL inside of (), like so:
To add a divider you can add three dashes or three asterisks:
--- or ***