Use CGI (Common Gateway Interface) Scripts with Apache httpd.
By default, CGI is allowed under the /var/www/cgi-bin directory. All files in this directory are processed as CGI scripts.
# Check the ScriptAlias directive
[root@www ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
# Output should show the ScriptAlias for cgi-bin
252: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
Create and test a CGI script:
# Create a test CGI script (Python3 example)
[root@www ~]# cat > /var/www/cgi-bin/index.cgi <<'EOF'
#!/usr/libexec/platform-python
print("Content-type: text/html\n")
print("CGI Script Test Page")
EOF
# Change permissions and test the CGI script
[root@www ~]# chmod 755 /var/www/cgi-bin/index.cgi
[root@www ~]# curl localhost/cgi-bin/index.cgi
CGI Script Test Page
To allow CGI in other directories, configure as follows. For example, enable CGI in /var/www/html/cgi-enabled.
# Create a configuration file for CGI in another directory
[root@www ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
# Add the following content
<Directory "/var/www/html/cgi-enabled">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
# Create the directory and reload httpd
[root@www ~]# mkdir /var/www/html/cgi-enabled
[root@www ~]# systemctl reload httpd
If SELinux is enabled and you enable CGI in a custom location, add SELinux rules.
# Add SELinux context for the new CGI directory
[root@www ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
[root@www ~]# restorecon /var/www/html/cgi-enabled
Create a CGI test page in the new directory and verify its functionality.
# Create a test CGI script in the new directory
[root@www ~]# vi /var/www/html/cgi-enabled/index.cgi
# Add the following content
#!/usr/libexec/platform-python
print("Content-type: text/html\n")
print("<html>\n<body>")
print("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">")
print("CGI Script Test Page")
print("</div>")
print("</body>\n</html>")
# Change permissions of the script
[root@www ~]# chmod 755 /var/www/html/cgi-enabled/index.cgi
After setting up the CGI script, you can test it by accessing it from a web browser.
http://[domain]/cgi-enabled/index.cgiReplace [domain] with your server's domain name or IP address. You should see the CGI Script Test Page displayed in the browser, indicating that the CGI script is functioning correctly.