0

I've been at this for hours and yet I cannot figure out why my setup isn't working. If I run the command that's in exec on its own, I can access the page through my browser just fine. But when I try to run it as a service, I get a 502: Bad Gateway.

I first tried using Unix sockets, but when that didn't work, I just plugged in the IP and port directly, still no luck.

gunicorn.service:

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

[Service]
User=username
WorkingDirectory=/home/username/naomiselect
Environment="PATH=/home/username/naomiselect/naomienv/bin"
ExecStart=/home/username/naomiselect/naomienv/bin/gunicorn --workers 3 --bind local_ip:8000 naomiselect.wsgi:application

[Install]
WantedBy=multi-user.target

nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

sites-available/naomiselect:

server {
    listen 80;
    server_name local_ip;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/username/naomiselect;
    }

    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://local_ip:8000/;
    }
}

The directory group and owner are www-data and username, respectively, recursively, with permissions drwxrwxr-x.

I have no idea what I'm doing incorrectly when I've referenced several tutorials and tried different methods and none of them work.

tayoung
  • 411
  • 1
  • 4
  • 16

1 Answers1

1

So you say (in the comments) that the status for gunicorn.service says "exited" rather than "running" after you start it.

Most of the times, this means a service Type= misconfiguration. The default is Type=simple, which expects a non-forking process. Probably, your daemon forks into background after startup, which confuses systemd:

If set to simple (the default if neither Type= nor BusName=, but ExecStart= are specified), it is expected that the process configured with ExecStart= is the main process of the service.

If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up.

So, to check this hypothesis, run the daemon binary from shell. If it releases the shell immediately, then your daemon is "forking" and you need to pass the corresponding option in your unit file:

[Service]
Type=forking
intelfx
  • 2,386
  • 1
  • 19
  • 32