In this article, we want to teach you How to set up an Apache virtual host file on Centos 7. Let’s see what a virtual host means and go through the setup guide.
What is a virtual host?
The concept of virtual hosts allows more than one Web site on one system or Web server. The servers are different by their hostname. Visitors to the Web site are routed by hostname or IP address to the correct virtual host. Virtual hosting allows companies to share one server to each have their own domain names.
Steps to Set up Apache virtual host file on Centos 7
Before you start setting up Apache virtual host on Centos 7 you need some requirements. Let’s what we need.
You must have access to your server a root or non-root user with sudo privileges. To do this, you can check this guide on the initial server setup with Centos 7.
Also, you need to install Apache on your server and set up a basic firewall. To do this, you can visit this guide on Install Apache on Centos 7.
Then, you need a valid domain name that is pointed to your server’s IP address.
Now proceed to the following steps to complete this guide.
Apache Virtual Host File Setup
To set up Apache virtual host on Centos 7 follow these steps:
1. Create the HTML directory for your domain name.
sudo mkdir -p /var/www/stack.orcacore.net/html
Note: You have to put your domain name instead of stack.orcacore.net.
2. Create an additional directory to store log files for the site:
sudo mkdir -p /var/www/stack.orcacore.net/log
3. Set ownership of the HTML directory with the $USER environmental variable:
sudo chown -R $USER:$USER /var/www/stack.orcacore.net/html
4. Set default permissions for your webroot:
sudo chmod -R 755 /var/www
5. create a sample index.html page using Vi editor:
sudo vi /var/www/stack.orcacore.net/html/index.html
Type the following content in your file:
<title> Here you are in stack.orcacore.net!</title>
<h1> The stack.orcacore.net virtual host is done!</h1>
Then save and close your file.
To set up an Apache virtual host on Centos 7, you need a sites-available and sites-enabled directory.
6. Create a sites-available directory and sites-enabled. Create both directories with the following command:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
7. In this step you need to edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files:
sudo vi /etc/httpd/conf/httpd.conf
Add this line to the end of the file:
Save and close the file.
8. Now you should create your Apache virtual host file on Centos 7:
sudo vi /etc/httpd/sites-available/stack.orcacore.net.conf
Type this in your editor:
CustomLog /var/www/stack.orcacore.net/log/requests.log combined
9. Create a symbolic link for each virtual host in the sites-enabled directory:
sudo ln -s /etc/httpd/sites-available/stack.orcacore.net.conf /etc/httpd/sites-enabled/stack.orcacore.net.conf
10. Run the following command to set a universal Apache policy:
sudo setsebool -P httpd_unified 1
Note: if you disable SELinux before, you don’t need to run this command.
11. check the context type that SELinux gave the /var/www/stack.orcacore.net/log directory:
sudo ls -dZ /var/www/stack.orcacore.net/log/
12. Generate and append to web application log files:
sudo semanage fcontext -a -t httpd_log_t "/var/www/stack.orcacore.net/log(/.*)?"
13. use the restorecon command to apply these changes:
sudo restorecon -R -v /var/www/stack.orcacore.net/log
Restart your web server with the following command:
sudo systemctl restart httpd
Now you are ready to test your Apache virtual host configuration on Centos 7. In your desired web browser, type your domain name:
You will see a page similar to the image below:
That’s it you are done.
At this point, you have learned to Set up the Apache Virtual Host file on Centos 7 step by step. You just need to have a valid domain name and follow the instructions. Hope you enjoy it. Also, you may interested in these articles: