![O3wOsNgmY3 1601326034](https://operavps.com/wp-content/uploads/2021/02/O3wOsNgmY3_1601326034.jpg)
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](https://operavps.com/wp-content/uploads/2021/02/1-virtuall-host-first-command-enter-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/2-virtuall-host-second-command-enter-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/3-virtuall-host-third-command-enter-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/4-virtuall-host-fourth-command-enter-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/install-nano-section-1.png)
Confirm by typing Y and enter it.
![install nano confirm section 1](https://operavps.com/wp-content/uploads/2021/02/install-nano-confirm-section-1.png)
Now the installation of the Nano editor has been completed
![install nano installation completed](https://operavps.com/wp-content/uploads/2021/02/install-nano-installation-completed.png)
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](https://operavps.com/wp-content/uploads/2021/02/5-virtuall-host-5th-command-nano-config-enter-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/6-virtuall-host-5th-command-nano-config-change-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/7-virtuall-host-5th-command-nano-config-save-and-exit-section.png)
Now confirm the save request by pressing enter.
![8 virtuall host 5th command nano config save and exit section 2](https://operavps.com/wp-content/uploads/2021/02/8-virtuall-host-5th-command-nano-config-save-and-exit-section-2.png)
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](https://operavps.com/wp-content/uploads/2021/02/9-virtuall-host-6th-command-directories.png)
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](https://operavps.com/wp-content/uploads/2021/02/10-virtuall-host-7th-command-nano-config-enter-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/11-virtuall-host-7th-command-nano-config-files-1.png)
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](https://operavps.com/wp-content/uploads/2021/02/12-virtuall-host-7th-command-nano-config-files-2.png)
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](https://operavps.com/wp-content/uploads/2021/02/15-virtuall-host-8th-command-nano-config-enter-section.png)
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](https://operavps.com/wp-content/uploads/2021/02/16-virtuall-host-8th-command-nano-config-files.png)
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](https://operavps.com/wp-content/uploads/2021/02/17-virtuall-host-9th-command-create-a-symbolic-link.png)
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](https://operavps.com/wp-content/uploads/2021/02/18-virtuall-host-10th-command-Adjusting-Apache-Policies-Universally.png)
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;
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.