How to run cgi scripts with Apache
In this example we will add support for the following file extensions: .cgi .pl .py
We have set folder to be /home/*/public_html/cgi-bin/ as this is by default, don’t set it to as public_html only as this will cause issues with your php scripts.
Create File: /usr/local/apache/conf.d/mod_cgid.conf
LoadModule cgid_module modules/mod_cgid.so <IfModule cgid_module> <Directory /home/*/public_html/cgi-bin/> Options ExecCGI SymLinksifOwnerMatch SetHandler cgi-script AddHandler cgi-script .cgi .pl .py Require all granted AllowOverride All </Directory> </IfModule>
Now restart apache and you are ready to run your cgi-scripts.
service httpd restart
Test if cgid module is now loaded
/usr/local/apache/bin/httpd -M|grep cgid
If module is loaded, you should get output containing this: cgid_module (shared)
For perl you might also need to install it:
yum install perl-CGI
If you will have multiple domains or users on the server then its highly recommended that you enable suexec module for additional security.
Test if suexec module is now loaded
/usr/local/apache/bin/httpd -M|grep suexec
If module is loaded, you should get output containing this: suexec_module (shared)
Example test script
You can save it to your /home/USERNAME/public_html/cgi-bin/ folder.
Example File: /home/cwppro/public_html/cgi-bin/test.cgi
#!/usr/bin/perl print "Content-Type: text/html\n\n"; print "Hello, world!\n"; system("whoami");
This example will also show you if you are running the script as nobody user or real domain owner. If username is shown as nobody then you don’t have mod_suexec running and scripts will run as nobody user. If you want that real user (owner) is running the script then please enable mod_suexec.
What is a CGI file?
CGI stands for Common Gateway Interface. Files that contain the .cgi file extension are are script files that are written in the C or Perl programming languages. These files are used to create web pages and they are usually stored within the cgi-bin folder of a website directory. The files define how web server software delegates the generation of certain web pages including executable files and stand-alone applications. These are often referred to as CGI scripts.
CGI scripts can be written in any programming language, but scripting languages are most commonly used to create these pages. CGI files are commonly used to run basic Web scripts, enabling website features such as integrated email message forms and rotating advertisements.
This info is for CWP Apache version 2.4.26-2 or later