• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

How to install apache webserver in CentOS and Red Hat

August 20, 2013 by Sharad Chhetri Leave a Comment

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

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. How to install and remove Apache webserver in Ubuntu and Debian
  2. How to install nginx webserver from source on CentOS and RHEL
  3. How to install and configure nagios nrpe in CentOS and Red Hat
  4. How to install whois client in CentOS and Red Hat
  5. Install MySQL Server 5.6 in CentOS 6.x and Red Hat 6.x Linux
  6. How to add/install Ubuntu fonts in CentOS/Red Hat Linux
  7. Install Ubuntu font family for individual user in CentOS/Red Hat
  8. lock user login after failed login attempts in Red Hat 6.x and CentOS 6.x
  9. How to find when Operating system was installed in linux CentOS and Red Hat
  10. Set GRUB password after installation of CentOS/Red Hat

Filed Under: Apache, Linux

Reader Interactions

Leave a Reply Cancel reply

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

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

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

Allow only members of Wheel group to use su command on RHEL/CentOS

Linux / Unix : whoami command use and advantage

Search keyword in all available man pages – man command

6 important examples of cd command on Linux and Unix Systems

grep command to show lines after and before the keyword

awk command to search keyword or strings in file

group ownership reference to file/dir from other file/dir in linux

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

df command not showing correct free space in linux

How to change hostname in Ubuntu 12.04 and 12.10 without system restart

How To Get SSH Public Key Fingerprint Information

Non interactive ,without typing password do ssh to Server : By sshpass

How to install korn shell ksh in Linux

send email after mysql backup through bash script in simple way

Convert hyphen to underscore in between all filenames shell script

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

Linux basic command

Linux Basic Commands For Every Beginner

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy