CATEGORIES

TAGS

SSL

PostgreSQL

cPanel

email

Error

WHM

Linux

Bash

Console

Laravel

ARCHIVE

2019
2018

How to set up Apache virtual server on CentOS 7

30th August
Linux
How to set up Apache virtual server on CentOS 7

Before you will set up Apache virtual server you should complete some steps:

At first, I recommend you to go and sleep.

The second main thing that you need is a CentOS server account with “sudo” privileges.

Then you need to install Apache. Use this command to do that:

Copied!

sudo yum –y install httpd



We recommend you to use the following command to enable Apache start automatically on boot time:
Copied!

sudo systemctl enable httpd.service



if you have done all these steps you should log in into your server. If you use the terminal of CentOS use the command “ssh” in Windows OS you can download “PuTTY” and use it for connecting to the server.

Step 1


Let’s create the directory structure

You should create all your files and directories for your site into /var/www. Because it is the top-level directory which Apache search to find the content to serve. Let’s create there the directory for our tutorial. Use the command “mkdir” to create some directories like this:

Copied!

mkdir –p /var/www/domainofyourtestsite.com/public_html



but I want to create just one more directory

Copied!

mkdir –p /var/www/domainofyourtestsite2.com/public_html



Are you interested in what is “-p”

INFO: the command “mkdir” can create just one directory but if you want to create nested directories you should use “-p”.



Step 2


Permission.

The Irony of Fate!!! we have created the directories but we haven’t any permission to modify the files in it. The problem is that files are owned by the root user. The solution is to change the ownership of that files by using the command “chown” like this:

Copied!

sudo chown –R $USER:$USER /var/www/domainofyourtestsite.com/public_html



And

Copied!

sudo chown –R $USER:$USER /var/www/domainofyourtestsite2.com/public_html



Now let’s change your permissions to be sure that you can make changes into that files. Use the command “chmod”: there are several ways to do that I prefer the following way:

Copied!

sudo chmod 755 /var/www



step 3


creating demo page.

let’s create an index.html file for each of our domains (domainofyourtestsite.com and domainofyourtestsite2.com). In this tutorial, I will use “nano” to edit files. You may use your favorite text editor:

Copied!

nano /var/www/domainofyourtestsite.com/public_html/index.html



in this file you need to create some html document like this:



Save and close that file.

INFO: to save your document with “nano” use “ctrl+o” then “ctrl+x” to exit.



you can use the command “nano” again and create the same document into “domainofyourtestsite2.com” or copy this content and make changes there like this:

Copied!

cp /var/www/domainofyourtestsite.com/public_html/index.html /var/www/domainofyourtestsite2.com/public_html/index.html



we have just copied the file, now let’s make some changes:

Copied!

nano /var/www/domainofyourtestsite2.com/public_html/index.html



then:



Save and close this file too. Now you have some pages to test your host configuration.

Step 4


Creating new files for virtual host.
In this step, we will create two directories by name “sites- available” and “sites-enabled”. In the first directory you need to add all your virtual host files, and in the second you need to keep the symbolic links of the virtual host the one you want to publish. Use the commands below to create the both of directories:

Copied!

sudo mkdir /etc/httpd/sites- available



and:

Copied!

sudo mkdir /etc/httpd/sites-enabled



now you should show Apache that it must look for virtual hosts in the “sites-enabled” directory. To do that you need to make changes into Apache’s main configuration file like here:
Copied!

sudo nano /etc/httpd/conf/httpd.conf



just add this line to the end of this file: >

Copied!

IncludeOptional sites-enabled/*.conf



Save and close the file. Now you are allowed to create your first virtual host file. Let’s open the new file in your editor which have root privileges.

Copied!

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



Now copy this bunch of code there then save and close that file:



Let’s understand what it means.



these two lines are responsible for designating the content as a virtual host which is listening on port 80 (80 is the default HTTP port)



Via this two lines you say that your main server name is “www.domainofyourtestsite.com” and at the same time, you make an alias for the server to point “domainofyourtestsite.com” to. Now the request for “www.Domainofyourtestsite.com” and “domainofyourtestsite.com” will show the same content.



These three lines tell Apache where to store the error page and request logs for this particular site.

Now let’s copy this content for second virtual server and make corresponding changes for it. Use this command:

Copied!

sudo cp /etc/httpd/sites-available/domainofyourtestsite.com.conf /etc/httpd/sites-available/domainofyourtestsite2.com.conf



Open this file to with command “nano”:

Copied!

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



now you should modify that file like this:



After modifying the file save and close it.

Step 5


Enable the new virtual host files.

To enable these files you need to create a symbolic link into “sites-enabled” directory like this:

Copied!

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



And

Copied!

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



After these changes you need to restart Apache:

Copied!

sudo apachectl restart



step 6


Testing.

Now you can search “domainofyourtestsite2.com” with your browser. You will see this:

success!!!! We have done it!!! That is all!!! Now you can go to drink a cup of coffee!!!


SUBSCRIBE by email and never miss a post!