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
Note

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
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 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>
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 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/
bash

Then restart Apache so that the changes take effect:

sudo systemctl restart apache2
bash

How 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>";
text

Save 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
Note

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
text

If 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.

Cloud backup from IONOS

Make costly downtime a thing of the past and back up your business the easy way!

Simple
Secure
Integrated
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.