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_htmlWe have used -p flag to create necessary parent directories.
Make a index.html file:
sudo nano /var/www/example.com/public_html/index.htmlWrite 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/logCreate two log files:
sudo touch /var/www/example.com/log/error.log
sudo touch /var/www/example.com/log/access.logStep 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_htmlEnsure default permissions for root user:
sudo chmod -R 755 /var/wwwStep 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-enabledOpen httpd.conf file using this command:
sudo nano /etc/httpd/conf/httpd.confAdd this line to the end of the file:
IncludeOptional sites-enabled/*.confAfter that let’s make a configuration file in the sites-available directory:
sudo nano /etc/httpd/sites-available/example.com.confNow 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.confOur 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 1Step 6 : Test the Virtual Host
Restart the web server and visit the website to see the welcome message.
sudo systemctl restart httpdThe article is over. Thanks for reading.
Md Obydullah
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.