django on nginx + uwsgi / static files don't show - django

So I want to run django on nginx with uwsgi. I installed everything, changed the ALLOWED_HOSTS and STATIC_ROOT in the settings.py and ran "collectstatic". With the terminal I can see that the admin-static-files were put exactly where I wanted them to be put. Yet I don't see the static files when I go to domainxy.de/admin . After my the installation I did this:
sudo mkdir -p /etc/uwsgi/sites
sudo nano /etc/uwsgi/sites/mysite.ini
with
####mysite.ini####
[uwsgi]
project = mysite
uid = root
base = /%(uid)
chdir = %(base)/%(project)
module = %(project).wsgi:application
master = true
processes = 5
socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true
and
sudo nano /etc/systemd/system/uwsgi.service
with
###uwsgi.service####
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown root:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
and
sudo nano /etc/nginx/sites-available/mysite.conf
with
####mysite.conf####
server {
listen 80;
server_name domainxy.de www.domainxy.de xx.xxx.xxx.xxx;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /root/mysite/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/mysite.sock;
}
}
and
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled
sudo systemctl restart nginx
sudo systemctl start uwsgi
sudo ufw allow 'Nginx Full'
Where did I make the mistake? I did change the folders in "mysite.conf" multiple times but nothing helped so far. Thanks for helping. Don't judge me that I am a newbie and also not that I used the root-user for simplifying here.
Edit: my django-project is in /root/mysite/ and the settings.py is
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Related

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.

Error on deploying django + uwsgi + nginx

I'm trying to deploy a django project with uWSGI and nginx. I have the next configuration files:
uwsgi.ini
[uwsgi]
#Django-related settings
chdir = /home/amlalchy/api_aml_platform
module = api.wsgi
#static-map = /static=/home/amlalchy/api_aml_platform/services/static
#Process-related settings
master = true
processes = 10 
# The socket(use the full path to be safe)
socket = /tmp/aml.sock
chmod-socket = 664
uid = www-data
gid = www-data 
# Clear environment on exit
vacuum = true
uwsgi.service
[Unit]
Description=uWSGI Emperor service
After=network.target
[Service]
#User=amlalchy
#Group=www-data
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown amlalchy:www-data /run/uwsgi'
ExecStart=/bin/bash -c 'cd /home/amlalchy/api_aml_platform && source /home/amlalchy/anaconda3/bin/activate AmlPlatform && uwsgi --ini /etc/uwsgi/sites/uwsgi.ini'
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
api.conf for the nginx configuration
server {
listen 80;
server_name server.prueba.com;
charset utf-8;
client_max_body_size 128M;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /home/amlalchy/api_aml_platform/services;
}
location /media {
root /home/amlalchy/api_aml_platform;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/aml.sock;
uwsgi_read_timeout 300s;
uwsgi_send_timeout 300s;
}
}
I'm getting this error in /var/log/enginx/error.log :
2020/01/13 10:30:12 [error] 12641#12641: *1 open() "/home/amlalchy/api_aml_platform/services/static/css/global.css" failed (2: No such file or directory), client: 127.0.0.1, server: server.prueba.com, request: "GET /static/css/global.css HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/api/login/
I am newbie and I don't know what I'm missing because the static files exists... so I would be really grateful for any tip on my error.
Thanks in advance.
Where are your static files (css/js/images)? How do you deploy them?
As #ruddra suggested, how do you run your?
./manage.py collectstatic

Gunicorn-Supervisor Django Setup Issues

I am attempting to setup a DigitalOcean Droplet to hold a Django application, and I am following this overview: https://simpleisbetterthancomplex.com/tutorial/2016/10/14/how-to-deploy-to-digital-ocean.html
The application runs fine when I execute it via: python manage.py runserver 0.0.0.0:8000
However, once the application is started via sudo supervisorctl restart all and I run sudo supervisorctl status, I get this, but the app doesn't work when I got to the correct URL:
site#SiteDroplet:~$ sudo supervisorctl status Site
Site RUNNING pid 3071, uptime 0:00:19
Can someone help?
Here is my directory structure:
site#SiteDroplet:~$ cd ../
site#SiteDroplet:/home$ cd ../
site#SiteDroplet:/$ ls
bin dev home initrd.img.old lib64 media opt root sbin srv tmp var vmlinuz.old
boot etc initrd.img lib lost+found mnt proc run snap sys usr vmlinuz
site#SiteDroplet:/$ ^C
site#SiteDroplet:/$ cd home
site#SiteDroplet:/home$ ls
site
site#SiteDroplet:/home$ cd site
site#SiteDroplet:~$ ls
Site bin include lib local logs run share
site#SiteDroplet:~$ cd bin
site#SiteDroplet:~/bin$ ls
activate activate.fish easy_install gunicorn_start pip2 python python2 wheel
activate.csh activate_this.py easy_install-2.7 pip pip2.7 python-config python2.7
site#SiteDroplet:~/bin$ cd ../
site#SiteDroplet:~$ cd Site
site#SiteDroplet:~/Site$ ls
Site app.yaml forms interface_login interface_management interface_resident interface_security objects requirements.txt utils
README.md cron.yaml interface_admin interface_maintenance interface_onsite interface_root manage.py objects_client templates
site#SiteDroplet:~/Site$ Site is my Django Project Directory
Here is my gunicorn_start file
#!/bin/bash
NAME="Site"
DIR=/home/site/Site
USER=site
GROUP=site
WORKERS=3
BIND=unix:/home/site/run/gunicorn.sock
DJANGO_SETTINGS_MODULE=Site.settings
DJANGO_WSGI_MODULE=Site.wsgi
LOG_LEVEL=error
cd $DIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DIR:$PYTHONPATH
exec ../bin/gunicorn_start ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $WORKERS \
--user=$USER \
--group=$GROUP \
--bind=$BIND \
--log-level=$LOG_LEVEL \
--log-file=-
Here is my supervisor config
[program:Site]
command=/home/site/bin/gunicorn_start
user=site
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/site/logs/gunicorn-error.log
Nginix config
upstream app_server {
server unix:/home/site/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
# add here the ip address of your server
# or a domain pointing to that ip (like example.com or www.example.com)
server_name 157.230.230.54;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/site/logs/nginx-access.log;
error_log /home/site/logs/nginx-error.log;
location /static/ {
alias /home/site/static/;
}
# checks for static file, if not found proxy to app
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
Latest gunicorn-error.log
/home/site/Site/../bin/gunicorn_start: line 16:
/home/site/Site/../bin/gunicorn_start: Argument list too long
/home/site/Site/../bin/gunicorn_start: line 16: /
/home/site/Site/../bin/gunicorn_start: Success
I don't know why you have such complex setup, here is my account website conf, works like a charm:
[group:accounting]
programs=accounting_web
[program:accounting_web]
command = /home/web/.virtualenvs/accounting/bin/gunicorn accounting.wsgi --workers=1 --threads=4 -b unix:/tmp/gunicorn_accounting.sock --log-level=DEBUG --timeout=120
directory = /home/web/accounting
user = web
environment=PATH="/home/web/.virtualenvs/accounting/bin"
Probably, you have a syntax error on gunicorn_start script. My suggestion is to use a smaller command line directly on supervisor .conf.
On nginx.conf:
1) try to change the Servername parameter with a valid URL, for example:
exactestate.com
2) change the sock configuration to TCP:
upstream gunicorn_panel {
# For a TCP configuration:
server 127.0.0.1:9000 fail_timeout=0; }
server {
listen 8080;
On supervisor.conf, instead of use the gunicorn_start file, try to write all command directly on the command variable:
[program:powerpanel]
command=/home/web/.virtualenvs/accounting/bin/gunicorn powerpanel.wsgi -b 127.0.0.1:9000 -w1 --pythonpath=/home/exactestate/ExactEstate --error-logfile=/home/exactestate/logs/gunicorn-error.log
user=webapp
autostart=true
autorestart=unexpected
startsecs=1
startretries=3
redirect_stderr=true

Linode Django uwsgi Nginx

The following setup is only letting me see the default Nginx html page. How can I get to Django?
I've been following Linode's documentation on how to set this up (and numerous other tutorials), but they don't use systemd, so things are a bit different.
https://www.linode.com/docs/websites/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04
I am using Linode with Fedora24. I have installed my virutalenv at
/home/ofey/djangoenv and activated it,
Django is installed using pip at
/home/ofey/qqiProject
Into the virtualenv I've installed uwsgi.
Firstly,
/etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/home/ofey/djangoenv/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
This executes,
/etc/uwsgi/sites/qqiProject.ini
[uwsgi]
project = qqiProject
base = /home/ofey
chdir = %(base)/%(project)
home = %(base)/djangoenv
module = %(project).wsgi:application
master = true
processes = 2
socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true
Also,
/etc/nginx/sites-available/qqiProject
server {
listen 80;
server_name qqiresources.com www.qqiresources.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django/qqiProject;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/django/qqiProject/qqiProject.sock;
}
}
The file /etc/nginx/nginx.conf has not been changed.
The user is ofey, I've used,
$ sudo systemctl daemon-reload
$ sudo systemctl restart nginx
$ sudo systemctl start uwsgi.service
Started Django with,
$ python manage.py runserver
To Django's settings.py I turned off debugging and added a host
DEBUG = False
ALLOWED_HOSTS = ['qqiresources.com']
I have also created a symbolic link,
sudo ln -s /etc/nginx/sites-available/qqiProject /etc/nginx/sites-enabled
Any help would be greatly appreciated,
Thanks
The following is working for me, however I am not sure if this is the correct or most efficient way to do it, especially where the sockets are concerned.
systemd runs the uwsgi.service and can be started with,
$ sudo systemctl start uwsgi.service
Sometimes it is necessary to reload systemd using,
$ sudo systemctl daemon-reload
/etc/sysemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/home/ofey/djangoenv/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
This calls the binary inside my django virtual environment directory with,
ExecStart=/home/ofey/djangoenv/bin/uwsgi
and also takes us to /etc/uwsgi/sites where the uwsgi configuration files is called djangoForum.ini
/etc/uwsgi/sites/djangoForum.ini
[uwsgi]
project = djangoForum
base = /home/ofey
chdir = %(base)/%(project)
home = %(base)/djangoenv
module = crudProject.wsgi:application
master = true
processes = 2
socket = 127.0.0.1:3031
chmod-socket = 664
vacuum = true
Django is at /home/ofey/djangoForum and my django project is at /home/ofey/djangoForum/crudProject
/etc/nginx/nginx.conf
events {
worker_connections 1024;
}
http{
upstream django {
# connect to this socket
# server unix:///tmp/uwsgi.sock; # for a file socket
server 127.0.0.1:3031; # for a web port socket
}
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name example.com; # substitute your machine's IP address or FQDN
charset utf-8;
#Max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ofey/djangoForum/fileuploader/uploaded_files; # your Django project's media files
}
location /static {
alias /home/ofey/djangoForum/noAppBoundStaticDirectory; # your Django project's static files
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
# uwsgi_pass 127.0.0.1:3031;
include /etc/nginx/uwsgi_params; # or the uwsgi_params you installed manually
}
}
}
nginx can be turned on with,
$ sudo systemctl start nginx.service
'start' can be replaced with, 'restart' or 'stop'.
These configuration files for uwsgi and nginx worked for me.
Useful links:
https://gist.github.com/evildmp/3094281
https://www.linode.com/docs/websites/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04

Gunicorn is not binding my domain by using ".sock" file

I am trying to host multiple sites on VPS using sock file but the problem is that I can't see the website up and running using gunicorn sock. But I can't see my website live. I need to know how do I change the following screen showing my app binds with particular port instead of sock file or if it has to be a sock file then why I can't see it in browser at mydomain.com.
Gunicorn upscript is as follows:
#!/bin/bash
NAME="dressika" # Name of the application
DJANGODIR=/django/mydomain # Django project directory
SOCKFILE=/django/mydomain/run/gunicorn.sock # we will communicte using this unix socket
USER=django # the user to run as
GROUP=django # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=mydomain.settings # which settings file should Django use
DJANGO_WSGI_MODULE=mydomain.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ../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 ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--bind=unix:$SOCKFILE \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--log-file=-
With above settings gunicorn startup script runs fine but I couldn't see my site live on browser or client end. I guess I need to bind it with some port. I am not sure if my assumption is correct. My app settings.py shows in ALLOWED_HOSTS=['mydomain.com', 'www.mydomain.com]. Still the url isn't working.
My Nginx settings are:
upstream mydomain_server {
server 127.0.0.1:9500 fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
root /home/django/mydomain;
index index.html index.htm;
client_max_body_size 4G;
server_name mydomain.com www.mydomain.com;
keepalive_timeout 5;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2|woff|ttf)$ {
expires 365d;
}
# Your Django project's media files - amend as required
location /media {
alias /home/django/mydomain/media/;
}
# your Django project's static files - amend as required
location static/static-only {
alias /home/django/mydomain/static-only/;
}
# Django static images
location /static/mydomain/images {
alias /home/django/mydomain/static-only/images/;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://mydomain_server;
proxy_connect_timeout 60s;
}
}
I've also tried binding /home/django/mydomain/run/gunicorn.sock with upstream server instead of IP:Port but still couldn't see the site up and running.
I have the same problem, .sock doesn't create. This method helps me.
Prerequests:
Installed nginx: when you type in browser 127.0.0.1 - obtain "Wellcome to nginx...".
You install python2 or 3 no matter, and other stuffs: pip, django, gunicorn...
You installed and settled virtualenv. (in my case, I use virtualenvwrapper - this is good staffs, saves all you env in one folder: /home/user/.virtualenvs/)
You created django project, and when: python manage.py runserver -
you obtain "It works..." - this good news.
When you type gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application - you have the same result, as a step 4.
Next step for setting you dj.project throgh gunicorn to nginx:
You create file in /etc/systemd/system/any_file_name.service - you can named this file as you want, at DO - it names as gunicorn.service.
my method:
$cd /etc/systemd/system
$sudo touch gunicorn.service
and open it your favorite text editor
$sudo subl gunicorn.service
Inside it you write:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=vetal
Group=www-data
WorkingDirectory=/var/www/apple.net
ExecStart=/home/vetal/.virtualenvs/univ/bin/gunicorn --workers 3 --bind unix:/var/www/apple.net/mysite/mysite.sock mysite.wsgi:application
[Install] WantedBy=multi-user.target
ExecStart - what will be started by nginx, when your virualenv will be turned off. Do you remember, gunicorn was install through pip, when your env was turn on ?
-- bind unix:... - this address WHERE your .sock will created! Pay attention for this!
CHECK EVERY LETTER!TWISE!!! (of course with you links..)
Type:
$ls -l
if you see in attributes to your 'gunicorn.service' something:
-rw-r--r-- 1 root root 0 Янв 12 11:48 gunicorn.service
this means - this file is not executable, and you .sock - file will never created! Make next:
$sudo chmod 755 gunicorn.service
and check:
$ls -l
if you get:
-rwxr-xr-x 1 root root 305 Янв 11 19:48 gunicorn.service
this good! Everything allright!
Then you created nginx block, in /etc/nginx/site-available/ it likes next:
server {
listen 80;
root /var/www/apple.net;
server_name apple.net;
location = /favicon.ico { access_log off; log_not_found off; }
location = /static/ {
alias /var/www/apple.net/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/apple.net/mysite/mysite.sock;
} }
Notice: proxy_pass - must be identicaly correct with folder where .sock file created in gunicorn.service!
Copies this file to /sites-enable
$ sudo cp /etc/nginx/site-avaliable/apple.net /etc/nginx/site-enable
I don't have any domaine, so I modify my /etc/hosts file, add row:
127.0.0.10 apple.net
Very important steps!!!
$pkill gunicorn - this step kill daemon, which you may started before. gunicorn in this case, means name of file which you created before with .service extention, in /etc/systemd/system - folder.
Start gunicorn.service daemon:
$sudo systemctl start gunicorn
$sudo systemctl enable gunicorn
Start(or restart nginx)
$sudo /etc/init.d/nginx (re)start
Check your domane name in browser.
Since gunicorn is running on a socket, you need to bind to that socket, not to a port, in the upstream section.
upstream mydomain_server {
server unix:/home/django/mydomain/run/gunicorn.sock fail_timeout=0;
}
I have nginx serving up a .sock file from gunicorn. My typical gunicorn call looks like this:
exec gunicorn \
--pid /web/gunicorn.pid \
--workers '4' \
--name myapp \
--chdir /src/myapp \
--bind unix:/web/.sock \
--log-file=- \
myapp.wsgi:application
My nginx conf for / looks like this; the main difference seems to be that your proxy_pass statement doesn't point to the .sock file:
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:/web/.sock;
}