This is almost all taken from (https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide) - I'm adding something here because I did not realize that I had port 81 blockked on my firewall and lost a bit of time figuring that out (was able to wget the expected url but could not navigate in a browser). Anyway, here are the basic steps. See the link google code link though.
In my case, I have an already running php server where I just want to test out some python web apps and don't particularly care about performance or anything like that. In reality, it might make sense to have a separate instance of apache running for the python apps. But that's beyond the scope of what I'm trying to accomplish today, which is just to get a hello world python app up and running.
-
Install mod_wsgi:
sudo apt-get install libapache2-mod-wsgi
-
For this installation we will run python apps on a different port, namely port 88. It is not necessary to do so though.
-
Make sure the port you are listening on is available. For example, if you are using iptables and want to open up port 81 to everyone, you may do something along these lines:
echo "-A INPUT -p tcp --dport 88 -j ACCEPT" >> etc/iptables.firewalls.rules sudo iptables-restore < /etc/iptables.firewall.rules
-
Edit your virtual host file to something like this (this is just for a very basic demo configuration)
1 WSGIApplicationGroup %{GLOBAL} 2 3 Listen 88 4 <VirtualHost *:88> 5 ServerAdmin webmaster@localhost 6 ServerName python.server.com 7 ServerAlias python.server.com 8 9 DocumentRoot /var/www/python/documents 10 <Directory /var/www/python/documents> 13 Order allow,deny 14 deny from all 15 </Directory> 16 17 WSGIScriptAlias /hw /var/www/python/helloworld/helloworld.wsgi 18 19 <Directory /var/www/python/helloworld> 22 Order allow,deny 23 Allow from all 25 </Directory> 26 27 ErrorLog ${APACHE_LOG_DIR}/error.log 28 LogLevel info 29 CustomLog ${APACHE_LOG_DIR}/access.log combined 30 31 </VirtualHost>
-
In the file
/var/www/python/helloworld/helloworld.wsgi
, put your application, e.g. (from (https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide)):def application(environ, start_response): status = '200 OK' output = 'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
-
Restart apache:
sudo service apache2 restart
(or/etc/init.d/apache2 restart
orsystemctl ...
etc depending on your system). -
GET
http://python.server.com:81/hw
(e.g. in your browser or withwget
).