-
Notifications
You must be signed in to change notification settings - Fork 0
CGI
The Common Gateway Interface (CGI) is a standard protocol for web servers to execute programs like console applications or scripts on the server to generate dynamic content. CGI programs can be written in any programming language, but most commonly in Perl, Python, or C.
When a web server receives a request for a CGI program, it executes the program and sends the output back to the client. The server passes information about the request to the CGI program through environment variables and standard input. The CGI program processes the request and generates the output, which is sent back to the client.
Example CGI diagram
- The Web surfer fills out a form and clicks, “Submit.” The information in the form is sent over the Internet to the Web server.
- The Web server “grabs” the information from the form and passes it to the CGI software.
- The CGI software performs whatever validation of this information that is required. For instance, it might check to see if an e-mail address is valid. If this is a database program, the CGI software prepares a database statement to either add, edit, or delete information from the database.
- The CGI software then executes the prepared database statement, which is passed to the database driver.
- The database driver acts as a middleman and performs the requested action on the database itself.
- The results of the database action are then passed back to the database driver.
- The database driver sends the information from the database to the CGI software.
- The CGI software takes the information from the database and manipulates it into the format that is desired.
- If any static HTML pages need to be created, the CGI program accesses the Web server computer’s file system and reads, writes, and/or edits files.
- The CGI software then sends the result it wants the Web surfer’s browser to see back to the Web server.
- The Web server sends the result it got from the CGI software back to the Web surfer’s browser.
To set up CGI on a web server, you need to configure the server to recognize CGI scripts and execute them. Here are the general steps to set up CGI on a web server:
-
Configure the web server to recognize CGI scripts by adding a handler for the CGI script file extension (e.g.,
.cgi). -
Set the permissions on the CGI script file to allow execution by the web server.
-
Place the CGI script file in a directory accessible by the web server.
-
Test the CGI script by accessing it through a web browser.
Here is an example of a simple CGI script written in Perl that generates a basic HTML page:
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "<html><body>";
print "<h1>Hello, CGI World!</h1>";
print "</body></html>";To run this script as a CGI program, save it as hello.cgi, set the permissions to allow execution (chmod +x hello.cgi), and place it in a directory accessible by the web server. You can then access the script through a web browser to see the output.
🔗 Example of CGI setup with NGINX 🔗 Details setup NGINX CGI - DigitalOcean
Install a FastCGI server like fcgiwrap to execute CGI scripts with NGINX:
sudo apt-get install fcgiwrapCreate a directory for CGI scripts (e.g., /var/www/cgi-bin) and set the permissions to allow execution:
sudo mkdir /var/www/cgi-bin
# Set permissions to allow execution
sudo chmod +x /var/www/cgi-binTo configure NGINX to execute CGI scripts, you need to add a location block in the server configuration file that specifies the CGI script directory and handler. Here is an example configuration for NGINX:
In the FastCGI server configuration file /etc/nginx/fastcgi.conf, add the following line:
location /cgi-bin {
alias /var/www/cgi-bin;
gzip off;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}-
location /cgi-binblock specifies the directory where CGI scripts are located (/var/www/cgi-bin) and the handler for executing the scripts. -
fastcgi_passdirective specifies the FastCGI server to use for executing the CGI scripts. -
fastcgi_param SCRIPT_FILENAMEdirective sets the path to the CGI script file. -
More directives: 📚 NGINX - List of fastCGI param/config
Next in the NGINX server configuration file (/etc/nginx/sites-available/default), add the following line to include the FastCGI configuration:
server {
...
include /etc/nginx/fastcgi.conf;
}After making these changes, restart NGINX to apply the configuration:
# Restart FastCGI
sudo systemctl restart fcgiwrap
# Restart NGINX
sudo systemctl restart nginxOnce NGINX is configured to execute CGI scripts, you can place your CGI scripts in the specified directory (/var/www/cgi-bin) and access them through a web browser.
When setting up CGI on a web server, it is important to consider security implications. CGI scripts can pose security risks if not properly configured and secured. Here are some security considerations for CGI scripts:
- Validate user input to prevent injection attacks.
- Limit the execution permissions of CGI scripts to prevent unauthorized access.
- Use secure coding practices to prevent common vulnerabilities like buffer overflows or code injection.
- Regularly update and patch the server software to address security vulnerabilities.