Apache2 and .psp files - python

I have a server setup, and working. Trying to get add python scripting support. I seem to have it working, but how do I configure Apache2 to look for index.psp with out me needing to be specific. Meaning, I can specify index.psp, and it works, but I want apache to search for index.html, then index.php, and then also look for index.psp. How do I include .psp in the search?

Set DirectoryIndex index.html index.psp index.psp (docs).

Related

Where to put django files on server?

I have a question. I work with one guy who is developing in Php, I am developing in Django right know. This is my first time doing something for other people and he firstly asked me to put my application to Apache Web server document root(I think he didn't know that much about Django, and he did everything this way), so I did it, because I didn't know that much about Apache and servers. But right know I am scared because I have all the python files in document root and I realized that this Web server document root \www\myproject might be accessible to other people. How should I change it, where should I put it, what is the best practice? I am deploying with mod_wsgi and Apache 2.4. In the documentation it says, that I shouldn't put code in Web server document root too, but if I put it somewhere else can I access it the same way, like I used to when it was on the Web server document root? Will I have to do something other than changing paths in Apache config?
You should put the Django project anywhere on the machine except for the apache document root, for security reasons as you have pointed out.
You can then configure apache to pint to the location of your wsgi.py which can live in the Django project if you wish:
WSGIScriptAlias / <path to the wsgi.py>
I'm not aware of a standard place to put the Django project root directory, I guess it depends on the OS and the distribution.

What is the meaning of the lines written in wsgi.conf

I'm trying to use Python in my project (without any other framework as requested by my teacher.)
So I've installed Apache 2.x and mod_wsgi and, following a tutorial, I've run my first Python script.
In the specific, the tutorial told me to write in the file /etc/apache2/conf-available/wsgi.conf the following line:
WSGISriptAlias /test /var/www/html/test.py
...and it works!
Despite of this, if I try to change the containing folder of the Python source, it won't work anymore. Can someone tell me how to make this work and what is the meaning of the line written above?
P.S. I need the Python script because my site, in relationship to a form input, have to decide which page to open next.
So I have something like:
...
<form action="test" method="GET" name="query">
...
</form>
I've omitted the .py extension because tutorial said it.
Thanks to all!
As per the documentation:
Description: Maps a URL to a filesystem location and designates the target as a WSGI script.
Syntax: WSGIScriptAlias URL-path file-path|directory-path
Context: server config, virtual host
So when someone hits the url /test on your site, the module at the filesystem path you specify gets run.

Python Shared Hosting

I would love to be able to use Python and Django for web applications at the company I work for. We currently use PHP because everyone is familiar with it and easy to deploy for a large number of clients. We can host anywhere between 10 to 100 websites on a single virtual server.
Is it possible to serve a number of websites from a single Apache and Python installation? Each website must have their own domain among other things, such as email accounts.
I wouldn't use Apache, the current best practice is an Nginx frontend proxying requests to uWSGI servers. Read about the uWSGI Emperor mode. It's very versatile. http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html. Each individual app can be modified, removed added to dynamically. We use it at PythonAnywhere to serve thousands of web applications
There are other WSGI servers that you can use as well. uWSGI just seems the most scalable in my experience.
Yes, It is definitely possible. In our setup, typically we have django behind mod_wsgi, Apache and nginx
You can configure apache's Virtualhost, to point to a specific mod_wsgi which in turn points to specific code.
Quoting from here - Refer to the SO post for further information.
There are at least two methods you can try to serve from a single
instance:
Use apache + mod_wsgi and use the WSGIApplicationGroup and/or
WSGIProcessGroup directives. I've never needed these before so can't
be completely sure these will work the way you want, but regardless
you can definitely use mod_wsgi in daemon mode to greatly improve
your memory footprint.
You can play with Django middleware to deny/allow URLs based on the
request hostname (see HttpRequest.get_host() in the Django docs).
For that matter, even though it would be a slight performance hit,
you can put a decorator on all your views that checks the incoming
host.
Yes, you can easily serve up many sites using a single Apache / mod_wsgi installation. Typically you would do that with a separate virtualhost section for each website. See the virtualhost docs. You want to use a different servername directive in each virtual host config to specify what hostnames get routed to which config. See more detailed documentation in name based virtual hosts

Setting up Python on Windows/ Apache?

I want to get a simple Python "hello world" web page script to run on Windows Vista/ Apache but hit different walls. I'm using WAMP. I've installed mod_python and the module shows, but I'm not quite sure what I'm supposed to do in e.g. http.conf (things like AddHandler mod_python .py either bring me to a file not found, or a forbidden, or module not found errors when accessing http://localhost/myfolder/index.py). I can get mod_python.publisher to work but do I "want" this/ need this?
Can anyone help?
Thanks!
Stay away from mod_python. One common misleading idea is that mod_python is like mod_php, but for python. That is not true. Wsgi is the standard to run python web applications, defined by PEP 333. So use mod_wsgi instead.
Or alternatively, use some web framework that has a server. Cherrypy's one is particulary good. You will be able to run your application both standalone and through mod_wsgi.
An example of Hello World application using cherrypy:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
application = HelloWorld()
if __name__ == '__main__':
cherrypy.engine.start()
cherrypy.engine.block()
Very easy huh? Running this application directly on python will start a webserver. Configuring mod_wsgi to it will make it run inside apache.
You do not NEED mod_python to run Python code on the web, you could use simple CGI programming to run your python code, with the instructions in the following link: http://www.imladris.com/Scripts/PythonForWindows.html
That should give you some of the configuration options you need to enable Python with CGI, and a google search should give you reams of other info on how to program in it and such.
Mod_python is useful if you want a slightly more "friendly" interface, or more control over the request itself. You can use it to create request filters and other things for the Apache server, and with the publisher handler you get a simpler way of handling webpage requests via python.
The publisher handler works by mapping URLs to Python objects/functions. This means you can define a function named 'foo' in your python file, and any request to http://localhost/foo would call that function automatically. More info here: http://www.modpython.org/live/current/doc-html/hand-pub-alg-trav.html
As for the Apache config to make things work, something like this should serve you well
<Directory /var/www/html/python/>
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
If you have /var/www/html/ set up as your web server's root and have a file called index.py in the python/ directory in there, then any request to http://localhost/python/foo should call the foo() function in index.py, or fail with a 404 if it doesn't exist.
AddHandler mod_python .py
Have you set 'PythonHandler'?
These days, consider using WSGI instead of native mod-python interfaces for more wide-ranging deployment options. Either through mod-python's WSGI support, or, maybe better, mod-wsgi. (CGI via eg. wsgiref will also work fine and is easy to get set up on a development environment where you don't care about its rubbish performance.)

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

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.
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/)
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.
Are you running Python on UNIX or Windows?
An alternative to mod_python and FastCGI is mod_wsgi. You can find out more at modwsgi
I have built and installed this on Solaris without problems. I had previously tried mod_python but ran into problems with shared libraries as part of the build. There are good install docs available.
The problem for me wasn't in Apache set up, but in understanding how mod_apache actually uses the .py files. Module-level statements (including those in a if __name__=='__main__' section) are not executed--I assumed that the stdout from running the script at the commandline would be what the server would output, but that's not how it works.
Instead, I wrote a module-level function called index(), and had it return as a string the HTML of the page. It's also possible to have other module-level functions (e.g., otherFunction()) that can be accessed as further segments in the URI (e.g., testScript/otherFunction for the file testScript.py.)
Obviously, this makes more sense than my original stdout conception. Better capability of actually using Python as a scripting language and not a humongous markup language.

Categories

Resources