Linux 部署django + uwsgi + nginx

因为我已经开发好了django项目,这里不做过多介绍怎么开发django项目。重点说明django部署中遇到的问题和解决方案。
1、安装 django

pip3 install Django

2、安装 Django REST FrameWork

 pip3 install djangorestframework

3、安装 uwsgi

pip3 install uwsgi

配置文件(我采用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

注意:配置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

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)

原因请对比以上两个配置文件。

nwsgi --stop myapp.ini   停止nwsgi
uwsgi --reload myapp.ini 重启nwsgi

安装 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 配置
找到nginx的安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,修改server配置:

 server {
        listen       80;
        server_name  localhost;

        location / {            
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:8891;    #必须和uwsgi中的设置一致;
        }
    }

启动Nginx

/usr/sbin/nginx  --启动
/usr/sbin/nginx -s stop --优雅停止
/usr/sbin/nginx -s reload --重启
pkill -9 nginx --强制停止

猜你喜欢

转载自blog.csdn.net/July_whj/article/details/81273149