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

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

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

$ sudo service apache2 reload

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])

Make this script executable by -

$ sudo chmod +x c.py

Now just curl to get response

Connect with me -

46