项目在本地已完成大框架开发,预想着能部署到公共lunix服务器上,
特意找了一台没有在使用的lunix服务器,集成部同事帮忙重新安装了较为新版的lunix,并安装了python3.7及django3:
Nginx安装可参考文章:Nginx 服务器安装及配置文件详解
安装服务器为lunix,常用开启、关闭、重启命令如下:
配置文件:/etc/nginx/nginx.conf
启动nginx: sudo nginx
测试配置: sudo nginx -t
重载nginx: sudo nginx -s reload
停止nginx:sudo nginx -s stop
cd到django项目根目录下,执行如下命令:
pip3 install gunicorn
如若安装错了路径,可执行卸载:
如下
[appsrv@localhost ~]$ pip3 uninstall gunicorn
Found existing installation: gunicorn 20.0.4
Uninstalling gunicorn-20.0.4:
Would remove:
/home/appsrv/.local/bin/gunicorn
/home/appsrv/.local/lib/python3.7/site-packages/gunicorn-20.0.4.dist-info/*
/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/*
Proceed (y/n)? ye^H
Your response ('ye\x08') was not one of the expected responses: y, n
Proceed (y/n)? y
Successfully uninstalled gunicorn-20.0.4
[appsrv@localhost ~]$
然后重新cd到项目根目录下,执行安装命令:pip3 install gunicorn
由于上一次安装gunicorn已自动下载了gunicorn 20.0.4- py2. py3- none any .whl文件,故此时安装不再重复下载,直接使用原本已下载的whl安装。
[appsrv@localhost ~]$ cd project/ManagerPlan
[appsrv@localhost ManagerPlan]$ pip3 install gunicorn
Defaulting to user installation because normal site-packages is not writeable
Collecting gunicorn
Using cached gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
Requirement already satisfied: setuptools>=3.0 in /usr/local/python3/lib/python3.7/site-packages (from gunicorn) (39.0.1)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.0.4
[appsrv@localhost ManagerPlan]$
配置nginx
修改配置文件:/etc/nginx/nginx.conf
如下:
重启nginx命令:
sudo service nginx reload
如果提示:
[appsrv@localhost ManagerPlan]$ sudo nginx -s reload
[sudo] appsrv 的密码:
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
[appsrv@localhost ManagerPlan]$ sudo nginx
[appsrv@localhost ManagerPlan]$
则说明nginx没有启动,重新启动即可。启动命令:sudo nginx
备注:如果你是root账号登录,重启还可以用这个命令:
sudo service nginx reload
如果非root账号,用此命令执行时会报错:
对不起,用户 appsrv 无权以 root 的身份在 localhost.localdomain 上执行 /sbin/service nginx reload。
1、gunicorn开启
在项目的根目录下,输入下方的代码。
gunicorn --bind unix:/tmp/域名.socket projectname.wsgi:application
例如我使用的:
gunicorn -b 192.168.101.240:8002 ManagerPlan.wsgi:application
2、查询
首先执行如下命令获取Gunicorn进程:
ps -ef|grep gunicorn
执行后如下:
[appsrv@localhost ~]$ ps -ef|grep gunicorn
appsrv 9781 1 0 4月21 ? 00:01:07 /usr/local/python3/bin/python3.7 /home/appsrv/.local/bin/gunicorn ManagerPlan.wsgi
appsrv 9803 9781 0 4月21 ? 00:00:07 /usr/local/python3/bin/python3.7 /home/appsrv/.local/bin/gunicorn ManagerPlan.wsgi
appsrv 10498 1 0 4月22 ? 00:00:58 /usr/local/python3/bin/python3.7 /home/appsrv/.local/bin/gunicorn -b 192.168.101.240:8002 ManagerPlan.wsgi:application
appsrv 15116 10498 0 16:46 ? 00:00:00 /usr/local/python3/bin/python3.7 /home/appsrv/.local/bin/gunicorn -b 192.168.101.240:8002 ManagerPlan.wsgi:application
appsrv 15255 15150 0 18:08 pts/1 00:00:00 grep --color=auto gunicorn
3、关闭
首先执行如下命令获取Gunicorn进程树:
ps -ef|grep gunicorn
之后输入如下指令关闭进程:
kill -9 pid
4、重启
输入如下指令重启进程:
kill -HUP pid
发现执行如下启动命令报错,8002端口被占用
gunicorn -b 192.168.101.240:8002 ManagerPlan.wsgi:application
查看是被哪个进程占用:
居然是nginx配置中的地址。那么此时把nginx停止运行(sudo nginx -s stop),再重新执行gunicorn启动命令.
现在报出其他错误,找不到ModuleNotFoundError: No module named ‘ManagerPlan.wsgi.plan’;
[appsrv@localhost ManagerPlan]$ gunicorn -b 192.168.101.240:8002 ManagerPlan.wsgi.plan
[2020-04-28 11:34:26 +0800] [16185] [INFO] Starting gunicorn 20.0.4
[2020-04-28 11:34:26 +0800] [16185] [INFO] Listening at: http://192.168.101.240:8002 (16185)
[2020-04-28 11:34:26 +0800] [16185] [INFO] Using worker: sync
[2020-04-28 11:34:26 +0800] [16188] [INFO] Booting worker with pid: 16188
[2020-04-28 03:34:26 +0000] [16188] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
self.load_wsgi()
File "/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
self.wsgi = self.app.wsgi()
File "/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
return self.load_wsgiapp()
File "/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
return util.import_app(self.app_uri)
File "/home/appsrv/.local/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
mod = importlib.import_module(module)
File "/usr/local/python3/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "" , line 1006, in _gcd_import
File "" , line 983, in _find_and_load
File "" , line 962, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'ManagerPlan.wsgi.plan'; 'ManagerPlan.wsgi' is not a package
[2020-04-28 03:34:26 +0000] [16188] [INFO] Worker exiting (pid: 16188)
[2020-04-28 11:34:26 +0800] [16185] [INFO] Shutting down: Master
[2020-04-28 11:34:26 +0800] [16185] [INFO] Reason: Worker failed to boot.
暂时没有解决ta。只是先要确定普通模式运行下django项目,看看它能否跑起来
cd到项目根目录下,运行命令:
python3 manage.py runserver 192.168.101.240:8002
[appsrv@localhost ManagerPlan]$ python3 manage.py runserver 192.168.101.240:8002
Performing system checks...
System check identified no issues (0 silenced).
April 28, 2020 - 03:44:07
Django version 2.1.1, using settings 'ManagerPlan.settings'
Starting development server at http://192.168.101.240:8002/
Quit the server with CONTROL-C.
在其他电脑(也即是远程)上访问:http://192.168.101.240:8002/index/
可正常访问到登录页面。故此项目是能正常运行的。目前紧要的就是解决找不到模块的问题。
原来是执行错命令了,用命令:
gunicorn -b 192.168.101.240:8002 ManagerPlan.wsgi:application
才是正确的。
gunicorn -b 192.168.101.240:8002 ManagerPlan.wsgi:application
wsgi:application 之间应该用:冒号,而不是.
总结:脱离了nginx,直接用gunicorn启动项目,运行。
考虑:把nginx用起来。(可参考:https://www.cnblogs.com/joest/p/setting_up_your_django_project_to_nginx.html)
https://blog.csdn.net/lanhaixuanvv/article/details/78258680?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1