37

I want to deploy a django site (it is the open source edx code on github).

I am faced with choosing between using

  1. Apache with mod_wsgi
  2. 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.

Mat
  • 202,337
  • 40
  • 393
  • 406
SamAko
  • 3,485
  • 7
  • 41
  • 77
  • 3
    Apache with gunicorn. Your webserver should be a pure-and-simple HTTP proxy, and shouldn't know anything about gunicorn - all apache has is the port number(s) to proxy to. Your app servers shouldn't know about one another at all, or that they're living behing an HTTP reverse proxy. Apache over nginx is purely because mod_proxy_http has docs, and nginx... well, frankly, the docs are shitty where they exist. Any docs you *do* find are probably out of date. (I really don't get the popularity.) – AdamKG Aug 04 '13 at 23:01
  • Oh, but, this is a judgement call, I'd just close the question, you won't get a substantive answer without triggering a flame war, at which point, the question will get closed anyway. – AdamKG Aug 04 '13 at 23:02
  • 1
    You might find this useful https://www.digitalocean.com/community/tutorials/django-server-comparison-the-development-server-mod_wsgi-uwsgi-and-gunicorn – Paolo Jan 05 '15 at 21:49

4 Answers4

32

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

Community
  • 1
  • 1
Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99
  • 5
    Apache supports several multi processing models. As of Apache 2.4 the event mpm module is the default on most unix like systems. See: https://httpd.apache.org/docs/2.4/mpm.html – Laurence Rowe May 06 '19 at 18:25
  • 4
    Not to mention Apache + mod_wsgi is unreasonably difficult to set-up with django. And if you want to use docker as well? Forget about it. Headache for days. – NoName Oct 28 '20 at 19:00
9

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.

Xiiryo
  • 3,021
  • 5
  • 31
  • 48
2

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.

Jdruiter
  • 335
  • 2
  • 10
2

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kaustubh Desai
  • 396
  • 3
  • 13
  • I know this is outdated, but working with Python 2.7 in July 2020 wasn't a smart move... Python 2 was deprecated Jan. 1st 2020 – Hussam Mar 04 '21 at 14:39