nginx + uwsgi socket 部署 Django 项目

版权声明:知识就是为了传播! https://blog.csdn.net/weixin_36171533/article/details/84768550

1. 运行开发服务器测试

python manage.py runserver

2. 安装 nginx

sudo yum install epel-release
sudo yum install python-devel nginx

2.2 安装 supervisor, 一个专门用来管理进程的工具,我们用它来管理 uwsgi 进程

这个工具pip我是没有安装成功,我是yum安装

pip install supervisor

或者

yum install supervisor

3. 使用 uwsgi 来部署

安装 uwsgi

pip install uwsgi --upgrade

测试运行:

uwsgi --http :8000 --chdir /web/dj.honvu --module demo1.wsgi

如果提示端口已经被占用:

probably another instance of uWSGI is running on the same address (:8002).
bind(): Address already in use [core/socket.c line 764]

这时可以把相关的进程 kill 掉:

按照端口进行查询:

lsof -i :8000
lsof -i :8000
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
uwsgi   25481 root    4u  IPv4 1351734      0t0  TCP *:irdmi (LISTEN)
uwsgi   25482 root    4u  IPv4 1351734      0t0  TCP *:irdmi (LISTEN)

4. 使用supervisor来管理进程

生成 supervisor 默认配置文件,比如我们放在 /etc/supervisord.conf 路径中:

echo_supervisord_conf > /etc/supervisord.conf

最后面添加

vim /etc/supervisord.conf 
[program:mysite]
command=uwsgi --http :8000 --chdir /web/dj.honvu/test/mysite --module mysite.wsgi
directory=/web/dj.honvu/test/mysite
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

启动 supervisor


(sudo) supervisord -c /etc/supervisord.conf
重启 zqxt 程序(项目):

(sudo) supervisorctl -c /etc/supervisord.conf restart demo
启动,停止,或重启 supervisor 管理的某个程序 或 所有程序:

(sudo) supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

5. 配置 Nginx

location /{
    proxy_pass http://127.0.0.1:3000/;
    }

故障排查:

Django运行访问项目出现的问题:DisallowedHost at / Invalid HTTP_HOST header

去django-admin.py startproject project-name创建的项目中去修改 setting.py 文件:
 

ALLOWED_HOSTS = ['*']  #在这里请求的host添加了*
supervisord.conf 配置文件:

备注:使用的uwsgi部署
先测试:uwsgi --http :8000 --chdir /web/dj.honvu/test/mysite --module mysite.wsgi
页面是否可以正常的访问
directory=/web/dj.honvu/test/mysite  你的项目目录
站点停止后是否重启

测试运行命令:

uwsgi --http :8000 --chdir /web/dj.honvu --module demo1.wsgi
项目目录结构:
/web/dj.honvu
[root@jesse dj.honvu]# pwd
/web/dj.honvu
[root@jesse dj.honvu]# tree
├── app01
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-36.pyc
│   │   ├── admin.cpython-37.pyc
│   │   ├── apps.cpython-36.pyc
│   │   ├── apps.cpython-37.pyc
│   │   ├── __init__.cpython-36.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-36.pyc
│   │   └── models.cpython-37.pyc
│   ├── tests.py
│   └── views.py
├── demo1
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-36.pyc
│   │   ├── urls.cpython-37.pyc
│   │   ├── wsgi.cpython-36.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── templates
vim /etc/supervisord.conf
[program:demo1]
command=uwsgi --http :8000 --chdir /web/dj.honvu --module demo1.wsgi
#directory=/web/dj.honvu/demo1
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
备注:
uwsgi --http :8000 --chdir /web/dj.honvu --module demo1.wsgi 在服务器上看看能不能跑起来
--http :8000 			端口
--chdir /web/dj.honvu  	网站的根目录
--module demo1.wsgi 	项目名+wsgi

启动:
supervisord -c /etc/supervisord.conf

调试:
supervisorctl -c /etc/supervisord.conf restart demo

猜你喜欢

转载自blog.csdn.net/weixin_36171533/article/details/84768550