In this post, we will be with you to see how we can setup Virtual Host On CentOS 7; Virtual Host is a common term in Apache; You have several websites that you want to activate on one server, and in addition to saving on server purchases per site, save time and not have to deal with multiple servers and startups separately. Virtual hosting can host numerous websites on one server that allows you to use the available resources appropriately.
Note: we need to install Apache web server before setting up Virtual Hosts on centos 7; if you need any help installing Apache webserver left a comment below, we will help you.
Using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server.
Make sure that your webroot has the default permissions set;
You can use the below command :
sudo chmod -R 755 /var/www
Create A Sample Index.html Page Using Nano Editor
You can Use other editor programs too, like Vim editor, but Nano editor is easy to use; You have to install Nano editor before creating a sample index.html page by using the below command:
Yum install Nano
Confirm by typing Y and enter it.
Now the installation of the Nano editor has been completed
we can create a sample index.html page using Nano editor
You can use the below command for that :
Sudo Nano /var/www/sample.com/html/index.html
Note: We have to use the Nano editor like in previous sections
Now we are inside the editor
we have to add the following sample HTML to the file:
<html>
<head>
<title>Welcome to sample.com!</title>
</head>
<body>
<h1>Success! The sample.com virtual host is working!</h1>
</body>
</html>
At this time, we are going to save the changes by using Ctrl+X and typing Y to exit from the Nano editor;
Now confirm the save request by pressing enter.
Create A Sites-Available Directory
In the other section; Before you create your virtual hosts, you will need to create a sites-available directory to store them in. You will also have to make the sites-enabled directory, which tells Apache that the virtual host is ready to serve visitors. The sites-enabled directory will hold symbolic links to virtual hosts that we want to publish.
Create both directories with the following command:
Tell Apache To look For Virtual Hosts In The Sites-Enabled Directory.
This time; You will tell Apache to look for virtual hosts in the sites-enabled directory; To accomplish this, edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files.
Add this line to the end of the file:
IncludeOptional sites-enabled/*.conf
Note: We have to use the Nano editor like in previous sections, to change the files for Setup Virtual Host On CentOS 7
Here you can use the below command to do it :
sudo nano /etc/httpd/conf/httpd.conf
In this section; Here you can see we are inside the config section on Nano editor
We have to go to the end of the file to add the file; Now save the changes like at the last time
Start by creating a new file in the sites-available directory;
That will automatically be confirmed, and You can see that in the below photo.
Your virtual host is now configured and ready to serve content.
Now Set A Universal Apache Policy
Before restarting the Apache service, we have to make sure that SELinux has the correct policies in place for your virtual hosts; adjusting SELinux Permissions for Virtual Hosts :
SELinux is configured to work with the default Apache configuration. Since you set up a custom log directory in the virtual host’s configuration file, you will receive an error if you attempt to start the Apache service. To resolve this, you need to update the SELinux policies to allow Apache to write to the necessary files. SELinux brings heightened security to your CentOS 7 environment; therefore, it is not recommended to disable the kernel module altogether.
Setting the Apache policy universally will tell SELinux to treat all Apache processes identically by using the httpd_unified boolean. While this approach is more convenient, it will not give you the same control level as an approach that focuses on a file or directory policy.
Now We can run the below command to set a universal Apache policy:
sudo setsebool -P httpd_unified 1
Then httpd_unified Is the boolean; That will tell SELinux to treat all Apache processes as the same type, So you enabled it with a value of 1
2. Adjusting Apache Policies On A Directory
Individually setting SELinux permissions for the /var/www/example.com/log directory will give you more control over your Apache policies but may also require more maintenance.
Since this option is not universally setting policies, you will need to manually set the context type for any new log directories specified in your virtual host configurations.
Now check the content type that SELinux gave the /var/www/example.com/log directory;
By using the following command :
sudo ls -dZ /var/www/sample.com/log/
3. Fix “Semanage Command Not Found” Error In CentOS 7 & RHEL 7 And Resume The Installation Progress
It’s necessary to resume Setup Virtual Host On CentOS 7; And also, for using another command, we have to enable it by using the following commands for semanage.
Here you can use the below commands to fix it; To figure out which package will provide semanage command on CentOS 7, run the below command:
dnf provides /usr/sbin/semanage
Sample output from my CentOS 7 server:
Note: If you couldn’t use the command, you have first to install the DNF and then use the command again to do it.
You can install the DNF by using the below command:
yum install dnf
Here we should confirm the installation by typing Y and press enter button to do it:
You can see the installing of DNF has been completed:
Let’s confirm the installation by typing Y and press enter to prove it.
We already installed the policycoreutils-python successfully;
Here we go; You can now use any Semanage commands at the server:
Change The Type Of The Content
The current context is httpd_sys_content_t, which tells SELinux that the Apache process can only read files created in this directory.
You will change the content type of the /var/www/example.com/log directory to httpd_log_t.
This type will allow Apache to generate and append to web application log files;
Now, after fixing problems, we can change the context type with the following command :
sudo semanage fcontext -a -t httpd_log_t "/var/www/sample.com/log(/.*)?"
We can use the command to apply these changes and have them persist across reboots;
Use the bellow command for it :
sudo restorecon -R -v /var/www/sample.com/log
At this time, you can list the contexts once more to see the changes;
Use the below command :
sudo ls -dZ /var/www/sample.com/log/
list the contexts once more:
4. Final part > Testing the Virtual Host
SELinux context has been updated with either method; Apache will write to the /var/www/sample.com/log directory.
You can now successfully restart the Apache service;
You can do it by using the below command:
sudo systemctl restart httpd
Now List the contents of the /var/www/sample.com/log directory to see that Apache created the log files;
You can use the below command to do that :
ls -lZ /var/www/sample.com/log
We can see that already installed on the graphical place, too, like in the below photo.
Congratulations, we completed the process to Setup Virtual Host On CentOS 7; and ready to use it; if you have any problems with each section of installing virtual host, you can contact us in the comments section. I hope this post will help you and solve your problem.