centos 7 上带有 gunicorn 和 django 的 nginx

nginx with gunicorn and django on centos 7

我正在尝试让我的 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 --bind 0.0.0.0:8000 wsgi:application

我试过wsgi, sc.wsgi, sc/wsgi, /whole_path/wsgi 总是一样的结果...找不到mofule...

我的 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...所以它在系统上并且可以工作...:[=​​22=]

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 它 运行s 为 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

您是否尝试过在 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:application

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

  1. 创建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"
  1. 创建“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
  1. 创建服务
[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