在上一节,我们已经为微信公众号搭建了基本的开发环境。
在使用python3 manage.py runserver后,已经可以从浏览器中去访问现有的内容。
为了结合生产环境,我们现在做一件事情,就是把为项目架设uwsgi+nginx服务。
在此记录下我的架设过程。
1.测试项目已有内容是否可以顺利运行
python3 manage.py runserver
2.在项目可以顺利运行的情况下,为系统安装python-dev nginx supervisor(当前系统为ubuntu)
sudo apt-get install python-dev nginx
pip3 install supervisor
其中supervisor是用来管理nginx进程的一个工具
3.安装uwsgi
pip3 install uwsgi --upgrade
4.使用uwsgi命令映射端口
尽量使用一个未被占用的端口来启动uwsgi服务,如果端口被占用,可以用kill -9 pid 杀掉进程。
uwsgi --http :8005 --chdir /path/to/project --module project.wsgi (注意–http后有个空格)
使用lsof -i 命令确认uwsgi测试服务成功启动.
或是使用程序名来查询uwsgi进程
5.使用刚刚安装好的进程管理器为uwsgi创建一个配置文件
并在配置文件supervisord.conf中的最后一行添加如下配置信息.
[program:zqxt]
command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi
directory=/path/to/zqxt
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
启动 supervisor
(sudo) supervisord -c /etc/supervisord.conf
重启程序(项目):
(sudo) supervisorctl -c /etc/supervisord.conf restart WX_CSZWY
启动,停止,或重启 supervisor 管理的某个程序 或 所有程序:
(sudo) supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
如果配置文件出错,修改后,可以用
supervisorctl update
来更新配置文件信息。
6.nginx配置django相关信息。
在项目中创建nginx.ini文件,并填入相关配置信息.
7.修改 supervisor 配置文件中的 command 一行:
然后重启一下 supervisor:
(sudo) supervisorctl -c /etc/supervisord.conf restart WX_CSZWY
8.配置nginx的80端口映射。
可以使用 service nginx configtest来检查配置语法是否有错误。
nginx 配置完成后,从浏览器中就可以访问自己的django项目了.
9.在访问django项目时,发现admin中的静态文件都无法访问。
现在要收集django的静态文件放入/static中
参考
https://blog.csdn.net/luojie140/article/details/76995501
现在所有django项目终于可以通过nginx正常访问了.