Static files configuration in AWS not working - django

I currently have an multi-docker container application (nginx, postgres RDS, Django) running on Elastic BeanStalk and I am able to use it but the static files (CSS files and JS scripts) are not loaded. This is my current configuration:
nginx setup file
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
client_max_body_size 100M;
server {
listen 80;
charset utf-8;
server_name mydashboard.com;
access_log /dev/stdout;
error_log /dev/stdout info;
location /media/ {
alias /var/www/media/;
}
location /static/ {
alias /var/www/static/;
}
location / {
proxy_pass http://web:8888;
proxy_set_header host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
.ebextensions (folder)
django.config (file)
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "mydashboard.settings"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: mydashboard/mydashboard/wsgi.py
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, "..", "www", "static")
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
If I remove the folder .ebextensions folder and load the app, it will work without displaying the static files but if I add the folder with the django.conf file the app won't deploy and I will encounter the error: Invalid option specification (Namespace: 'aws:elasticbeanstalk:container:python:staticfiles', OptionName: '/static/'): Unknown configuration setting.
In one post I found (Serving static files in Django) it is mentioned that all staticfiles directives from .config files should be removed and under the Software Configuration I should configure the static files under the Static File section, however, this Static File section is not even displayed. What code am I missing for displaying the static files? Thanks in advance for your suggestions and answers.

There is new settings for EBS
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: mysite.wsgi:application
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
container_commands:
01_collectstatic:
command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py collectstatic --noinput"
02_migrate:
command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py migrate --noinput"
leader_only: true

Related

Serving static folder not from nginx but from uwsgi

I am using nginx - uwsgi setting.
I want to serve the static folder from uwsgi not nginx.
So I didn't set any static settings on nginx
nginx setting
server {
listen 80;
server_name dockerhost;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:8011/;
include /etc/nginx/uwsgi_params;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_pass_header X-XSRF-TOKEN;
}
}
uwsgi start command
uwsgi --http :8011 --processes 4 --threads 1 --module myapp.wsgi --logto /tmp/mylog.log
and my django settings is here below.
settins.py
STATIC_URL = '/static/'
STATIC_ROOT = "/usr/src/app/static"
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'frontend/dist'),
)
urls.py
urlpatterns += [
path('sp/', SeparateView.as_view())
]
if settings.DEBUG:
urlpatterns += [
re_path(r'^static/(?P<path>.*)$', views.serve),
]
When DEBUG=True, it works, static files are served,but DEBUG=False
{% static %} in template doesn't work for it, the file under static directories are 404 error.
I have done manage.py collectstatic and confirmed that there are files under /usr/src/app/static
Where should I check ?

Unable to serve static file in Django deployed to production in Digital ocean

Currently I deployed my django app in Digital ocean droplet. In localhost it works well but it cant serve js/css files in static folder when deployed to prod. Here are configs:
server {
server_name keywordprocessor.prodsite.com www.keywordprocessor.prodsite.com>
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/projects/backend/crawler;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
...
}
BY digital oceans default, the project resides inside root directory
`cd projects` `pwd` returns /root/projects/
Settings
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = "/static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "/")
THis is how the project folder looks like
backend/
crawler/
static/
templates
.gitignore
requirements.txt
/etc/systemd/service/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/root/projects/backend/crawler
ExecStart=/usr/local/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
crawler.wsgi:application
[Install]
WantedBy=multi-user.target
All js and css files cant be served
`Failed to load resource: the server responded with a status of 404 (Not Found)`
It does load the page but css messed up. I did some googling for possible solutions, nothing works for me.
copy your static files to /var/www/static and change your nginx conf
1.Copy command in project directory cp -r ./static /var/www/static/
2.Permission to static folder which is in /var/www/static/command
sudo chmod 755 static
3.Change your nginx.conf like
server {
listen 80;
server_name Your-IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /var/www/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
4.Do this commands for nginx and gunicorn
a) sudo nginx -t
b) sudo systemctl restart nginx
c) sudo systemctl restart gunicorn
5.If you will get any error check logs using sudo tail -F /var/log/nginx/error.log and try again.

django+gunicorn+nginx gives 404 while serving static files

django+gunicorn+nginx gives 404 while serving static files
I am trying to deploy a Django project using nginx + gunicorn + postgresql. All the configuration is done, my admin panel project static file will serve , but other static files; it returns a 404 error.(iam use run python manage.py collectstatic)
my error.log nginx :: "/blogpy/static/home/test.css" failed (2: No such file or directory)"
Structure:
blogpy
-blogpy
-config
-nginx
-nginx.conf
-docker-compose.yml
-Dockerfile
-home
-static
-home
-test.css(not working)
- requirements
-static
-templates
-.env
-docker-compose.yml
-Dockerfile
setting.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
nginx configuration:
---- nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
upstream blogpy{
server blogpy:8000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_pass http://blogpy;
}
location /static/ {
alias /blogpy/static/;
}
}
}
Try
In ---- nginx.conf:
location /static/ {
autoindex off;
alias /home/ubuntu/blogpy/static/; #add full path of static file directry
}
To get /blogpy/home/static/ files been copied into /blogpy/static/ by collectstatic command, you need to specify STATICFILES_DIRS setting
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
BASE_DIR / 'home' / 'static',
]
It is recommended to serve static files directly from nginx.
Add the following to your nginx site config.
location /static/ {
alias /path/to/static/directory/;
}

Django/Nginx: statics files are not served

I try to deploy my Django project in a remote server using Nginx but my files are not served.
I guess my path is incorrect but don't really know why...
python3 manage.py collectstatic get all my files in intensecov_static folder.
/home/zebra/
- intensecov_app
- intensecov
- coverage (project)
- manage.py
- static
- ...
- intensecov_static
- css
- style.css
- images
- ...
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
os.path.join(BASE_DIR,'randomization_management/static'),
os.path.join(BASE_DIR,'randomization_settings/static'),
os.path.join(BASE_DIR,'randomization/static'),
)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = '/home/zebra/intensecov_static'
/etc/nginx/sites-available/intensecov
server {
listen 80;
server_name 192.168.80.9;
root /home/zebra/intensecov_app;
location /static {
alias /home/zebra/intensecov_static;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
break;
}
}
}
Wishing your nginx.conf you declared:
location /static {
alias /home/zebra/intensecov_static;
}
Try this:
location /static {
alias /home/zebra/intensecov_static/;
}
It seems nginx needs the trailing / to make the folder work.
Or at least that the serving of static-files broke when removing the trailing /.
STATIC_ROOT is where all the static files are collected when you run the command python manage.py collectstatic
and STATIC_URL is what points to the STATIC_ROOT.
Check here for reference https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-STATIC_ROOT
Changing the location in nginx file should solve the porblem.

Gunicorn on lighttpd - changing base URL for static files from localhost:8080

Right now I'm working on Django project and this is my first time with running Django app on server (Lighttpd) via gunicorn. The problem is - I run application with gunicorn like this:
$PYTHON $MANAGEPATH run_gunicorn -D -b 127.0.0.1:18002 -p $GUNICORN_PIDPATH --log-file $GUNICORN_LOGPATH
Part of Django settings.py:
from __future__ import absolute_import
import os
from socket import gethostname
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
HOSTNAME = gethostname()
def make_path(path):
return os.path.join(BASE_DIR, path)
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = make_path('static_root')
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
In /etc/lighttpd/vhost.d/my_conf.conf I have nice redirect for non-static files:
$SERVER["socket"] == ":82" {
server.document-root = "/home/mefioo/public_html/generator/static"
$HTTP["url"] !~ "\.(js|css|gif|jpg|png|ico|txt|swf|html|htm|svg|xlsx)$" {
proxy.server = ("" => ( "localhost" => (
"host" => "127.0.0.1",
"port" => 18002,
"fix-redirects" => 1
) ) )
}
}
Idea - static files like .js or .css are in
/home/mefioo/public_html/generator/static
(which is proxy to static_root dir in Django app) and should be avaiable from url like
my_domain.com:82/my_file.js
Rest of the app, with all the urls like this:
my_domain.com:82/url
should look for django app like that:
127.0.0.1:18002/url
because of lighttpd conf.
The problem is - when I try to reach my static files, I got 404, because app is looking for
localhost:8080/my_file.js
instead of
my_domain.com:82/my_file.js
I don't understand why, especially when I have two instances of that app, one on my own server (second PC, doesn't work) and one on external VPS (works as intended). Am I running gunicorn wrong way, or missing something in lighttpd settings?
I am new to django/gunicorn too but here is how I configured my app:
settings in settings.py
STATIC_ROOT = "/path to static files"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I'm not using lighttpd but nginx and here is the configuration:
server{
server_name example.com www.example.com;
access_log off;
location /static {
autoindex on;
alias /path_to_static_files;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
After that we have we have to collect the static files in the configured directory. This is done with: python3 manage.py collectstatic
All files should be copied in the correct directory and lightppd or nginx can serve them whereas gunicorn do the django/python - part