• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Execute Python on Apache2

Contents

  • Execute Python on Apache2
    • Find the Apache2 config
    • Add CGI
    • Test it

I have to run a very simple Python script on an Apache2 web server. These were the steps with which I made it work:

Find the Apache2 config

$ /usr/sbin/apache2 -V

It is in /etc/apache2/apache2.conf

Add CGI

Somewhere in the /etc/apache2/apache2.conf there is a <Directory ...> part. I've added Options ExecCGI and AddHandler cgi-script .py. Now this part looks like this:

<Directory /var/www/>
    Options Indexes FollowSymLinks ExecCGI
    AddHandler cgi-script .py
    AllowOverride None
    Require all granted
</Directory>

Test it

Create the following test.py file:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

print("Content-Type: text/html\n")
print("Hello World! The answer to live, the universe and everything is %i." % (2 * 21))

Now call http://localhost/test.py.

If that doesn't work, take a look at the apache log files:

$ tail -f /var/log/apache2/error.log

Published

Feb 1, 2015
by Martin Thoma

Category

Code

Tags

  • Apache 3
  • Python 141

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor