Apache Http Server is an open-Source Web server that is being used by many organizations for hosting their applications. Apache Http server when used with a J2ee application server as a back end can provide lot of features to the applications and developers. In this article we will see how we can install and configure apache http server in RHEL5.
If you need to configure apache from source, check here.
- Install the Http Server (Or) check whether it is available
- Starting and Stopping Server.
- Construct an Site www.sam.com and associate an IP address to it
- Construct multiple Sites with Same IP address
- Construct an Alias to the Site.
1. Install the Http Server (Or) check whether it is available
The Http server packages are already available in the Linux. We just need to find whether they are available .Once we are sure they are available we need to install the server. The packages that we need to check are httpd, httpd-devel and apr.
We can check using,
Yum list installed | grep httpd
If they are already installed, then the list will be shown by using this command.
2. Starting and Stopping Server
In Order to start the server we can use,
Service httpd start
service httpd restart : restarts the Server
service httpd stop : Stops the Server
The configuration file is available in location /etc/httpd/.
3. Construct a Site www.sam.com and associate an IP address to it
Now we will see how we can construct a sample site .First create an html file for the site. The files that apache web server will check will be available in /var/www/ location. For every site we create we can create files here or we can create some other places and provide the location in the configuration file. Create the directories and files like,
mkdir -p /var/www/virtual/www.sam1.com/html
Create an index.html page with some sample html content.
vi /var/www/virtual/www.sam1.com/html/index.html
<html><head>www.sam1.com</head>
<body>
<b>www.sam1.com
</body></html>
Now it’s time to edit the configuration file, open the configuration file like,
vi /etc/httpd/conf/httpd.conf
Search for a term NameVirtualHost
Uncomment the NameVirtualHost and add the ip address besides it like
NameVirtualHost 10.5.11.119 (This is the ip address on which we are hosting our application)
Note: The NameVirtualHost allows hosting multiple sites with single IP address.
Once the NameVirtualHost is uncommented.Add the VirtualHost element at the end of the file likes,
The Virtual host configuration block is used to create an independent set of configuration directives that apply to a particular hostname or IP address. It is often used in conjunction with system level IP aliasing or dummy network interfaces in order to establish one or more "virtual" servers which all run on the same physical machine. Add the following at the end of the file,
<VirtualHost *:8080>
ServerAdmin root@www.sam1.com
DocumentRoot /var/www/virtual/www.sam1.com/html/
ServerName www.sam1.com
ErrorLog logs/dummy-www.sam1.com-error_log
CustomLog logs/dummy-www.sam1.com-access_log common
</VirtualHost>
The elements are pretty much explanatory. The ‘DocumentRoot’ is the file that will be displayed for the user when he enter the site.It is the index.html file that we created in the location /var/www/virtual/www.sam1.com/html/.The ServerName is the name of the host and others belong to logs locations.
Once the configuration in the httpd.conf file is done, we need to make changes to the /etc/hosts file.
The hosts file is a computer file used in an operating system to map hostnames to IP addresses.
Vi /etc/hosts and add the following at the end,
192.168.0.254 www.sam1.com
Once these changes are done, we need to restart the network and http server like,
Service network restart
Service httpd restart
Once these are ok with no issues, try accessing www.sam1.com from a browser and we will see the index.html page.
4. Construct multiple Sites with Same IP address
Create file locations for the second site like,
mkdir -p /var/www/virtual/www.sam2.com/html
Create an index.html page with some sample html content.
vi /var/www/virtual/www.sam2.com/html/index.html
Once these are done, modify the /etc/hosts file and add a new host like,
# do not remove the following line, or various programs
# that require network functionality will fail.
10.5.11.119 www.sam1.com
10.5.11.119 www.sam2.com
Once the file is modified, restart the network. After this update the httpd.conf file with this,
<VirtualHost 10.5.11.119>
ServerAdmin root@www.sam2.com
DocumentRoot /var/www/virtual/www.sam2.com/html/
ServerName www.sam2.com
ErrorLog logs/dummy-www.sam1.com-error_log
CustomLog logs/dummy-www.sam1.com-access_log common
</VirtualHost>
Restart the httpd server and access www.sam2.com.
5. Construct an Alias to the Site.
In order to create an alias for a site, we need modify the /etc/hosts and httpd.conf file like
10.5.11.119 www.sam1.com
10.5.11.119 www.sam2.com www.sam3.com
I have added the www.sam3.com as an alias to www.sam2.com
Restart the network and edit the httpd.conf file.For the www.sam2.com site we are creating an alias.
<VirtualHost 10.5.11.119>
ServerAdmin root@www.sam2.com
DocumentRoot /var/www/virtual/www.sam2.com/html/
ServerName www.sam2.com
ServerAlias www.sam3.com
ErrorLog logs/dummy-www.sam1.com-error_log
CustomLog logs/dummy-www.sam1.com-access_log common
</VirtualHost>
Once done, restart httpd server and access www.sam2.com and www.sam3.com which gives us the same pages.