3

I am trying to make the apache to run python scripts. I have MOD_WSGI , Apache , Python installed on RHEL6. I even edited httpd.conf file to include these lines.

<Directory /var/www/>
  Options Indexes FollowSymLinks MultiViews ExecCGI

  AddHandler cgi-script .cgi
  AddHandler wsgi-script .wsgi

  AllowOverride None
  Order allow,deny
  allow from all
</Directory>

I restarted the apache server also. But when i try to execute the python scripts , its just getting printed as plain text in the browser. Its not at all getting executed. Please somebody help.

ASKN
  • 133
  • 1
  • 1
  • 7

2 Answers2

4

See if the module was loaded properly by issuing apache2ctl -t -D DUMP_MODULES or apachectl -t -D DUMP_MODULES. If it wasn't, edit your httpd.conf or an included file to include the following (replace lib with lib64 if needed):

LoadModule wsgi_module /usr/lib/httpd/modules/mod_wsgi.so

Do note that you can't execute any python script with WSGI - it has to support an interface with the WSGI handler, if I remember correctly. If you want to execute any python script perhaps you should use plain old CGI, which is slow (don't use it in production if you have more users than yourself) but easy. If your scripts are in cgi-bin and they have the right shebang at the top they should pretty much just work already.

If you want to use mod_python to have Apache execute regular python scripts first make sure you're loading the module:

  LoadModule python_module modules/mod_python.so

And then add this to your configuration, for example inside a Directory block, removing your other changes first:

  AddHandler mod_python .py
  PythonHandler mod_python.publisher
Eduardo Ivanec
  • 14,531
  • 1
  • 35
  • 42
  • I ran this comment - **apachectl -t -D DUMP_MODULES** and it said **syntax ok** so that means WSGI is ok. And i even tried editing the httpd.conf and it said **mod_wsgi already loaded** . module has been loaded. now whats next? – ASKN Jun 17 '11 at 12:45
  • The scripts you're trying to run have a .wsgi extension, right? – Eduardo Ivanec Jun 17 '11 at 12:49
  • you mean python script. its name is test.py . And i am running it by **http://10.193.187.52/test.py** like this – ASKN Jun 17 '11 at 12:54
  • @Ashin, he (@Eduardo) explained the problem twice - Apache is looking for scripts that end in **.wsgi**, not *.py*. Look at the rules you listed: `AddHandler wsgi-script .wsgi`. Handling Python files is a different process. – Cyclops Jun 17 '11 at 13:27
  • Ok then i changed it to this now `AddHandler wsgi-script .py` and now i am getting 403 error **You don't have permission to access /test.py on this server.** , and so i tried `chmod 777 test.py` , gave the python file full permission. Still getting the 403 error. – ASKN Jun 17 '11 at 13:50
  • @Cyclops @Eduardo My actual requirement is to make Apache to execute python scripts. So should i need to make use of **MOD_PYTHON** instead of **MOD_WSGI** – ASKN Jun 17 '11 at 13:56
  • @Ashin - regarding that 403 error, your web server user probably doesn't have enough permissions to get to that file - the directory it's in, or some other directory along the path to the root, hasn't got 'rx' for it. Yes, given your requirement you're probably better off sticking to mod_python - see my latest edit. – Eduardo Ivanec Jun 17 '11 at 14:41
  • @Eduardo Thank you very much Eduardo. It worked so fine :) – ASKN Jun 17 '11 at 15:23
0

Both mod_python and mod_wsgi require special code to work with those respective handlers. If your goal is to simply execute plain python scripts, use cgi. In Redhat, the default location is /var/www/cgi-bin/, and is already mapped to the path /cgi-bin/ in apache.

JimB
  • 1,924
  • 12
  • 15