How to enable CGI scripts on Apache
CGI scripts are a practical solution to lighten the load on the Apache web server. The necessary Apache configurations can be set up quickly and the process for granting permissions for the directory and the CGI files is straightforward.
What are the requirements for CGI scripts on Apache?
In order to use the Common Gateway Interface (CGI) to submit scripts to your Apache web server, you need the following setup:
- a cloud server or virtual private server (VPS)
- a Linux server distribution such as CentOS 8 or Ubuntu 22.04
- an Apache web server that is installed and running
A standard Linux installation comes with Apache already installed. If your server was created with the Minimal installation option, you will need to install and configure Apache before you proceed. Learn how to install and configure Apache for WordPress in our related article.
How to enable CGI scripts in the Apache configurations
Two things need to be set up in order to run CGI scripts on a Linux server with Apache:
- Apache needs to be configured so the webserver can run CGI scripts.
- The script needs to be uploaded to the correct location and given the correct permissions.
Apache settings for CGI scripts on CentOS
Open the Apache configuration file httpd.conf
for editing:
sudo nano /etc/httpd/conf/httpd.conf
bashFind the section which reads:
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
textReplace the line Options None
with the following two lines:
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py
textThe first line tells Apache to execute CGI files that are uploaded to the /var/www/cgi-bin directory
. The second line tells Apache that any file ending in .cgi, .pl (Perl script), or .py (Python script) is considered a CGI script.
The section now reads:
#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Require all granted
</Directory>
textSave and exit the file. Now restart Apache so that the changes take effect:
sudo systemctl restart httpd
bashApache settings for CGI scripts on Ubuntu
On Ubuntu systems such as Ubuntu 22.04, Apache is configured by default to allow for the execution of CGI scripts in the designated /usr/lib/cgi-bin
directory. You don’t need to change any Apache configurations. However, the Apache CGI module must be enabled before CGI scripts can be executed. To do this, you will need to create a symlink (symbolic link):
sudo ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/
bashThen restart Apache so that the changes take effect:
sudo systemctl restart apache2
bashHow to upload CGI script and set permissions
To verify the CGI script functionality on your Apache server, we recommend starting with a test script. Create the file test.cgi
in the server’s designated cgi-bin and open the test script for editing:
-
CentOS:
sudo nano /var/www/cgi-bin/test.cgi
-
Ubuntu:
sudo nano /usr/lib/cgi-bin/test.cgi
Add the following content to this file:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<h1>Hello world</h1>";
textSave and exit the file. In the next step, give the file the necessaryexecute permissions:
-
CentOS:
sudo chmod 755 /var/www/cgi-bin/test.cgi
-
Ubuntu:
sudo chmod 755 /usr/lib/cgi-bin/test.cgi
By using the chmod 755
parameters, the script can be read, edited and executed by the owner. For the group and other users, there is read access and the possibility to execute the script.
View the script in a browser, using either the domain name or IP address:
http://example.com/cgi-bin/test.cgi
http://192.168.0.1/cgi-bin/test.cgi
textIf the setup was successful, the message “Hello world!” will appear.
How to troubleshoot CGI script errors
404 error: A 404 error means that the URL cannot be found. Check that the script has been added to the right directory.
-
CentOS: The default CGI directory is
var/www/cgi-bin/
-
Ubuntu: The default CGI directory is
/usr/lib/cgi-bin
Server 500 error: When error 500 shows up in connection with CGI scripts on Apache, it is usually due to the script not having the correct permissions. Check that the script has execute (chmod 755) permissions.
- Automatic backup & easy recovery
- Intuitive scheduling and management
- AI-based threat protection