How to install apache webserver in CentOS and Red Hat

In this tutorial we will learn about how to install apache webserver.
Web server as the name says it is server which is used for web services. The websites and web applications required web server to run.
Here for webserver we will install apache. There are other web server which are also widely use in linux like nginx,lighttpd,cherokee etc.

Web Server details after installation:

By Default port: 80
Document Root: /var/www/html
Default user name: apache
Configuration File: /etc/httpd/httpd.conf
Note: You can also create a configuration file in /etc/httpd/conf.d

Server Details:

IP Address: 10.0.0.22
Operating System: CentOS release 6.4 (Final)
Arch: i686

Note: The practical has been performed in CentOS with minimal installation.

Follow the given below steps to install your first Apache web server

Step 1 To install Apache Web Server ,use given below command

yum install httpd

Set SELINUX for apache

setsebool -P allow_httpd_anon_write=1

Set IPTABLE for Web Server

For Temporary IPTABLE setting (means the iptable rule for http will be gone after server restart)

[root@webserver ~]# iptables -A INPUT -p 80 -j ACCEPT
[root@webserver ~]# iptables -A OUTPUT -p 80 -j ACCEPT
[root@webserver ~]# iptables-save

For Permanent IPTABLE setting

Edit the file /etc/sysconfig/iptables and append the given below 2 lines

-A INPUT -p tcp –dport 80 -j ACCEPT
-A OUTPUT -p tcp –dport 80 -j ACCEPT

Reference of my system, See the line number where I append the iptables rule
Note: I used INPUT rule only in INPUT Section, for OUTPUT I append the iptable rule below FORWARD line

[root@webserver ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A OUTPUT -p tcp --dport 80 -j ACCEPT
COMMIT
[root@webserver ~]# 

Now check the iptables rule by using below given command

iptables -nL

See the below given reference from my server.

[root@webserver ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
[root@webserver ~]# 

How to start,stop,restart HTTP service or Apache service

For Starting HTTP service

service httpd start
or
/etc/init.d/httpd start

For stopping HTTP service

service httpd stop
or
/etc/init.d/httpd stop

For restarting HTTP service

service httpd restart
or
/etc/init.d/httpd restart

For checking HTTP service status

service httpd status
or
/etc/init.d/httpd status

When you start the httpd service, you may get the below given error message

[root@localhost ~]# /etc/init.d/httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for webserver
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

To solve this issue, set the FQDN of Web server means set the Fully qualified domain name of your machine
To set FQDN and hostname, you have to follow the given steps

My server IP Address is 10.0.0.22 (Replace the IP address with your machine IP address)
Domain name: For practical purpose I am using domain name example.com (Replace with your domain name or you can also use this one for practice purpose)

Hostname: webserver
FQDN: webserver.example.com

To know about host name and FQDN read this post (The concept is equally applicable to CentOS and Red Hat)

In /etc/hosts file ,append the IP address and give Hostname and make FQDN name as per given below format.

[root@localhost ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.22   webserver.example.com webserver

Now edit /etc/sysconfig/network and give host name information in HOSTNAME

[root@localhost ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=webserver
[root@localhost ~]# 


Restart the server so that it could completely set the hostname and FQDN

init 6

or

shutdown -r now

Once the server is restarted, check the hostname and FQDN. See the given below format for Hostname and FQDN

hostname command for checking hostname
hostname -f command for checking FQDN

[root@webserver ~]# hostname
webserver
[root@webserver ~]# hostname -f
webserver.example.com
[root@webserver ~]# 

Now start or restart the httpd service

service httpd start

or

/etc/init.d/httpd start

Check the status of httpd service

service httpd status
or
/etc/init.d/httpd status

Reference from my server:

[root@webserver ~]# /etc/init.d/httpd start
Starting httpd: [ OK ]
[root@webserver ~]#
[root@webserver ~]# /etc/init.d/httpd status
httpd (pid 1372) is running…
[root@webserver ~]#

Enable the http service in Runlevel

To enable the http service in runlevel so that when system restart at particular runlevel the service must be running

chkconfig httpd on

Note: Above given command will enable the service in Run level 2,3,4 and 5

Reference of my system

[root@webserver html]# chkconfig --list|grep http
httpd          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@webserver html]# chkconfig httpd on
[root@webserver html]# chkconfig --list|grep http
httpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@webserver html]#

check the welcome page in the web browser

Now open the web browser and in URL type –

http://webserver-ipaddress

You will get your first Welcome page

apache
apache

Create your first index.html page

Now create your first index.html page for further testing

vi /var/www/html/index.html
<html>
<title>
Test page
</title>
<body>
<h2> Linux</h2>
 
<p>
Linux  is a Unix-like computer operating system assembled under the model
of free and open source software development and distribution. The defining
component of Linux is the Linux kernel,an operating system kernel first
released on 5 October 1991, by Linus Torvalds.Since the C compiler that builds
Linux and the main supporting user space system tools and libraries
originated in the GNU Project, initiated in 1983 by Richard Stallman, the Free
Software Foundation prefers the name GNU/Linux when these tools and libraries are used.
</p>
</body>
</html>


Change the group and ownership of index.html file

chown apache:apache /var/www/html/index.html

Now refresh the webbrowser or type in URL again http://ip-address-ofwebserver
You will get this page

apache2

Note: It is good practice to change the ownership and group of file. With the working experience in various web apps,CMS etc. you will know how and which file will get permission and ownership.

Important Note: To learn more about SELINUX , use the given below reference

http://fedoraproject.org/wiki/SELinux/apache

http://wiki.centos.org/HowTos/SELinux

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.