Nginx + Gunicorn + Django deployment note

Source: Internet
Author: User
Tags install django pip install django

During this time, I used Django to develop a forum and planned to become a Cocoa developer community. I have been learning how to deploy these days. Just a few days ago, UCloud sent three months of hosts and can be used as a trainer. VPS uses 64-bit Ubuntu 12.04. After two days, it passes through and is recorded in a hurry...

Apt-get
The first thing the server needs to get is to update apt-get. You need to search for a large number of sources. Previously, Ubuntu 10.04 was accidentally selected because of the system, and encountered a lot of problems. Later, everything went smoothly to 12.04.

Apt-get update
Pip
Pip is recommended to change the source. The speed is just like flying. For more information, see the article "Pythoner benefits, PyPI source of Douban".

Sudo apt-get install python-pip
Django
Django must be installed with pip. I usually use the latest version.

Sudo pip install django
Nginx
It is a good habit to configure the default Nginx. The directory is located in/etc/nginx/sites-available/default.

Cp/etc/nginx/sites-available/default. bak
Edit Vim

The code is as follows: Copy code

Vim/etc/nginx/sites-available/default
Server {
Listen 80;

Server_name www.111cn.net;
Access_log/var/log/nginx/isaced. log;

Location /{
Proxy_pass http: // 127.0.0.1: 8000;
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
        }

Location/static /{
Root/data/isaced; # directory of the Django project
        }

The above is the simplest Nginx configuration. The purpose is to be able to run it. For more detailed configurations, see other articles.

Gunicorn
Gunicorn is recommended by a friend (CloverStd), an open-source Python wsgi unix http server. The Github repository address is here. It is said that it is fast (fast configuration and fast running) and simple, synchronization is performed by default. Gevent and Eventlet asynchronization are supported, and Tornado is supported. For more information, see the official documentation.

You need to add: gunicorn to INSTALLED_APPS in settings. py of your Django project.

Gunicorn -- worker-class = gevent isaced. wsgi: application
• -- Worker-class
Specify the working method. Here, gevent is used.
If You are prompted to use this worker, You have not installed gevent.
• Isaced. wsgi: application
This refers to your project name. When Django creates a project, it will automatically generate wsgi. py in the corresponding name folder. This refers to it.
Nohup
Nohup is a Linux command used to run a command without hanging up. Here we use it to execute gunicorn to ensure that the gunicorn process will not be hung up.

Nohup gunicorn -- worker-class = gevent NSLoger. wsgi: application-B 127.0.0.1: 8000 &
-- Worker-class to specify the working mode as gevent, and-B to specify the address and port number.

Note: adding the & (and) character at the end indicates running in the background

After executing this command, you can use the ps command to view the process and then you can see the gunicorn ~

Start
Okay, the above is the configuration of the entire server. Restart Nginx and refresh the page to view your Django App.

Sudo service nginx restart
The deployment Diary of a white paper is here. If you have any mistakes, please give me some comments!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.