Current Location: Home> Latest Articles> Complete Guide to PHP CGI Setup and Usage on Kali Linux

Complete Guide to PHP CGI Setup and Usage on Kali Linux

gitbox 2025-07-28

Introduction to PHP CGI

In modern web development, PHP CGI is a mode of running PHP that processes dynamic content through the Common Gateway Interface (CGI). It enables flexible interaction between PHP code and the web server, supporting dynamic webpage generation. Understanding how it works is essential for proper setup and use.

Installing PHP CGI on Kali Linux

Installing PHP CGI on Kali Linux is straightforward. Simply update the package index and install the PHP CGI package via command line:

sudo apt-get update
sudo apt-get install php-cgi

After installation, verify that PHP CGI is successfully installed by checking its version with:

php-cgi -v

If the PHP version information is displayed, the installation was successful.

Configuring Apache to Support PHP CGI

To run PHP CGI properly, it needs to be integrated with the Apache web server. The configuration steps include:

sudo apt-get install apache2
sudo a2enmod cgi
sudo systemctl restart apache2

These commands install Apache, enable the CGI module, and restart the server to apply changes.

Creating and Testing a PHP CGI Script

Once configured, create a dedicated folder for CGI scripts under the web server root directory, for example:

mkdir /var/www/html/mycgi

Then create a test script file named test.php in that directory with the following content:

<?php
header('Content-Type: text/html');
echo 'Hello, this is a PHP CGI script!';
?>

Make sure the script has executable permissions:

chmod +x /var/www/html/mycgi/test.php

Accessing the Test Script

Open your browser and visit the following URL to verify if the script works correctly:

http://your-ip-address/mycgi/test.php

If the page displays “Hello, this is a PHP CGI script!”, the setup is successful.

Conclusion

This article has provided a comprehensive guide on installing, configuring, and running PHP CGI on Kali Linux. By correctly installing the PHP CGI package, configuring Apache, and writing test scripts, developers can efficiently build dynamic web services. Mastering PHP CGI enhances flexibility and productivity in web development.