Debian / Ubuntu Linux Apache mod_python Configuration

I‘ve installed a Web server (Apache2) and like to use Python interpreter correctly. How do I configure mod_python under Debian or Ubuntu Linux running Apache server version 2.x?

You need to install the following software:

a] libapache2-mod-python : Mod_python is a module that embeds the Python language interpreter within the server, allowing Apache handlers to be written in Python. Mod_python brings together the versatility of Python and the power of the Apache Web server for a considerable boost in flexibility and performance over the traditional CGI approach.

b] Apache2 webserver.

Install mod_python

Open terminal and type the following command:
$ sudo apt-get update
$ sudo apt-get install libapache2-mod-python libapache2-mod-python-doc

Configure mod_python

You need to create directory to host your python scripts. Type the following command:
$ sudo mkdir /var/www/py
Give your account permission to access the scripts:
$ sudo chown yourname:www-data /var/www/py
Now, open /etc/apache2/sites-available/default, enter:
$ sudo -s
# vi /etc/apache2/sites-available/default

Add the following config code:

   <Directory /var/www/py>
        AddHandler mod_python .py
        PythonHandler hello
        PythonDebug On

Save and close the file. Restart Apache2 server:
# /etc/init.d/apache2 restart
# tail -f /var/log/apache2/error.log

You should see mod_python/3.3.1 Python/2.5.2 loaded:

[Tue Mar 17 02:42:18 2009] [notice] FastCGI: process manager initialized (pid 15572)
[Tue Mar 17 02:42:18 2009] [notice] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads.
[Tue Mar 17 02:42:18 2009] [notice] mod_python: using mutex_directory /tmp 
[Tue Mar 17 02:42:18 2009] [notice] Apache/2.2.9 (Ubuntu) mod_fastcgi/2.4.6 PHP/5.2.6-2ubuntu4.1 with Suhosin-Patch mod_python/3.3.1 Python/2.5.2 mod_perl/2.0.4 Perl/v5.10.0 configured -- resuming normal operations

How do I test mod_python?

Simply create the script as follows:
$ vi /var/www/py/
The following simple mod_python program serves as a good test:

from mod_python import apache
def handler(req):
        req.content_type = 'text/html'
        req.write('<html><head><title>Testing mod_python</title></head><body>')
        req.write('Hello World!')
        return apache.OK

Save and close the file. Now fire a webbrowser and type the url:
You hould see “Hello World!” on screen.

Further readings:

This is just getting started tutorial and setting up the Python interpreter correctly. I recommend getting mod_python book or refer to Python Language Website.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • Ronny Pfannschmidt Mar 18, 2009 @ 17:21

    the biggest misstake of all is to use mod_python for anything new – mod_wsgi is there, use it, its the standard

  • Bruno Feb 4, 2010 @ 19:44

    Hello friend, congratulations for the article, was excellent, I was killing me to install mod_python and you explained in a clear and objective.


  • Larry Delaney Feb 23, 2011 @ 18:16

    I followed your installation procedures carefully. However, when I ask for the from a browser I get the download menu asking me if I want to save or execute the file rather than getting a web page. How can I fix this? Any suggestions?

    During installation the error.log was OK. The chown worked OK also.

    • Larry Delaney Feb 23, 2011 @ 18:21

      Sorry, false alarm, I was able to figure this out myself. Just another stupid mistake. Thanks anyway.

      • vincent Jan 17, 2014 @ 8:45

        I have the same problem, my browser ask me if I want download or open the file. How could you fix the problem ?? Thanks

  • Marcelo Martinovic May 16, 2011 @ 23:32

    Excelent, very easy !!!!! Thanks from Argentina !!!

  • Önder Kalacı May 21, 2011 @ 6:33

    It’s great, thank you very much

  • bellszhu Aug 18, 2011 @ 15:22

    i have question!!
    Syntax error on line 44 of /etc/apache2/sites-enabled/000-default:
    Invalid command ‘PythonHandler’, perhaps misspelled or defined by a module not included in the server configuration
    Action ‘configtest’ failed.
    The Apache error log may have more information.

  • pc-moon Sep 17, 2011 @ 0:03

    its great thank u so much

    hope god bless u

  • serina Nov 4, 2011 @ 2:45

    greate,thanks so much

  • Maui1983 Aug 7, 2012 @ 12:13


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum