简体   繁体   中英

nginx with gunicorn and django on centos 7

I'm trying to make my nginx and gunicorn start working... but seemingly trying everything I could do, fails...

If I do:

systemctl restart nginx
systemctl status nginx

It shows green, and it works... If I do:

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

It shows green and works fine... But if I do:

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

it shows me the following message:

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.

I kind of understand it is bind problem and I followed this question: Gunicorn, no module named 'myproject

But whatever module I try to bind with the following command, it just doesn't work:

gunicorn --bind 0.0.0.0:8000 wsgi:application

I tried wsgi, sc.wsgi, sc/wsgi, /whole_path/wsgi It's just always the same result... mofule not found...

my gunicorn.service looks like this:

#!/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

The structure is as following:

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

I'm in the world of pain at the time. I was struggling with Apache ... I'm noob at centos 7/ linux servers management... Apparently I am stuck... If I forgot to give some extra information... Please let me know, I will happily make an update...

Also I did make sure that I set up gunicorn... So it is on the system and works...:

yum install python-gunicorn

Update After all fight with the options for gunicorn, it is using python 2.7. I have python 3.8.5... I tried to follow this tutorial How to get Gunicorn to use Python 3 instead of Python 2 (502 Bad Gateway) , but so far I cannot figure out completely what to do... I did everything and it's still using python2.7... Interesting thing:

I tried to delete gunicorn... pip uninstall guncorn And system tells me that I don't have gunicorn, same story with pip3... I installed gunicorn with pip3 install gunicorn, but it still trying to use 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>

But when I try uninstall gunicorn, it tried to uninstall gunicorn for python3.8... While I'm trying to run gunicorn it runs for python2.7... Unbelievable...

[ [/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

If you have virtualenv follow this steps:

  1. create guncorn.conf config file

ex: touch /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. create "gunicorn_start" file. ex: touch /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. create service
[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

Have you tried to add "--chdir " in ExecStart?

like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM