Would you like to learn how to install Nginx and enable the use of Python CGIs on a computer running Ubuntu Linux? In this tutorial, we are going to show you how to enable the CGI feature and enable Python scripts to run on the Nginx server.

• Ubuntu 18
• Ubuntu 19
• Ubuntu 20
• Nginx 1.18.0
• Python 3

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Tutorial Ubuntu - Python CGI on Nginx

Install the Python package.

Copy to Clipboard

Install the Nginx server and the Fcgiwrap package.

Copy to Clipboard

Create a configuration file for the CGI gateway.

Copy to Clipboard

Here is the file content.

Copy to Clipboard

Create a directory to store the CGI files.

Copy to Clipboard

Edit the Nginx configuration file for the default website.

Copy to Clipboard

Insert the following line in the area named SERVER.

Copy to Clipboard

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

Restart the Nginx service.

Copy to Clipboard

As an example, let's create a Python CGI script.

Use the PIP application to install a Python library named ART.

Copy to Clipboard

Access the Nginx's CGI directory.

Copy to Clipboard

Create a test page using Python.

Copy to Clipboard

Here is the file content.

Copy to Clipboard

Change the file permission.

Copy to Clipboard

Open your browser and enter the IP address of your web server plus /cgi-bin/test.py.

In our example, the following URL was entered in the Browser:

• http://172.31.7.220/cgi-bin/test.py

The Python page should display the following message.

NGINX CGI Python

Congratulations! Nginx is now able to use Python scripts as CGI.