Im trying to collect static files on django production hosting
website: jumentosemuaresonline.com.br
My configuration:
DEBUG = False
ALLOWED_HOSTS = [".jumentosemuaresonline.com.br"]
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
STATIC_URL = os.path.join(BASE_DIR,'/static/')
When i print static root on setting, returns the exact path of the ftp
/home/jumentosemuaresonline/apps_wsgi/website/static/
when i execute python manage.py collectstatic -v 0
It also return the exact path of the ftp
/home/jumentosemuaresonline/apps_wsgi/website/static/
But when i execute python mange.py collectstatic it dont fill the static folder with the static files, only returns
0 static files copied, 109 unmodified.
Anyone have idea what am i missing?
I figured out what was happening:
The collect static command was working fine but I was using Nginx in front of the application as a reverse proxy, Nginx has to serve the static path not django.
upstream django_app {
server djangohost:8000;
}
server {
listen 80;
location / {
proxy_pass http://django_app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /my/static_path/;
}
}
It says 109 unmodified. The files are already there.
Try ls /home/jumentosemuaresonline/apps_wsgi/website/static/
That should show you all the files there.
Just in case. Try this too.
rm -rf /home/jumentosemuaresonline/apps_wsgi/website/static/
mkdir /home/jumentosemuaresonline/apps_wsgi/website/static/
python manage.py collectstatic
This should show you 109 copied and 0 modified.
Related
My Django project deployed on EC2 instance. Everything is fine except static files. Here is my project directory path tree
my_project
└─admin
├── admin
│ ├── admin
│ ├── keywords
│ ├── manage.py
│ └── static
├── README.md
And this is my settings.py setting code about staticfiles
# settings.py
debug=False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_DIR = os.path.dirname(BASE_DIR)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'my_project/static')
STATICFILES_DIRS = [
STATIC_ROOT
]
And this is my nginx.conf files
root /usr/share/nginx/html;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
alias /static/;
}
location / {
proxy_set_header Host $http_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-Proto $scheme;
proxy_pass http://unix:/home/devel/run/gunicorn.sock;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
And also my machine's environments:
Centos 7.9
python 3.6.8
Django 4.0.6
I already did python manage.py collectstatic command.
When I enter into this project's Admin page, Every UI have loaded without CSS and JS though.
Is somewhere of my setting is wrong? I wonder why django cannot find any static files.
Assuming your BASE_DIR is the same as /usr/share/nginx/html, and the BASE_DIR/myproject/static exists and has content, try
location /static/ {
root /myproject/;
}
(Using root rather than alias as the end directories are the same, as per nginx docs)
I am assuming your earlier root definition will prefix the location - if not you might have to use root /usr/share/nginx/html/myproject/ explicitly instead
I'm trying to deploy a django app in a EC2 instance but I'm currently having issues serving the static files of the app.
Currently, my nginx's conf file looks like this:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://0.0.0.0:8000;
}
location /static/ {
#autoindex on;
root /home/ubuntu/some/folder/static/;
}
}
Also in my settings.py file in my django project I have the following config related to my static files:
STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubuntu/some/folder/static/'
and Gunicorn, I'm launching it as the following:
gunicorn3 --bind 0.0.0.0:8000 myproject.wsgi:application
And with that, if I access http://ec2-my-instanceI see the html content, but all my js and css files get error 404.
I'm kinda lost and I don't know what I'm missing to make it works, I already migrated the static content and I checked the folder which nginx is looking for the static files have them, but if I check the browser's console I see this kind of errors:
GET http://ec2-my-instance/static/somefile.js net::ERR_ABORTED 404 (Not Found)
You want 'alias' not 'root':
location /static/ {
alias /home/ubuntu/some/folder/static/;
}
I haveNginx + Gunicorn + Django server deployed on Ubuntu. It running with Debug=False settings and everything works fine. CSS is loaded fine, JS works as well. But when I try to update the css file or js file, the changes I made are not reflected when I run the server.
I tried to update an old static files, also static file created after collectstatic command, I also tried clean collectstatic as well as systemctl restart nginx (and gunicorn). I also cleaned my cache in browser. But when I look a page source code, these changes are not there.
this is how my nginx config looks like
server {
listen 80;
server_name mediadbin.n-media.co.jp;
client_max_body_size 500M;
access_log /home/mediaroot/mediadbin/logs/nginx-access.log;
error_log /home/mediaroot/mediadbin/logs/nginx-error.log;
server_tokens off;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static { $ I TRIED APPLY CHANGES HERE <- NOTHING happens
alias /home/mediaroot/mediadbin/mediadbin/static;
}
location /media {
alias /home/mediaroot/mediadbin/mediadbin/media;
}
include global404;
}
HERE is my gunicorn config
#!/bin/bash
# Name of the application
NAME="mediadbin"
# Django project directory
DJANGODIR=/home/mediaroot/mediadbin/mediadbin
# how many worker processes should Gunicorn spawn
NUM_WORKERS= $(( $(nproc) * 2 + 1 ))
# which settings file should Django use
DJANGO_SETTINGS_MODULE=mediadbin.settings
# WSGI module name
DJANGO_WSGI_MODULE=mediadbin.wsgi
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
ENV=new_django
source /root/.virtualenvs/new_django/bin/activate
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--timeout 600
--name $NAME \
--workers &NUM_WORKERS \
--bind=127.0.0.1 \
--log-level=debug \
--log-file=-
>>> sudo find / -name menu_detail_look.js
/home/mediaroot/mediadbin/mediadbin/static/main_app/js/menu_detail_look.js
/home/mediaroot/mediadbin/mediadbin/main_app/static/main_app/js/menu_detail_look.js
↑ Updated both, nothing happens (no err in js also)
First of all, if you set static location nginx then static files will definitely be loaded through nginx.
make you static block like (instead of alias use root):
location /static/ {
root /home/mediaroot/mediadbin/mediadbin/static/;
}
Also make sure static root is set to: /home/mediaroot/mediadbin/mediadbin/static/
Running collectstatic will copy all static files to static root folder and nginx will serve it.
Hmm it's been so long , Aliright if now anyone working on this can check this solution
in urls.py
from django.conf import settings
from django.conf.urls.static import static
then at bottom define
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
OR (DEPEND on your env.)
if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and in settings.py
define media path
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "/media/"
Make sure to define path in /etc/nginx/sites-available/domainxxx
If your folder path is /home/admin/django/project/media
location /media/ {
root /home/admin/django/project;
}
I'm running my django project on amazon AMI machine and have problem with nginx serving static files. Project static folder path is /home/user/projectname/app/static and nginx.conf is
server {
listen 80;
location /static {
alias /home/user/projectname/app/static;
}
location / {
proxy_pass http://localhost:8000;
}
I tried to make collectstatic and change static location to alias /home/user/static; but it didn't help. What am i doing wrong?
Make sure that you chown the home directory. This makes the static files accessible to nginx.
chown -R user:user /home
chmod -R ug+r /home
Also, try running python manage.py collectstatic --noinput
Hope this solves the problem.
please change the static directory into staticstorage
server {
listen 80;
location /static {
alias /home/user/projectname/app/staticstorage;
}
location / {
proxy_pass http://localhost:8000;
}
Hi I am new with nginx server, and I have uploaded my index.py file at /var/www/pyth/index.py ...
I am a little bit confused because in my local I can run freely
python index.py and access http://127.0.0.1:8080
I was wondering how can I do that in nginx, I have run python index.py but I can't access to mysite.com:8080
this is my config in /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;`
#root /usr/share/nginx/html;
#index index.php index.py index.html index.htm;
root /var/www/mysite.com;
index index.php index.py index.html index.htm;
# Make site accessible from http://localhost/
server_name mysite.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied reques$
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
...
does anyone has an idea about my case? any help will be appreciated.. thanks in advance
You should set up either a uwsgi (or similar), or a proxy_pass in nginx.
The option with UWSGI is better because it'll use the protocol designed for working with web-servers; though it's a bit harder to set up than just proxying everything via nginx.
proxy_pass
web.py has a web-server just for the development purposes, it shouldn't be used in production environment because it's really slow and inefficient in that case, and using proxy_pass wouldn't be a great idea if you are planning to release it.
With proxy_pass, you leave the 127.0.0.1:8080 server online, and then in nginx (on the same server), set up like that:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
The proxy_pass option redirects everything to the web.py server at 127.0.0.1:8080, the other ones - redirect the data about the connection (IP of the connected client and the host that was used for the connection on the nginx's side)
UWSGI
Using UWSGI, in short, is like that:
1) install uwsgi using your distro's package manager, or a pip, or using setup.py install.
2) in nginx, set up a server that will pass everything to the UWSGI server:
server {
listen 80;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
}
}
3) Then, in your web.py application (let's suppose it's called yourappfile.py), instead of app.run(), use:
app = web.application(urls, globals())
application = app.wsgifunc()
You can still have app.run(), just make sure to put it inside the if __name__ == '__main__' block; and make sure the application = app.wsgifunc() is outside so UWSGI could see it.
Then start a UWSGI server:
uwsgi --http :9090 --wsgi-file yourappfile.py
Take a look at these manuals, it may help you:
UWSGI Quickstart
Web.py running on the nginx uwsgi
Deployment of Web.py Applications Using uWSGI and
Nginx
UWSGI Wiki - Examples