This is my first real Django project and I am trying to configure it for production using NGINX and uWSGI. It is running on a Digital Ocean Ubuntu server. Everything is set up and working besides serving static CSS files. The strange thing about this is that its serving static images and JavaScript files fine, the only thing that isn't being served is the CSS.
This is what my site configuration file for NGINX looks like ('nebula' is the Ubuntu User and the name of the Django project):
# configuration of the server
server {
server_name example.com www.example.com;
charset utf-8;
# max upload size
client_max_body_size 75M;
# Django media and static files
location /media {
alias /home/nebula/nebula/media;
}
location /assets {
alias /home/nebula/nebula/assets;
}
# Send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/nebula/nebula/uwsgi_params;
}
}
This is what my Settings.py file looks like:
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/assets/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
STATIC_ROOT = os.path.join(BASE_DIR, "assets/")
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)
This is what my base directory looks like (assets and static are the same, I duplicated it in an attempt to solve the issue):
assets demo.py media nebula.sock static uwsgi_params
db.sqlite3 manage.py nebula nebula_uwsgi.ini set store
This is inside of 'assets/':
admin css images jazzmin js vendor
The location path for your static files in your nginx config should match your STATIC_URL setting
location /assets {
alias /home/nebula/nebula/assets;
}
Seems like there could be an issue here
Could you try Changing
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'media/'),)
to
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)
Related
I've recently uploaded a django project to an Ubuntu 22.04 DigitalOcean droplet. Nginx, gunicorn, and postgres have all been set up without a hitch, but the static files are all being 403'd.
In both development and production, I never used collectstatic. The reason being, I always put all my static files in a static folder in the root of my project, so I figured I didn't have to do it. I'm not sure if collectstatic needs to be done in order for static files to be shown in production, but it seems that other people have still encountered this problem even when they've done collectstatic.
My project on the server is called pyapps, and in that folder there are the static and media folders. The static folder is further broken down into css, js, and images folders. The media folder is further broken down into photos, then folder by year, month, then day, all of which correlate to the time the photos were uploaded.
Below are my settings.py, urls.py and /etc/nginx/sites-available/ files' code.
settings.py
STATIC_URL = 'static/'
STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'static'))]
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
/etc/nginx/sites-available/Car-terra
server {
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/djangoadmin/pyapps/Car-terra/static/;
}
location /media/ {
alias /home/djangoadmin/pyapps/Car-terra/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
server {
if ($host = www.carterrarfw.online) {
return 301 https://$host$request_uri;
} # managed by Certbot
}
if ($host = carterrarfw.online) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name 146.190.210.138 carterrarfw.online www.carterrarfw.online;
return 404; # managed by Certbot
}
What I've Tried
chmod 777 on the static folder (in retrospect not a good choice, but it didn't work nonetheless)
changing root /home/djangoadmin/pyapps/Car-terra; to alias /home/djangoadmin/pyapps/Car-terra/static/ and media;
changing debug to True just to see if there was a difference (there wasn't)
I'm not sure if this is a problem with collectstatic, or if I'm telling nginx to look in the wrong place.
Is running collectstatic absolutely mandatory?
Thank you for your help.
server {
listen 80;
server_name 13.xx.xx.xxx;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
location / {
include proxy_params;
include /etc/nginx/mime.types;
proxy_pass http://unix:/home/ubuntu/studykarma/django-react-redux-base/src/djangoreactredux.sock;
}
}
my nginx config file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_dist'),
)
my settings.py
I have my static files in static_dist folder and my reactjs code in static
folder.
My static files are not loading and giving me 404,
If i change path to /static_dist/ then also i am getting empty content.
I am using this template: https://github.com/Seedstars/django-react-redux-base
About Nginx configuration
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
This configuration tells Nginx that on accessing via say example.com/static/app.js, look app.js file inside src folder root, if you want static_dist, then the configuration change will be
location /static/{
root /home/ubuntu/studykarma/django-react-redux-base/src/path/to/static_dist/;
}
About Django configuration
Your Django settings is logically wrong,
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
Above settings tells django that you expect all your static files from STATICFILES_DIRS to STATIC_ROOT, the configuration is effective/used by django when you use collectstatic django manage command, which ineffect copies all your files from STATICFILES_DIRS to STATIC_ROOT( but this also means that, you have to point /static/ location nginx config to static_root instead of static_dist.
Use this,
location /static_root/ { root /home/ubuntu/studykarma/django-react-redux-base/src/static_root/; }
it will work
I created my website and media was working fine, but now in production static files are okay but media files doesn't work. When making a new post by a user usually you would have to put a thumbnail and you also have the ability to use the text editor to add pics into your post using django-summernote. Anyways it should upload them to media root in the server but it doesn't. It just gives me 500 server error and in the editor it gives me
Got an error uploading an image: Failed to save attachment
my urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static-root/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
# '/var/www/static/',
]
MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/www/media-root/'
I've read that I might need to serve my media and static in another server
why? Static works fine but media doesn't.
I'm using Debian for the Django code.
If you are using nginx you need to tell it where to find the /media/ files, so in your nginx configuration server file you need to add:
location /media/ {
autoindex on;
alias /var/www/media-root/;
}
Also, I prefer use just MEDIA_ROOT = os.path.join(BASE_DIR, 'media') to have the media files inside of my project folder. In your way you need to create the folder and give it permissions mkdir /var/www/media-root and chmod -R ug+rw /var/www/media-root
I feel a bit awkward asking a question which has been answered before - however I feel there is a point of difference with regard to having node serve out the static files as opposed to django. The css bundled by webpack and served by node is working with no problems, where I'm having issues is serving up the admin css and another couple of other files using the get_static_prefix decorator.
The file structure is as follows:
root
|
public
- templates
- static
| <-- collectstatic adding files here
- vendor
|
server
- app1
- app2
| settings.py
/etc/nginx/sites-available/project
server {
listen xxx.xxx.xxx.xxx:8000;
server_name xxx.xxx.xxx.xxx;
location /static {
alias /root/se/env/public/static/;
}
}
and the setup in settings.py
STATIC_URL = '/root/se/env/public/static/'
MEDIA_URL = '/media/'
STATIC_ONLY_URL = '/static_only/'
if not DEBUG:
MEDIA = '/media',
STATIC_ROOT = '/root/se/env/public/static/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'public', 'media')
STATICFILES_DIRS = '/root/se/env/public/vendor/static/',
I've been going around in circles for a while now. I'm pretty new to Django and don't really get how this works. From what I can understand the the STATICFILES_DIRS are where collectstatic gathers the static files from and the STATIC_ROOT is where the static files are dumped after collectstatic is run. I think the STATIC_URL is where I'm going wrong - but I'm not having much luck figuring it out.
As it stands the admin css isn't working when DEBUG = True now either, so I've misconfigured something. It's resulting in:
Not Found: /static/admin/css/base.css
Your location /static is wrong. The alias directive substitutes parts of the URI when forming the pathname. The location parameter and the alias parameter should both end with a /, or neither end with a /:
location /static {
alias /root/se/env/public/static;
}
or:
location /static/ {
alias /root/se/env/public/static/;
}
In fact, because the alias parameter ends with the location parameter, you should not be using the alias directive at all. See the note at the end of the alias documentation.
location /static {
root /root/se/env/public;
}
I ran into very weird issue for which I could not find a reason. I have a django app with uWSGI as my app server and Nginx as our reverse proxy. My initial setting for the static url in Django are as below:
PROJECT_DIR = "/home/ubuntu/src/myapp"
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'staticfiles')
STATIC_URL = '/staticfiles/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
and what's there in the nginx conf is as below:
server {
listen 80;
server_name stg1.myapp.com
client_max_body_size 5G; # adjust to taste
access_log /var/log/nginx/myapp-access.log combined;
error_log /var/log/nginx/myapp-error.log;
location /media {
alias /home/ubuntu/src/myapp/media/; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/src/myapp/staticfiles/;
}
location / {
uwsgi_pass django;
uwsgi_read_timeout 600s;
uwsgi_buffering off;
uwsgi_send_timeout 600s;
proxy_read_timeout 600s;
include /home/ubuntu/src/myapp/uwsgi_params;
}
}
Now, when I was trying to access the server I was getting this error for the static files:
2016/12/13 20:33:03 [error] 30533#0: *194 open() "/home/ubuntu/src/myapp/staticfiles/files/css/base.css" failed (2: No such file or directory), client: 172.31.4.166, server: stg.myapp.com, request: "GET /staticfiles/css/base.css HTTP/1.1", host: "stg.myapp.com", referrer: "http://stg.myapp.com/profile/user1"
Collectstatic copied all the files in the given STATIC_ROOT location. But the path actually searched was - STATIC_ROOT/files.
When I changed the STATIC_ROOT to
STATIC_ROOT = os.path.join(PROJECT_DIR, 'staticfiles/files')
it worked. I am still clueless about why the directory staticfiles/files was being looked for and not just the staticfiles directory as given in nginx conf. Where should I be looking for a possible reason?
EDIT - I had restarted nginx service whenever there was a change done there. So no issues there.
I found the reason.
STATIC_URL = '/staticfiles/' is creating issue. It has to be
STATIC_URL = '/static/'.
The earlier one appends the files in the path on request which was causing my issue.
Cannot find the directory files anywhere in your first set up. [I have not privileges to comment yet].
The set up looks good for me, maybe you just forget to restart Nginx when you modified the configuration file.
Refer link at STATIC_ROOT in Django on Server
Also Static Root and Static Url confusion in Django