Linux deployment django + uwsgi + nginx

Because I have developed django project, I will not introduce how to develop django project here. Focus on the problems and solutions encountered in django deployment.
1. Install django

pip3 install Django

2. Install Django REST FrameWork

 pip3 install djangorestframework

3. Install uwsgi

pip3 install uwsgi

Profile (I use ini):

[uwsgi]
socket = 127.0.0.1:8891
master = true
vacuum = true
chdir = /root/django/accounts/
wsgi-file = accounts/wsgi.py
processes = 2
threads = 2

vacuum=true
socket=/root/uwsgi/uwsgi.sock
stats=/root/uwsgi/uwsgi.status
pidfile=/root/uwsgi/uwsgi.pid
daemonize=/root/uwsgi/uwsgi.log

Note: when configuring the ini file, start using the following configuration file:

[uwsgi]
socket = 127.0.0.1:8891
master = true
vacuum = true
chdir = /root/django/accounts
wsgi-file = /accounts/wsgi.py
processes = 2
threads = 2

vacuum=true
socket=/root/uwsgi/uwsgi.sock
stats=/root/uwsgi/uwsgi.status
pidfile=/root/uwsgi/uwsgi.pid
daemonize=/root/uwsgi/uwsgi.log

The following error occurred while uwsgi – ini myapp.ini

mapped 416880 bytes (407 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
failed to open python file xxx/uwsgi.ini
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 4865)
spawned uWSGI worker 1 (pid: 4866, cores: 2)
spawned uWSGI worker 2 (pid: 4867, cores: 2)

Please compare the above two profiles for reasons.

nwsgi --stop myapp.ini stop nwsgi
 uwsgi --reload myapp.ini restart nwsgi

Install Nginx

wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && make install

Nginx configuration
Find the installation directory of nginx (for example, / usr/local/nginx /), open the conf/nginx.conf file, and modify the server configuration:

 server {
        listen       80;
        server_name  localhost;

        location / {            
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:8891;    #Must be consistent with the settings in uwsgi;
        }
    }

Start Nginx

/usr/sbin/nginx -- start
 /usr/sbin/nginx -s stop -- elegant stop
 /usr/sbin/nginx -s reload -- restart
 pkill -9 nginx -- force stop

Tags: Nginx Django socket REST

Posted on Mon, 27 Jan 2020 07:35:35 -0800 by jeffery