46

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:

  1. 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:
    1. 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.
    2. If using one server, what is better, Apache or NginX. I am experienced in Apache, but I have heard only good things about NginX.
  2. What are the advantages to using FastCGI as opposed to mod_wsgi?

Many thanks in advance

Barry Steyn
  • 1,563
  • 3
  • 19
  • 25
  • 3
    In my experience, Apache with mod_wsgi comes with far fewer headaches. – nullability Jul 15 '14 at 21:01
  • @Barry What did you end up doing? It seems Django [docs](https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/) suggest using Nginx as the primary choice for static media and Apache as the [primary choice](https://docs.djangoproject.com/en/1.11/topics/install/) for Django apps. – Anupam Jun 27 '17 at 09:01

7 Answers7

12

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);

Community
  • 1
  • 1
kalvatn
  • 198
  • 1
  • 8
3

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.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • 5
    Having nginx as a front end to Apache/mod_wsgi actually has various benefits because it allows Apache to perform better, using less resources, when running the dynamic Python web application. So it is definitely not a 'horrible idea'. – Graham Dumpleton Feb 06 '12 at 23:34
  • Umm... yeah, it really is. If you're already running nginx, all you need for Django is something like uwsgi, which is orders of magnitude faster and more light-weight than Apache on its best day. There's simply no good reason to have an entire Apache instance running when you already have another server acting as a reverse proxy. – Chris Pratt Feb 07 '12 at 05:48
  • 1
    You are brave to say that. Even the uWSGI authors themselves don't make that claim. They know that when Apache is setup properly for Python there isn't a great deal of difference. The difference is even less noticeable when you stick a fat Python web application on top. A saving in microseconds per request at the web server layer is nothing when you consider that most well tuned Python web applications run with responses in order of 10s of milliseconds. So the reason anything is going to be slow is not going to be due to the web server unless you really screwed up the configuration of it. – Graham Dumpleton Feb 07 '12 at 08:26
  • @ChrisPratt Do you think thats still true for latest versions of Django? I was just reading the [docs](https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/#serving-files) and it seems they recommend a separate server for static media. – Anupam Jun 11 '17 at 06:26
3

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/

nicowernli
  • 3,250
  • 22
  • 37
  • Hi nicowernli. Thanks for the link to the tutorial. I have a question about this author of this tutorial insist on compiling everything from source (e.g. DJango and NginX are all downloaded and compiled from source) rather than getting binary packages via the package manager? – Barry Steyn Feb 06 '12 at 23:13
  • Actually I prefer using pip. if you are using ubuntu/debian add the nginx repository to your apt sources list, install nginx from the repositories and then use pip to install virtualenv, uwsgi and Django. I wrote a tutorial for this but is in spanish – nicowernli Feb 07 '12 at 04:41
  • Ironic, I am in Spain right now (Pamplona). I know that you maybe South American, but on the off chance that you are Spanish and are based in Navara, the cerveza is on me :) Why do you like pip over aptitude? – Barry Steyn Feb 07 '12 at 14:29
  • Well yes, Im South American, from Argentina actually. Here is my blog entry [link](http://nwernli.com/blog/servidores/nginx-uwsgi-virtualenv-django.html). I like pip more because you will get always the latest stable version of the packages. – nicowernli Feb 07 '12 at 20:34
  • 9
    The link to senya.pl is not working, temporary or permanent? – michel.iamit Feb 22 '13 at 09:35
2

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)

Community
  • 1
  • 1
marue
  • 5,588
  • 7
  • 37
  • 65
  • Hi. Why can't NginX be also used for serving Python - I mean, if NginX is good for static files, surely it will also be good for Pthon files. – Barry Steyn Feb 06 '12 at 15:16
  • It can be used. Apache is just more widespread and common, so if you stumble upon some special demands it is more likely you find a mature solution. On the other side Chris Pratts answer has to be taken into account as well. – marue Feb 06 '12 at 21:24
1

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/

michel.iamit
  • 5,788
  • 9
  • 55
  • 74
0

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/

radtek
  • 34,210
  • 11
  • 144
  • 111
0
  1. The less, the better.
  2. The best way to deploy Django application over Nginx is use uwsgi. It's pure WSGI and built in supported by new version Nginx.
risent
  • 136
  • 1
  • 4