deploy app django digitalocean

Awahab@django411:~/apporder$ sudo systemctl start gunicorn.socket
Job failed. See "journalctl -xe" for details.

-xe

Nov 13 18:17:56 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 sudo[44895]:    wahab : TTY=pts/0 ; PWD=/home/wahab/apporder ; USER=root ; COMMAND=/usr/bin/systemctl start gunicorn.socket
Nov 13 18:17:56 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 sudo[44895]: pam_unix(sudo:session): session opened for user root(uid=0) by root(uid=1000)
Nov 13 18:17:56 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 sudo[44895]: pam_unix(sudo:session): session closed for user root

gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

gunicorn.service

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

[Service]
User=wahab
Group=www-data
WorkingDirectory=/home/wahab/apporder
ExecStart=/home/wahab/apporder/env/bin/gunicorn \
          --access-logfile - \
          -k uvicorn.workers.UvicornWorker \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          OrderLine1.asgi:application

[Install]
WantedBy=multi-user.target

You’re probably going to need to look at other log files in /var/log. The first will be syslog. After that it may just be an issue of looking around for a log file that appears to have useful information. (e.g. You might find one for gunicorn itself.)

The other way to approach this is to manually verify that your gunicorn command works properly from the command line, along with verifying the directories and files existing.

in my log folder I have many other files to look at

wahab@django411:/var/log$ ls

alternatives.log  auth.log.1             dist-upgrade              gunicorn    landscape    mail.log.1           private   sysstat                     ufw.log.1
apport.log        btmp                   dmesg                     journal     lastlog      nginx                redis     ubuntu-advantage-timer.log  unattended-upgrades
apt               cloud-init-output.log  dpkg.log                  kern.log    letsencrypt  one_click_setup.log  syslog    ubuntu-advantage.log        wtmp
auth.log          cloud-init.log         droplet-agent.update.log  kern.log.1  mail.log     postgresql           syslog.1  ufw.log

I opened the syslog file but the file is huge

I also get this message when I look at the status of gunicorn.socket

sudo systemctl status gunicorn.socket

○ gunicorn.socket - gunicorn socket
     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
     Active: inactive (dead)
   Triggers: ● gunicorn.service
     Listen: /run/gunicorn.sock (Stream)

Nov 13 18:17:56 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: gunicorn.socket: Socket service gunicorn.service already active, refusing.
Nov 13 18:17:56 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: Failed to listen on gunicorn socket.
Nov 13 20:46:03 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: gunicorn.socket: Socket service gunicorn.service already active, refusing.
Nov 13 20:46:03 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: Failed to listen on gunicorn socket.
Nov 13 20:48:47 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: gunicorn.socket: Socket service gunicorn.service already active, refusing.
Nov 13 20:48:47 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: Failed to listen on gunicorn socket.
Nov 13 20:52:49 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: gunicorn.socket: Socket service gunicorn.service already active, refusing.
Nov 13 20:52:49 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: Failed to listen on gunicorn socket.
Nov 13 20:52:58 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: gunicorn.socket: Socket service gunicorn.service already active, refusing.
Nov 13 20:52:58 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 systemd[1]: Failed to listen on gunicorn socket.

@KenWhitesell he error came from the fact that I had to add my user wahab to the www-data group with this command

sudo adduser {USER-NAME-HERE} {GROUP-NAME-HERE}

and restart with this

systemctl restart gunicorn.service

@KenWhitesell Sorry to bother you but just a little problem I don’t understand why my style doesn’t work yet I have set up my static file can I show you?

We can continue this thread if you’d like - or open a new one, it’s up to you.

Generally, if you’ve got a css file that isn’t being loaded, there are a handful of things to check to see what the problem may be:

  • What is the url being issued by the browser for that file?
  • Are you seeing the request with a 404 on the server?
  • Is the STATIC_ROOT setting correct?
  • Does the file exist in the proper directory, relative to both the STATIC_ROOT and STATIC_URL settings?
  • Does the web server have permission to read the file from that location?

These are all factors that need to be checked in a deployment environment.

I just checked, the URL is good, in my console I get a 403 error, the settings_root is normally also good, I checked every thing, in fact when I launch with gunicorn it works correctly but when I put nginx the style is removed even though I have configured my static in nginx

look at my settings.py

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

my path

wahab@django411:/$ ls home/wahab/apporder/
OrderLine1  README.md  cert.pem  db.sqlite3  env  key.pem  manage.py  master  media  myapp  requirements.txt  static

and my nginx config

wahab@django411:/$ sudo nano /etc/nginx/sites-enabled/apporder.conf

server {
    listen 80;
    server_name (I have hidden the ip);

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/wahab/apporder/static/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Does the account that is being used to run nginx (usually www-data) have the necessary permissions to access that directory?

(I generally consider it a much better practice to point STATIC_ROOT completely outside the project - generally to something like /var/www/html/project_name, it makes the appropriate management of permissions a lot easier.)

to point STATIC_ROOT, is this the right place where I placed the path?

STATIC_ROOT = os.path.join(BASE_DIR, ‘/var/www/html/static’)

oh no sorry like this ?

STATIC_ROOT = os.path.join(var/www/html/, ‘static’)

More simply:
STATIC_ROOT = /var/www/html/static

@KenWhitesell
after 50 tries I started again from A to Z and the static file works correctly, but since I am very lucky, the websocket does not work

network I have this error :face_with_diagonal_mouth:

WebSocket connection to 'ws://******/ws/' failed: 
(anonymous)	@	commande:217

I have always created two separate entries in nginx between websockets and regular requests. (I can’t speak for any other mechanism written about elsewhere.)

My nginx config for websockets looks (in part) something like this:

    location /ws/ {
        proxy_pass http://unix:/run/daphne.sock:/ws/;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
    }

Note that this is in addition to the regular Django paragraph - the key difference being the Connection "upgrade" clause to flag this as a websocket connection.

See Using NGINX as a WebSocket Proxy and WebSocket proxying among other available sources.

but I don’t use daphne I use django-channels, and say again, so i have to change something in the /ws/ location?

That’s just the name of the socket file. It’s a file name. It can be anything you need it to be. There’s nothing specific to daphne in this config.

Also, for clarification:

They are two separate and distinct things. Using one does not imply using (or not using) the other.

so I have to replace with the name of my sock file?

by this
proxy_pass http://unix:/run/gunicorn.sock;