Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set up Python scripts to work in Apache 2.0?

Tags:

I tried to follow a couple of googled up tutorials on setting up mod_python, but failed every time. Do you have a good, step-by step, rock-solid howto?

My dev box is OS X, production - Centos.

like image 980
deadprogrammer Avatar asked Aug 07 '08 18:08

deadprogrammer


People also ask

Can you use Python with Apache?

The Apache HTTP Server is a widely deployed web server that can be used in combination with a WSGI module, such as mod_wsgi or a stand-alone WSGI server to run Python web applications.

How do I enable a Python script?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


2 Answers

There are two main ways of running Python on Apache. The simplest would be to use CGI and write normal Python scripts while the second is using a web framework like Django or Pylons.

Using CGI is straightforward. Make sure your Apache config file has a cgi-bin set up. If not, follow their documentation (http://httpd.apache.org/docs/2.0/howto/cgi.html). At that point all you need to do is place your Python scripts in the cgi-bin directory and the standard output will become the HTTP response. Refer to Python's documentation for further info (https://docs.python.org/library/cgi.html).

If you want to use a web framework you'll need to setup mod_python or FastCGI. These steps are dependent on which framework you want to use. Django provides clear instructions on how to setup mod_python and Django with Apache (http://www.djangoproject.com/documentation/modpython/)

like image 141
Cristian Avatar answered Oct 10 '22 14:10

Cristian


Yes, mod_python is pretty confusing to set up. Here's how I did it.

In httpd.conf:

LoadModule python_module modules/mod_python.so  <Directory "/serverbase/htdocs/myapp">   AddHandler mod_python .py   PythonHandler myapp   PythonDebug On 

and in your application directory:

$ /serverbase/htdocs/myapp$ ls -l total 16 -r-xr-xr-x 1 root sys        6484 May 21 15:54 myapp.py 

Repeat the configuration for each python program you wish to have running under mod_python.

like image 35
Mark Harrison Avatar answered Oct 10 '22 14:10

Mark Harrison