In this article, we want to teach you How to set up Apache virtual host on centos 7.
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.
How to set up Apache virtual host on centos 7
Requirements for setting up Apache Virtual host
Before you start setting up Apache virtual host on Centos 7 you need to review our article about the initial server setup with Centos 7. Also, you need to install Apache first by following our article about How to install Apache on Centos 7. and then customize the firewall.
let’s see how to set up a virtual host on centos 7.
Set up virtual hosts on centos 7
To set up Apache virtual host on centos 7 follow these steps:
1. Create the Html directory for stack.orcacore.net:
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 this in your editor:
<html> <head> <title> Here you are in stack.orcacore.net!</title> </head> <body> <h1> The stack.orcacore.net virtual host is done!</h1> </body> </html>
Then save and close your file.
To set up 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:
<VirtualHost *:80> ServerName www.stack.orcacore.net ServerAlias stack.orcacore.net DocumentRoot /var/www/stack.orcacore.net/html ErrorLog /var/www/stack.orcacore.net/log/error.log CustomLog /var/www/stack.orcacore.net/log/requests.log combined </VirtualHost>
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.
Remember to check out the Initial server setup with Centos 7 and How to install Apache on Centos 7. In this article, by following the steps you can easily set up your Apache virtual host.
Hope you enjoy this article about how to set up Apache virtual host on centos 7.
you might be interested: