Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache or Nginx to serve Django applications? [closed]

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

like image 504
Barry Steyn Avatar asked Feb 06 '12 14:02

Barry Steyn


People also ask

Does Django use Apache or nginx?

Both nginx and Apache are excellent choices for a web server. Most people deploying Django nowadays seem to be using nginx, so, if you aren't interested in learning more about what you should choose, pick up nginx. Apache is also widely used, and it is preferable in some cases.

Which server is best for Django?

Gunicorn is the recommended HTTP server for use with Django on Heroku (as referenced in the Procfile above). It is a pure-Python HTTP server for WSGI applications that can run multiple Python concurrent processes within a single dyno (see Deploying Python applications with Gunicorn for more information).

Is Apache good for Django?

Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi. You'll probably want to start with the installation and configuration documentation.

Does Django use nginx?

It takes you through the steps required to set up Django so that it works nicely with uWSGI and nginx. It covers all three components, providing a complete stack of web application and server software. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.


2 Answers

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
like image 186
kalvatn Avatar answered Oct 07 '22 17:10

kalvatn


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)

like image 38
marue Avatar answered Oct 07 '22 15:10

marue