Howto Setup Python For Web Development on Ubuntu
I’ve been checking out python on and off for a while. It’s one of those languages I’ve been meaning to learn. I hear it’s great for desktop development as well as web development.
To setup Apache 2 on Ubuntu to run python scripts like PHP, CF, ASP, et al, you can do the following (this information came from a couple places mostly here and here it’s assumed you are already running apache2):
Most of this will require root so you’ll want to just switch now:
rob@server:~$ sudo bash
now grab python if it’s not there already:
root@server:~# apt-get install python
Grab the mod_python apache2 mod (the plugin to connect apache to python):
root@server:~# apt-get install libapache2-mod-python
After the mod_python is there, make a sim link to the available mods
root@server:~# cd /etc/apache2/mods-enabled/ root@server:~# ln -s ../mods-available/mod_python.load mod_python.load
After the link is made, you can either add the ability to run python to the whole server or to a specified directory. If you want to do global:
root@server:~# cd /etc/apache2/sites-available/ root@server:~# vi default
And add the following lines:
... <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride AuthConfig Order allow,deny allow from all #add these 3 AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On #/add these 3 # Uncomment this directive is you want to see apache2's # default start page (in /apache2-default) when you go to / #RedirectMatch ^/$ /apache2-default/ </Directory> ...
If you are like me though, and just want to play with python, you can add it to an alias directory. Make a directory in your home directory and add to the alias file:
root@server:~# vi /etc/apache2/conf.d/alias
And add (replacing the paths with your own):
Alias /python "/home/rob/python" <Directory "/home/rob/python"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On </Directory>
Once you’ve added either the global setting or the alias setting, restart apache:
root@server:~# /etc/init.d/apache2 restart
And finally, add a test page to make sure all went well (save with a .py extension):
def index(req):<br></br> req.write("Hello World!")
Careful with typing that, tabs vs. spaces matter in python (the one thing so far I find I dislike about the language).