Unable to run Gunicorn as service in systemd 203/EXEC
Asked Answered
C

2

6

I am trying to get deploy a FlaskApp with Gunicorn/WSGI/Nginx. I've been trying to get this to work for awhile and can't find any thing other than the Digital Ocean guides that I've followed to a T. Below are my files I've got in their current states. I have tried several different tweaks to mywebapp.service file because I am pretty sure this is where my problem lay. I can run /bin/gunicorn --workers 3 --bind 0.0.0.0:8000 -u nginx -g nginx wsgi and gunicorn will work. I'm pretty sure its some small possibly fundamental thing that I am missing but I lost. My nginx user owns the app directory.

[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 {
#        }
#    }

}
Clew answered 30/6, 2018 at 17:30 Comment(0)
C
1

I finally figured out I could look in journalctl and found the actual log. I had to chown the gunicorn file for the nginx user. Now its working and I just gotta tweak my nginx stuff cause its not find thing darn socket..

Clew answered 30/6, 2018 at 18:28 Comment(4)
Could you tell exactly what you did please?Abundance
pardon me as I'm doing this from memory right now... but its something like this.. jounralctl has the log and will show you what file is being run. When you know the file that is attempting to be run you need to make sure that the nginx user or whatever user you have to run nginx is the owner of the file, else it wont run because it doesn't have the correct permissions. When I get home i'll try to refactor my answer.. good luckClew
@MattCamp can you refactor your answer now?Jandel
@MattCamp hey, I havd a same problem )Hailstone
A
3

Based on the answers provided here's how I solved it.

  1. Navigate to your virtualenv as below

    cd /my-env/bin/

  2. chown the current use (user specified on the gunicorn.service file)

Below example has user as 'sammy' in file /etc/systemd/system/gunicorn.service

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myprojectdir
ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
  1. On the bin folder, run command ls to confirm you have file 'gunicorn'

  2. Chown the file with this Linux command

    sudo chown sammy gunicorn

  3. Run these commands to restart gunicorn and confirm

    sudo systemctl enable gunicorn sudo systemctl start gunicorn sudo systemctl status gunicorn

PS: My environment was Ubuntu 18.04

Atomize answered 15/5, 2020 at 17:2 Comment(0)
C
1

I finally figured out I could look in journalctl and found the actual log. I had to chown the gunicorn file for the nginx user. Now its working and I just gotta tweak my nginx stuff cause its not find thing darn socket..

Clew answered 30/6, 2018 at 18:28 Comment(4)
Could you tell exactly what you did please?Abundance
pardon me as I'm doing this from memory right now... but its something like this.. jounralctl has the log and will show you what file is being run. When you know the file that is attempting to be run you need to make sure that the nginx user or whatever user you have to run nginx is the owner of the file, else it wont run because it doesn't have the correct permissions. When I get home i'll try to refactor my answer.. good luckClew
@MattCamp can you refactor your answer now?Jandel
@MattCamp hey, I havd a same problem )Hailstone

© 2022 - 2024 — McMap. All rights reserved.