Using Python as PHP on Apache / Windows

I understand that I have to use mod_wsgi to run Python, and I'm trying to get this setting, but I'm confused:

This is an example 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 that I need to configure my web server to point to a 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 connect my web server to the most basic state, where I can download the file with print "Hello World" , request it and say "Hello World"?

+4
source share
5 answers

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 to access python code, because apache has to reload python every time a page accesses it. Ok for a one-time maintenance script, you only call it from time to time, but it’s not suitable for active content that users access.

Edit: I did not understand that you are on Windows. Something like this should be possible. Try googling python apache cgi .

Edit: if you have apache running in cgi mode, you don't need to restart it every time. If the script is present and executed at the specified URL path, it will be launched. If this is not the case, you will get an error on page 404

Edit: I did a very quick Google search for "python cgi" and found these slides 10 years ago by the creator of Python. They document an outdated version of the language, but slides from the age of 41 may be useful to you. As I said, people have left scripted 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 service framework can be useful. I can recommend Web2py as a very reliable and secure environment that will allow you to write scripts and dynamically add them. It has a version of Windows that includes a simple web server, or you can also configure apache. Since everything is inclusive, you can start up within minutes if you read the introductory information. If you have not used web frameworks before and are not familiar with lanugage, for example, the "model view controller", do not delay. http://www.web2py.com/

+3
source

Most similar to the PHP model, perhaps Python Server Pages , PSP.

mod_python has a PSP handler . The files you created 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 forces the PSP model to take another step, just like Webware for Python .

This article is a good introduction to mod_python PSP.

+6
source

I don’t think that WSGI / mod_wsgi is compatible with what you want to do - the WSGI application usually uses URL parsing, which makes it incompatible with the requirement “works like PHP”.

If you don't want to use CGI, I think mod_python Publisher is closer to what you want: http://modpython.org/live/mod_python-3.3.1/doc-html/tut-pub.html

+2
source

Contrary to what others say, you can get mostly where you want. StackOverflow, however, is not a good place to get an explanation of what to do. Instead, in this case, you'd better go to the official mod_wsgi mailing list on Google Groups at:

http://groups.google.com/group/modwsgi

You can also get some ideas about the path you want to read:

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

For something, it’s really akin to how PHP works with respect to or mapping URLs, you don’t want to use WSGIScriptAlias ​​and instead use the AddHandler method to designate the files as a WSGI script.

I would also recommend reading:

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

This is not the whole picture, although with regard to reloading the code, since there are changes in mod_wsgi 3.X that are not documented there, which adds even more options for reloading the source code.

In general, although you could follow the PHP models, I would really like to offer a high-level Python web environment. This, combined with the mod_wsgi daemon mode function to trigger a reboot by touching the WSGI script file, is usually more than enough and more predictable.

+2
source

Wandering around a bit from your original question, but trying to answer some of what comes up in response to sparklewhiskers:

For an example of how WSGI works in a shared hosting environment, see these pages about Passenger WSGI support on Little Orange and Dreamhost , where the necessary server configuration is done through a .htaccess file or a web dashboard.

I think Python hosting on shared servers is still relatively unusual; I expect Python applications to run on some kind of VPS or Google AppEngine .

Some Python web applications do not start from the apache or nginx module at all, they have their own permanent server, and if you want them to be on the same server, like something else, you configure your other server to proxy for your Python server. But this other story is completely.

+1
source

Source: https://habr.com/ru/post/1299306/


All Articles