CGI scripts are a practical solution to lighten the load on the Apache web server. The necessary Apache con­fig­ur­a­tions can be set up quickly and the process for granting per­mis­sions for the directory and the CGI files is straight­for­ward.

What are the re­quire­ments 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 dis­tri­bu­tion such as CentOS 8 or Ubuntu 22.04
  • an Apache web server that is installed and running
Note

A standard Linux in­stall­a­tion comes with Apache already installed. If your server was created with the Minimal in­stall­a­tion 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 con­fig­ur­a­tions

Two things need to be set up in order to run CGI scripts on a Linux server with Apache:

  • Apache needs to be con­figured so the webserver can run CGI scripts.
  • The script needs to be uploaded to the correct location and given the correct per­mis­sions.

Apache settings for CGI scripts on CentOS

Open the Apache con­fig­ur­a­tion file httpd.conf for editing:

sudo nano /etc/httpd/conf/httpd.conf
bash

Find 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>
text

Replace the line Options None with the following two lines:

Options +ExecCGI
AddHandler cgi-script .cgi .pl .py
text

The 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 con­sidered 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>
text

Save and exit the file. Now restart Apache so that the changes take effect:

sudo systemctl restart httpd
bash

Apache settings for CGI scripts on Ubuntu

On Ubuntu systems such as Ubuntu 22.04, Apache is con­figured by default to allow for the execution of CGI scripts in the des­ig­nated /usr/lib/cgi-bin directory. You don’t need to change any Apache con­fig­ur­a­tions. 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/
bash

Then restart Apache so that the changes take effect:

sudo systemctl restart apache2
bash

How to upload CGI script and set per­mis­sions

To verify the CGI script func­tion­al­ity on your Apache server, we recommend starting with a test script. Create the file test.cgi in the server’s des­ig­nated 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>";
text

Save and exit the file. In the next step, give the file the necessaryexecute per­mis­sions:

  • CentOS: sudo chmod 755 /var/www/cgi-bin/test.cgi
  • Ubuntu: sudo chmod 755 /usr/lib/cgi-bin/test.cgi
Note

By using the chmod 755 para­met­ers, the script can be read, edited and executed by the owner. For the group and other users, there is read access and the pos­sib­il­ity 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
text

If the setup was suc­cess­ful, 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 con­nec­tion with CGI scripts on Apache, it is usually due to the script not having the correct per­mis­sions. Check that the script has execute (chmod 755) per­mis­sions.

Cloud Backup powered by Acronis
Mitigate downtime with total workload pro­tec­tion
  • Automatic backup and easy recovery
  • Intuitive schedul­ing and man­age­ment
  • AI-based threat pro­tec­tion
Go to Main Menu