mod_wsgi is used to run python scripts in Apache Web Server. This is also used to deploy python framework like Django under Apache.
To install mod_wsgi on Ubuntu 14.04, run
sudo apt-get install libapache2-mod-wsgi-py3
Now you need to edit your web site configuration file and add
AddHandler cgi-script .cgi .pl .py
Restart Apache
sudo service apache2 restart
Now you will be able to execute python scripts under Apache.
List all modules loaded by Apache.
boby@fwhlin:~/www$ sudo apache2ctl -M Loaded Modules: core_module (static) so_module (static) watchdog_module (static) http_module (static) log_config_module (static) logio_module (static) version_module (static) unixd_module (static) access_compat_module (shared) alias_module (shared) auth_basic_module (shared) authn_core_module (shared) authn_file_module (shared) authz_core_module (shared) authz_host_module (shared) authz_user_module (shared) autoindex_module (shared) cgi_module (shared) deflate_module (shared) dir_module (shared) env_module (shared) filter_module (shared) mime_module (shared) mpm_prefork_module (shared) negotiation_module (shared) php5_module (shared) rewrite_module (shared) setenvif_module (shared) status_module (shared) wsgi_module (shared) boby@fwhlin:~/www$
You can see “wsgi_module (shared)” loaded.
My Apache configuration/test python script
boby@fwhlin:~$ cat /etc/apache2/sites-available/000-default.conf
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster@localhost
SetEnv APP_ENV "dev"
DocumentRoot /home/boby/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/boby/www/>
Options All
AllowOverride All
Require all granted
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /home/boby/www/cgi-bin/
<Directory "/home/boby/www/cgi-bin/">
AllowOverride None
Require all granted
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
AddHandler cgi-script .cgi .pl .py
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
boby@fwhlin:~$ cat ~/www/1.py
#!/usr/bin/python
print "Content-type: text/html"
print
print "<pre>"
import os, sys
from cgi import escape
print "<strong>Python %s</strong>" % sys.version
keys = os.environ.keys()
keys.sort()
for k in keys:
print "%s\t%s" % (escape(k), escape(os.environ[k]))
print "</pre>"
boby@fwhlin:~$
