How to Enable CGI Scripts in Apache on Ubuntu

Updated: January 22, 2024 By: Guest Contributor Post a comment

Introduction

Common Gateway Interface (CGI) scripts are essential for web developers who want to generate dynamic content on their websites. CGI scripts can be written in any programming language, such as Perl, Python, or Shell, which the server executes to produce dynamic web pages. This tutorial will guide you through the process of enabling CGI scripts in Apache on an Ubuntu server.

Prerequisites

  • A Ubuntu server
  • Apache web server installed
  • sudo privileges

Step-by-Step Instructions

Step 1: Installing Apache

If you have not already installed the Apache web server, you can do so by running the following commands:

sudo apt update
sudo apt install apache2

Enable the Apache service to start automatically upon system boot:

sudo systemctl enable apache2

Start the Apache server:

sudo systemctl start apache2

Step 2: Enabling CGI module

To execute CGI scripts, you must enable the mod_cgi or mod_cgid module in Apache. These modules are responsible for handling CGI script requests. Enable the CGI module with the following command:

sudo a2enmod cgi

Restart Apache to apply the changes:

sudo systemctl restart apache2

Step 3: Configuring Apache to handle CGI scripts

You’ll need to configure Apache to recognize and execute CGI scripts. You can perform this by editing the Apache configuration file or by creating a new configuration file specific for your CGI scripts.

Editing the Apache configuration file

Edit the default Apache configuration file:

sudo nano /etc/apache2/sites-available/000-default.conf

Add the following section within the <VirtualHost> block to specify the directory where you will place your CGI scripts, and to allow Apache to execute them:

<Directory "/var/www/html/cgi-bin">
 Options +ExecCGI
 AllowOverride None
 Require all granted
 AddHandler cgi-script .cgi .pl
</Directory>

Save the changes and exit the editor.

Creating a new configuration file

Alternatively, create a new configuration file specifically for handling your CGI scripts. This method helps keep your configuration organized, especially if you have a complex server setup.

sudo nano /etc/apache2/conf-available/cgi-enabled.conf

Insert the following configuration code, similar to the above but in a separate file:

<Directory "/var/www/html/cgi-bin">
 Options +ExecCGI
 AllowOverride None
 Require all granted
 AddHandler cgi-script .cgi .pl
</Directory>

Enable the configuration:

sudo a2enconf cgi-enabled

Restart Apache to apply the changes:

sudo systemctl restart apache2

Step 4: Testing CGI Scripts

Create a CGI directory and place a test script in it:

sudo mkdir /var/www/html/cgi-bin
sudo nano /var/www/html/cgi-bin/test.cgi

Write a simple ‘Hello World’ script in Perl:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<h1>Hello, World!</h1>";

Make the script executable:

sudo chmod +x /var/www/html/cgi-bin/test.cgi

Open your browser and navigate to http://your_server_ip/cgi-bin/test.cgi. You should see a ‘Hello, World!’ message if everything is set up correctly.

Troubleshooting

If your CGI script does not work as expected, check these common issues:

  • Ensure the shebang line is correct in your CGI script.
  • Confirm the script has the executable permission set.
  • Check Apache error logs for more details: sudo tail /var/log/apache2/error.log

Conclusion

You have successfully enabled and tested CGI scripts in your Apache server on Ubuntu. This opens up a broad range of possibilities for server-side scripting and dynamic content on your website. With your Apache server now ready to handle CGI scripts, you’re well on your way to creating more complex and interactive web applications.