Enable Nginx FastCGI on Ubuntu 24.04

This article explains how Nginx can be configured to run CGI scripts on Ubuntu 24.04.

A CGI (Common Gateway Interface) script is a standard protocol that allows web servers to interact with executable programs, often written in languages like Perl, Python, or PHP, to generate dynamic web content.

CGI enables generating dynamic web content that responds to user input or other factors.

Enabling CGI on a Nginx server supports dynamic interactions and allows for functionalities like search features, login systems, and other interactive elements on websites.

Install FastCGI

To use and run CGI script on Nginx, you must first install and configure FastCGI. With Nginx installed, run the command below to install FastCGI.

sudo apt install fcgiwrap

After installing the FastCGI package, we’ll define a location to store our CGI scripts. We’ll create a new /cgi-bin folder in the Nginx root directory for this post.

The URL to the CGI scripts directory will be [/var/www/cgi-bin/].

First, create the CGI directory by running the command below. Then, adjust the directory permissions as needed.

sudo mkdir /var/www/cgi-bin
sudo chmod 755 /var/www/cgi-bin

Next, run the command below to create an Nginx configuration file and define our CGI script location and other settings.

sudo nano /etc/nginx/fcgiwrap.conf

Then, copy the block below, paste it into the file, and save.

location /cgi-bin/ {
gzip off;
root /var/www;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

After creating the FastCGI config above, include it in your Nginx server block. We’ll use the default server block for this post, but you can create new ones.

Open Nginx default server block.

sudo nano /etc/nginx/sites-available/default

Then, include the configuration file created above in the server {} section.

server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;
index index.html index.htm index.nginx-debian.html;

server_name example.com;

include fcgiwrap.conf;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
............
............

Save and exit the file.

Next, enable FastCGI and reload Nginx services by running the command below.

sudo  systemctl enable fcgiwrap
sudo systemctl reload nginx

Test Apache CGI execution

You should be ready to get your CGI settings in Apache.

Run the command below to create a blank CGI index file.

sudo nano /var/www/cgi-bin/index.cgi

Copy the script below, paste it into the file, and save.

#!/usr/bin/python3

print("Content-type: text/htmln")
print("<html>n<body>")
print("<p style="width: 100%; font-size: 60px; font-weight: bold; text-align: center;">")
print("CGI is Enabled!!!")
print("</p>")
print("</body>n</html>")

Make the file executable by running the command below.

sudo chmod 705 /var/www/cgi-bin/index.cgi

Now, open your browser and browse the script.

http://example.com/cgi-bin/index.cgi

Apache CGI enabled

That should do it!

Conclusion:

  • Configuring Nginx to run CGI scripts on Ubuntu 24.04 enhances the server’s capability to deliver dynamic web content.
  • FastCGI plays a crucial role in handling CGI requests efficiently and effectively.
  • Setting up the /cgi-bin directory allows for organized management of your CGI scripts.
  • Testing the configuration through a simple Python script ensures the setup works correctly.
  • Overall, this configuration opens up possibilities for interactive web features, improving user experience on your website.

Frequently Asked Questions

How do I install FastCGI on Ubuntu 24.04?

To install FastCGI on Ubuntu 24.04, open your terminal and run the command 'sudo apt install fcgiwrap'. This will install the necessary FastCGI package to enable CGI script execution on your Nginx server.

What is the purpose of the /cgi-bin directory in Nginx?

The /cgi-bin directory is where you store your CGI scripts that Nginx will execute. By default, you can create this directory in your Nginx root directory, typically located at /var/www/cgi-bin.

How do I configure Nginx to use FastCGI?

To configure Nginx to use FastCGI, you need to create a configuration file, such as fcgiwrap.conf, and define the location of your CGI scripts. Include this configuration in your Nginx server block to enable FastCGI processing.

What command do I use to reload Nginx after making changes?

After making changes to your Nginx configuration, you can reload the service by running the command 'sudo systemctl reload nginx'. This will apply your changes without disrupting active connections.

How can I test if my CGI script is working on Nginx?

To test if your CGI script is working, create a simple CGI script in the /cgi-bin directory and ensure it has executable permissions. Access the script via your web browser using the appropriate URL, and you should see the output generated by the script.

Categories:

Leave a Reply

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