Unable to load staticfiles after deploying django app on aws-ec2 - django

I am a beginner in aws and I am trying to deploy a django app on aws-ec2. I have set-up gunicorn application server and nginx web server and app loads but without static files. I have followed many answers on stackoverflow but I am unable to fix my problem.
I tried with both root and alias but they didn't work. The project structure is as follows:-
/home/ubuntu/myskillhut/
django.conf(nginx configuration file)
server {
...
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myskillhut/app.sock;
}
location /static/ {
autoindex on;
alias /home/ubuntu/myskillhut/static/;
}
}
settings.py
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"), )
...

Configuration below works for my project
location ~* \.(jpe?g|gif|png)$ {
root /home/ubuntu/myskillhut/static/;
expires 1M;
}
location ~* \.(css|js)$ {
root /home/ubuntu/myskillhut/static/;
expires 1d;
}
Also, make sure nginx can access those files in that dir (../static/)

Related

Static files not loading for deployed django rest framework

I have built and successfully deployed a django rest framework using gunicorn and nginx on Ubuntu 18.04. However, the static files are not being pulled up.
Django web app without loaded static files
Here is my nginx configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name [my_server_domain_or_IP];
#root /var/www/html;
location = /favicon.ico { access_log off; log_not_found off; }
location = /static/ {
root /home/ubuntu/myprojectdir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
And in my settings.py file:
STATIC_URL = '/static/'
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
I have checked that DEBUG is set to False.
I have also already run collectstatic and the static files are located in a folder called static in: /home/ubuntu/myprojectdir/static.
Every change I tried to make in the nginx configuration, I restarted nginx with the following command: sudo systemctl restart nginx.
I mainly followed this tutorial, the only difference is that I edited the default nginx configuration instead of creating a new one because it wasn't deploying my django application this way.
I can't seem to figure out what's wrong and have been trying to solve it for days. Am I missing something here?
You don't need equals sign here:
location = /static/ {
root /home/ubuntu/myprojectdir;
}
Instead try this:
location /static/ {
root /home/ubuntu/myprojectdir;
}
Try to giving permissions to your home folder
chmod a+x /home/ubuntu -R

django nginx not serving static files

I am depploying django using nginx and gunicorn. When I access the website I get the following errors.
open() "/home/x/aroundu/core/static/static/rest_framework/js/default.js" failed
it's accessing static files wrong, because the path should be like this
open() "/home/x/aroundu/core/core/static/rest_framework/js/default.js" failed
server {
listen 80;
location /static/ {
alias /home/x/aroundu/core/core/static/;
}
location /media/ {
alias /home/x/aroundu/core/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
This should be:
location /static/ {
root /home/x/aroundu/core/core/;
}
location /media/ {
root /home/x/aroundu/core/;
}
Also for static you have /core/core/ and for media you have only /core/. I don't know which is the right one, but given your settings they should be the same.

Statics problem with django docker nginx gunicorn

I have a small problem with access to my django statics through nginx.
I serve the django server running in a docker, on a url with a location in nginx:
location /external/ {
proxy_pass http://external;
}
For static configuration in nginx i add:
location /external/static/ {
autoindex on;
alias /external/static/;
}
For the configuration of my statics on Django I set this code in the settings:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/' + PREFIX + 'static/' ## -> with my config = /external/static/
In my Dockerfile I run my django server with gunicorn and I expose it through port 8000. When I access directly to the server with port 8000 (http://serveraddr.com:8000) I don't have statics problems but when I access it with nginx (https://serveraddr.com/external) I get a not found on my statics.
Help me please
Assuming your static files are in static directory
location /external/static/ {
autoindex on;
alias /full/path/to/folder/static;
}

Nginx not serve media files

I develop an application with Django and Angular 4, before I can see media files without problems in my localhost, but in my LightSailServer I delete my sock file created with gunicorn and deploy my app again, but I don't understand why I don't see my media files in this moment, this is my nginx file, but I don't touch this in all moment, I think my problem is in other way, any ideas?
server {
listen 80;
server_name 54.12.12.142;
location = /favicon.ico {
alias /home/ubuntu/tracker-web/trackerServer/favicon.ico;
}
include /etc/nginx/mime.types;
location /static/ {
root /home/ubuntu/tracker-web/trackerServer;
}
location /media/ {
autoindex on;
alias /home/ubuntu/tracker-web/trackerServer/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/tracker-web/trackerServer/trackerServer.sock;
}
}
I don't see problems in this file, but I can't see my media files and these exists!
PD: I obtain correct path to media files from my web server, only I can't see these.
This I have in my settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static-root')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Thanks
SOLVED
I had deleted the image folder from my server

django nginx configuration issue

I am encountering an issue with my nginx django setting.
Well, when I load the project using their development server everything is ok.
When I load it from nginx, it looks it's not really loading good project.
I made a test, I edited the settings.py file leaving a syntax error and well it didn't load on the internal server of django, but, it loaded with nginx. So I guess nginx isn't linked to the right config but I can't get why as I only created one project.
Config file is the following (nginx):
server {
listen 80;
server_name sub.example.com;
access_log /var/www/sub.example.com/log/access.log;
error_log /var/www/sub.example.com/log/error.log;
## Default location
location / {
include fastcgi_params;
fastcgi_pass 127.0.0.1:8888;
fastcgi_split_path_info ^()(.*)$;
}
location /static/ { # STATIC_URL
alias /var/www/sub.example.com/static/; # STATIC_ROOT
expires 30d;
}
location /media/ { # MEDIA_URL
alias /var/www/sub.example.com/media/; # MEDIA_ROOT
expires 30d;
}
}