uwsgi & Django with port 80 - django

If I run uwsgi with uwsgi --ini kb_uwsgi.ini --http :80 it gives me an error:
[uWSGI] getting INI configuration from kb_uwsgi.ini
*** Starting uWSGI 2.0.14 (64bit) on [Thu Jan 26 12:34:51 2017] ***
compiled with version: 5.4.0 20160609 on 06 January 2017 11:55:37
os: Linux-4.4.0-53-generic #74-Ubuntu SMP Fri Dec 2 15:59:10 UTC 2016
nodename: ip-172-31-16-133
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory: /home/ubuntu/webapps/kenyabuzz
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/ubuntu/webapps/kenyabuzz
your processes number limit is 64137
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
bind(): Permission denied [core/socket.c line 769]
the site loads however if i preceed the command with sudo or use port :8000 eg uwsgi --ini kb_uwsgi.ini --http :8000
Now I tried to make the site live with nginx and uwsgi I get a 502 Bad Gateway nginx/1.10.0 (Ubuntu) nginx works okay because the favicon loads and I can access static files with \static\... I know this is Django having an issue rendering. I've tried also loading uwsgi --ini kb_uwsgi.ini --http :8000 without loading the djangoenv and it still loads.
The kb_uwsgi.ini file looks like this:
# kb_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/webapps/kenyabuzz
# Django's wsgi file
module = kb.wsgi
# the virtualenv (full path)
home = /home/ubuntu/webapps/djangoenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /tmp/kb.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
and the kb_nginx.conf like so
# kb.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/kb.sock; # for a file socket
#server 0.0.0.0:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name kenyabuzz.nation.news; # 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/ubuntu/webapps/kenyabuzz/kb/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/webapps/kenyabuzz/kb/static; # your Django project's static files - amend as required
}
location /favicon.ico {
alias /home/ubuntu/webapps/kenyabuzz/kb/static/kb/favicon.ico; # favicon
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/webapps/kenyabuzz/uwsgi_params; # the uwsgi_params file you installed
}
}
The log has:
2017/01/26 13:02:17 [crit] 4065#4065: *10 connect() to unix:///tmp/kb.sock failed (2: No such file or directory) while connecting to upstream, client: 197.232.12.165, server: kenyabuzz.nation.news, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///tmp/kb.sock:", host: "kenyabuzz.nation.news"
uwsgi should create the file automatically (it does) and it deletes it when it's closed using the example above.
Appreciate any help on this.
UPDATE
edited the .ini file and uncommented out the chmod-socket=664 the kb.sock file is still not created on restarting nginx.

For nginx:
# kb.conf
...
# the upstream component nginx needs to connect to
upstream django {
server unix:/tmp/kb.sock;
}
...
For uWSGI:
# kb_uwsgi.ini file
[uwsgi]
...
# the socket (use the full path to be safe
socket = /tmp/kb.sock
chmod-socket = 666
...

Related

Why I am getting the NGINX welcome page instead of my Django application

I have deployed my Django Application on a VPS server. I have configured NGINX and uWSGI server.
Here is my Nginx configuration
# the upstream component nginx needs to connect to
upstream django {
server unix:///root/PIDC/ProjetAgricole/ProjetAgricole.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name my_public_IP_address; # 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 /root/PIDC/ProjetAgricole/media;
}
location /static {
alias /root/PIDC/ProjetAgricole/static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /root/PIDC/ProjetAgricole/uwsgi_params;
}
}
Here is my mysite_uwsgi.ini file
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/PIDC/ProjetAgricole
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /root/my_project/venv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /root/PIDC/ProjetAgricole/ProjetAgricole.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
When I am running the Django application with uwsgi and nginx by using this command:
uwsgi --socket ProjetAgricole.sock --module ProjetAgricole/ProjetAgricole.wsgi --chmod-socket=664
I am getting this output as a result of the command:
(venv) root#vmi851374:~/PIDC/ProjetAgricole# uwsgi --socket ProjetAgricole.sock --module ProjetAgricole/ProjetAgricole.wsgi --chmod-socket=664
*** Starting uWSGI 2.0.20 (64bit) on [Sun Apr 24 18:57:17 2022] ***
compiled with version: 9.4.0 on 17 April 2022 18:54:03
os: Linux-5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022
nodename: vmi851374.contaboserver.net
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory: /root/PIDC/ProjetAgricole
detected binary path: /root/my_project/venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 31601
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address ProjetAgricole.sock fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x55d5d758ada0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
ModuleNotFoundError: No module named 'ProjetAgricole/ProjetAgricole'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 327968, cores: 1)
The content of /root/PIDC/ProjetAgricole/uwsgi_params is:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
When I run my application via the web browser it is displaying the NGINX welcome page instead of my project home page which is supposed to appear.
Are there any mistakes in the configurations files?
Please assist

Permission Denied - Django Deployment on Ubuntu 16.04 with web server Nginx

I am trying to deploy a django website on a Ubuntu 16.04 system using Nginx webserver but I am having issues. I am pretty new to system administration.
ERRORS
Error I get from my nginx error.log file:
connect() to unix:/home/teddycrepineau/contoursandcolors/contoursandcolors.sock failed (13: Permission denied) while connecting to upstream, client: 64.125.191.37, server: 173.255.210.63, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/teddycrepineau/contoursandcolors/contoursandcolors.sock:", host: "173.255.210.63", referrer: "http://173.255.210.63/"
uwsgi status
uwsgi[25361]: thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi[25361]: uwsgi socket 0 bound to UNIX address /home/teddycrepineau/contoursandcolors/contoursandcolors.sock fd 3
uwsgi[25361]: Python version: 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
uwsgi[25361]: !!! Python Home is not a directory: /home/teddycrepineau/Env/contoursandcolors !!!
uwsgi[25361]: Set PythonHome to /home/teddycrepineau/Env/contoursandcolors
uwsgi[25361]: Fatal Python error: Py_Initialize: Unable to get the locale encoding
uwsgi[25361]: ImportError: No module named 'encodings'
uwsgi[25361]: Current thread 0x00007f0ec8429700 (most recent call first):
uwsgi[25361]: Thu Apr 12 13:31:57 2018 - [emperor] curse the uwsgi instance contoursandcolors.ini (pid: 4955)
uwsgi[25361]: Thu Apr 12 13:32:00 2018 - [emperor] removed uwsgi instance contoursandcolors.ini
SET UP
Using Python 3
Ubuntu 16.04
Using Virtualenvwrapper and virtualenv
Virtualenvwrapper set up
echo "export WORKON_HOME=~/Env" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
/etc/uwsgi/sites/contoursandcolors.ini
[uwsgi]
project = contoursandcolors
uid = teddycrepineau
base = /home/%(uid)
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 2
socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true
/etc/nginx/sites-available/contoursandcolor file set up
server {
listen 80;
server_name 173.255.210.63;
location = /favicon.io { access_log off; log_not_found off; }
location /static/ {
root /home/teddycrepineau/contoursandcolors;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/teddycrepineau/contoursandcolors/contoursandcolors.sock;
}
}
/etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown teddycrepineau: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
WHAT I'VE TRIED
Changed default python version from 2.7 to 3.5
Change socket file location from project to run (get me a file not found error)
I looked at quite a few SO post, but none have resolved my issue thus far
From what I read, my issue may be related to virtuaenv location set up, but I am unsure as to what do exactly do to resolve this.
I ended up using gunicorn (instead of uWSGI). It worked fine and was pretty easy to set up. Here is the link to the tutorial I used: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

django nginx uwsgi not working

i ve created a django application not using virtual environment. I ve installed nginx and trying to integrate them via uwsgi application.
Here my configurations files.
[uwsgi]
chdir = /home/elastic/workspace/ES_Brevetti
wsgi-file = ES_Brevetti/wsgi.py
master = true
processes = 5
uid = nginx
gid = nginx
socket = unix:///socket/uwsgi.sock
chmod-socket = 666
vacuum = true
i've created the file /sockect/uwsgi.sock with permission 777
chown nginx:nginx -R /sockect/uwsgi.sock
and below nginx conf file:
upstream django {
server unix:///socket/uwsgi.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
server {
listen 80;
server_name 10.184.2.231;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
charset utf-8;
location /static/ {
alias /home/elastic/workspace/ES_Brevetti;
}
location / {
include uwsgi_params;
uwsgi_pass unix:///socket/uwsgi.sock;
}
}
When i launch "systemctl start nginx" nginx is started with error:
connect() to unix:///socket/uwsgi.sock failed (111: Connection refused) while connecting to upstream,
When i run uwsgi --ini /etc/uwsgi/sites/ES_Brevetti.ini it doesnt run with error:
.....
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
bind(): Permission denied [core/socket.c line 230]
What i am doing wrong? On google i can only see configurations with VENV while i am not using virtual environment.
The docs (which I'm guessing you were following) suggest that the socket configuration option in your uwsgi ini file should take a path to the socket, not a URL. Could you try changing:
[uwsgi]
...
socket = unix:///socket/uwsgi.sock
to
[uwsgi]
...
socket = /socket/uwsgi.sock
The solution was change the socket = unix:///socket/uwsgi.sock to socket = /socket/uwsgi.sock in the uwsgi.ini file. I dont know why in some doc it appears the double slash for escaping.

deploying django app on nginx with uwsgi returns a 502 - permission denied

I'm trying to setup a simple django app on nginx with uswgi.
When I send a request, it returns a 502 bad gateway.
Here is my uwsgi.ini
[uwsgi]
master = true
socket = /usr/share/nginx/firstsite/nginx.sock
chdir = /usr/share/nginx/firstsite
wsgi-file = /usr/share/nginx/firstsite/firstsite/wsgi.py
chmod-socket = 664
vacuum = true
uwsgi output:
uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.11.2 (64bit) on [Sat Jun 18 15:09:30 2016] ***
compiled with version: 4.8.3 20140911 (Red Hat 4.8.3-9) on 02 December 2015 19:47:02
os: Linux-3.10.0-327.4.4.el7.x86_64 #1 SMP Tue Jan 5 16:07:00 UTC 2016
nodename: centos_prod
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /usr/share/nginx/firstsite
detected binary path: /root/venv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /usr/share/nginx/firstsite
your processes number limit is 94006
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /usr/share/nginx/firstsite/nginx.sock fd 3
Python version: 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x21f6ce0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x21f6ce0 pid: 3421 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3421)
spawned uWSGI worker 1 (pid: 3426, cores: 1)
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
upstream django {
server unix:///usr/share/nginx/firstsite/nginx.sock;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/firstsite;
client_max_body_size 300M; # adjust to taste
location /media {
alias /usr/share/nginx/firstsite/media;
}
location /static {
alias /usr/share/nginx/firstsite/static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /usr/share/nginx/firstsite/uwsgi_params; # the uwsgi_params file you installed
}
}
}
I'm not using a virtualenv and my manage.py is in /usr/share/nginx/firstsite/
I'm able to run the app without nginx on uwsgi with:
uwsgi --wsgi-file /usr/share/nginx/firstsite/firstsite/wsgi.py --http :80
but what I'm trying to do is let nginx use port 80 and uwsgi use file socket.
Please help.
Update:
I don't know why I was not seeing any error before, but now nginx error log says:
nginx Permission denied while connecting to upstream
so looking at this
I tried adding these options to uwsgi:
--uid root --gid www-data
so the socket file is owned by root which is part of the www-data group.
I'm still getting a permission error.
[crit] 6804#0: *1 connect() to unix:///usr/share/nginx/firstsite/nginx.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.168.201, server: _, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///usr/share/nginx/firstsite/nginx.sock:"
Update:
I'm still running into permission issue.
I created a django user and a django group.
Both nginx and django users are members of django group.
Changed ownership of all the folders under
/usr/share/nginx/firstsite
owned by django.
set --gid and --uid to django.
ran
uwsgi --ini /usr/share/nginx/firstsite/uwsgi.ini
as root. I got the permission error.
ran:
runuser -l django -c "uwsgi --ini /usr/share/nginx/firstsite/uwsgi.ini"
the same.
Note that /usr/share/nginx is owned by root.
I tried changing the nginx config file:
uwsgi_pass unix:///usr/share/nginx/firstsite/nginx.sock;
restarted nginx and ran uwsgi. I'm getting this error:
connect() to unix:///usr/share/nginx/firstsite/nginx.sock failed (13: Permission denied) while connecting to upstream, client: 192.123.123.123, server: _, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///usr/share/nginx/firstsite/nginx.sock:"
I also had to setup a python virtualenv and set home in uwsgi.ini to detect django but I cannot get passed this socket failure.

Permission denied Accessing static files Nginx +uwsgi +Django

I have setup Django project on CentOS 6.5 with Nginx and uwsgi.
I am Getting error while accessing static content as below (/var/log/nginx/error.log)-
2015/11/02 19:05:37 [error] 29701#0: *52 open() "/home/amar/workspace/myproj/config/static/rest_framework/js/default.js" failed (13: Permission denied), client: 172.29.100.104, server: myapi.dev, request: "GET /static/rest_framework/js/default.js HTTP/1.1", host: "myapi.dev", referrer: "http://myapi.dev/api/v1/datasets/"
My /etc/nginx/conf.d/virtual.conf is as shown below -
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/uwsgi.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
#
#API
#
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name myapi.dev; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
autoindex on;
alias /home/amar/workspace/myproj/config/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
Here is my uwsgi.ini file :
[uwsgi]
chdir = /home/amar/workspace/myproj
#home = %(base)/.virtualenvs/myproj
module = config.wsgi:application
home = /home/amar/.virtualenvs/myproj
master = true
processes = 3
socket = /tmp/uwsgi.sock
chmod-socket = 777
vacuum = true
Could someone point me in the right direction?
It took time but I've fixed the problem myself. Changed the user from amar to root and set static folder permission to 666. Hope it helps someone in future.
Probably related to SELinux. You will need to allow HTTPD scripts and modules to connect to the network.
setsebool httpd_can_network_connect on -P