Okay guys, so this is the error I'm stuck on for days.I am trying to get my django+nginx+uwsgi work properly but it's been 3 days and it's still not working, I tried a lot,rebuilded the server,tried python2 and 3 and blah blah blah but the error I hook on is the one above.
Here's my uwsgi file
[uwsgi]
project = salesproject
base = /home/ubaid
logto = /var/log/uwsgi/error.log
chdir = %(base)/%(project)
chmod-socket= 777
plugins = python3
wsgi-file = %(project)/wsgi.py
master = true
processes = 2
socket = /var/uwsgi/%(project).sock
virtualenv = %(base)/%(project)/venv
vacuum = true
uid = www-data
gid =www-data
Here's my Nginx conf located in /etc/nginx/sites-available/
server {
listen 80;
server_name 72.14.184.213;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubaid/salesproject;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/var/uwsgi/salesproject.sock;
}
}
Now see how the problem unfolds in steps,
First when I go to my website,
1.It gives me an error like this
connect() to unix:/home/ubaid/var/uwsgi/salesproject.sock failed (2: No such file or directory)
Then I manually make the .sock file by sudo nano .sock so when I run my website,It gives me an error like this
connect() to unix:/var/uwsgi/salesproject.sock failed (13: Permission denied)
Then to resolve this I run sudo chmod 777 .sock so now when I check my file it gives the final error,The big goblin one
and that is
connect() to unix:/var/uwsgi/salesproject.sock failed (111: Connection refused)
Another thing when i run uwsgi --ini, I check my uwsgi logs and the logs are printed but when I go to my website i don't see anything in logs,
Can it be because in reality uwsgi and nginx are not actually talking to each other or something else,whatever it is please help me in resolving this.
Related
I was following this tutorial and successfully ran the project manually. However, after setting up nginx and systemd, it says 502 Bad Gateway.
I have looked through other similar threads to no avail.
I see that my gunicorn workers are running by doing ps -ax | grep gunicorn.
my nginx configuration:
server {
listen 8000;
server_name 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
}
}
and the systemd file:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/myproject
ExecStart=/home/ubuntu/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
The contents of /var/log/nginx/error.log:
2017/02/18 17:57:51 [crit] 1977#1977: *6 connect() to unix:/home/ubuntu/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 10.0.2.2, server: 172.30.1.5, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/myproject/myproject.sock:/", host: "localhost:8000"
Manually running /home/ubuntu/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application is also working. That is, sock file is created.
I feel like I missing something very simple.
What could I be doing wrong?
There could be an ordering thing here - nginx is probably starting before gunicorn, so the socket is not yet there to connect. You should add gunicorn.service to the After directive in the nginx systemd file.
My nginx and uwsgi configuration perfectly working for first one or two requests . Then nginx showing 502 Bad Gateway .When I restart the uwsgi service , same thing happens again. I am using Ubuntu 16.04. Here is my all conf and error log:
nginx conf:
upstream book {
server unix:///tmp/book.sock;
}
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
......
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
charset utf-8;
client_max_body_size 100M;
access_log /var/log/nginx/example.com_access.log;
error_log /var/log/nginx/example.com_error.log;
location /media {
alias /home/prism/prod/example.com/media;
}
location /static {
alias /home/prism/prod/example.com/static;
}
location / {
uwsgi_pass book;
include /etc/nginx/uwsgi_params;
}
}
/var/log/nginx/example.com_error.log
2016/05/25 17:44:26 [error] 5230#5230: *214 connect() to unix:///tmp/book.sock failed (111: Connection refused) while connecting to upstream, client: 27.*.*.*, server: example.com, request: "GET / HTTP/2.0", upstream: "uwsgi://unix:///tmp/book.sock:", host: "example.com"
hiren.ini
[uwsgi]
chdir=/home/prism/prod/example.com
home = /home/prism/prod/example.com/.env
module=hiren.wsgi
master=True
process = 5
pidfile=/tmp/book.pid
socket= /tmp/book.sock
vacuum=True
max-requests=5000
daemonize=/home/prism/prod/example.com/hiren.log
uid = www-data
gid = www-data
die-on-term = true
and service file:
[Unit]
Description=uWSGI instance to serve example.com
[Service]
User=prism
ExecStart=/bin/bash -c 'cd /home/prism/prod/example.com; source .env/bin/activate; uwsgi --ini hiren.ini'
[Install]
WantedBy=multi-user.target
Solved the problem by tweaking systemd script
[Unit]
Description=uWSGI instance to serve example.com
[Service]
ExecStart=/bin/bash -c 'su prism; cd /home/prism/prod/example.com; source .env/bin/activate; uwsgi --ini hiren.ini'
[Install]
WantedBy=multi-user.target
If you are able to run uwsgi and your .ini file within your virtual environment, and nginx is starting adequately, the issue is then with a systemd file.
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 trying to use Flask app on uwsgi/nginx.
Following
http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
and
http://www.markjberger.com/flask-with-virtualenv-uwsgi-nginx/, I could make wiki.ini file,
[uwsgi]
vhost = true
socket = /tmp/flask_app.sock
venv = /home/ubuntu/webapp/flask/hello/.env
chdir = /home/ubuntu/webapp/flask/hello
module = flaskapp
callable = app
chmod-socket = 666
I checked the wiki.ini file works fine with uwsgi --ini wiki.ini.
Then, I tried to start the Flask app when booting.
From sudo update-rc.d uwsgi enable, I could start the uwsgi service in booting time, and copied the wiki.ini file in /etc/uwsgi/apps-enabled directory.
This is the conf file for nginx.
server {
listen 80;
server_name wiki.example.com;
access_log /var/log/nginx/uwsgi_access.log;
error_log /var/log/nginx/uwsgi_error.log;
location / { try_files $uri #riki; }
location #riki {
include uwsgi_params;
uwsgi_pass unix:/tmp/flask_app.sock;
}
error_page 404 /404.html;
}
However, when I rebooted my ubuntu server, the Flask app isn't working.
I checked the error log to find this error message.
2015/11/07 17:48:17 [crit] 1055#0: *1 connect() to
unix:/tmp/flask_app.sock failed (2: No such file or directory)
while connecting to upstream, client: 68.203.30.28, server: wiki.example.com,
I created the /tmp/flask_app.sock file and run chown -R www-data:www-data /tmp/flask_app.sock to make the application working.
> touch /tmp/flask_app.sock
> sudo chown www-data:www-data /tmp/flask_app.sock
> sudo service uwsgi restart
> sudo service nginx restart
However, I had another connection refuse error.
2015/11/07 17:50:38 [error] 1055#0: *4 connect() to
unix:/tmp/flask_app.sock failed (111: Connection refused) while
connecting to upstream, client: 68.203.30.28,
server: wiki.example.com, request: "GET / HTTP/1.1",
upstream: "uwsgi://unix:/tmp/flask_app.sock:", host: "wiki.example.com"
What might be wrong? How to teach uwsgi to create the unix domain socket? Also, how to make the connection work? I use ubuntu 14.04.
EDIT
Removing the /tmp/flask_app.sock and run uwsgi --ini /etc/uwsgi/apps-enabled/wiki.ini makes the app working fine.
The main issue seems to be from the uwsgi service; it just doesn't work.
I found another way to start uwsgi at startup: upstart and uwsgi --emperor from http://uwsgi-docs.readthedocs.org/en/latest/Upstart.html and http://upstart.ubuntu.com
The process is just make a flask.conf file in /etc/init directory. uwsgi --emperor controls all the ini files in the uwsgi directory.
# simple uWSGI script
# http://uwsgi-docs.readthedocs.org/en/latest/Upstart.html
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
exec uwsgi --emperor /etc/uwsgi/apps-enabled
I also had to sudo update-rc.d uwsgi disable so that uwsgi service should be disabled.
I also found this site http://flaviusim.com/blog/Deploying-Flask-with-nginx-uWSGI-and-Supervisor/ for invoking uswgi at startup, but I didn't test it.
I followed this post to serve my django project. The project runs well with manage.py runserver and I want to set it up for production. Here are my setting files:
nginx.conf:
upstream django {
server /tmp/vc.sock;
#server 10.9.1.137:8002;
}
server {
listen 8001;
server_name 10.9.1.137;
charset utf-8;
client_max_body_size 25M;
location /media {
alias /home/deploy/vc/media;
}
location /static {
alias /home/deploy/vc/static;
}
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
}
}
uwsgi.ini:
[uwsgi]
chdir = /home/deploy/vc
wsgi-file = vc/wsgi.py
master = true
processes = 2
#socket = :8002
socket = /tmp/vc.sock
chmod-socket = 666
vacuum = true
If I use TCP port socket (server 10.9.1.137:8002 and socket = :8002), it's going to be fine. However if I comment them out and use Unix sockets(server /tmp/vc.sock and socket = /tmp/vc.sock), the server will return 502 error. How should I fix it?
EDIT
Here's the nginx error log when I run /etc/init.d/nginx restart
nginx: [emerg] invalid host in upstream "/tmp/vc.sock" in /etc/nginx/conf.d/vc.conf:2
nginx: configuration file /etc/nginx/nginx.conf test failed
And this is the warning when I run uwsgi --ini vc/uwsgi.ini:
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Can't I run uWSGI as root?
roberto's comment should be an answer!
the syntax in nginx for unix socket path is wrong, you need to prefix
it with unix:
Check your nginx error log, very probably is telling you it does not have permissions to the socket. Unix sockets honour file system permissions, so nginx must have write privilege over the socket file. Short answer: 664 is not enough, you need 666