Django+nginx+uwsgi Deployment Tutorial (centos7+ubuntu16.4)

Source: Internet
Author: User
Tags install openssl django server ssh server virtual environment git clone nginx reverse proxy

Project Deployment Tutorials

Online Demo

1.1. Introduction of the principle

Django

    • A python-based open-source web framework

Uwsgi

    • One is a Web server, can also be used as a middleware

Nginx

    • Common high-performance proxy servers

wsgi.py

    • A WSGI interface file that is carried by the Django project

Nginx

Nginx is an HTTP and reverse proxy server What is a reverse proxy server? Forward is the browser actively want to make a proxy server request, after the Proxy server processing and then to the target server reverse is regardless of the browser and do not agree, the request will be processed by the proxy server and then sent to the target server the difference is that must go through Nginx reverse proxy server, Here are a few benefits of using Nginx: security: Regardless of the request to go through the proxy server, so that the external program to avoid direct attacks on the Web server load balancing: According to the request situation and server load situation, the request is assigned to a different Web server, Ensure server performance improves the IO performance of your Web server: I don't understand that, in summary, it takes time for a request to be uploaded from a client to a Web server.
How long it will take to get this process blocked, and through the reverse proxy, you can accept the request in the reverse proxy, and then
To the Web server to ensure server performance, and some simple things (such as static files) can be directly handled by the reverse proxy, not through the Web server

Process

First, the client requests service resources, nginx as a direct external service interface, receive the client sent over the HTTP request, will unpack, analyze, if the static file request is based on the static file directory Nginx configuration, return the requested resource, if it is a dynamic request, nginx through the configuration file , pass the request to Uwsgi;uwsgi to process the received package, and forward a file or function that Wsgi,wsgi the Django project based on the request, and Django will give the return value to Wsgi,wsgi to package and forward the return value to Uwsgi, Uwsgi is received and forwarded to Nginx,nginx eventually returns the return value to the client (such as a browser). * Note: Transfer of information between different components involves conversion of data formats and protocols

Role

1. The first level of nginx is not required, UWSGI can complete the entire and browser interaction process; 2. Add security or other restrictions to the nginx, can achieve the role of the protection program; 3. Uwsgi itself is an intranet interface, open multiple work and processes may not be enough, and Nginx can proxy more than UWSGI complete uwsgi load balancing; 4. Django does not have a good ability to handle static files under Debug=false, and it is more efficient to use nginx for processing.

Deployment of CENTOS7

Take the new server as an example:

Yum-y Update

Yum Install gccyum-y install Zlib*yum install openssl-devel-y
1.2.SSH Installation
Yum Install Openssh-server-yservice sshd Restart#xshell is not connected, the SSH server does not allow password authentication. #服务端开启密码验证的方法: vim/etc/ssh/sshd_config passwordauthentication to Yes restart service sshd restart

11.2.mysql Installation
#1. Installing wget HTTP://DEV.MYSQL.COM/GET/MYSQL-COMMUNITY-RELEASE-EL7-5.NOARCH.RPMRPM-IVH Mysql-community-release-el7-5.noarch.rpmyum install mysql-community-server#2. Restart the Services service mysqld restart#3. Set Bind-ip    vim/etc/my.cnf    in [mysqld]:        add a row below        bind-address = 0.0.0.0#4. Log in to Mysqlmysql-u root#5. Set the external IP to access the #mysql Input command: #后面用navicat连接远程服务器mysql的用户名和密码GRANT all privileges on * * to ' root ' @ '% ' identified by ' 123456 ' With GRANT OPTION; FLUSH privileges; #6. Set the MySQL password to enter mysql:set password =password (' 123456 ');     #密码123456flush privileges;

Installation of 11.3.pip and python3.6
#安装pip

wget https://bootstrap.pypa.io/get-pip.py --no-check-certificatesudo python get-pip.py
#安装python3.61. Get wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgztar-xzvf python-3.6.2.tgz-c  /tmpcd  /tmp/ PYTHON-3.6.2/2. Install the Python3.6 to the/usr/local directory./configure--prefix=/usr/localmakemake altinstall3. Change/usr/bin/python link ln-s/usr/local/bin/python3.6/usr/bin/python3

11.4. Virtual Environment Installation
Yum Install python-setuptools python-develpip install virtualenvwrapper# edit. bashrc file vim ~/.bashrc# add in Export workon_ Home= $HOME/.virtualenvssource/usr/bin/virtualenvwrapper.sh#sudo Find/-name virtualenvwrapper.sh      See where your virtualenvwrapper.sh is # reload the. bashrc file source ~/.bashrc# The path saved by the virtual Environment CD ~/.virtualenvs/      (the created virtual environment will be saved in this directory, Previously set) #创建指定python版本的虚拟环境方法mkvirtualenv-P/usr/local/bin/python3.6 mxonline
Workon Mxshop
#进虚拟环境安装依赖包

首先 pip freeze > requirements.txt
Export the local virtual environment installation package and upload it to the server
Pip install-r requirements.txt# Install mysqlclient problem CentOS 7: yum install python-devel mariadb-devel-y Ubuntu: sudo apt-get install Libmysqlclient-dev then: pip Install mysqlclient

11.5.git Installation
Yum Install gitgit config--global user.name "Your name" git config--global user.email "[Email protected]" CD ~/&& Ssh-keygen-t rsa-c "Your Mailbox" #提示的信息, press Enter directly to the CD. SSH copy the code from the public key file (id_rsa.pub) to the github# to start the clone code git clone [email Protected]:d Erek-zhang123/mxonline.git

This is my project directory and the Virtual environment directory

    • Project directory:/home/gitpackage/mxonline
    • Virtual Environment directory:/root/.virtualenvs/mxshop

11.6. Pull the project static file
#在django的setting文件中, add the following line:    static_root = Os.path.join (Base_dir, "static/") #运行命令    python manage.py Collectstatic

Other places to set in Settings

DEBUG = Trueallowed_hosts = [' * ']   #自己设置可以访问的域名, ' * ' means that all can be accessed

11.7.uwsgi

(1) Installation

Install into a virtual environment

Workon Mxonlinepip Install Uwsgi

(2) Create a new Uwsgi.ini file in the project directory

Mxonine/uwsgi.ini

[Uwsgi]socket =127.0.0.1:8000chdir =/home/gitpackage/mxonlinemodule = Mxonline.wsgimaster = trueprocesses = 4vacuum = tr uevirtualenv =/root/.virtualenvs/mxshoplogto =/tmp/mylog.log

注:    chdir:     表示需要操作的目录,也就是项目的目录    module:    wsgi文件的路径    processes: 进程数    virtualenv:虚拟环境的目录
11.8.nginx

(1) Installation

Here are the installation methods:https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7

sudo yum install nginx# may use the command service Nginx restartservice nginx stopservice nginx Start

(2) configuration file

Create a new mxonline.conf under/ETC/NGINX/CONF.D

# The upstream component Nginx needs to connect Toupstream Django {# server unix:///path/to/your/mysite/mysite.sock; A file Socketserver 127.0.0.1:8000; # for a Web port socket (we'll use this first)}# configuration of the serverserver {# The Port your site'll be served on Listen      80;# The domain name it would serve Forserver_name 180.76.56.222; # Substitute your machine ' s IP address or FQDN CharSet     utf-8;# Max upload sizeclient_max_body_size 75M;   # adjust to taste# Django medialocation/media  {    alias/home/gitpackage/mxonline/media;  # pointing to the Django Media directory}location/static {    alias/home/gitpackage/mxonline/static; # pointing to the Django static directory}# Finally, send All Non-media requests to the Django Server.location/{    uwsgi_pass  Django;    Include     
}
}

after the configuration is ready

Nginx-t       #提示success说明没问题service nginx Restart

11.9.navicat data transfer

When everything is configured, upload the data from the local database to the server

(1) Connect to your server database

(2) New Project Database

(3) Transfer data

Once the data transfer is complete and the configuration is configured, you can start to access the

#创建超级用户

Python manage.py Createsuperuser

#把uswgi服务开启

Uwsgi--ini Uwsgi.ini #访问http://Your IP address/

Basic Environment setup for Ubuntu

sudo apt-get updateapt-get install gcc apt-get install libssl-devsudo apt-get install Opensslapt-get install Zlib1gapt-get Install Zlib1g.dev

Mysql

sudo apt-get install mysql-server enter password mysql-u root-psudo vim/etc/mysql/mysql.conf.d/mysqld.cnf bind-address = 0.0.0.0
    #添加进去sudo service MySQL restart# set remote access GRANT all privileges on * * to ' root ' @ '% ' identified by ' 123456 ' with GRANT optio N;flush privileges;

Pip and python3.6

wget https://bootstrap.pypa.io/get-pip.py  --no-check-certificatesudo python get-pip.py
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgztar-xzvf python-3.6.2.tgz-c  /tmpcd  /tmp/ PYTHON-3.6.2/2. Install the Python3.6 to the/usr/local directory./configure--prefix=/usr/localmakemake altinstall3. Change/usr/bin/python link ln-s/usr/local/bin/python3.6/usr/bin/python3

Virtual Environments

Pip Install Virtualenvpip install virtualenvwrappervim ~/.bashrcexport workon_home= $HOME/.virtualenvssource/usr/ Local/bin/virtualenvwrapper.shsource ~/.BASHRC
Mkvirtualenv mxonline--python=python3.6worko mxonlinesudo apt-get Install Libmysqlclient-devpip install-r Requirements.txt

Git

sudo apt-get updatesudo apt-get install gitgit config--global user.name "Your name" git config--global user.email "[Email Protected] "

Error when installing software

E:sub-process/usr/bin/dpkg returned an error code (1) Errors resolution occurs when installing software with apt-get similar to install-info:no dir file specified; Try--help for more information.dpkg: Error processing GetText (--configure): Child process post-installation Script returned error number 1 error occurred while processing: Findutil Se:sub-process/usr/bin/dpkg returned an error code (1) means the following: 1.$ sudo mv/var/lib/dpkg/info/var/lib/dpkg/info_old//Now INF  O folder rename 2.$ sudo mkdir/var/lib/dpkg/info//Create a new info folder 3.$ sudo apt-get update, then $sudoapt-get-f Install//Don't explain it 4.$ sudo mv/var/lib/dpkg/info/*/var/lib/dpkg/info_old//After performing the previous operation, some files are generated under the new Info folder, and now all these files are moved to the Info_old folder 5.$ sudo rm-rf/ Var/lib/dpkg/info//delete your new info folder 6.$ sudo mv/var/lib/dpkg/info_old/var/lib/dpkg/info//Change the previous Info folder back to the name to solve the problem

GitHub

CD ~/&& ssh-keygen-t rsa-c "Your Mailbox" #提示的信息, press Enter directly to CD. SSH copy the code from the public key file (id_rsa.pub) to github# to start the clone code git clone [email protected]:d Erek-zhang123/mxonline.git

Django+nginx+uwsgi Deployment Tutorial (centos7+ubuntu16.4)

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.