Sep
6
Install Python on Linux Ubuntu
September 6, 2008 |
In this article, I will explain how to install and run Python as a web service anywhere on Linux Ubuntu as localhost.
1. you should have have the latest version of Python installed, to do so, type the following line of code in the terminal:
sudo apt-get install python
2. Now to run it as a web service with Apache, you need Apache mod_python, to install it, type the following line of command in the terminal:
sudo apt-get install libapache2-mod-python
3. Now if you use the default localhost directory - /var/www/, then following the instruction a, if you have previously configured Apache and run localhost at the place other than the default /var/www/, follow the instruction b:
a.
execute the following command in the terminal:
cd /etc/apache2/sites-available/
and execute the following command:
sudo gedit default
now with the file opened, find the code below:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
change it to:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
run in terminal:
sudo gedit /var/www/test.py
in the opened file, type:
def index(req): return "Test successful";
save the file, and it should work
visit http://localhost/test.py and it should say “Test successful” in plain text
b.
execute the following command in the terminal:
sudo gedit /etc/apache2/apache2.conf
assume that you are running Python code from the directory /home/usr/Documents/Host/lab/python/
add the following code to the bottom of the file:
Alias /python/ /home/usr/Documents/Host/lab/python/ <Location /python/> AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On </Location>
run in terminal:
sudo gedit /home/usr/Documents/Host/lab/python/test.py
in the opened file, type:
def index(req): return "Test successful";
save the file, and it should work
visit http://localhost/test.py and it should say “Test successful” in plain text
Similar Posts
- None Found
Comments
3 Comments so far



















Great, thanks for the tip. I want to learn python.
but I seem to have a problem because the browser wants to download the file instead of displaying it even though I restarted apache. I think it might be because I have the wrong directory, is it the directory where you have the localhost files or where python is?
if the browser tries to download the file, that means it doesn’t recognize the py file as an executable type. the main cause could be:
1. it doesn’t have the right permission, try chmod 775 to make it executable.
2. you may have the wrong directory, it should be the place you have your localhost files, for instance, if your .py file is at /home/usr/bin/test.py, then /home/usr/bin/ should be the directory. and following the instruction b, it would be altered to:
Alias /python/ /home/usr/bin/
Thanks for the quick reply. I tried changing the permissions and didn’t work and I already did the change of the directory like this:
Alias /python/ /home/myname/www/
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
and still it doesn’t work. going to research about it. any ideas?