Everything worked very well before gunicorn and nginx, static files were served to the website.
But now, it doesn't work anymore.
Settings.py
STATICFILES_DIRS = [
'/root/vcrm/vcrm1/static/'
]
STATIC_ROOT = os.path.join(BASE_DIR, 'vcrm/static')
STATIC_URL = '/static/'
MEDIA_ROOT = '/root/vcrm/vcrm1/vcrm/media/'
MEDIA_URL = '/media/'
/etc/nginx/sites-available/vcrm
server {
listen 80;
server_name 195.110.58.168;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /root/vcrm/vcrm1/vcrm;
}
location = /media {
root /root/vcrm/vcrm1/vcrm;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
When I run collectstatic:
You have requested to collect static files at the destination
location as specified in your settings:
/root/vcrm/vcrm1/vcrm/static
This will overwrite existing files!
Are you sure you want to do this?
and then:
Found another file with the destination path 'admin/js/vendor/jquery/jquery.min.js'. It will be
ignored since only the first encountered file is collected. If this is not what you want, make sure
every static file has a unique path.
0 static files copied to '/root/vcrm/vcrm1/vcrm/static', 251 unmodified.
NGINX + Gunicorn + Django
Django project:
djangoapp
- ...
- database
- djangoapp
- settings.py
- urls.py
- ...
- media
- static
- manage.py
- requirements.txt
Server: install venv, requirements.txt:
sudo apt-get update
sudo apt-get install -y git python3-dev python3-venv python3-pip supervisor nginx vim libpq-dev
--> cd djangoapp
pathon3 -m venv venv
source venv/bin/activate
(venv) pip3 install -r requirements.txt
Server: install NGINX:
sudo apt-get install nginx
sudo vim /etc/nginx/sites-enabled/default
Server: NGINX config:
server {
listen 80 default_server;
listen [::]:80 default_server;
location /static/ {
alias /home/ubuntu/djangoapp/static/;
}
location /media/ {
alias /home/ubuntu/djangoapp/media/;
}
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_redirect off;
add_header P3P 'CP="ALL DSP COR PSAa OUR NOR ONL UNI COM NAV"';
add_header Access-Control-Allow-Origin *;
}
}
Server: setup supervisor:
cd /etc/supervisor/conf.d/
sudo vim djangoapp.conf
Server: supervisor config:
[program:djangoapp]
command = /home/ubuntu/djangoapp/venv/bin/gunicorn djangoapp.wsgi -b 127.0.0.1:8000 -w 4 --timeout 90
autostart=true
autorestart=true
directory=/home/ubuntu/djangoapp
stderr_logfile=/var/log/game_muster.err.log
stdout_logfile=/var/log/game_muster.out.log
Server: update supervisor with the new process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart djangoapp
Run python manage.py collectstatic from your project directory.
Access nginx's webserver config file and add a location in your config file.
Reload nginx server with sudo systemctl reload nginx.
Related
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 developed my project in localhost, everything is working. Then I pushed the project to github and finally cloned it in my AWS EC2 Ubuntu server. This is the sites public IP: http://3.16.1.224 . In the said public IP, all my contents is displaying, except for images and css. I tried to login into the admin panel but same, no css and images as well.
My folder structure is this:
jangooCMS
accounts /* an app for user creation */
articles /* an app for article creation */
assets
logo-jangoo.png
db.sqlite3
jangooCMS /* my main app */
settings.py
urls.py
views.py
wsgi.py
manage.py
media /* all the articles images inside here */
4-kitties-low.jpg
requirements.txt
templates
base-layout.html
Now my base-layout.html have this on top:
{% load static from staticfiles %}
Now going to my AWS, my settings.py is below:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
STATIC_ROOT = os.path.join(BASE_DIR, "assets/")
Please help.. my first time to post here. Thank you!
this is the instruction I followed on deploying my project from github to AWS EC2:
cd Downloads/
mv blogoo.pem ~/Desktop/
cd ..
cd desktop
chmod
ssh
yes
sudo apt-get update
sudo apt-get install python3-pip python3-dev nginx git
Y
sudo apt-get update
sudo pip3 install virtualenv
git clone https://github.com/madonnadevt/jangooCMS.git
cd jangooCMS
virtualenv venv
source venv/bin/activate
pip3 install -r requirements.txt
pip3 install django bcrypt django-extensions
pip3 install gunicorn
cd jangooCMS
sudo vim settings.py
# Inside settings.py modify these lines allowed host public IP address I for INSERT
i
ALLOWED_HOSTS = ['3.16.1.224']
# add the line below to the bottom of the file
STATIC_ROOT = os.path.join(BASE_DIR, "assets/")
Save your changes and quit. ESC :wq
cd ..
python manage.py collectstatic
gunicorn --bind 0.0.0.0:8000 jangooCMS.wsgi:application
ctrl+c
sudo vim /etc/systemd/system/gunicorn.service
i
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/jangooCMS
ExecStart=/home/ubuntu/jangooCMS/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/jangooCMS/jangooCMS.sock jangooCMS.wsgi:application
[Install]
WantedBy=multi-user.target
ESC :wq
deactivate
(directory should be in ubuntu)
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo vim /etc/nginx/sites-available/jangooCMS
i
server {
listen 80;
server_name 3.16.1.224;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/jangooCMS;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/jangooCMS/jangooCMS.sock;
}
}
ESC :wq
sudo ln -s /etc/nginx/sites-available/jangooCMS /etc/nginx/sites-enabled
sudo nginx -t
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart
Your website is requesting http://13.59.50.215/static/styles.css - note the static/ path in there - and it does not find it. That is because you have declared the folder name as assets in your settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, "assets/")
And in the nGinx config, you refer to it as static/
location /static/ {
root /home/ubuntu/jangooCMS;
}
So, in order to fix it, is much preferred to use
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
I am taking the help of following docs http://tutos.readthedocs.io/en/latest/source/ndg.html and trying to run the server. I have following file in sites-enabled :
upstream test_server {
server unix:/var/www/test/run/gunicorn.sock fail_timeout=10s;
}
# This is not neccessary - it's just commonly used
# it just redirects example.com -> www.example.com
# so it isn't treated as two separate websites
server {
listen 80;
server_name dehatiengineers.in;
return 301 $scheme://www.dehatiengineers.in$request_uri;
}
server {
listen 80;
server_name www.dehatiengineer.in;
client_max_body_size 4G;
access_log /var/www/test/logs/nginx-access.log;
error_log /var/www/test/logs/nginx-error.log warn;
location /static/ {
autoindex on;
alias /var/www/test/ourcase/static/;
}
location /media/ {
autoindex on;
alias /var/www/test/ourcase/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://test_server;
break;
}
}
#For favicon
location /favicon.ico {
alias /var/www/test/test/static/img/favicon.ico;
}
#For robots.txt
location /robots.txt {
alias /var/www/test/test/static/robots.txt ;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/test/ourcase/static/;
}
}
My server's domain name is dehatiengineers.in.
The gunicorn_start.sh file is:
#!/bin/bash
NAME="ourcase" #Name of the application (*)
DJANGODIR=/var/www/test/ourcase # Django project directory (*)
SOCKFILE=/var/www/test/run/gunicorn.sock # we will communicate using this unix socket (*)
USER=root # the user to run as (*)
GROUP=webdata # the group to run as (*)
NUM_WORKERS=1 # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=ourcase.settings # which settings file should Django use (*)
DJANGO_WSGI_MODULE=ourcase.wsgi # WSGI module name (*)
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /var/www/test/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /var/www/test/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user $USER \
--bind=unix:$SOCKFILE
Now, I am running both of my nginx and gunicorn_start.sh file. nginx is running, as you can see at http://www.dehatiengineers.in/ but the django is not connected to nginx. Now on sudo sh gunicorn_start.sh, I am getting following output:
Starting ourcase as root
gunicorn_start.sh: 16: gunicorn_start.sh: source: not found
[2016-05-20 06:21:45 +0000] [10730] [INFO] Starting gunicorn 19.5.0
[2016-05-20 06:21:45 +0000] [10730] [INFO] Listening at: unix:/var/www/test/run/gunicorn.sock (10730)
[2016-05-20 06:21:45 +0000] [10730] [INFO] Using worker: sync
[2016-05-20 06:21:45 +0000] [10735] [INFO] Booting worker with pid: 10735
But, in browser, I am just getting nginx output, not the original one.
Apart from this, I am having issue in running gunicorn_ourcase.service file:
[Unit]
Description=Ourcase gunicorn daemon
[Service]
Type=simple
User=root
ExecStart=/var/www/test/gunicorn_start.sh
[Install]
WantedBy=multi-user.target
running issues:
$ systemctl enable gunicorn_ourcase
systemctl: command not found
Please help me to solve this.
Everything I was doing was fine, except the fact that nginx was using it's default config file. I had to delete/edit the file located in /etc/nginx/sites-enabled/ , with name default. After editing it, everything works fine.
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;
}
I'm trying to setup django with nginx and gunicorn by these tutorials http://senko.net/en/django-nginx-gunicorn/ and http://honza.ca/2011/05/deploying-django-with-nginx-and-gunicorn
Setup:
virtualenv --no-site-packages qlimp
cd qlimp
source bin/activate
pip install gunicorn django
django-admin.py startproject qlimp
cd qlimp
python manage.py runserver 0.0.0.0:8001
Gunicorn setup is same as here http://senko.net/en/django-nginx-gunicorn/
changes made (run.sh):
LOGFILE=/var/log/gunicorn/qlimp.log
USER=nirmal
GROUP=nirmal
cd /home/nirmal/qlimp/qlimp
Upstart is also the same as the tutorial in above link
changes made:
exec /home/nirmal/qlimp/qlimp/run.sh
Nginx setup:
server {
listen 80;
server_name qlimp.com;
access_log /home/nirmal/qlimp/log/access.log;
error_log /home/nirmal/qlimp/log/error.log;
location /static {
root /home/nirmal/qlimp/qlimp;
}
location / {
proxy_pass http://127.0.0.1:8888;
}
}
Then I restarted nginx and run the gunicorn server:
sudo /etc/init.d/nginx restart
(qlimp) cd qlimp/qlimp
(qlimp) gunicorn_django -D -c run.sh
When I run the gunicorn server, I'm getting this error:
Failed to read config file: run.sh
Traceback (most recent call last):
File "/home/nirmal/qlimp/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 65, in load_config
execfile(opts.config, cfg, cfg)
File "run.sh", line 3
LOGFILE=/var/log/gunicorn/qlimp.log
^
SyntaxError: invalid syntax
Could anyone guide me? Thanks!
Try /etc/gunicorn.d instead of your own sh file
I Configure Django App with Gunicorn and Nginx on EC2
nano /etc/init/site1.conf
description "site1 web server"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /home/scripts/gunicorn_runserver.sh
and in gunicorn_runserver.sh
#!/bin/bash
set -e
LOGFILE=/var/log/nginx/site1.log
NUM_WORKERS=10
# user/group to run as
USER=www-data
GROUP=adm
cd /home/projects/project_name/
# source ../../bin/activate
exec gunicorn_django -w $NUM_WORKERS \
--user=$USER --group=$GROUP --log-level=error \
--log-file=$LOGFILE 2>>$LOGFILE
Nginx conf
upstream app_server_hana {
server localhost:8000 fail_timeout=0;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_site1;
break;
}
}
Finally
/etc/init.d/nginx restart
service site1 start