Running Python scripts like PHP
Asked Answered
J

3

9

There are Apache 2 + mod_wsgi + Python 2.6 on server.

I would like to run scripts from Python like PHP scripts. The idea may seem silly, but I'm so accustomed (at least at first learning Python).

Example:

PHP - http://example.com/script.php

Python - http://example.com/script.py

P.S. I know about mod_rewrite and other similar tricks that can do that. But it is only a disguise, and not a direct run.

UPD: My .htaccess file. index.py works, but other python scripts get 404 error.

<Files *.py>
SetHandler wsgi-script
Options ExecCGI FollowSymLinks
</Files>
 DirectoryIndex index.py
Jacquelinejacquelyn answered 12/3, 2012 at 9:37 Comment(0)
Q
3

That doesn't look as cool as having a wsgi app running, so I recommend that you use the flask framework which is as simple as can be a sane framework.

Here's a link describing the install procedure on mod_wsgi.

Later on, you might want to consider a cool framework like Django, Pyramid, Grok ...

If you really want to use mod_wsgi like mod_php check Graham Dumpleton's great answer.

Queston answered 12/3, 2012 at 9:46 Comment(3)
I wrote somewhere about mod_python? I know that he is out of date... WSGI has just one point of running a script?Jacquelinejacquelyn
I guess I mis-read the article. I don't think you can use WSGI to do a mod_python/php-like configuration. I'm adding a relevant link to my answer.Queston
It is completely wrong to say 'It expect to connect to a running WSGI process through a socket.'. It does not work the way you seem to think it does.Charr
C
2

Technically what you are doing should work, but see AddHandler method for configuring mod_wsgi in:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive

That way you don't have to fiddle with Files directive.

SetHandler does similar thing but all files in context are treated as WSGI script files even if they may be static HTML or PHP files. You got away with it because qualified with Files, but better to just use AddHandler.

Do note that code reloading will not work like you are used to with PHP. See:

http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode

Charr answered 12/3, 2012 at 17:52 Comment(2)
The way with WSGIScriptAlias ​​I did not like.Jacquelinejacquelyn
WSGIScriptAlias is the way practically everyone uses because the norm is that people use a framework of some description. It is highly discouraged that you write WSGI stuff from scratch as it is too easy to get it wrong. If it doesn't work for other .py URLs then there is likely something else going on in the rest of your Apache configuration which is affecting it as it is possible to do.Charr
R
0

I run Python scripts like PHP using mod_cgi

Here is a tutorial by Apache. And the cgi interface to use is here by Python.org

A second good tutorial that I used is here once your up and running.

I would add that there is a simiplier way to configure Apache.

Step 1: The first step is not mentioned in the guides above is to enable CGI processing in apache.

sudo a2enmod cgi

This will automatically enable mod_cgid if your server is configured with a multi-threaded MPM, which was the case for me.

Step 2: Edit your httpd.conf or whatever it is named in /etc/apache2/sites-enabled in Linux Mint 19.2.

Enable a script for / with an index.py

<VirtualHost *:80>
   DocumentRoot /your/www/html
   DirectoryIndex  index.py
</VirtualHost>

Step 3: Enable other python scripts so they can also run in the same folder or in subdirectories.

<Directory "/your/www/html/*">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    AddHandler cgi-script .py
    AddHandler default-handler .jpg .png .gif .css .js .ico
</Directory>

ScriptAlias /   /your/www/html/

There are two caveats that I have encountered that must be adhered to to run python scripts successfully.

  1. When running in linux, make sure the line endings of each python file.py are unix line endings. Otherwise the python script will not run. For example, Notepad++ has Edit, EOL Conversion, Linux (LF) in its menu,tool bar.

  2. Ensure that the permission of each python file.py has execute permissions.
    In Linux Mint 19.2 I right click the file, go to Properties, go to Permissions, then check the checkbox at Execute: Allow executing program as file. Or just run the command:

chmod a+x python_script.py

Realpolitik answered 16/8, 2019 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.