RSS Feed

OSS Ramblings

http://www.ossramblings.com/node/380

 

Making Apache Run Python

I've always been a PHP guy, but recently I had a little Python web scraper utility I wrote that I wanted a nice interface to, and I didn't feel like writing a complete GTK interface for it, or rewriting it in PHP with CURL.

So, I thought, "Hey, there's these people that run Python on their servers instead of PHP, why don't I try that! It shouldn't be any harder than running PHP, right?"

Wrong. It's not hard, but it brings back memories of my first attempt to get Perl scripts to work properly with Apache.

First, you need to install "libapache2-mod-wsgi". This is the module for Apache that lets you run Python scripts from inside your web server. I know, why doesn't it have the word "Python" in the name? Don't worry - just make sure you don't actually try to use the module that DOES have Python in the name. It's the old and outdated way of doing things.

So, use Synaptic to install the module, or at a console enter:

sudo apt-get install libapache2-mod-wsgi

Next, you need to enable the script processing in the web server setup. Now the proper and more secure way to do this is to place all your scripts in a "cgi-bin" or other directory specifically for scripts. Since I was only going to run this on my own workstation for "localhost" behind my firewall I elected to enable Python scripts for the root of the web server. Edit your /etc/apache2/sites-available/default file and change the setup for your root directory of the web to something like this. I added bold to the areas I changed:

<Directory /var/www/>
   Options Indexes FollowSymLinks MultiViews ExecCGI
   AllowOverride None
   Order allow,deny
   allow from all
   AddHandler cgi-script .py
</Directory>

Now, restart apache:

sudo /etc/init.d/apache2 force-reload

The one change you'll need to know for any Python scripts written to run from the console is that unlike PHP, you must provide a content-type header or Apache will throw a "500 server error." You also want to make sure you have the "shebang" line that specifies where your Python interpreter is located. Like this:

#! /usr/bin/python

print "Content-Type: text/html\n\n";
print "Hello world!"

If all went well, you should now have Python support in your Apache server!



CGI doesn't require either mod_python or mod_wsgi.

Graham Dumpleton's picture

You need not have installed mod_wsgi, nor mod_python, as all you are doing here is using CGI. This doesn't even run your stuff in the Apache server, but executes a separate program for every request.

I think you need to do some more homework on the differences between CGI and mod_python/mod_wsgi.

Thanks

tony's picture

Well that was helpful!

So... help me out here - I actually made this post after researching online and found 3 sites describing what I just did.

Any helpful links you could provide?

For how to use mod_wsgi

Graham Dumpleton's picture

For how to use mod_wsgi properly, start with:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

Read other documentation on that site as necessary.

For what WSGI is, start out with:

http://www.wsgi.org/wsgi/Learn_WSGI

http://www.erreauk.com

erreauk's picture

First, you need to install "libapache2-mod-wsgi". This is the module for Apache that lets you run Python scripts from inside your web server. I know, why doesn't it have the word "Python" in the name? Don't worry - just make sure you don't actually try to use the module that DOES have Python in the name. It's the old and outdated way of doing things.

erreauk

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Are you a BOT? What's this say?
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

 
 
 




Image 01 Image 02 Image 03