skip to Main Content

I’m trying to deploy a django website on a vps and it’s now up and running(after a lot of trouble!), but now my media and static files are not shown in my website and I really tried a lot of ways but none of them worked.

My nginx configuration:

server {
    listen 80;
    server_name domain_name;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/real_estate/static/;
    }

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


gunicorn.service:

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

[Service]
User=shahriar
Group=www-data
WorkingDirectory=/home/shahriar/Amlak/real_estate
ExecStart=/home/shahriar/Amlak/env/bin/gunicorn 
          --access-logfile - 
          --workers 3 
          --bind unix:/run/gunicorn.sock 
          real_estate.wsgi:application

[Install]
WantedBy=multi-user.target

settings.py:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = '/var/www/real_estate/static/'
STATIC_ROOT = '/var/www/real_estate/static/assets'
STATICFILES_DIRS = [ 'static/', BASE_DIR/'static/', '/var/www/real_estate/static/'

]

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# Media Settings

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR.joinpath('media')



And eventually I restarted every service possible but not a single static file was shown.

I tried changing settings.py from this:

STATIC_URL = 'static/'
STATIC_ROOT = 'assets/'
STATICFILES_DIRS = [ 'static/', BASE_DIR/'static/'
]

to this:

STATIC_URL = '/var/www/real_estate/static/'
STATIC_ROOT = '/var/www/real_estate/static/assets'
STATICFILES_DIRS = [ 'static/', BASE_DIR/'static/', '/var/www/real_estate/static/'

]

I tried changing from this :

server {
    listen 80;
    server_name domain_name;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/real_estate/static/;
    }
    location /assets/ {
        root /var/www/real_estate/assets/;
    }

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

to this:

server {
    listen 80;
    server_name domain_name;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/real_estate/static/;
    }

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

2

Answers


  1. Chosen as BEST ANSWER

    I did it like this:

    location /static {
        alias /var/www/real_estate/static/;
    }
    location /media {
        root /home/path/to/real_estate/;
    }
    

    settings.py:

    STATIC_URL = 'static/'
    STATIC_ROOT = '/var/www/real_estate/static/'
    STATICFILES_DIRS = [BASE_DIR/'static/',]
    

    Here is what I did about media files: Media files are not shown: open() "/home/shahriar/Amlak/real_estate/media/images/image1.jpg" failed (13: Permission denied) - Django Nginx


  2. In your Django settings, it’s crucial to ensure that you have the following configurations set up correctly for serving static files:

    # settings.py
    
    STATIC_URL = '/static/'
    STATIC_ROOT = '/var/www/real_estate/static/assets'
    

    Once you’ve made these changes to your Django settings, the next step is to collect your static files into the designated STATIC_ROOT directory. You can achieve this by running the following command within your Django project directory:

    python manage.py collectstatic
    

    Also, check your settings for media files similar to what we did for the static files below:

    # settings.py
    
    MEDIA_URL = '/media/'
    MEDIA_ROOT = BASE_DIR.joinpath('media')
    

    These settings define the URL and root directory for your media files.

    We also need to ensure that the permissions for your static and media directories are correctly set to allow Nginx and Gunicorn to read these files. You can adjust the permissions as follows:


    sudo chown -R www-data:www-data /var/www/real_estate/static
    sudo chown -R www-data:www-data /path/to/media
    

    By following these steps and confirming that your configurations and permissions are accurate. Let me know if it resolves your issue!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search