Python about deploying a Django project in Ubuntu

Source: Internet
Author: User

There are many ways in which Django can be deployed, and the way to adopt NGINX+UWSGI is one of the more common ones.

In this way, it is common practice to use Nginx as the server's front-end, which will receive all requests from the web and manage requests uniformly. Nginx handles all static requests themselves (this is Nginx's strength). Nginx then passes all non-static requests through Uwsgi to Django, which is processed by Django to complete a Web request.

As can be seen, the UWSGI function is similar to a bridge. Play a role as a bridge.

The strength of Linux is to do the server, so the entire deployment process below we choose to complete under Ubuntu.

First, install Nginx

Nginx is a lightweight Web server/reverse proxy server and e-mail (IMAP/POP3) proxy server that is released under a bsd-like protocol. It is characterized by the possession of less memory, concurrency is strong, in fact, nginx concurrency is actually in the same type of Web server performance better.

Nginx is also currently a very popular Web server. Using it to deploy Django, we also do a brief introduction here.

sudo  Install nginx  # installation Nginx

Start Nginx:

[Email protected]:~$/etc/init.d/nginx start [email protected]:~$/etc/init.d/nginx stop[email protected] :

Configure Nginx

Nginx will read /etc/nginx/sites-enabled/default the configuration in the file by default:

server {        8080  default_server;        Listen [::]:8080  default_server;        ...........

Enter your Ubuntu IP to access, normal is 127.0.0.1:80

If it does, the installation of Nginx has been successfully configured.

Second, installation Uwsgi
Install Uwsgi

To test Uwsgi, create a test.py file:

DEF application (env, start_response):    start_response ('ok', [( ' Content-type ','text/html')]    return [b]Hello World "]

Run the file through Uwsgi

[Email protected]:~$ uwsgi--http:8001 --wsgi-file test.py

Next, configure Django to connect to the USWGI. My Django Project location is:

UWSGI support is started by configuration file, can accept more parameters, highly customizable. We created a new INI file under the same name as the Django project Default_app_uwsgi.ini

# django-related Settingssocket= :8001# The base directory (full path) chdir=/usr/app/dataanalysis# Django s WsgifileModule= /dataanalysis.wsgi# Process-related settings# mastermaster=true# Maximum number of worker processesprocesses=4# ... with appropriate permissions-May be needed#chmod-socket =664# ClearEnvironment on Exitvacuum=true

Where chdir is the root of the Django project, the module is the relative path to the wsgi.py file under the directory with the same name as the project

Next, switch to the Django project directory and read the Default_app_uwsgi.ini file from the Uwsgi command to start the project.

[Email protected]:/usr/app/dataanalysis/dataanalysis$ Uwsgi--INI default_app_uwsgi.ini[uwsgi] Getting ini configuration from Default_app_uwsgi.iniStarting Uwsgi2.0. -(64bit) on [Tue June5  -: the: - 2018] ***compiled with version:5.4.0 20160609On -May2018  -: $: AOs:linux-4.10.0- --generic # +~16.04.2-ubuntu SMP Thu Jul - Ten: +: -Utc .nodename:WUZLX004machine:x86_64clock source:unixdetected Number of CPU cores:8Current working directory:/usr/app/dataanalysis/dataanalysisdetected binary path:/home/wuzlxadmin/.local/bin/Uwsgi!!! No internal routing support, rebuild and PCRE support!!!chdir () to/usr/app/dataanalysisyour Processes number Limit is15706your memory page size is4096bytesdetected MaxfileDescriptor Number:1024x768lock Engine:pthread Robust Mutexesthunder lock:disabled (you can enable it with--thunder-lock) UWSGI socket0Bound to TCP address:8001Fd3Python Version:3.5.2(Default, Nov at  ., -:Panax Notoginseng: on) [GCC5.4.0 20160609]Python threads support is disabled. You can enable it with--enable-threads * * *Python main interpreter initialized at0x1d6b0d0Your server socket Listen backlog is limited to -Connectionsyour Mercy forGraceful operations on workers are -secondsmapped364520Bytes (355KB) for 4coresOperational mode:preforking * * *Importerror:no module named'/dataanalysis'Unable to load app0(mountpoint="') (callable not found or import error)No app loaded. GoinginchFull dynamic mode ****** UWSGI is runninginchMultiple Interpreter mode * * *spawned UWSGI master process (PID:29756) spawned UWSGI worker1(PID:29757, Cores:1) spawned UWSGI worker2(PID:29758, Cores:1) spawned UWSGI worker3(PID:29759, Cores:1) spawned UWSGI worker4(PID:29760, Cores:1)

The next thing to do is to modify the nginx.conf configuration file. Open the/etc/nginx/nginx.conf file and add the following.

Server {Listen8081; server_name10.202.143.240CharSet UTF-8; Access_log/var/log/nginx/Dataanalysis_access.log; Error_log/var/log/nginx/Dataanalysis_error.log;    Client_max_body_size 75M; Location/{include uwsgi_params; Uwsgi_pass10.202.143.240:8001; Uwsgi_read_timeout2; } Location/static {expires 30d;         AutoIndex on; Add_header Cache-Control Private; Alias/usr/app/dataanalysis/static/; } }

Listen Specifies the port number of the Nginx proxy Uwsgi external.

Uwsgi_pass 10.202.143.240:8001; The port number of the native IP that refers to must be the same as the INI file configuration.

Restart Nginx.

Ps:

When you manipulate the/etc/nginx/nginx.conf file, remove the comment from the following sentence.

;

Also note the location of the newly added server:

http{    server{}      }

Test if the Nginx configuration is OK

Nginx-t-c/etc/nginx/nginx.conf

Python about deploying a Django project in Ubuntu

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.