Monday, 13 April 2015

HTTP (httpd) basic configuration in RHEL7/CentOS7

 Apache config files are located in '/etc/httpd/conf/' and '/etc/httpd/conf.d/' directories. The main config file is '/etc/httpd/conf/httpd.conf'.

Apache log files are located in '/var/log/httpd/' directory. The main log files are '/var/log/httpd/error_log' and '/var/log/httpd/access_log'.

Apache manual is available in 'httpd-manual' package. To access the manual, navigate to 'http://localhost/manual'.  

In this tutorial,we will configure Apache Web Server in RHEL 7 for basic operation. Apache looks for web pages in the directory specified by the DocumentRoot directive. By default, this directive points to the '/var/www/html' directory. So to get the web server up and running, just transfer web pages to the '/var/www/html' directory.

The SELinux context for the /var/www directory is httpd_sys_content_t. Run the ls -lZ /var/www command to view the SELinux context.  When you create a dedicated directory to store the web pages for a web site, ensure that the directory has the 'httpd_sys_content_t'  context. In the 2nd part of this tutorial, we will see how to create a dedicated web directory.

Consider the following scenario:
HTTP Server IP Address:
                      Host Name:

Server Configuration:

1)  Install httpd
[shabbir@server2 ~]$ sudo yum -y install httpd httpd-manual

2) Edit the config file '/etc/httpd/conf/httpd.conf' and make the following changes:


DocumentRoot "/var/www/html"       

3) Check config file syntax errors
[shabbir@server2 ~]$ sudo httpd -t

4) Create a file 'index.html' in /var/www/html'
              <h1> Hello World  </h1>

5) Open HTTP (Port 80) in firewall
[shabbir@server2 ~]$ sudo firewall-cmd --zone=public --add-service=http --permanent
[shabbir@server2 ~]$ sudo firewall-cmd --reload

6)  Start httpd
[shabbir@server2 ~]$ sudo systemctl start httpd

7)  Enable on boot
[shabbir@server2 ~]$ sudo systemctl enable httpd

Client Configuration:

1)  Make entry in '/etc/hosts' if DNS Server is not configured


2)  Install command-line browser 'elinks'
[shabbir@meru ~]$ yum -y install elinks

3) Browse web site
[shabbir@meru ~]$ elinks

Creating a dedicated web directory:

In the above tutorial, we stored web pages in the default dir '/var/www/html'. We will now create a dedicated dir '/mysite' for the above web site.

On the Web Server, perform the following steps:

1) Create dir to store web pages
[shabbir@server2 ~]$ sudo mkdir /mysite

2) Change SELinux file context
[shabbir@server2 ~]$ sudo chcon -t httpd_sys_content_t /mysite

3) Create a file '/mysite/index.html'
              <h1> Hello World. mysite  </h1>

4) Edit the config file '/etc/httpd/conf/httpd.conf' and make the following changes:

DocumentRoot "/mysite" 

<Directory "/mysite">
#allow open access
Require all granted      

5) Restart web server.
[shabbir@server2 ~]$ sudo systemctl restart httpd

No comments:

Post a Comment