在 centos 7 上使用 gunicorn 和 django 的 nginx

Posted

技术标签:

【中文标题】在 centos 7 上使用 gunicorn 和 django 的 nginx【英文标题】:nginx with gunicorn and django on centos 7 【发布时间】:2020-12-29 03:19:03 【问题描述】:

我正在尝试让我的 nginx 和 gunicorn 开始工作...但似乎尝试了我能做的所有事情,但失败了...

如果我这样做:

systemctl restart nginx
systemctl status nginx

它显示为绿色,并且可以正常工作... 如果我这样做:

systemctl start gunicorn.socket
systemctl status gunicorn.socket -l

它显示绿色并且工作正常... 但如果我这样做:

systemctl start gunicorn.service
systemctl status gunicorn.service -l

它向我显示以下消息:

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2020-09-10 14:17:23 UTC; 15min ago
  Process: 22145 ExecStart=/home/scorg/pro/sc_project/bin/gunicorn --workers 3 --bind unix:/home/scorg/pro/projects/sc/sc.sock sc.wsgi:application (code=exited, status=3)
 Main PID: 22145 (code=exited, status=3)

Sep 10 14:17:23 gunicorn[22145]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
Sep 10 14:17:23  gunicorn[22145]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
Sep 10 14:17:23  gunicorn[22145]: File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
Sep 10 14:17:23 : ModuleNotFoundError: No module named 'sc'
Sep 10 14:17:23 : [2020-09-10 14:17:23 +0000] [22152] [INFO] Worker exiting (pid: 22152)
Sep 10 14:17:23 : [2020-09-10 14:17:23 +0000] [22145] [INFO] Shutting down: Master
Sep 10 14:17:23 : [2020-09-10 14:17:23 +0000] [22145] [INFO] Reason: Worker failed to boot.
Sep 10 14:17:23 : gunicorn.service: main process exited, code=exited, status=3/NOTIMPLEMENTED
Sep 10 14:17:23 : Unit gunicorn.service entered failed state.
Sep 10 14:17:23 : gunicorn.service failed.

我有点理解这是绑定问题,我关注了这个问题: Gunicorn, no module named 'myproject

但是无论我尝试使用以下命令绑定什么模块,它都不起作用:

gunicorn --bind 0.0.0.0:8000 wsgi:application

我试过 wsgi、sc.wsgi、sc/wsgi、/whole_path/wsgi 总是一样的结果... mofule not found...

我的 gunicorn.service 看起来像这样:

#!/bin/sh

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
#scorg
Group=root
#www-data
Environment=SECRET_KEY=secret
WorkingDirectory=/home/scorg/pro/projects/sc/sc
ExecStart=/home/scorg/pro/sc_project/bin/gunicorn --workers 3 --bind unix:/home/scorg/pro/projects/sc/sc.sock  sc.wsgi:application

[Install]
WantedBy=multi-user.target

结构如下:

sc
├── manage.py
├── sc
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── sc_site
    ├── admin.py
    ├── apps.py
    ├── forms.py
    ├── models.py
    ├── __pycache__
    ├── static
    │   ├── admin
    │   ├── css
    │             
    ├── templates
    ├── urls.py
    └── views.py

当时我正处于痛苦的世界中。我在 Apache 上苦苦挣扎……我是 centos 7/linux 服务器管理的菜鸟……显然我被卡住了……如果我忘记提供一些额外的信息……请告诉我,我会很乐意做一个更新...

我还确保我设置了 gunicorn...所以它在系统上并且可以正常工作...:

yum install python-gunicorn

更新 毕竟与 gunicorn 的选项作斗争,它使用的是 python 2.7。我有python 3.8.5 ...我尝试遵循本教程How to get Gunicorn to use Python 3 instead of Python 2 (502 Bad Gateway),但到目前为止我无法完全弄清楚该怎么做...我做了所有事情,它仍在使用python2.7 ...有趣的事情:

我试图删除 gunicorn... pip uninstall guncorn 系统告诉我我没有gunicorn,和pip3一样…… 我用 pip3 install gunicorn 安装了 gunicorn,但它仍在尝试使用 python2.7:

[[/home/scorg/pro]]# sc_project/bin/gunicorn_start
Starting scorg_app as root
DJANGO_SETTINGS_MODULE
PYTHONPATH
2020-09-14 11:15:43 [4865] [INFO] Starting gunicorn 18.0
2020-09-14 11:15:43 [4865] [DEBUG] Arbiter booted
2020-09-14 11:15:43 [4865] [INFO] Listening at: unix:/home/scorg/pro/run/gunicorn.sock (4865)
2020-09-14 11:15:43 [4865] [INFO] Using worker: sync
2020-09-14 11:15:43 [4871] [INFO] Booting worker with pid: 4871
2020-09-14 11:15:43 [4871] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495,in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/site-packages/gunicorn/workers/base.py", line106, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/site-packages/gunicorn/util.py", line 354, inimport_app
    __import__(module)
ImportError: No module named projects.sc.sc.wsgi
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495,in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/site-packages/gunicorn/workers/base.py", line106, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/site-packages/gunicorn/util.py", line 354, inimport_app
    __import__(module)
ImportError: No module named projects.scrap.scrap.wsgi
2020-09-14 11:15:43 [4871] [INFO] Worker exiting (pid: 4871)
2020-09-14 11:15:43 [4872] [INFO] Booting worker with pid: 4872
2020-09-14 11:15:43 [4872] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495,in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/site-packages/gunicorn/workers/base.py", line106, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/site-packages/gunicorn/util.py", line 354, inimport_app
    __import__(module)
ImportError: No module named projects.sc.sc.wsgi
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495,in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/site-packages/gunicorn/workers/base.py", line106, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/site-packages/gunicorn/util.py", line 354, inimport_app
    __import__(module)
ImportError: No module named projects.sc.sc.wsgi
2020-09-14 11:15:43 [4872] [INFO] Worker exiting (pid: 4872)
2020-09-14 11:15:43 [4873] [INFO] Booting worker with pid: 4873
2020-09-14 11:15:43 [4873] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495,in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/site-packages/gunicorn/workers/base.py", line106, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/site-packages/gunicorn/util.py", line 354, inimport_app
    __import__(module)
ImportError: No module named projects.scrap.scrap.wsgi
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495,in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/site-packages/gunicorn/workers/base.py", line106, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/site-packages/gunicorn/util.py", line 354, inimport_app
    __import__(module)
ImportError: No module named projects.sc.sc.wsgi
2020-09-14 11:15:43 [4873] [INFO] Worker exiting (pid: 4873)
Traceback (most recent call last):
  File "/bin/gunicorn", line 9, in <module>
    load_entry_point('gunicorn==18.0', 'console_scripts', 'gunicorn')()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 71, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 143, in run
    Arbiter(self).run()
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 203,in run
    self.halt(reason=inst.reason, exit_status=inst.exit_status)
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 298,in halt
    self.stop()
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 341,in stop
    self.reap_workers()
  File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 452,in reap_workers
    raise HaltServer(reason, self.WORKER_BOOT_ERROR)
gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

但是当我尝试卸载 gunicorn 时,它尝试为 python3.8 卸载 gunicorn...当我尝试运行 gunicorn 时,它为 python2.7 运行...难以置信...

[ [/home/scorg/pro]]# pip uninstall gunicorn
Found existing installation: gunicorn 20.0.4
Uninstalling gunicorn-20.0.4:
  Would remove:
    /usr/local/bin/gunicorn
    /usr/local/lib/python3.8/site-packages/gunicorn-20.0.4.dist-info/*
    /usr/local/lib/python3.8/site-packages/gunicorn/*
Proceed (y/n)? y
  Successfully uninstalled gunicorn-20.0.4

【问题讨论】:

看看这个教程,这很有帮助,会解决你的问题:digitalocean.com/community/tutorials/… 谢谢...我会做...我希望它会有所不同,因为我仔细检查了大约 5 个其他教程...并阅读了许多关于 *** 的帖子,修复错误并试图修复这个特定的问题。谢谢你的建议。 【参考方案1】:

如果您有 virtualenv,请按照以下步骤操作:

    创建 guncorn.conf 配置文件

例如:触摸 /opt/yourproject/gunicorn.conf

 import multiprocessing
 workers = multiprocessing.cpu_count()*2+1
 bind='unix:/var/run/gunicorn.sock'
 logfile="/var/log/guni.log"
 errorlog = '-'
 loglevel = 'info'
 accesslog = '-'
 timeout=120
 proc_name = "yourproject"
 user="root"
 group="root"
    创建“gunicorn_start”文件。 例如:触摸 /opt/yourproject/gunicorn_start
 #!/bin/bash 
 NAME="yourproject" 
 DJANGODIR= **** #ex: /home/yourproject/  #change this
 ENVBIN=/yourvirtualenvdir/bin/   
 SOCKFILE=/var/run/gunicorn.sock
 USER=root 
 GROUP=root 
 NUM_WORKERS=5 #change this 
 DJANGO_SETTINGS_MODULE= ****   #ex: yourproject.settigns.main #change this
 DJANGO_WSGI_MODULE=****.wsgi  #ex: yourproject.wsgi #change this
 TIMEOUT=120 
 echo "Starting $NAME as `whoami`" 
 ## Activate the virtual environment
 cd $DJANGODIR 
 source $ENVBIN/activate 
 cd $DJANGODIR 
 export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE 
 export PYTHONPATH=$DJANGODIR:$PYTHONPATH 
 ## Create the run directory if it doesn't exist
 RUNDIR=$(dirname $SOCKFILE) 
 test -d $RUNDIR || mkdir -p $RUNDIR
 ## Start your Django Unicorn
 ## Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
 exec $ENVBIN/gunicorn $DJANGO_WSGI_MODULE:application -c=/opt/yourproject/gunicorn.conf
    创建服务
[Unit]
Description=My Python Service

[Service]
User=root
Restart=always
Type=simple
WorkingDirectory=/opt/yourproject/ 
ExecStart=/usr/bin/sh gunicorn_start

[Install] WantedBy=multi-user.target

【讨论】:

感谢您的帮助!我会试试看。此外,我正在与在 Ubuntu 方面有 10 多年经验的人交谈,他告诉我它也可能是服务器上安装的 Centos 的损坏版本......他建议我先用 Centos 尝试我的系统,如果应用程序运行,然后只需重新安装 centos 并执行与虚拟 centos 相同的安装... 您好!你把我推向了正确的方向。谢谢你。但是我仍在为某些设置而苦苦挣扎...我不知道我是否正确[说得对,但我会尝试...如果您不介意并且可以的话,请告诉我是否我是对还是错: 我的主目录在安装 django 后是标准的,并且不知何故被分叉了。我是什么意思... 1. source /pro/sc_project/bin/activate 在一个目录中,而 django_settings_module 在 pro/projects/sc/settings.py 中,而 django_wsgi_module 在 pro/projects/sc/sc/wsgi 中 应该吗? : djangodir=/home/scorg/pro/ source ..sc_project/bin/activate django_settings_module=projects.sc.settings django_wsgi_module=projects.sc.sc.wsgi 对我来说这是有道理的......感觉? 如果你的 djangodir 是 /home/scorg/pro/ 和 django_settings_module=projects.sc.settings, django_wsgi_module=projects.sc.sc.wsgi ,这意味着对于 gunicorn 来说 settings.py 的位置是 /home /scorg/pro/projects/sc/settings.py 和 wsg_module 位置是 /home/scorg/pro/projects/sc/sc/wsgi.py。【参考方案2】:

您是否尝试在 ExecStart 中添加“--chdir”?

喜欢

ExecStart=/home/scorg/pro/sc_project/bin/gunicorn --chdir /home/scorg/pro/projects/sc/sc --workers 3 --bind unix:/home/scorg/pro/projects/ sc/sc.sock sc.wsgi:应用程序

【讨论】:

gunicorn[16327]:错误:无法 chdir 到 '/home/scorg/pro/projects/sc/sc' 我发现我写的目录有错误...我又试了一次 ModuleNotFoundError: 没有名为“sc”的模块 我之前以为,我有wsgi模块...它的名字不是sc.wsgi...所以我只尝试了名称中的wsgi...但它是相同的故事和相同的错误... 感谢您的建议!

以上是关于在 centos 7 上使用 gunicorn 和 django 的 nginx的主要内容,如果未能解决你的问题,请参考以下文章

VMware+CentOS+Apache+php+python+Django+gunicorn配置

VMware+CentOS+Apache+php+python+Django+gunicorn配置

Gunicorn + Django + Virtualenv + init.d 服务(CentOS)

气流网络服务器启动 - gunicorn 工作人员正在关闭

如何使用Nginx和uWSGI或Gunicorn在Ubuntu上部署Flask Web应用

使用 gunicorn 和 supervisord 将 django 部署到 CentOS 启动附加进程 django-admin.py