I have looked everywhere and tried many suggested solutions, still without the required result: to run a python file from my lamp server. I can not seem to integrate all the pieces of the puzzle ... Complicating the story is that many solutions either use old apache version (<2.4), which changed the config files significantly. No more httpd.conf! so this executing-a-python-script-in-apache2 does not help; But also the python version being> 3 complicates matters.
specs:
- linux Kubuntu, apache 2.4, python 3.5
- apache is running
- website files are in root/var/www/html/, I have sudo access to this folder.
- apache2 cgi module enabled: a2enmod cgi
- the python 3.5 path is usr/bin/env python3
the python script, simplest of scripts, has been made executable
#!/usr/bin/env python3 print ("Content-type: text/html\n") print ("Hello world!")
lets boil it down to the simplest case: I would like to have apache interpret the spark.py script and spit out the html: "Hello world!"
Questions:
- is the script file correct as is?
- which config files do I need to change and what do I need to add to these config files?
I know for security reasons, you should not have apache run script in your root dir.
2 Answers 2
The python documentation for modwsgi seems to fit what you are asking for. The following webpage has a really simple example and the necessary configuration for a python3-apache2 setup.
http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html
You will need to install the mod_wsgi for the configuration to work. Take note of the different "_" underscore and "-" dash character used in apt and pip3.
$ sudo apt install apache2-dev libapache2-mod-wsgi-py3
$ sudo pip3 install mod_wsgi
libapache2-mod-wsgi-py3 and mod_wsgi seems to be the same thing. However, my test deployment only works after installing mod_wsgi. Could be configuration issue. The following are the details of the configuration I have tested on Ubuntu 16.04.2.
Application file /home/user/wsgi_sample/hello.wsgi:
def application(environ, start_response):
status = '200 OK'
output = b'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Apache2 configuration /etc/apache2/sites-available/000-test.conf
<VirtualHost *:80>
ServerName testmachine
<Directory /home/user/wsgi_sample>
Order allow,deny
Allow from all
Require all granted
</Directory>
WSGIScriptAlias /hello /home/user/wsgi_sample/hello.wsgi
</VirtualHost>
Enable the site in apache2.
sudo a2ensite 000-test.conf
Open your browser to testmachine/hello.
wsgi may also be deployed on Apache2 using passenger. It demands a slighter longer configuration. Ask a new question if passenger/python3 is desired.
1 Comment
Yes, your minimum code seem correct. The Apache config information is answered here https://stackoverflow.com/a/57531411/4084546
mod_wsgi, it's much better than plain old CGI. The main Ubuntu archives have alibapache2-mod-wsgi-py3package, I'm sure there's one available for Kubuntu as well.