DEV Community

Yash Indane
Yash Indane

Posted on

Enable CGI in Apache2

By default when you install apache2, and navigate to /var/www directory you won't find the cgi-bin folder. To enable CGI in apache2 follow this steps -

Install apache2

$ sudo apt install apache2 -y
Enter fullscreen mode Exit fullscreen mode

Enable CGI module

We can find the available modules in /etc/apache2/mods-available. Here you will see the cgi.load module which you need to load.

Enable by-

$ sudo ln -s /etc/apache2/mods-available/cgi.load
Enter fullscreen mode Exit fullscreen mode

Now as Apache configuration has changed, reload the apache service by -

$ sudo service apache2 reload
Enter fullscreen mode Exit fullscreen mode

Create CGI-script

Apache2 recognizes cgi-scripts in the directory /usr/lib/cgi-bin, so we need to place the script in this folder.

Lets create a cgi script in python that returns the output of free -m command.

#!/usr/bin/python3

from subprocess import getstatusoutput as gso
import cgi

print("content-type:text/plain")
print()

print(gso("free -m")[1])
Enter fullscreen mode Exit fullscreen mode

Make this script executable by -

$ sudo chmod +x c.py
Enter fullscreen mode Exit fullscreen mode

Now just curl to get response

Image

Connect with me -

Linkedin

GitHub Profile

Top comments (0)