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:
sudo yum –y install httpd
We recommend you to use the following command to enable Apache start automatically on boot time:
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.
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:
mkdir –p /var/www/domainofyourtestsite.com/public_html
but I want to create just one more directory
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”.
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:
sudo chown –R $USER:$USER /var/www/domainofyourtestsite.com/public_html
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:
sudo chmod 755 /var/www
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:
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:
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:
Save and close this file too. Now you have some pages to test your host configuration.
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:
sudo mkdir /etc/httpd/sites- available
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:
sudo nano /etc/httpd/conf/httpd.conf
just add this line to the end of this file: >
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.
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:
sudo cp /etc/httpd/sites-available/domainofyourtestsite.com.conf /etc/httpd/sites-available/domainofyourtestsite2.com.conf
Open this file to with command “nano”:
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.
Enable the new virtual host files.
To enable these files you need to create a symbolic link into “sites-enabled” directory like this:
sudo ln –s /etc/httpd/sites-available/domainofyourtestsite.com.conf /etc/httpd/sites-enabled/domainofyourtestsite.com.conf
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:
sudo apachectl restart
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!!!