How To Setup Virtual Host On CentOS 7

Linux

February 9, 2021 by Oscar Borayn

How To Setup Virtual Host On CentOS 7

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.

O3wOsNgmY3 1601326034

1. Need To Set A Domain To Setup Virtual Host On CentOS 7

In this step,  you will set up a domain called sample.com

Note: but you should replace this with your domain name

Here is the user domain for this tutorial : ( m.test.operavps.com )

At This Step; We Need To Set Permissions To Setup Virtual Host On CentOS 7

Create the HTML directory for sample.com as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/sample.com/html

1 virtuall host first command enter section

Create an additional directory to store log files for the site ;

You can use the below command :

sudo mkdir -p /var/www/sample.com/log

2 virtuall host second command enter section

Next, assign ownership of the HTML directory with the $USER environmental variable:

You can use the below command :

sudo chown -R $USER:$USER /var/www/sample.com/html

3 virtuall host third command enter section

Make sure that your webroot has the default permissions set;

You can use the below command :

sudo chmod -R 755 /var/www

4 virtuall host fourth command enter section

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

install nano section 1

Confirm by typing Y and enter it.

install nano confirm section 1

Now the installation of the Nano editor has been completed

install nano installation 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

5 virtuall host 5th command nano config enter section

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>

6 virtuall host 5th command nano config change section

At this time, we are going to save the changes by using Ctrl+X and typing Y to exit from the Nano editor;

7 virtuall host 5th command nano config save and exit section

Now confirm the save request by pressing enter.

8 virtuall host 5th command nano config save and exit section 2

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:

sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled

9 virtuall host 6th command directories

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

10 virtuall host 7th command nano config enter section

In this section; Here you can see we are inside the config section on Nano editor

11 virtuall host 7th command nano config files 1

We have to go to the end of the file to add the file; Now save the changes like at the last time

12 virtuall host 7th command nano config files 2

Start by creating a new file in the sites-available directory;

with the bellow command:

sudo nano /etc/httpd/sites-available/sample.com.conf

Note: We have to use the Nano editor like in previous sections.

Now add in the following configuration, and change the sample.com domain to your domain name:

<VirtualHost *:80>
    ServerName www.sample.com
    ServerAlias sample.com
    DocumentRoot /var/www/sample.com/html
    ErrorLog /var/www/sample.com/log/error.log
    CustomLog /var/www/sample.com/log/requests.log combined
</VirtualHost>

Do these changes like the last moment of using Nano editor.

15 virtuall host 8th command nano config enter section

You can see we are inside the config section on Nano editor;

At first, you will not see anything inside it; Add in the following configuration block, then save the changes and exit from the Nano.

16 virtuall host 8th command nano config files

Create A Symbolic Link For Each Virtual Host

You have created the virtual host files, and you will enable them so that Apache knows to serve them to visitors.

To do this, create a symbolic link for each virtual host in the sites-enabled directory;

With the bellow command :

sudo ln -s /etc/httpd/sites-available/sample.com.conf /etc/httpd/sites-enabled/sample.com.conf

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.

17 virtuall host 9th command create a symbolic link

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

18 virtuall host 10th command Adjusting Apache Policies Universally

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/

19 virtuall host 11th command Adjusting Apache Policies on a directory

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

4 let us find out which package provides the semanage command

Sample output from my CentOS 7 server:

5 Sample output from CentOS semanage

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

1 install dnf

Here we should confirm the installation by typing Y and  press enter button to do it:

2 install dnf confirm section

You can see the installing of  DNF has been completed:

3 install dnf finished 1

Install Policycoreutils-python-2.2.5-11.el7_0.1.x86_64 Package

Using the following command as the root user:

yum install policycoreutils-python

7 install policycoreutils python

Let’s confirm the installation by typing Y and press enter to prove it.

8 install policycoreutils python confirm section

We already installed the policycoreutils-python successfully;

Here we go; You can now use any Semanage commands at the server:

9 install policycoreutils python finished

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(/.*)?"

21 semanage command

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

22 use the restorecon command to apply these changes and have them persist across reboots

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:

23 list the contexts once more to see the changes

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

24 Testing the Virtual Host

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

25 Apache was able to create the error.log and requests.log

We can see that already installed on the graphical place, too, like in the below photo.

2021 02 02 00 10 43 Welcome to m.test .operavps.com

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.

Helpful

About the Author Oscar Borayn

Leave a Reply

Your email address will not be published. Required fields are marked *