
How to set up Python as a CGI module in Apache on Arch Linux (Manjaro, BlackArch)
May 21, 2023
Setting up Python CGI for a single directory
Create a directory /srv/http/cgi-bin/ - this is where the Python scripts will be located:
sudo mkdir /srv/http/cgi-bin/
Open the /etc/httpd/conf/httpd.conf file – the web server configuration file:
sudo gedit /etc/httpd/conf/httpd.conf
Find a group of lines:
<Directory "/srv/http/cgi-bin"> AllowOverride None Options None Require all granted </Directory>
And replace it with:
<Directory "/srv/http/cgi-bin"> AllowOverride None Options ExecCGI Require all granted </Directory>
Note: if you do not see the difference, then pay attention to the “Options” directive – the “ExecCGI” option has been added.
Find a group of lines
<IfModule mpm_prefork_module> #LoadModule cgi_module modules/mod_cgi.so </IfModule>
And replace with
<IfModule mpm_prefork_module> LoadModule cgi_module modules/mod_cgi.so </IfModule>
That is, uncomment the line.
Notice the line
ScriptAlias /cgi-bin/ "/srv/http/cgi-bin/"
It is not necessary to change it, but if you wish, you can use any other folder instead of /srv/http/cgi-bin/, you just need to create a new folder and specify it with this directive instead of /srv/http/cgi-bin/.
Close the file.
Restart the web server for the changes to take effect:
sudo systemctl restart httpd.service
Create file /srv/http/cgi-bin/test.py:
sudo gedit /srv/http/cgi-bin/test.py
Copy the following content to this file:
#!/usr/bin/python3 print ("Content-type: text/html") print ("") print ("") print ("<html><head>") print ("") print ("</head><body>") print ("Hello.") print ("</body></html>")
Make this file executable:
sudo chmod +x /srv/http/cgi-bin/test.py
Open http://localhost/cgi-bin/test.py in a web browser
If everything is configured correctly, then you should see a line in the web browser
Hello.
Setting up Python CGI for the entire web server
Open the /etc/httpd/conf/httpd.conf file – the web server configuration file:
sudo gedit /etc/httpd/conf/httpd.conf
Find a group of lines
<IfModule mpm_prefork_module> #LoadModule cgi_module modules/mod_cgi.so </IfModule>
And replace with
<IfModule mpm_prefork_module> LoadModule cgi_module modules/mod_cgi.so </IfModule>
That is, uncomment the line.
Then find the line
Options Indexes FollowSymLinks
and add “ExecCGI” to it. You should get the following line (ATTENTION: your set of options may be different):
Options Indexes FollowSymLinks ExecCGI
Now find the line:
#AddHandler cgi-script .cgi
Uncomment it, that is, remove the # symbol at the beginning of the line and add .py to the end of the line. The new line will look something like this:
AddHandler cgi-script .cgi .py
Close the file.
Restart the web server for the changes to take effect:
sudo systemctl restart httpd.service
Create file /srv/http/test.py:
sudo gedit /srv/http/test.py
Copy the following content to this file:
#!/usr/bin/python3 print ("Content-type: text/html") print ("") print ("") print ("<html><head>") print ("") print ("</head><body>") print ("Hello.") print ("</body></html>")
Make this file executable:
sudo chmod +x /srv/http/cgi-bin/test.py
Open http://localhost/test.py in a web browser
If everything is configured correctly, then you should see a line in the web browser
Hello.
Related articles:
- How to install Python as a CGI module in Apache on Linux (100%)
- How to set up Python as a CGI module in Apache on Debian (Ubuntu, Linux Mint) (89.1%)
- How to get data from a web page using GET and POST methods in a Python script (89.1%)
- phpMyAdmin error “Error: Undefined constant "SODIUM_CRYPTO_SECRETBOX_KEYBYTES"” (SOLVED) (68.7%)
- Error “Cannot load modules/libphp7.so” (SOLVED) (62.2%)
- What is the difference between “systemctl reboot” and “reboot” and “systemctl poweroff” and “poweroff” (RANDOM - 50%)