Django + uWSGI + Nginx project deployment

Background: today carried out a project to deploy (N + 1 times the first deployment of the project), so I decided to put together a complete material, directly next CTRL + C , CTRL + V on OK.

A, Django project configuration

Create a project is not to say, and personal recommendations to create a virtual environment.

Change the file setting.py

DEBUG = False
ALLOW_HOSTS = ['*'] #表示可以访问服务器的ip,*表示所有

Two, uWSGI configuration

  1. Installation uWSGI

    pip install uwsgi
    
  2. Under the new project directory uwsgi.inito fill in the configuration file

    [uwsgi]
    socket = 127.0.0.1:8080                           # 和nginx通信的端口,直接做web服务器时把socket换成http
    chdir = /home/nginx_test                          # 项目路径(我的测试项目叫test)
    wsgi-file = /home/nginx_test/nginx_test/wsgi.py   # wsgi.py文件的路径
    processes = 4
    threads = 2
    master = true
    pidfile = uwsgi.pid                               # pid文件,用于脚本启动、停止进程
    daemonize = uswgi.log                             # 日志文件
    
  3. Start uWSGI

    Before starting Note:

    1. Before starting the best to use python manage.py runserverto start the test to make sure that there is no problem
    2. uwsgi log will uwsgi.inigenerate in the relative path, if there is an error can be viewed in this log (uwsgi.log) in
    启动:uwsgi --ini uwsgi.ini
    停止:uwsgi --stop uwsgi.pid
    重启:uwsgi --reload uwsgi.pid
    

    It started successfully expressed as follows:

    (venv) [root@localhost nginx_test]# uwsgi --ini uwsgi.ini 
    [uWSGI] getting INI configuration from uwsgi.ini
    (venv) [root@localhost nginx_test]# 
    

Three, Nginx configuration

  1. Install Nginx

    I downloaded the nginx-1.9.9, others please visit: http: //nginx.org/download/

    cd /home/                                          # 找一个目录下载 Nginx
    yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel    # 安装依赖包
    wget http://nginx.org/download/nginx-1.9.9.tar.gz  # 下载源码包
    tar -zxf nginx-1.9.9.tar.gz                        # 解压源码包
    cd nginx-1.9.9/                                    # 进入目录
    ./configure --prefix=/usr/local/nginx              # 安装路径为/usr/local/nginx
    make && make install                               # 编译安装
    
  2. Start Nginx

    /usr/local/nginx/sbin/nginx            # 启动
    /usr/local/nginx/sbin/nginx -s stop    # 停止
    /usr/local/nginx/sbin/nginx -s reload  # 重启
    

    There is no prompt instructions to start successfully
    after starting the input virtual machine or server on the browser IP test, following figure illustrates Nginx start successfully:

    If you can not access may be a firewall problem:
    turn off the firewall: systemctl stop firewalld.service
    disable the firewall boot from the start: systemctl disable firewalld.service

    Here Insert Picture Description

  3. Configuration nginx.conf
    open the /usr/local/nginx/conf/nginx.conffile, follow these kinds modify or add

    user root; #在文件的开头加上
    
    http {
    	......
    	
    	server {
    	    listen 80;  #需要监控的端口
    	    server_name  localhost;
    	    
    	    location / {
    	        include uwsgi_params;
    	        uwsgi_pass 127.0.0.1:8080; #与uwsgi中ip:端口相同
    	        uwsgi_send_timeout 600;
    	    }
    		#静态文件
    		location /static { 
    	        alias /home/nginx_test/static/; #静态文件路径
    	    }
    	}
    	......
    }
    

    Like modified (too much text, did not cut the whole, the red box to modify the contents of):
    Here Insert Picture Description

  4. Restart Nginx

    /usr/local/nginx/sbin/nginx            # 启动
    /usr/local/nginx/sbin/nginx -s stop    # 停止
    /usr/local/nginx/sbin/nginx -s reload  # 重启
    
  5. test

    80-port it can be omittedHere Insert Picture Description

    urls.py:

    Here Insert Picture Description
    views.pyHere Insert Picture Description

Fourth, how to troubleshoot configuration after error

  1. ip is correct
  2. The correct port (Nginx configuration file settings: the listen 80 )
  3. Nginx and uwsgi connections are correct (whether ip port nginx.conf and uwsgi.ini connection agreement)
  4. Whether the project can be started (use python manage.py runservertest)
Welcome attention of the same name micro-channel public number: Program ape Miscellany

Program ape Miscellany

Technology | exchange | welfare
Published 63 original articles · 87 won praise · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_44110998/article/details/104001529