operating system linux ubuntu 18.04 now I've installed the lamp and can run php codes seamlessly. the directory I'm running / var / www / html. but what i want to do is run python cgi. I have reviewed several sources but I have not succeeded.
I've had some success with the following:
1). Install libapache2-mod-python
$ sudo apt install libapache2-mod-python
$ sudo a2enmod python
2). Add this to a new file in /etc/apache2/sites-enabled/ -- changing the relevant parts to whatever suits you
<VirtualHost *:80>
#
ServerName yourpythonserver.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/py
<Directory /var/www/py>
AddHandler mod_python .py
PythonHandler index
PythonDebug On
DirectoryIndex index.py
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3). As you can see from the config above, this will serve requests directed to yourpythonserver.com
from /var/www/py
- defaulting to index.py
4). Create the file /var/www/py/index.py
with the following:
#!/usr/bin/env python3
from mod_python import apache
def handler(req):
ret = ""
req.content_type = "text/html"
req.write("<h1>Hello World!</h1>")
return apache.OK
5). Finally,
sudo systemctl reload apache2
This is where it starts to get fuzzy for me, but perhaps someone else out there can help us both out with this... I'm not sure why the name of the function needs to be handler, and I'm not sure how to get things PHP normally provides such as $_SERVER
variable. But either way, this works and will serve python files using apache.