Hi all,

Nice to see you all on my another blog post

Here I’m going to guide step by step guidance to setup a virtual host in Cent OS based server

Requirements:

  • A Cent OS 6 server with SSH access
  • Putty/SSH client

What is virtual host?

First of all we need to understand what is virtual host, Virtual host is an site which will display the various kind of information’s to the visitor depending on the site

Virtual host can be accessible via server IP address directly from the browser and also there is no limitation in the number of virtual hosts creation in the server

Before starting up the process make sure you have root access to the server, IF you don’t have the root access then you can’t perform the below commands or else communicate the root user to grant root privileges to your user

Before starting the steps make sure your server had already installed Apache, If its not installed then you can install using below command

yum install httpd

Step 1: Creating a directory:

Before startup the virtual host we need to create a new directory, this is the directory which will keeps your website information

And also this will be the document root in the Apache configuration for the virtual host

<pre>mkdir -p /var/www/example.com/public_html</pre>

Step 2 :Granting permissions

Once the directory created we need to grant permission for the directory, without granting permission user can’t access the directory so granting permission to the directory is an important one

chown -R apache:apache /var/www/example.com/public_html

And then we need to give permission to read our new fles to the users, for that execute the below command

chmod 755 /var/www

Now we have done the permission part

Step 3 : Creating the Index Page

We have created a directory and then we done the permissions, now we need to create a index page

For that kindly create a new file using below command

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

Once the above command executed index.html file will be created and the editor will shows the blank page

Here we need to enter our welcome script/information about your website

<html>
<head>
<title>www.example.com</title>
</head>
<body>
<h1>You have set up a virtual host successfully</h1>
</body>
</html>

Once you entered the all information save and quit from the nano editor

Step 4 :Virtual host configuration

This is the important step which is an configuring virtual host

For that we need to edit the Apache configuration file

Open the Apache configuration file using nano/vi editor using below command

nano /etc/httpd/conf/httpd.conf

Scroll down to the very bottom of the httpd.conf file to reach the virtual host configuration

NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
DocumentRoot /var/www/yourdomain.com/public_html
ServerName www.yourdomain.com
ServerAlias yourdomain.com
ErrorLog /var/www/yourdomain.com/error.log
CustomLog /var/www/yourdomain.com/requests.log
</VirtualHost>

Step 5 : Restarting Apache

Once we completed the virtual host setup we need to rebuild the Apache setup by restarting the httpd service

service httpd restart

That’s all, we setup an virtual host successfully

Now we can test our index.html page by accessing the server IP via local browser

If you successfully completed the process you can see the index.html content on your browser at the time of accessing via browser

If you have any queries related to this blog-post don’t worry just share your queries in the comment box I will respond you as earlier and also don’t forget to share this post on social medias

Leave a Comment