I'm making this small tutorial so others can be spared from researching how to get mod_wsgi working with Apache. It took me quite awhile to find out because there was no documentation indexed on Google (it seemed like).
Assuming you have Apache2, PHP, mySQL, and phpmyadmin installed, you'll need to install the wsgi module:
Code:
sudo apt-get install libapache2-mod-wsgi
This should install the module. Now to activate it:
Code:
sudo a2enmod mod-wsgi
Now to restart Apache2:
Code:
sudo /etc/init.d/apache2 restart
Okay, we have the module installed and ready to go, but now we need to associate the module with the ".wsgi" extension on your server. This will make files with a .wsgi extension use the wsgi module for processing. Open up /etc/apache2/sites-available/default:
Code:
sudo gedit /etc/apache2/sites-available/default
Around line 10, you should have something like this:
Code:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride None
Order allow,deny
allow from all
</Directory>
Replace it with this:
Code:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews ExecCGI
AddHandler cgi-script .cgi
AddHandler wsgi-script .wsgi
AllowOverride None
Order allow,deny
allow from all
</Directory>
If you don't want to replace the whole thing, all you need to do is add "ExecCGI" to the Options list, and add the handlers cgi-script and wsgi-script (shown above).
That's it. Restart Apache:
Code:
sudo /etc/init.d/apache2 restart
An example program:
Code:
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]
You're done!
If you want to make "index.wsgi" act as an index for your directory (much like index.htm, index.html, index.php), open up /etc/apache2/mods-enabled/dir.conf:
Code:
sudo gedit /etc/apache2/mods-enabled/dir.conf
And change line 3 (the line that has the DirectoryIndex) to:
Code:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm index.wsgi
Okay, that's it. Please reply to the thread if you see any problems or possible improvements. I didn't spend a whole lot of time on this... so I hope everything works. PHP and wsgi files should run next to each other perfectly.
Bookmarks