How To Set Up Apache Virtual Hosts on CentOS 6

                                           

About virtual Hosts

Virtual Hosts are used to run more than one domain using a single ip address. This is especially useful to people who  need to run several sites with  one  virtual private server. The sites display different information to the visitors, depending on with which the user accessed the site.There is no limit number of virtual hosts that can be  added to a VPS.

 

Before you proceed, These are basic commands kindly click Commands Link

step 1:  Install Apache

# yum install httpd

Step 2: Create new directory

The Second step  is create a directory for virtual host, In that directory we will keep the new website’s information

# mkdir -p /var/www/domain.com/public_html

 

You will need to designate a actual  DNS approved domain

 

You can use unapproved domain name to test the process . It need some changes in local pc and see the optional step 7

Step 3: Grant Permission

We need to grant ownership of the directory to the user

# chown -R apache:apache /var/www/domain.com/public_html

 

Additionally, it is important to make sure that everyone will be able to read our new files

# chmod 755 /var/www

 

Now you all done with permission

 Step 4: Create the page

We need to create a new file called index.html within our configuration directory.

# nano /var/www/domain.com/public_html/index.html

 

We can add some text to the file, It will display the content when access the website or ip in browser URL

<html>
<head>
<title>www.domain.com</title>
</head>
<body>
<h1>Success: You Have Set Up a Virtual Host</h1>
</body>
</html>

 

Save and Exit

Step 5: Setting up the Virtual Hosts

The  next step is to enter into the apache configuration file

# nano /etc/httpd/conf/httpd.conf

There are few lines to look for.

 

Make sure that below text available or not

#Listen 12.34.56.78:80

Listen 80

 

Scroll down to the very last of the document to the section called Virtual Hosts.

#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 webmaster@example.com
#DocumentRoot /var/www/domain.com/public_html
#ServerName www.domain.com
#ServerAlias domain.com
#ErrorLog /var/www/domain.com/error.log
#CustomLog /var/www/domain.com/requests.log
</VirtualHosts>

 

The most important lines to focus on NameVirtualHost, VirtualHost, Document Root, and ServerName.

 

You need to uncomment the (#) before the line NameVirtualHosts.

 

It should be,

NameVirtualHosts *:80

And also uncomment (#)  before <VirtualHosts *:80> ..  .  . </VirtualHosts>

It should be,

<VirtualHost *:80>
ServerAdmin webmaster@domain.com
DocumentRoot /var/www/domain.com/public_html
ServerName www.domain.com
ServerAlias doamin.com
ErrorLog /var/www/domain.com/error.log
CustomLog /var/www/domain.com/requests.log
</VirtualHost>

 

Step 6: Restart the Apache

We have made a lot of the changes to the configuration. It will not effect untill Apache is restarted

 

First stop all apache processes:

# apachectl -k stop

 

Then start up apache once again.

# service httpd start

Optional step 7: Setting Up the Local Hosts

Its for Windows user only,

 

a). Press the Windows key.

 

b).Type Notepad in search field.

 

c).In search result , right click Notepad and select Run as administrator.

 

d).From Notepad, open the following file: c:\windows\System32\Drivers\etc\hosts

 

e).Make the necessary changes to the file

 

(i.e)

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost

#Virtual Hosts
12.34.56.789                        www.domain.com

 

Save it

Step 8: Results of the Virtual Host

 

Type  your ip address into the browser (ie. http://12.34.56.789)

 

 

 

 

 

 

 

 

 

Leave a Reply

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