Ubuntu server with Django, Gunicorn, Nginx does not give static
The file /var/log/nginx/error.log contains errors of the following type:
[error] 714#714: *5 open() "/home/<user>/<projects>/static/css/custom.css" failed (13: Permission denied), client: ip, server: domain, request: "GET /static/css/custom.css HTTP/1.1", host: "domain", referrer: "http://domain/"
The following settings are specified in the /etc/nginx/sites-available/<project>
file:
server {
listen 80;
server_name <domain>;
location = /favicon.ico { access_log off; log_not_found off; }
location /collected_static/ {
root /home/<user>/<project>;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
The following settings are specified in the /etc/systemd/system/gunicorn.service
file:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=vsevolod
Group=www-data
WorkingDirectory=/home/<user>/<project>
ExecStart=/home/<user>/<project>/myvenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
config.wsgi:application
[Install]
WantedBy=multi-user.target
В файле settings.py
указаны следующие настройки:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
BASE_DIR / 'static',
)
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
Am I right in understanding that in the Nginx
configuration file after location
I need to specify exactly the directory that is generated after the python3 manage.py collectstatic
command, so that Nginx
gives it exactly that one?
I ran the command sudo -u www-data stat /home/<user>/<project>/collected_static
and got an error about the user not having enough rights. I ran the command sudo gpasswd -a www-data <user>
, rebooted Nginx
and the server just in case, but the statics still didn't appear, and the logs still had the same errors.
The projects that I post were developed 2 years ago and then I also posted them on a virtual server and everything was ok, I set everything up pretty quickly, but now I can't figure out what's what.
I'm not very good at system administration, I do everything according to guides. So I really ask for help.
Thanks in advance for any response
UPD: Tried setting permission 750 for all directories in the path /home/<user>/<project>/collected_static
Now there are no errors at all in the logs, but the statics still don't load
Ensure Correct File and Directory Permissions
Since Nginx runs as www-data
, it must have read & execute access to the static files.
Update Ownership
sudo chown -R <user>:www-data /home/<user>/<project>/collected_static``
Set Permissions
sudo chmod -R 755 /home/<user>/<project>/collected_static
also, check the parent directories permissions
Nginx needs execute (x
) permission on every parent directory to access the static files.
After that you need to restart the services
sudo systemctl restart nginx
sudo systemctl restart gunicorn