Online production environment deployment Djngao + Nginx + Uwsgi

Have you ever wished to run django project migration from windows to Linux, Linux performance is well known as a server running Django's very appropriate friends, share today how online cloud environment to run Linux machine Django project.

客户端访问服务端的流程
1.首先客户端请求服务资源, 
2.nginx作为直接对外的服务接口,接收到客户端发送过来的http请求,会解包分析。
3.如果是静态文件请求就根据nginx配置的静态文件目录,返回请求的资源,否则会根据django配置文件设置的static目录去找资源。
4.如果是动态的请求,nginx就通过配置文件,将请求传递给uwsgi;
5.uwsgi 将接收到的包进行处理,并转发给wsgi, wsgi根据请求调用django工程的某个文件或函数,处理完后,django将返回值交给wsgi, wsgi将返回值进行打包,转发给uwsgi,
6.uwsgi接收后转发给nginx,nginx最终将返回值返回给客户端(如浏览器)。

Add that knowledge:
1.wsgi is a low level between web servers and web program interface
2.uwsgi is a WEB-based interface to uwsgi agreement, wsgi Http protocol and protocol, it simply is django web access to files through uwsgi

First, prepare the environment

  1. A Linux server
  2. Nginx package
  3. MySQL packages
  4. Django project

Second, the deployment environment
1. Linux install python, according to their own version of the project will need to install python

1.1安装相关依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

1.2 compile and install

cd /usr/local/
tar xf Python-3.5.1.tgz 
cd Python-3.5.1/
./configure --enable-optimizations
cd Python-3.5.1/
make && make install

1.3 modify Python file

mv /usr/bin/python /usr/bin/python2.7.5
ln -s /usr/local/bin/python3.5 /usr/bin/python
python -V   #这里输出是你编译版本就OK啦
Python 3.5.1

1.4 modify yum configuration files, because the amendment to the python file, yum can not be used

vim /usr/bin/yum 
#!/usr/bin/python2.7.5   修改为之前移动的python文件

There are some error because the installation can be modified according to the above Yum

2. Install Modules

2.1 安装uwsgi模块
pip3 install uwsgi
2.2 安装pymysql模块
pip3 install pymysql
2.3 安装django
pip3 install django==2.0

3. Modify the configuration file django
3.1 init file in the project by writing the code, if you do not fill in Kazakhstan will not connect to the database

import pymysql
pymysql.install_as_MySQLdb()

3.2 setting.py modify configuration files

DEBUG = False    #这里需要改为False,不然程序报错,会在WEB页面显示出来
ALLOWED_HOSTS = ['IP']      #这里设置你允许哪些IP访问
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  #这是指连接数据库的驱动
        'NAME': 'xxxxx',                      #你的项目数据库名
        'USER': 'xxxxx',                       #连接数据库用户
        'PASSWORD': xxxxxxxxx',      #数据库密码
        'HOST': "xxxxxxxx",                 #主机
        'PORT': '3306',                         #端口
    }
}
TIME_ZONE = 'Asia/Shanghai'               #修改为中国区域
USE_TZ = False                                      #关闭
STATIC_ROOT= "/data/autoops/static"    #设置为你的静态目录

4. compile and install niginx
5. binary install MySQL
6. Upload django project code to the Linux server, placed in the specified directory according to your needs
Online production environment deployment Djngao + Nginx + Uwsgi
7. Writing uwsgi file, my file in /data/autoops/script/uwsgi.ini (emphasis )

[uwsgi]
# 项目目录
chdir=/data/autoops/
# 指定项目的application
module=autoops.wsgi:application
# 进程个数
workers=8
pidfile=/data/autoops/script/uwsgi.pid
# 指定IP端口
#http=192.168.10.155:8003    ##可以不启用,利用socket文件,如果只是测试,需要启用
# 指定静态文件目录
static-map=/static=/data/autoops/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/data/autoops/script/uwsgi.log
# 指定sock的文件路径
socket=/data/autoops/script/uwsgi.sock
die-on-term=true 

8.uwsgi command

uwsgi --reload uwsgi.pid  重启
uwsgi --stop uwsgi.pid 关闭
uwsgi --ini uwsgi.ini 启动

9. Before you start, you need to pay attention, if you are setting the DEBUG is off, you need to execute python manage.py collectstatic in django project, the role of Django project is to collect all the static resources involved, the statistics put STATIC_ROOT management visit, if you do not perform, you will find, django project can not be loaded CSS styles, JS script.
As shown below
Online production environment deployment Djngao + Nginx + Uwsgi

10. Initialization ORM django projects

   1. python manage.py makemigrations 把models.py里面的改动记录下来,记录到migrations文件夹下面
   2. python manage.py migrate   把改动翻译成SQL语句去数据库执行

11. Nginx configuration file and check the syntax start

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       你的端口号;
        server_name  公网IP;
        location / {
                include uwsgi_params;             
                uwsgi_connect_timeout 30;          
                uwsgi_pass unix:/data/autoops/script/uwsgi.sock;    #你的uwsgi.sock文件位置
        }
    }
}

12. Start uwsgi and nginx

[root@xxxxxxxxx08:56:29/data/autoops]# uwsgi --ini script/uwsgi.ini 
[uWSGI] getting INI configuration from script/uwsgi.ini
[uwsgi-static] added mapping for /static => /data/autoops/static

Third, test access nginx
Online production environment deployment Djngao + Nginx + Uwsgi

Summary: deployment difficult, need to take note, the article detailed some places did not say that he can think Ha, next time share

Guess you like

Origin blog.51cto.com/beckoning/2422101
Recommended