How to host multiple web sites on single server using Apache?

Ans : We can host multiple sites on single server. This can be done using multiple Virtual hosts with different host-names pointing to different directories in the server. In this post we taken two hosts to test this.

Step1 : check  the package is  installed or not

#rpm -qa | grep httpd

Step2 : Install the package

#yum install httpd

Step3 : For host based web access we require DNS  server entries of our host.

Example host in this post is : server1.example.com(Actual server name) and hostbase1.example.com(our first virtual host name)

hostbase2.example.com(our second virtual host name)

So we have to give A record for the server1.example.com(192.168.0.1) then CNAME record for hostbase1.example.com and hostbase2.example.com which points to server1.example.com

Step3 : Create the root web directory and an index.html for this testing for both the virtual hosts.

I want to create my web directory in /hostnamebase1 for first virtual host and /hostbase2 for second virtual host

#mkdir /hostnamebase1

Now create index.html and try to write something which is meaning full in that file then save the file.

#vi /hostnamebase1/index.html

save and exit the file.

#mkdir /hostnamebase2

Now create index.html and try to write something in to that file then save the file.

#vi /hostnamebase2/index.html

save and exit the file.

Step4 : Now edit the httpd.config file

#vi /etc/httpd/conf/httpd.conf

Step4(a) : Please specify the NameVirtualHost at any point in the file if this entry is not there or commented.

NameVirtualHost 192.168.0.1

What is the significance of this entry?

Ans : This directive is set to IP address/actual system name if you want to configure your server to host name based virtual hosting.  So here I am using IP address of my system. You can try to put actual host name if you want to test it.

Step4(b) : Now configure virtual host entry as shown below.

Go to last line and write the below content

for hostbase1..

<VirtualHost 192.168.0.1>

ServerName hostbase1.example.com

DocumentRoot /hostnamebase1/

DirectoryIndex index.html

</VirtualHost>

Now For hostbase2

<VirtualHost 192.168.0.1>

ServerName hostbase2.example.com

DocumentRoot /hostnamebase2/

DirectoryIndex index.html

</VirtualHost>

Save the file and exit.

Let me explain ServerName entry

ServerName hostbase1.example.com this is used to specify what is the virtual host name of your new Virtualhost.  So user can type this FQDN to access this site.

Step5 : Check for the syntax errors in the httpd.conf file before restarting the apache service.

#httpd -t

or

#httpd -k graceful

Step6 : Now start the service and then add it to booting scripts so that it will start automatically at every boot of the system

#service httpd restart

#chkconfig httpd on

Step7 : Now check the site using http://hostbase1.example.com and http://hostbase2.example.com in your browser.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.