Deploying django in a production server

15,528

Solution 1

  1. A typical configuration involves two server processes (which can be run together on the same actual hardware or virtual server) so that the proxy server in front can buffer slow clients. For instance: a slow client will connect to nginx with a request. Nginx will pass the request on to Gunicorn and Gunicorn will respond. Nginx will then consume the Gunicorn response immediately, freeing up the Gunicorn resources right away. At that point, the slow client can take as much time as it wants to consume the response from Nginx without tying up much in the way of server resources. Alternatives to the two-server-process model are to use async workers with Gunicorn and put Gunicorn itself in front, or to use an async-sync combo like Waitress. Nginx in front has the added benefit of doubling as a ready-to-use statics server, though.

    Note that "slow clients" can describe: mobile phones that lose their connection and leave the TCP socket hanging until timeout mid-request; mobile phones that are just slow; unreliable connections of all types; hostile denial-of-service clients who are deliberately trying to use server resources; sometimes any old connection that has a hiccup or malfunction for any reason. So this is a problem that will affect nearly any site.

  2. You won't need shell scripting per se but getting used to Ubuntu will take some time. There is a lot to learn even outside of scripting, like how to use the package manager, how to configure packages once they're installed in ways that won't confound future updates, etc. And you will definitely have to learn to use SSH; it is one of the most fundamental server administration tools in the *nix world.

    An alternative to learning to use Ubuntu or another server platform is to use a Platform-as-a-Service option like Heroku, as PaaS hosting providers really will take care of all of that stuff for you. I recommend this approach. That having been said, even though I think PaaS is a good option for people who want to focus on development and not server admin regardless of their level of skill, it's also true that a little bit of experience with Linux server platforms goes a long way in helping you to understand the environment that your code runs in. So even if you go with PaaS, you would still benefit from tinkering with Ubuntu a little (or a lot).

    Another benefit from a PaaS is that normally their infrastructure handles the Nginx part of the deal (buffering of slow requests via proxy). This is the case with Heroku, for instance. So you won't have to worry about that part of the infrastructure at all.

  3. This part of the question is too broad to answer, but let me know in the comments if you need clarification.

Solution 2

I'm doing it almoast like in this tutorial: http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
Nginx is my proxy to django app running on gunicorn and its serving statics, virtualenv for my python enviroment, supervisor to watch my app's running.
It's possible you will run in some error's if not using Postgresql, ask then I will help (used MySQL in the past now it's Postgresql)

Solution 3

Firstly, there's no need to use Ubuntu if you're happier with Windows. I don't know if nginx works on Windows, but I'd be very surprised if it doesn't (in fact, here are the nginx docs for installing on Windows). Apache, meanwhile, definitely does work on Windows. The Django documentation has a full explanation of how to set up Apache/mod_wsgi to serve Django.

You don't need two servers. I'm not sure why you think you do: the usual reason for that is to have the static assets on a separate server, but you don't mention that as a reason. Since you're only talking about a small site, though, you don't even need to do that. One server configured to serve both Django and the static assets will do fine. Again, the docs explain exactly how to do that.

Share:
15,528

Related videos on Youtube

Benjamin Smith Max
Author by

Benjamin Smith Max

Updated on June 04, 2022

Comments

  • Benjamin Smith Max
    Benjamin Smith Max almost 2 years

    First of all please let me be clear that I am a windows user and very new to the web world. For the past months I have been learning both python and django, and it has been a great experience for me. Now I have somehow created a small project that I would like to deploy in the production server. Since django has its built-in development server there was no problem for me. But now that I have to deploy it to a production server I googled around and found Nginx + uWSGI or Nginx + Gunicorn as the best option for it. And as uWSGI and Gunicord are incompatible with Windows, I think I should adapt Ubuntu or other Unix system.

    So my questions are:

    1. Just to be clear, as I will have to work with one of the above, please explain to me why do I need two servers?
    2. If I have to adapt the Ubuntu environment, do I have to learn Ubuntu shell scripting, SSH and other stuff? Or the hosting provider will help me do that?
    3. Please let me be aware of what else do I need for the above concerned.

    Thank you so much for your time and please pardon if my question was a lame question. Hoping for positive response answers.

  • Benjamin Smith Max
    Benjamin Smith Max over 10 years
    Thank you for the detailed explanation. Was really very confused with those two. Now I guess I will take a vps and use putty for ssh. Just one more thing, How do I make changes to the file that I have deployed? I have heard about fabric, but it was really hard to grasp the logic behind it. Could you please explain about how do so? Thanks again.
  • Benjamin Smith Max
    Benjamin Smith Max over 10 years
    Thank you for the answer. As I googled around I found that uWSGI/Gunicorn with nginx were the best practices. So, I thought about implementing those two and asked the question here.
  • Andrew Gorcester
    Andrew Gorcester over 10 years
    @Benjamin Ultimately you just need to get your updated program files on the server and restart the web server process (or otherwise have it reload your code). fabric is a tool to make this easier, so if it's more complex than you need, don't use it. scp or sftp through putty is fine. Any other method like using git to download directly from your source code repository from the server is also fair game.