• Home
  • RHCE Work With Apache HTTPD Web Service Part 5-Intergrating Dynamic web Content Dev Ops Lanka

RHCE Work With Apache HTTPD Web Service Part 5-Intergrating Dynamic web Content Dev Ops Lanka

Apache HTTPD serving dynamic web content

What is dynamic web content ?

Previous lessons https://devopslanka.com/category/apache-httpd/.we have discuss how to serve static html content using Apache httpd. But most of the website in internet does not serve static web pages. They have serve dynamic webpages that interact with the user inputs and requirements.as example think about user login page . There is user login page that have created with html ,css ,java script and ajax and more.When user enter user name and password user input fields captured and analyze with back end.May be back end can be php,python or any web development language.And compare with the records on the data base  .Data base solution can be MY SQL ,Mongo DB or anything.After authenticate user then showing next page.This is basic scenario of dynamic web content.

Common Gateway Interface(CGI)

This is the oldest method of generating the dynamic web content called as common gateway interface AKA CGI.if cgi content requested apache httpd does not server the CGI Content.CGI content most of time written by scripting language like Python ,Perl even bash script also run. To serve cgi content apache httpd should have to configure.

Set Up Apache HTTPD CGI on RHEL 7

Previous post i have discuss how to add ssl to domain and serve ssl with apache httpd.Today im continue with the previous post.if you are not refer that just take a look at previous post..


Now we have devopslanka.com with SSL.Because i have add DNS record to the /etc/host file


Today im going to execute shell script via navigating the https://devopslanka.com/cgi-bin/time.sh

Ok lets get’s started

First grep and check ScriptAlias for /etc/httpd/conf/httpd.conf

cat /etc/httpd/conf/httpd.conf |grep "ScriptAlias"

it will show result like this.

so there is

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


in default httpd.conf file so if we put shell script there.it should execute and give a result.lets display today date and time

create file

nano /var/www/cgi-bin/time.sh

First we are defining what type of script is this this is bash script so we add #!/bin/bash.Then we are teling what type of content should provide this is html or you can use text.


echo "Content-type: html"
echo "This is server Time : `date`"

After that add executable permission to the bash file

chmod +x /var/www/cgi-bin/time.sh

Then run script manually and check its working.

cd /var/www/cgi-bin


This is result

Now lets try to execute this script via url

first restart the apache httpd

systemctl restart httpd

then open web browser and navigate to


But instead of executing the script web browser consider this as normal web file.


CGI script downloads instead executing

simply change the extension time.sh to time.cgi

then navigate to https://devopslanka.com/cgi-bin/time.cgi

How to Running scripts outside of the cgi-bin default directory

Lets create different directory inside /home/myscript

mkdir -p /home/myscript

then check context type for previous time.sh file created inside /var/www/cgi-bin/time.sh

ll -Z /var/www/cgi-bin/
-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 time.cgi

context type is httpd_sys_script_exec_t

ok lets copy /var/www/cgi-bin/time.sh file to /home/myscript/time.sh

cp /var/www/cgi-bin/time.cgi /home/myscript/time.sh

check context type of /home/myscript/time.sh

ll -Z /home/myscript/time.sh
-rwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/myscript/time.sh

see new file don’t have context type

lets add context type to it

search man page for semanage example

man -k semanage

select man page semanage-fcontext

man semanage-fcontext

then shift+g to go down

there is example

# semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
# restorecon -R -v /web

change example as your requirement

semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"


get context type from cgi-bin folder and change it as below

ll -Z /var/www/cgi-bin/
-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 time.cgi
[root@server0 cgi-bin]# semanage fcontext -a -t httpd_sys_script_exec_t "/home/myscript(/.*)?"

after that execute restorecon -R -v /web

[root@server0 cgi-bin]# restorecon -R -v /home/myscript
restorecon reset /home/myscript context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_script_exec_t:s0
restorecon reset /home/myscript/time.sh context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_script_exec_t:s0

Now check context type again for new location /home/myscript

ll -Z /home/myscript
-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 time.sh

now context type is changed.

Ok now lets go back to /etc/httpd/conf/httpd.conf file and change ScriptAlias for our custom one before changing the script alias i have commented previous one for backup.

this is original one

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

i’m changing this to this

ScriptAlias /sam/ "/home/myscript/"

And addition im adding directory tag also.so everything looks like this

    #ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
ScriptAlias /sam/ "/home/myscript/"
<Directory "/home/myscript">

    AllowOverride None
    Options None
    Require all granted

check for syntax errors

 httpd -t
Syntax OK

Then restart apache httpd

systemctl restart httpd

Now open web browser and navigate to https://devopslanka.com/sam/time.sh

but you will face previous error.file not executing instead of executing browser asking for download file.

change time.sh file to time.cgi

i tried this method also but it wont help

Options ExecCGI
     AllowOverride None
     Order allow,deny
     Allow from all
     AddHandler cgi-script .cgi .sh

https://devopslanka.com/sam/time.cgi is working perfectly

well we have done the all the things with /etc/httpd/conf/httpd.conf default file.lets take a look how to do this in virtual host file.


How to Running scripts outside of the cgi-bin default directory with Virtual host file

we have created virtual host file called devops.conf inside /etc/httpd/conf.d/devops.conf in previous lesson


Now comment ScriptAlias tag in /etc/httpd/conf/httpd.conf file and copy the content that we previously created.

This content

#ScriptAlias /sam/ "/home/myscript/"
<Directory "/home/myscript">

Options ExecCGI
AddHandler cgi-script .cgi .sh
AllowOverride None
Require all granted

you can see i have commented that #ScriptAlias /sam/ “/home/myscript/” tag in default httpd.conf file

then open our virtual host file located in


add this copied content to the below of your virtual host file

so final everything is looks like this

<VirtualHost *:80>
ServerName devopslanka.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]

<VirtualHost *:443>
ServerName devopslanka.com
SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLCertificateFile "/etc/pki/tls/certs/devopslanka.com.crt"
SSLCertificateKeyFile "/etc/pki/tls/private/devopslanka.com.key"

#Pointing the document root
DocumentRoot "/var/www/devops"
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted

#New Section for cgi in virtual host
ScriptAlias /sam/ "/home/myscript/"
<Directory "/home/myscript">
Options ExecCGI
AddHandler cgi-script .cgi .sh
AllowOverride None
Require all granted


Then navigate to https://devopslanka.com/sam/time.sh

its working perfectly

I hope you will learn something from this tutorial.if you like this tutorial please share with your friends.Share ow facebook ,twitter,google plus.

next tutorial i will discuss about how to use Dynamic web content Python,php and mysql.See you in next tutorial.

Thanks for watching

Have a good day

Sameera Dissanayaka





Leave A Comment