I have tried to get rid of the error message for over 6 hours, but I can't still figure it out. At First, I had my Django+React project directory at /home/pi/pidjango and I got a 403 Forbidden(css, js) Error message from Nginx, so I searched for a long time and I moved my project (pidjango) to /home/pi/.local/share and it seemed to be fine. Then I got a 502 Bad Gateway Error, and also fixed that (It was a problem from nginx) and I got 403 error again. It is the error about nginx not getting static files(css, js) and I gave chmod 755 (-rwxr-xr-x) and it still doesn't work. Can anybody solve this? Thank you.
I tried this tutorial(but except postgres)
$ sudo tail -F /var/log/nginx/error.log
*3 open() "/home/pi/.local/share/pidjango/static/css/main.js" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /static/frontend/main.js HTTP/1.1", host:"127.0.0.1", referrer: "http://127.0.0.1"
*3 open() "/home/pi/.local/share/pidjango/static/css/index.css" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /static/frontend/index.css HTTP/1.1", host:"127.0.0.1", referrer: "http://127.0.0.1"
*3 open() "/home/pi/.local/share/pidjango/static/css/main.js" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /static/frontend/main.js HTTP/1.1", host:"127.0.0.1", referrer: "http://127.0.0.1"
# nginx (sites-enabled)
server {
listen 80;
server_name localhost 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/pi/.local/share/pidjango;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/.local/share/pidjango
ExecStart=/home/pi/.local/share/pidango/djenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock diary.wsgi:application
[Install]
WantedBy=multi-user.target
gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
Directory
/home/pi/.local/share/pidjango
-diary (folder that contains wsgi.py, settings.py, etc)
-djenv (virtual env)
-frontend (react)
-static (static folder)
-db.sqlite3
-manage.py
I have made sure I gave chmod 755 to all the static files in /static
Can anybody help?
*I have written all the scripts above by hand(not copy-pasting) so there could be typos
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 working at my django project with nginx and gunicorn, as it said here:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04
My site works locally, but when I start nginx and gunicorn server I had
502 Bad Gateway error.
OS is UBUNTU 14.04
I'm trying to make my project working, and reinstall everything as root (I know its bad) - the same mistake.
Here is my "error.log":
2016/04/20 20:15:10 [crit] 10119#0: *1 connect() to
unix:/root/myproject/myproject.sock failed (13: Permission denied)
while connecting to upstream, client: 46.164.23
When i run comand "nginx":
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
My gunicorn.conf
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid root
setgid www-data
chdir /root/myproject
exec myprojectenv/bin/gunicorn --workers 3 --bind unix:/root/myproject/myproject.sock myproject.wsgi:application
Thats my "/etc/nginx/sites-available/myproject"
server {
listen 80;
server_name www.mysite.ru;
error_log /nginx_error.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/root/myproject/myproject.sock;
}
}
I will appreciate your help very much!!!
proxy_pass http://unix:/root/myproject/myproject.sock;
The socket is in the superuser's home folder. That's pretty much inaccessible to all other users including your nginx users. Please more the socket to a different location. /var/log/gunicorn/ is a good place.
Also do i see you running gunicorn as root?. Not recommended.
setuid root
Please use some other user here.
I think you have some other process using port 80, according to your nginx error log.
You'll have to figure out what that process is, and kill it if unneeded. Or use another port.
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'm trying to deploy my django version 1.6 project using nginx and gunicorn. In my server, I added my project nginx file:
error_log /var/log/nginx/myproject-error.log;
access_log /var/log/nginx/myproject-access.log;
server {
listen 80;
server_name <domain_name>;
root <path_to_my_root_project>;
location /static/ {
root <path_to_my_root_project>;
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Schema $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://localhost:8333/;
}
error_page 500 502 503 504 /static/50x.html;
}
In my django settings file, I set static paths:
# Static
STATIC_ROOT = BASE_DIR + '/static'
STATIC_URL = '/static/'
And the I run gunicorn as following:
$ gunicorn_django -b localhost:8333
!!!
!!! WARNING: This command is deprecated.
!!!
!!! You should now run your application with the WSGI interface
!!! installed with your project. Ex.:
!!!
!!! gunicorn myproject.wsgi:application
!!!
!!! See https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/gunicorn/
!!! for more info.
!!!
So when I run a curl on my server curl localhost, I get <h1>Bad Request (400)</h1> and the logs don't say much further than 127.0.0.1 - - [18/Oct/2014:14:38:57 +0200] "GET / HTTP/1.1" 400 37 "-" "curl/7.26.0"
And gunicorn stays still, as if no request is sent to it.
Any ideas?
UPDATE:
I run gunicor: gunicorn myproject.wsgi -b localhost:8333
The curl localhost returns my static/50x.html error page. And I got this in nginx logs:
2014/10/18 15:06:31 [error] 16037#0: *32 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: servername.local.fr, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8333/", host: "localhost"
This one tells you what to do.
!!! You should now run your application with the WSGI interface
!!! installed with your project. Ex.:
!!!
!!! gunicorn myproject.wsgi:application
NGINX is working fine, the gunicorn just isn't receiving your request.
Just to to your project dir and run gunicorn with proper parameters as described here: http://docs.gunicorn.org/en/latest/run.html#gunicorn-django