Sharing Ilmu

Sharing for useful things

How To Install and Configure Apache2 webserver (httpd) on Centos 8

Hello everyone, in this tutorial I’ll discuss about how to install and configuration of httpd webserver on linux centos 8. I used ip address 11.36.36.10 for this server.

First install httpd packages on your system

[root@server1 ~]# yum install httpd

 

Set enabled and start httpd service

[root@server1 ~]# systemctl enable --now httpd

 

Configure on /etc/httpd/conf/httpd.conf as the following :

[root@server1 ~]# vim /etc/httpd/conf/httpd.conf

 

<Directory "/var/www/html">
.........
 Options Indexes FollowSymLinks
.........
AllowOverride ALL
.........
</Directory>

.....
<IfModule dir_module>
           DirectoryIndex index.html index.php
</IfModule>
......

 

Restart httpd service

[root@server1 ~]# systemctl restart httpd

 

Create Index.html on /var/www/html directory

[root@server1 ~]# echo "TEST MY HTTPD WEBSERVER" > /var/www/html/index.html

 

Configure Firewall for allow httpd service :

[root@server1 ~]# firewall-cmd --permanent --add-port=80/tcp
[root@server1 ~]# firewall-cmd --reload

If you use IPtables on your system, add new configuration for INPUT tcp 80 on /etc/sysconfig/iptables :

[root@server1 ~]# vim /etc/sysconfig/iptables
# Generated by iptables-save v1.8.4 on Wed Oct 5 20:51:38 2022
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [24:2016]
:OUTPUT ACCEPT [887:65352]
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i enp0s8 -p udp -m udp --dport 53 -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited

Now test on your browser :

 

 

Configure Custom Html Directory

In the configuration above, it used  the default html directory. But how if we will to used custom html directory for the web content ?. For example we will to direct the html directory to /web/html.

First configure at  /etc/httpd/conf/httpd.conf

[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
.....

DocumentRoot "/web/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/web/html">
#
# Possible values for the Options directive are "None", "All",
.....
Options Indexes FollowSymLinks

......

</Directory>

 

Create new directory :

[root@server1 ~]# mkdir -p /web/html

Display selinux context on /web/html

[root@server1 ~]# ls -dZ /web/html/
unconfined_u:object_r:default_t:s0 /web/html/

as you can see, file context is default_t. Change to httpd_sys_content_t so that it can be used for html directory.

[root@server1 ~]# chcon -R --reference=/var/www/html/ /web/html/

 

Display selinux context again :

[root@server1 ~]# ls -dZ /web/html/
system_u:object_r:httpd_sys_content_t:s0 /web/html/

 

Create index.html on /web/html directory :

[root@server1 ~]# echo "TEST CUSTOM HTML DIRECTORY FOR MY SERVER" > /web/html/index.html

Restart httpd services :

[root@server1 ~]# systemctl restart httpd

 

Test on your browser :

 

z

Leave a Reply

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

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>