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
I did it like this:
settings.py:
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
In your Django settings, it’s crucial to ensure that you have the following configurations set up correctly for serving static files:
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:
Also, check your settings for media files similar to what we did for the static files below:
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:
By following these steps and confirming that your configurations and permissions are accurate. Let me know if it resolves your issue!