Wednesday, September 3, 2014

CentOS : How to install Apache webserver in CentOS and Red Hat

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

Set SELINUX for apache

Set IPTABLE for Web Server

For Temporary IPTABLE setting (means the iptable rule for http will be gone after server restart)
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
Now check the iptables rule by using below given command
See the below given reference from my server.

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

For Starting HTTP service
For stopping HTTP service
For restarting HTTP service
For checking HTTP service status
When you start the httpd service, you may get the below given error message
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.
Now edit /etc/sysconfig/network and give host name information in HOSTNAME

Restart the server so that it could completely set the hostname and FQDN
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
Now start or restart the httpd service
Check the status of httpd service
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
Note: Above given command will enable the service in Run level 2,3,4 and 5
Reference of my system

check the welcome page in the web browser

Now open the web browser and in URL type -
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

Change the group and ownership of index.html file
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

No comments: