Deploying django(python) project with mod_wsgi - python

I am new to django and mod_wsgi, when I was trying to deploy a django project(in python) demo on Apache using mod_wsgi, I encountered some problem as following:
When the demo runs on django's built-in webserver with command 'python manage.py runserver', everything looks normal like Running on built-in server
while when running on Apache/mod_wsgi with command 'mod_wsgi-express start-server wsgi.py' , it looks like this:
Running on Apache/mod-wsgi
I have no idea which part of my code or configuration goes wrong, could anyone help me out?

This is because runserver serves css files for you. But in production you need to set up Apache to serve your static folder. Add alias to your apache config, e.g.
Alias /static /path/to/static_folder
Read more on managing static files.

Related

Is mod_WSGI replacing what apache is doing and how I can make wsgi work at a specific path only

I am quite new to Apache and WSGI, there are a lot of things confusing me.
After a fresh install of apache2 on ubuntu, I can open this URL to see the default apache page http://localhost/index.html, the file is residing in /var/www/html. So this is the function that apache server provices, serving http connection.
Here's the current situation:
I've created a simple Django project and install mod_wsgi onto my apache server,and finally I managed to deploy the project to apache and the page iIve created can be accessed correctly.
BUT now when I try to access the index.html i mentioned above, it shows the url mapping cannot be found(yes I do not have this mapping in the django project).Is Django taking over all the path that point to the server?
How can I make the django project only map to a specific path like http://localhost/Django/[MY URL MAPPING] and keep other url mapping untouched.
So I can access the static HTML(index.html) by accessing http://localhost/index.html/ and access my Django project by accessing http:/localhost/Django/[xxx]
Is this possible? or I have to use another virtual host at another port?
Any help will be much appreciated.
You can do this by mounting Django at a different point:
WSGIScriptAlias /Django /path/to/mysite.com/mysite/wsgi.py
This runs the WSGI application at /Django, and leaves Apache to serve the rest of the files directly.

Restarting Python to show changes

I am new to the python script. How do you restart a python script with Django through SSH?
I believe that you project is django based.
Django framework has a project directory, where static files are initially placed. And when you run your project for development purposes, django takes all static from the project static directory.
But for the production django deployment usually get runned command manage.py collectstatic to copy all static into another place. And sometimes there are another command - compress. To compress that static.
This is done to make webserver (apache or ngingx) respond static files without asking django process requests like "five me that static file" and gives ability to cache static files. And it speed-up all work.
So, if you serveer is setted up to take static files from static dir (looks like DOMAIN/public/static/main/ is the static dir) it will have no idea about changes in the project dir (looks like DOMAIN/project_book/main/static/main/ is a project dir).
But I agree with #Sause, looks like you have to be very carefull and have exact understanding of what you're doing with killing any process on the production server.
I think it could be useful for you to read Django documentation about static files too. https://docs.djangoproject.com/en/1.9/howto/static-files/
running pkill python in the ssh works.

how to enable mod_xsendfile in apache2 in case of using mod_wsgi-express and django

I'm a newbie in django and lately I've been trying to use apache as my django web server to test mod_xsendfile for a project. My environment: ubuntu 14.04, python 3.4, apache2, django 1.8, having mod_wsgi installed using pip. I also have downloaded and compiled mod_xsendfile on apache2
I have several problems though:.
Problem1: In the documentation provided at here it states to set mod_xsendfile on and provide a path for it in the virtual host configuration; I searched and noticed the virtual host config takes place in a file named httpd.conf and in ubuntu 14.04 having apache2 installed it seems that the whole structures has changed so I found no such file.
Problem2: I am using mod_wsgi-express for running my server and it performs all the configurations automatically. I have read the documentations in here and here but I couldn't find in any of them being stated where the general configurations (apache configurations to be specific) are pulled from.
Long story short: my question being: how can I enable mod_xsendfile considering my environment and the fact that I am using mod_wsgi-express which generates the configs automatically.
Note:: I run the server using the following command: $python manage.py runmodwsgi
Thanks to Graham Dumpleton here's the solution that worked for me:
I put the following directive into a file named extra.conf:
LoadModule xsendfile_module 'usr/lib/apache2/modules/mod_xsendfile.so'
XSendFile On
XSendFilePath /tmp/PrivateFiles
Then I passed the extra.conf path as an arguement to the --include-file=<> option of modwsgi command as follows:
$python manage.py runmodwsg --include-file=/etc/apache2/sites-available/extra.conf

Where is Flask running from on my OS X machine?

I'm used to building my websites with PHP, and on my OS X machine I expect to have to ensure that I have my scripts living in an explicitly specified location that I define as my Apache server's document root. But when I follow the simple instructions for building a Flask website, I magically get a working website, with nothing at all in any of the places on my machine that serve as document roots, regardless of where I have my Flask script. This is especially confusing since I always think if deployment as involving careful duplicating the file structure of my site under document root on the deployment server's document root.
Where is Flask "running from" on my OS X machine? Where do I "put it" when I deploy it (and what to I put)?
It's running from wherever you put it. You surely know where you saved the code: that's where it is.
But your mistake is in thinking that this development environment is running through Apache, or indeed has anything to do with how you'll run it in production. Neither is true. You're using a development server, the separate Werkzeug project, but that is not suitable for running in prod.
When you are ready to deploy, Flask has full instructions on how to either connect it to Apache through mod_wsgi, or set up a separate WSGI server which you'll usually connect to through a reverse proxy such as nginx.
Supposed you have your main.py under /path/to/my_project/, when you run the internal server python main.py, Flask is then running under your project folder.
Of course that built-in server is only good for development, when you're trying to deploy for production, normally Gunicorn (via wsgi app, read more HERE) or other web server is more appropriated (and advised by Flask) itself. And your production folder can be placed wherever you want, just like Apache PHP you may place your folder under /var/www/ (EDITED: as Daniel Roseman pointed out, you may try to change this folder location for security concern), it's the same for Flask, that's nothing stops you placing the folder but rather have the permission set properly.
Hope this helps.

Files being served are stale / cached ; Python as fcgi + web.py + nginx

I'm serving files in ubuntu using Nginx and fcgi, python and web.py. My index.py contents are:
app = web.application(urls, globals(), True)
if __name__ == "__main__":
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()
And I'm launching with:
spawn-fcgi -n -d /usr/share/nginx/www -f ~/Projects/index.py -a 127.0.0.1 -p 9002
Which works fine, EXCEPT, once I make changes to the source files (index.py or any class it includes), those new files are never loaded. I have to stop spawn-fcgi and restart it to see any changes. This make development very cumbersome.
In addition I've turned off the generation of python .pyc/cache files.
TIA
I deploy my apps using nginx+uwsgi or apache+mod_wsgi, both of them reload app if I touch code.py. But I run apps from integrated server when developing.
If running web.py integrated server in development mode that has its own reloader is not an option then the only option is to write your own dispatcher with reload functionality.
That is most likely by design.
You do normally not want modules reloaded in a production environment (performance, and due to the fact that a module reload in Python does not always have the intended effect).
For development, use some other simpler server model (for example, Django provides its own development server for this exact purpose, I have not used webpy but it appears to have the same functionality according to the tutorial). Use nginx only when deploying the webapp, not in your development environment.
You should not have to bother about .pyc files under normal circumstances (exceptions are in some problematic NFS setups, or when .pyc files are created by the wrong user with the wrong permissions).

Categories