Enable CGI scripts on Apache
Learn how to enable CGI scripts to run on the Apache web server. This tutorial includes step-by-step instructions for adding the necessary Apache configurations to allow CGI scripts to run, and how to set the correct permissions on the directory and CGI files.
Requirements
- A Cloud Server with CentOS 7 or Ubuntu 16.04 and Apache installed and running.
Apache is installed and running on a Standard Linux installation by default. If your server was created with a Minimal installation, you will need to install and configure Apache before you proceed.
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 to allow CGI scripts to run.
- The script needs to be uploaded to the correct location, and given the correct permissions.
Apache Settings for CGI Scripts on CentOS 7
Open the httpd.conf file for editing:
sudo nano /etc/httpd/conf/httpd.conf
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>
Add the following two lines to this section:
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py
The first line tells Apache to execute CGI files which are uploaded to the /var/www/cgi-bin directory. The second line tells Apache that any file ending in .cgi, .pl, or .py 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>
Save and exit the file. Then restart Apache so that the changes take effect:
sudo systemctl restart httpd
Apache settings for CGI scripts on Ubuntu 16.04
On Ubuntu 16.04, Apache is configured by default to allow the execution of CGI scripts in the designated /usr/lib/cgi-bin directory. You will not need to change any Apache configurations.
However, Apache's CGI module will need to be enabled before CGI scripts can run. To do this, you will need to create a symlink:
sudo ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/
Then restart Apache so that the changes take effect:
sudo systemctl restart apache2
vServer (VPS) from IONOS
Low-cost, powerful VPS hosting for running your custom applications, with a personal assistant and 24/7 support.
Upload the CGI script and set permissions
To verify the CGI script functionality on your server, we recommend starting with a test script. Create the file test.cgi in the server's designated cgi-bin and open it for editing:
- CentOS 7:sudo nano /var/www/cgi-bin/test.cgi
- Ubuntu 16.04: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>";
Save and exit the file.
Next, give the file the correct executable permissions:
- CentOS 7:sudo chmod 755 /var/www/cgi-bin/test.cgi
- Ubuntu 16.04:sudo chmod 755 /usr/lib/cgi-bin/test.cgi
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
You will see "Hello world."
Troubleshooting CGI script errors
404 error: This means that the URL is not found. Check that the script has been added to the correct directory.
- CentOS 7: The default CGI directory is var/www/cgi-bin/
- Ubuntu 16.04: The default CGI directory is /usr/lib/cgi-bin
Server 500 error: This means that the script does not have the correct permissions. Check that the script has executable (chmod 755) permissions:
jdoe@localhost:/etc/apache2# ll /usr/lib/cgi-bin/test.cgi
rwxr-xr-x 1 jdoe jdoe 85 Jul 22 16:53 /usr/lib/cgi-bin/test.cgi*
The correct permissions for the file are rwxr-xr-x.
Cloud backup from IONOS
Make costly downtime a thing of the past and back up your business the easy way!