【问题标题】:Unable to run Gunicorn as service in systemd 203/EXEC无法在 systemd 203/EXEC 中将 Gunicorn 作为服务运行
【发布时间】:2018-12-09 13:20:18
【问题描述】:

我正在尝试使用 Gunicorn/WSGI/Nginx 部署 FlaskApp。我一直试图让它工作一段时间,除了我遵循的数字海洋指南之外找不到任何东西。以下是我目前状态下的文件。我对mywebapp.service 文件尝试了几种不同的调整,因为我很确定这是我的问题所在。我可以运行/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 -u nginx -g nginx wsgi,gunicorn 就可以了。我很确定这是我缺少的一些可能是基本的小东西,但我丢失了。我的nginx 用户拥有应用程序目录。

[root@localhost mywebapp]# systemctl status mywebapp

● mywebapp.service - Gunicorn instance to serve mywebapp
   Loaded: loaded (/etc/systemd/system/mywebapp.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sat 2018-06-30 13:00:35 EDT; 25min ago
 Main PID: 29706 (code=exited, status=203/EXEC)

Jun 30 13:00:35 localhost.localdomain systemd[1]: Started Gunicorn instance to serve mywebapp.
Jun 30 13:00:35 localhost.localdomain systemd[1]: Starting Gunicorn instance to serve mywebapp...
Jun 30 13:00:35 localhost.localdomain systemd[1]: mywebapp.service: main process exited, code=exited, status=203/EXEC
Jun 30 13:00:35 localhost.localdomain systemd[1]: Unit mywebapp.service entered failed state.
Jun 30 13:00:35 localhost.localdomain systemd[1]: mywebapp.service failed.

/home/nginx/mywebapp

mywebappenv  mywebapp.py  __pycache__  wsgi.py

/etc/systemd/system/mywebapp.service

[Unit]
Description=Gunicorn instance to serve mywebapp
After=network.target

[Service]
User=nginx
Group=nginx
WorkingDirectory=/home/nginx/mywebapp
Environment="PATH=/home/nginx/mywebapp/mywebappenv/bin"
ExecStart=/bin/gunicorn --workers 3 --bind unix:/home/nginx/mywebapp/mywebapp.sock -u nginx -g nginx wsgi
Restart=always

[Install]
WantedBy=multi-user.target

/home/nginx/mywebapp/mywebapp.py

from flask import Flask
application = Flask(__name__)


@application.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

/home/nginx/mywebapp/wsgi.py

from mywebapp import application

if __name__ == "__main__":
    application.run()

[root@localhost mywebapp]# cat /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  10.8.0.173;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://unix:/home/nginx/mywebapp/mywebapp.sock;

    }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

【问题讨论】:

    标签: nginx flask gunicorn wsgi


    【解决方案1】:

    根据此处提供的答案,我是如何解决它的。

    1. 如下所示导航到您的 virtualenv

      cd /my-env/bin/

    2. chown 当前使用(用户在 gunicorn.service 文件中指定)

    以下示例在文件 /etc/systemd/system/gunicorn.service 中有用户为“sammy”

    [Service]
    User=sammy
    Group=www-data
    WorkingDirectory=/home/sammy/myprojectdir
    ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
    
    1. 在 bin 文件夹中,运行命令 ls 以确认您有文件 'gunicorn'

    2. 用这个 Linux 命令控制文件

      sudo chown sammy gunicorn

    3. 运行这些命令重启gunicorn并确认

      sudo systemctl 启用 gunicorn sudo systemctl start gunicorn sudo systemctl status gunicorn

    PS:我的环境是 Ubuntu 18.04

    【讨论】:

      【解决方案2】:

      我终于发现我可以查看journalctl 并找到实际的日志。我必须 chown nginx 用户的 gunicorn 文件。现在它可以工作了,我只需要调整我的 nginx 东西,因为它找不到东西该死的套接字..

      【讨论】:

      • 你能具体说说你做了什么吗?
      • 请原谅我,因为我现在正在从内存中执行此操作......但它是这样的......jounralctl 有日志,并将显示正在运行的文件。当您知道正在尝试运行的文件时,您需要确保 nginx 用户或您必须运行 nginx 的任何用户是该文件的所有者,否则它不会运行,因为它没有正确的权限.当我回到家时,我会尝试重构我的答案.. 祝你好运
      • @MattCamp 你现在可以重构你的答案吗?
      猜你喜欢
      • 2022-08-17
      • 2020-12-18
      • 2020-03-21
      • 2019-09-17
      • 1970-01-01
      • 2019-09-30
      • 2017-08-01
      • 2019-08-19
      • 2015-10-09
      相关资源
      最近更新 更多