Django and nginx. Do I still need apache? - django

I've searched around on this topic, and the advice seems to be that nginx should be there to serve static files and apache+wsgi for dealing with Django. A lot of this information is a couple of years old, so I was wondering if there was a way to simplify this without performance degradation and just rely on Nginx and fastCGI and/or wsgi.
I'm new to non-heroku deployment, so this is why I probably sound like I don't know what I'm talking about.

No you don't need Apache+wsgi along with Nginx+fCGI/wsgi. Nginx can serve static files really fast and it will use fCGI/wsgi for rest of the requests.
You should read answer to this questions[1] and other related questions mentioned there.
[1]. What is the disadvantage of using Django's fastcgi server

If you want to go the nginx route, the best choices are:
nginx -> gunicorn
nginx -> uWSGI
Running Python WSGI applications on top of FASTCGI is not generally as good an experience due to issues with the FASTCGI/WSGI adapters and how they are deployed with servers.
Apache/mod_wsgi is still a more than acceptable solution and it will actually perform better with less resources when run as:
nginx -> Apache/mod_wsgi
Because the bottlenecks aren't going to be the web server, ultimately it doesn't matter which you choose, so long as you set it up properly, something which most people wouldn't do as there site doesn't get enough traffic anyway, or they have no monitoring in place to know what they need to change.
Overall, picking which you think is easier to manage is the best thing to do when starting out.
For some background on what your real performance bottlenecks are going to be and the importance of monitoring, watch:
http://lanyrd.com/2012/pycon/spcdg/
That all said, you mention Heroku. Right now there is really only the once choice with Heroku and that is to use gunicorn and you wouldn't need to be worrying about nginx. That is a problem in itself though, as gunicorn alone is not a good option for serving static media assets so almost forced with Heroku to serve static assests elsewhere.

Related

Why is django's development automatic static file server not suitable for production?

As stated in: https://docs.djangoproject.com/en/dev/howto/static-files/
When DEBUG is set to True, the server automatically serves the static file, but it states:
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
But what exactly is inefficient and insecure about it? I just have a small-ish project on Heroku that I haven't set to "production" mode yet and I'm wondering what are the exact downsides.
Performance related reasons:
web servers are orders of magnitude better at serving static files.
AFAIK the development server is mono-threaded and can respond only one request at time, concurrent requests will block (most browsers make 4 concurrent requests trying to download assets in parallel).
Security related reasons:
using the app to serve static content is overkill (simplification is good for security)
the developers like to be on the safe side, so it is kind of a disclaimer
debug mode exposes a lot of information about the server
Django started in the news publishing industry where in general there is enough traffic to justify serving static content from a dedicated web server, probably the original developers have a bias for this arrangement.
That said, there are projects that replace the default development server by a more robust implementation based on gunicorn or tornado.
Kenneth (the author of requests, employed by Heroku) has a different opinion (source):
In reality, serving static files through Python/Django is fine for
production — those requests are no different than dynamic ones.
Performance will be fantastic, but not as good as nginx.
If you're that heavily concerned about efficiency then you shouldn't
be hosting those files on your server anyway, you'd be pushing them to
an CDN like S3+Cloudfront and the like.
Another benefit to this approach is development:production parity.
And on heroku, you can't use Nginx to server static files, actually you can't do it on most other PaaS too, I got the same problem on cloud foundry last year. But there is a workaround:
On Heroku, your application directly responds to HTTP requests,
instead of going through an additional web server like Apache or
Nginx.
We recommend most applications serve their assets strait from Django
or a CDN.
Django doesn't recommend serving static files in production because of
the design of its static file handler.
Luckily, there is a library called DJ-Static which makes uses a
production-ready WSGI asset server.
I've written up a guide for Django and Static Assets here:
https://devcenter.heroku.com/articles/django-assets
Read the following discussions for more details:
Serving static files for a Django app
serving static files via gunicorn

Choice of server for local Django webapp?

I've put together a Django app that was intended to run on Pythonanywhere. However, I soon found out that Pythonanywhere's free plan blocks the remote sites which I intend to interact with, so I decided running the app on a local machine would be good enough for my purposes.
Now, I know that Django's development server isn't meant to be run in a production setting. So, what server daemon do I run on the aging Windows workstation the app's probably gonna be deployed to? Apache, lighttpd, Cherokee? Something else?
My first priority here is conserving system resources as much as I can; I'm probably also going to use Python's built-in sqlite3 instead of MySQL.
Personally I deploy Django with fcgi and Nginx. Nginx offers various strategies to conserve system resources; its use as reverse proxy is well documented and widely used.
The question/answer pair here might help you clear some initial questions about the various components when deploying Django.
Sorry to hear you can't use PythonAnywhere's free version :-(
I'll second the usage of nginx; it's what we use as the front-end for PA. However, we use uwsgi for the backend. It works really well for us and is very easy to configure.

Apache or Nginx to serve Django applications? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I want to deploy a Django web application, and hence I need to choose a web server to serve the Python files.
I should mention that my production site will be on a single server, which will host the database and the web server. As momentum picks, I aim to move the database to dedicated server etc.
Here are my questions:
Should I use one web server or two? The context of this question is that lots of people recommend using NginX to serve static media files and Apache to serve the Python, which beckons the following questions:
Why can't we use just one server. I understand Apache may be a beast at times, therefore I would suspect people to use NginX to serve BOTH static media files and python files.
If using one server, what is better, Apache or NginX. I am experienced in Apache, but I have heard only good things about NginX.
What are the advantages to using FastCGI as opposed to mod_wsgi?
Many thanks in advance
Should I use one web server or two? The context of this question is
that lots of people recommend using NginX to serve static media files
and Apache to serve the Python, which beckons the following questions:
Why can't we use just one server. I understand Apache may be a beast
at times, therefore I would suspect people to use NginX to serve BOTH
static media files and python files.
If you currently have no other sites that are already configured in one way or another, or you need some specific features that are mutually exclusive between the various servers, I see no reason for using multiple servers. This just adds unnecessary complexity and configuration.
If using one server, what is better, Apache or NginX.
I am experienced in Apache, but I have heard only good things about NginX.
As with all "which is better" questions this is usually a matter of preference. And to get a specific answer you probably need to ask more specific questions.
If you already have experience with a specific server and you just want to get up an running quickly, then I would suggest going with what you already know for the time being. You can always switch to another web server later.
On the other hand it's a good opportunity to learn about the alternatives.
tl;dr : I would go for what is easier to configure and manage. Personally I would go for a nginx and gunicorn, mainly because it's easy and there are plenty of resources available if you should get stuck.
I wouldn't worry too much about the performance until you actually need to. All staple web servers are tried and tested so it mostly comes down to the requirements of the application and the actual load, which needs monitoring and modeling and testing for fine tuning anyways.
What are the advantages to using FastCGI as opposed to mod_wsgi?
Differences and uses between WSGI, CGI, FastCGI, and mod_python in regards to Python?
articles to read (some old, some new);
http://gunicorn-docs.readthedocs.org/en/latest/deploy.html
https://docs.djangoproject.com/en/dev/howto/deployment/
http://serversforhackers.com/editions/2014/03/25/nginx/
http://blog.dscpl.com.au/2009/05/blocking-requests-and-nginx-version-of.html
http://www.thegeekstuff.com/2013/11/nginx-vs-apache/
http://raspberrywebserver.com/raspberrypicluster/comparing-the-performance-of-nginx-and-apache-web-servers.html
http://www.bearfruit.org/2013/04/19/reddit-is-melting-our-server-heres-what-we-did-nginx-apache-django-and-mysql/
In production, Apache + mod_wsgi or Nginx + mod_wsgi?
http://www.peterbe.com/plog/fcgi-vs-gunicorn-vs-uwsgi
http://www.aosabook.org/en/nginx.html
http://c2.com/cgi/wiki?PrematureOptimization
I'm not sure who is recommending to you that you use both Nginx and Apache, but that's a horrible idea. Whichever you choose, either will simply act as the reverse proxy, serving only static resources and handing everything else off to a subprocess like uwsgi.
I prefer Nginx because it's light-weight and extremely fast out of the box. Apache can be just as good, but requires building from source and knowing exactly what configuration to use to match Nginx. However, Apache has more features and is a little easier to work with. It's really up to you and the needs of your application.
However, whichever you choose, you only need one -- not both.
I think the best choices is virtualenv, uwsgi and nginx.
I changed all my servers now and I'm really happy with performance.
Here is good tutorial on how to setup you webserver
http://senya.pl/2011/03/sexy-nginx-uwsgi-stack-for-django-with-virtualenv/
Question 1) You can use just one server, but for serving static media a solution like lighttpd or nginx will be much faster. I would stick with Apache if you really want to use only one server, it has all the flexibility you need and it is the most common webserver.
Question 2) Depends on your purpose. You can find info here: Deploying Django (fastcgi, apache mod_wsgi, uwsgi, gunicorn)
I tried to follow the suggested link by Nicowernli, but senya.pl was down at that moment.
This seems like a good alternative tutorial....
Gonna try it, just read the first 2 chapters, but seems very complete and really step by step:
http://www.abidibo.net/blog/2012/04/30/deploy-django-applications-nginx-uwsgi-virtualenv-south-git-and-fabric-part-1/
The less, the better.
The best way to deploy Django application over Nginx is use uwsgi. It's pure WSGI and built in supported by new version Nginx.
I have used gunicorn + eventlet as the Python server, and nginx as the reverse proxy with great success. Recently I switched to uWSGI and it seems to be just as good of a solution if not better. I have yet to try apache and Django although I was an apache user prior to using Django. Here is a good write up on getting it all done: http://radtek.ca/blog/django-production-deployment-via-nginx-and-gunicorn-and-virtualenv/

Deploying first Django project

I run a small VPS with 512M memory of memory that currently hosts 3 very low traffic PHP sites and a personal email account.
I have been teaching myself Django over the last few weeks and am starting to think about deploying a project.
There seem to be a very large number of methods for deploying a Django site. Given the limited resources I have available, what would be the most appropriate option?
Will the VPS be suitable to host both python and PHP sites or would it be worth getting a separate server?
Any advice appreciated.
Thanks.
There aren't really a great number of ways to do it. In fact, there's the recommended way - via Apache/mod_wsgi - and all the other ways. The recommended way is fully documented here.
For a low-traffic site, you should have no trouble fitting it in your 512MB VPS along with your PHP sites.
Django has documentation describing possible server arrangements. For light weight, yet very robust set up, I'd recommend Nginx setup. It's much lighter than Apache.
I run several low-traffic Django sites on a 256 VPS without problem. I have Nginx setup as a reverse proxy and to serve static files (javascript, CSS, images) and Apache using mod_wsgi for serving Django as described in the documentation.
Running PHP sites as well may add a little overhead, but, if you're talking about low-traffic "fun" sites then you should be fine.

Help to set up nginx+fastcgi webserver for django

It is the first time that I am going to deploy a django project in a production server. I am a big fan of pure nginx+fastcgi and try to avoid apache at any cost. On the other hand I know that configuring nginx+fastcgi sometimes can be VERY painful. I wasted literally days to figure out how to set up and fix 502, 504 errors on my nginx+php-fastcgi arrangement. But if you could climb that mountain of difficulties it is rock solid and fantastic, specially for server tight resources.
So if you think this practice worth doing with django, I appreciate if you could point me to a painless tutorial to set up nginx+fastcgi to serve django, or share your experience about it
Instead of going through all that, I suggest you look at Gunicorn, which is preconfigured to handle Django, and written in pure Python. Don't let that fool you though, it's fast. You simply proxy_pass the request to gunicorn on a unix socket or regular network port and it handles the rest. Here's the gunicorn webpage.