APACHE Server

It is the most widely used web server in the world. Almost 65%-70% of the world’s web server use apache server. Reason it is famous is because its open source (so money saved), highly secure, very fast & very reliable. It can be customized to meet our needs with the help of using various modules & extensions.
To define it in a bit more technical terms, It is a modular, process based web server that creates a new thread everytime a new connection is made.
Apache is also has a major advantage, that it can support multiple website hosting on a single server. There are actually two types of hosting :-

1- IP address based hosting- For IP based hosting, we need to have a different IP for every website that we are hosting. These IPs are then attached to a single or multiple NICs.

2- Name based Virtual hosting- Name based hosting is used to host multiple virtual websites using a single IP address.

In this tutorial, we are going to learn to configure Apache server & host both IP address based websites & name based websites but before we do that, we are going to install apache on our server.

(Recommended Read: Redirect http to https: Apache Server)

Also Read: Localhost 127.0.0.1 – Setup your own Web Server

 

APACHE Installation

There are three different ways in which we can install apache on our RHEL/CentOS machines
1- Using RPM - We need rpm package for it. Once you obtain the rpm package for apache, we can install it using the following command (Learn more about RPM HERE)

$ rpm –ivh httpd-2.4.x-1.i686.rpm

 

2- Using YUM- Yum is the easiest way to install apache on your system. We don’t need to download any package when using yum, just use the following (Learn more about YUM HERE)

$ yum install httpd

 

3- Using source package- This process is a bit complex but provides us with various options to modify our apache installation as we are using source packages to compile & install the apache package. Firstly we need to download source package (Download from here), we will then extract it, compile it & then install it,

$ tar–xvf httpd-2.4.6.tar.gz
$ cd httpd-2.2.26
$./configure --prefix=PREFIX (here you will modify the apache installation)
$ make
$ make install

This way we will install apache from source, alternatively you can also build a rpm package from source package

$ rpmbuild -tb httpd-2.4.6.tar.bz

& then install it using the rpm command, as we did above.
Now we have our apache server ready & will now configure to host websites.

Configure Apache server

Name Based hosting

We will first configure 2 websites (test1.com & test2.com) using the name based hosting. Firstly open the apache configuration file which is /etc/httpd/conf/httpd.conf & add the following lines to the bottom of file,

$ vi /etc/httpd/conf/httpd.conf

 

<VirtualHost 192.168.0.120:80>
ServerAdmin webmaster@test1.com
DocumentRoot /var/www/html/test1.com
ServerName www.test1.com
ErrorLog logs/www.test1.com-error_log
CustomLog logs/www.test1.com-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webmaster@test2.com
DocumentRoot /var/www/html/test2.com
ServerName www.test2.com
ErrorLog logs/www.test2.com-error_log
CustomLog logs/www.test2.com-access_log common
</VirtualHost>
Next, search for the “NameVirtualHost” in the same file & uncomment it by removing ‘#’ from the starting of the line & add the IP of the server, so it should look like

NameVirtualHost 192.168.0.120:80

Now save the file & exit.

We will now create ‘index.html’ file for both the websites but we will firstly create a directory for both the websites,

$ mkdir /var/www/html/test1.com
$ mkdir /var/www/html/test2.com

Now add some content to index.html to identify both the sites,

$ vi /var/www/html/test1.com/index.html

<html>
<head>
<title>This is test1.com</title>
</head>
& do the same for test2.com

$ vi /var/www/html/test2.com/index.html

<html>
<head>
<title>This is test2.com</title>
</head>

You can also type 'this is test1.com or test2.com ' without using html tags. We now have our apache server ready with name based hosting. We will now restart our server to implement changes & will then check our sites

$ systemctl restart httpd.service or
$ service httpd restart

Now, open your browser & enter the website url

www.test1.com
www.test2.com

You should now see two different webpages with their unique contents.

IP based hosting

We need 2 different IPs assigned to our single NIC or 2 different NICs with different IPs to achieve IP based hosting. We are going to use a single NIC card with different IPs assigned to it. if you don’t know how to do it, read our tutorial here.
After we have configured our IP addresses (192.168.0.120 , 192.168.0.125), we will start our configuration by editing ‘/etc/httpd/conf/httpd.conf’

$ vi /etc/httpd/conf/httpd.conf

& search for ‘Listen 80’ . Next uncomment it & add IP address, so it should look like

Listen 192.168.0.120:80

& add the following server configurations to the bottom of the same file i.e. httpd.conf

 

VirtualHost 192.168.0.120:80>
ServerAdmin webmaster@test1.com
DocumentRoot /var/www/html/test1
ServerName www.test1.com
ErrorLog logs/www.test1.com-error_log
TransferLog logs/www.test1.com-access_log
</VirtualHost>

<VirtualHost 192.168.0.125:80>
ServerAdmin webmaster@test2.com
DocumentRoot /var/www/html/test2
ServerName www.test2.com
ErrorLog logs/www.test2.com-error_log
TransferLog logs/www.test2.com-access_log
</VirtualHost>

Create index.html for both websites in their respective folders & write content to it , as we did above & lastly restart your apache service for changes to take effect.
Open your browser & access your websites, either using name or IP address.

That’s it guys, we have successfully created installed & configure apache server . Please leave your queries/comments below in our comment section.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter - Donate us some of you hard earned money: [paypal-donation]

Linux TechLab is thankful for your continued support.