Reputation: 38564
I understand that I should use mod_wsgi to run Python, and I have been trying to get that set up, but I'm confused about it:
This is a sample configuration I found for web.py:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias /appname /var/www/webpy-app/code.py/
Alias /appname/static /var/www/webpy-app/static/
AddType text/html .py
<Directory /var/www/webpy-app/>
Order deny,allow
Allow from all
</Directory>
So... I understand I have to configure my web server to point to the python application? Isn't there a way to use it like PHP, where, when you request a .py
file, Python interprets it? How can I get my web server to the very basic state, where I can upload a file containing print "Hello World"
, request it, and have it say "Hello World"?
Upvotes: 6
Views: 2631
Reputation:
I don't think WSGI/mod_wsgi is compatible with what you want to do-- a WSGI application generally takes over URL parsing, which makes it incompatible with your "works like PHP" requirement.
If you don't want to use CGI, I think mod_python's Publisher is closer to what you want: http://modpython.org/live/mod_python-3.3.1/doc-html/tut-pub.html
Upvotes: 2
Reputation: 910
I think you can use mod_cgi with apache and put in the URL of an accessible python file, with the first line of the script
#!/usr/bin/python
however this is a VERY inefficient way of accessing the python code because apache has to reload python every time the page is accessed. Ok for a one-off maintenance script only you call every now and then, but not suitable for active content accessed by users.
Edit: I didn't realise you were on Windows. Something similar should be possible. Try googling python apache cgi
.
Edit: If you have apache working in cgi mode, you don't need to restart it every time. If the script is present and executable at the URL path given it will run. If it isn't then you'll get a 404 page not found error
Edit: I did a very quick Google search for 'python cgi' and found these slides from ten years ago by Python's creator. They document an obsolete version of the languange but the slides from 41 onwards may be useful to you. Like I say, people have moved away from scripting web applications using this method but if your requirements are simple it will still work. http://legacy.python.org/doc/essays/ppt/sd99east/index.htm
Edit: The best approach depends on what you are trying to do. Using a framework that handles the serving for you may be useful. I can recommend Web2py as very capable, secure framework that would allow you to write scripts and add them dynamically. It has a windows version that includes a simple web server, or you can configure apache as well. As everything is included you could be up and running within minutes if you read the introductory information. If you've not used web frameworks before and aren't familiar with lanugage such as "model view controller" then don't be put off. http://www.web2py.com/
Upvotes: 4
Reputation: 100776
Most similar to the PHP model is perhaps Python Server Pages, PSP.
mod_python
has a PSP handler. The files you create look like this:
<html>
<body>
<%
for n in range(3):
# This indent will persist
%>
<p>This paragraph will be
repeated 3 times.</p>
<%
# This line will cause the block to end
%>
This line will only be shown once.
</body>
</html>
Spyce takes the PSP model a step further, as does Webware for Python.
This article is a good introduction to mod_python
's PSP.
Upvotes: 6
Reputation: 58523
Contrary to what others say, you can get mostly where you want. StackOverflow is however not a very good place to get an explanation of what to do. Instead, you would in this case be better off going over to the official mod_wsgi mailing list on Google Groups at:
http://groups.google.com/group/modwsgi
You can also get some ideas of the path required by reading:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines
For something truly akin to how PHP works in respect or URL mapping, you don't want to be using WSGIScriptAlias and instead should use the AddHandler method of designating files as being WSGI script files.
I would also advise reading:
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
That latter isn't the whole picture though in respecting of code reloading as there are changes in mod_wsgi 3.X which aren't documented in there which add even more options for automatic source code reloading.
Overall, although you could follow the PHP model of doing things, I would very much suggest a high level Python web framework. That in conjunction with daemon mode feature of mod_wsgi for triggering reloads by touching the WSGI script file is generally more than enough and more predictable.
Upvotes: 2
Reputation: 4798
Wandering a bit afield from your original question, but trying to answer some of what's come up in reply to sparklewhiskers:
For an example of how WSGI works in a shared hosting environment, see these pages on Passenger's WSGI support at A Small Orange and Dreamhost, in which the necessary server configuration is done through a .htaccess file or a web panel.
I think Python hosting on shared servers is still relatively uncommon; I expect more Python applications are run on some sort of VPS or Google AppEngine.
Some Python webapps aren't launched from an apache or nginx module at all, they have their own persistent server, and if you want to have them on the same server as something else you configure your other server to proxy to your Python server. But that's another story entirely.
Upvotes: 1