Apache Django Mod_Wsgi Sessions Development Environment - django

I'd like to know how I can maintain sessions while developing on my local machine (django, apache, mod-wsgi).
Each time I make updates to python code I need to restart Apache for the changes to take effect.

Read the documentation. See:
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
It explains in a great amount of detail about source code reloading in Apache/mod_wsgi.
Just touching the WSGI script file will not do anything if you aren't use mod_wsgi in the correct mode.
And yes you can use Apache/mod_wsgi as a development server if you set it up with daemon mode and code change monitor as documented in that page.
As for MaxRequestsPerChild, that is not recommended and will only work in embedded mode of mod_wsgi and not in daemon mode.

You only need to touch your WSGI script for the changes to take effect.

Put this in your Apache conf file.
MaxRequestsPerChild 1
This will force Apache to reload the python files after each request.

Related

Apache + mod_wsgi vs nginx + gunicorn

I want to deploy a django site (it is the open source edx code on github).
I am faced with choosing between using
Apache with mod_wsgi
nginx with gunicorn
I have used Apache with mod_wsgi and it's cool enough, but i have no experience with the second option.
Which of these would be a better option in terms of speed and also to some extent, ease of use?
NB: I would need to run two different django sites on say, port 80 and 81 and access them from two different subdomains.
Nginx is a really light and easy to use solution and along with gunicorn it allows us to run any wsgi application and scale it easily.
Nginx is better at handling requests since it does not spawn a new process for every request unlike Apache.
I have written an answer on how to deploy django with nginx for a related question:
Deploying Django project with Gunicorn and nginx
Well,the few milliseconds you get with Nginx will not make a hudge difference regarding the time other processes take. Nginx may save RAM but it would only be a great difference on servers with a few RAM. For specific uses on big website there could be some more notable differences but this will become an expert affair then.
The real difference for most is probably the ease of learning. I don't find Apache to be specifically hard to use and the doc is clean. However most of Python tutorials I found are about using Nginx with Gunicorn.
If you already know how to use Apache with Python it would probably be more straight to the point to use it, unless you want to learn Nginx too to improve your CV.
However, if you are a newcomer, there is more documentation about Nginx with Python. It makes it the easier option.
I have good experience with nginx and gunicorn. They keep on working great when I've finally set all the settings right and got it running.
For nginx and gunicorn they are:
* nginx configuration files (/etc/nginx/sites-enabled/ and /etc/nginx/nginx.conf)
* gunicorn configuration files (/etc/init/gunicorn.conf and /etc/gunicorn.d/)
I've seen a tutorial for apache + mod_wsgi and it seems so much simpler to set up.
I have primarily worked with nginx and gunicorn. I am currently working with apache + mod_wsgi. It is actually easy if your Python version is 2.7 because mod_wsgi when installed directly from the package manager will work normally. But if your code is in a different Python version. mod_wsgi has to be built from source with the same version. If you installed your Python also from source then the procedure to get the whole web application working is fairly difficult.
Nginx and gunicorn on the other hand do not have any version issues, since the proxypass param makes it easy to forward requests to gunicorn. All we need to ensure is that gunicorn is running with the same version of Python that your code is in.

Do I need to restart apache each time after changing code

I am deploying a Django app on webfaction. All code are uploaded, but sometimes there are still some modification I need to make. My workflow is edit local file in Pycharm and sync the change to remote server (Apache). So, for example, do I need to restart apache every time after changing views.py for the change to take effects?
If you are using modpython yes, you have to reload apache every time or your modules will not be reloaded.
If you are using apache and mod_wsgi then touching the wsgi module is enough.
As other already suggested nginx + gunicorn (or better nginx + uwsgi) are better options than apache + mod_python to serve your django app.
In fact, you do not have to, you just have to « touch » the wsgi.py file (if you are using the mod_wsgi apache2 module). You can do it by uploading the wsgi.py file every time you upload something.

bitnami django, solution to restarting service?

Hey I use installed bitnami django 1.3.0,
but whenever I add changes to urls.py or views.py in my system due to some error. The error won't disappear after refresh.
I have to restart my bitnami Service, "stop" and then "start" it, which is time consuming, I feel like I'm coding C# apps in visual studio. Sometimes even that doesn't work, I have to sometimes restart my computer and then I suddenly realize "oh wow, the error is solved now!"
Any solution to this? Why does everything require a runserver / restart?
You can use Apache for deploy your application in production but use the Django server for development. You will need to configure your application for being served by apache later (modifying the settings.py and the apache configuration file) but during the development you won't need to restart the server for every change.
Everything requires a restart because of the way that the python process operates. It does not reload the file when it's changed (outside of runserver..which is an anomaly, and just there for convenience)
Python execution isn't like PHP execution in that way, and your code isn't dynamically loaded on every page refresh, it's loaded on every server restart.
Hope that helps.

how to restart single apache site on a ubuntu vps rather than all sites

I've got a ubuntu vps and apache mod wsgi installed and serving my django sites.
however i have to restart all of apache rather than the site i have amended and its going to be a bit shonky if i tell clients that i have restarted their site cos i updated another site.
is there a tutorial somewhere to teach me how to configure this? i couldnt find one in googles keywords soup.
I'm already using virtualenvs if it helps.
Assuming you use mod_wsgi in daemon mode on UNIX/Apache 2.X system to run Django and have shell access to your machine all you need to do is touch the wsgi configuration for your project.
touch your_project.wsgi
See mod_wsgi documentation on Reloading Source Code and Django - mod_wsgi wiki for more references.

Reload django wsgi scripts without root

I have an install of django on apache using embedded wsgi. I DON'T have root on the machine.
How can I tell apache to reload the python instance when I deploy new source code?
I tried removing all the .pyc files and it still is running the old code.
If you are running mod_wsgi in daemon mode with apache, you may not have to restart apache to get it to reload.
I just touch my wsgi file (unix: touch updates the 'saved date' of a file) and apache reloads it on next access/web-hit.
See http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
... as long as you can alter the timestamp on the WSGI script.
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
This article about reloading source code with mod_wsgi goes into how to reload source code, and even how to create a monitor script to automatically reload the daemon when you make source changes. It's really good for setting up a development environment using mod_wsgi.