Tutorial: Enabling cgi with httpd

The Common Gateway Interface (CGI) is a standard protocol for web servers to execute programs that execute like console applications (https://en.wikipedia.org/wiki/Common_Gateway_Interface)

I’m using CGI to query data from some Linux servers and VMs I’m running at home.

Enabling CGI is easy enough, yet I found most tutorials incredibly lacking. Therefore, I’m going to walk you through the procedure, starting with a freshly installed CentOS 7.

I’m assuming you have working internet access on your server or VM.

1. Installing a web server

We start by installing a web server. For this example, I have chosen apache2. Apache2 is known as httpd in the context of CentOS and I will use this synonym throughout the rest of the article.

Httpd is part of the official CentOS repos and we can use yum to install it.

# yum install httpd

Afterwards, we will start and enable httpd (which means it will start and automatically run on server startup).

# systemctl start httpd
# systemctl enable httpd

We can check if everything went well with

# systemctl status httpd

which should proclaim that httpd is active and running.

From now on, your server/VM will answer to requests made to that host’s IP on port 80.

Excursus: System configuration – SELinux and firwalld

Pointing your browser to the server probably yield a connection refused – error since the server’s  local firewall will block the connection.

The firewall can be stopped by a simple

# systemctl stop firewalld

and furthermore permanently disabled by

# systemctl disable firewalld

If your server is in any shape or form reachable through the public internet, you should probably sit down and properly configure your firewall instead of simply turning it off.

Centos 7 comes with SELinux enabled by default. Configuring SELinux is beyond the scope of this article so the suggestion here is to set it to permissive, which means it will merely report security incidents but not perform any blocking action. SELinux can be disabled by editing /etc/selinux/config and setting “Current mode” to permissive.

2. Configuring httpd

httpd’s config files are located in /etc/httpd.

The most important file here is conf/httpd.conf, the global httpd configuration file.

In httpd.conf, we need to insert the following line to load the correct module:

LoadModule cgid_module modules/mod_cgid.so

Afterwards, we need to restart httpd:

# systemctl restart httpd

3. Hello, CGI

When we installed httpd, there was already a designated cgi directory for us: /var/www/cgi-bin. For a first test, a very simple script is sufficient. Create a file with the following content:

print "Content-type: text/html\n\n";
print "Hello, CGI.";

For the sake of convenience, I’m assuming it will be called helloworld.cgi. Afterwards, we need to make it executable with

# chmod +x helloworld.cgi

4. Testing

Now, we’re good to go. Point the browser of your choice to <IP>/cgi-bin/helloworld.cgi

This is what you should see:

Congratulations, you got your first CGI-script up and running.

Leave a Reply

Your email address will not be published. Required fields are marked *