nginx django uwsgi page not found error - python

I am trying to setup uwsgi and Django on Nginx but showing page not found error and error logs are empty.
I cannot identify the error because the error logs are empty.
Error log /var/log/nginx/error.log:
-rw-r--r-- 1 www-data root 0 Feb 26 12:31 error.log
uswgi is running properly because I tested this on following method:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir
/home/flybegins/python/django/sample -w sample.wsgi
virtual host
server {
listen 80;
server_name test.aaaaaaa.com;
error_log /var/log/nginx/error.log
location /static/ {
root /home/flybegins/python/django/sample/
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
} }
Virtual host permission:
-rw-r--r-- 1 root root 333 Feb 27 08:54 test.aaaa.com
Thanks in advance!

You need to install python plugin for uwsgi
sudo apt-get install uwsgi-plugin-python
or for python 3
sudo apt-get install uwsgi-plugin-python3

You are running your project using port 8080 with this code:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir /home/flybegins/python/django/sample -w sample.wsgi
And you are trying to bind NGINX to a socket file that doesn't exist using this configuration:
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
}
That why it doesn't work.

I did two mistakes One is nginx virtual host configuration and another one is socket permission error
uWSGI Configuration
[uwsgi]
project = prd
base = /home/flybegins/python/django
chdir = %(base)/%(project)
home = %(base)/venv
module = %(project).wsgi:application
master = true
processes = 5
gid = www-data
uid = www-data
socket = /var/uwsgi/%(project).sock
chmod-socket = 664
vacuum = true
To create the space for the socket to exist, you just have to pick a persistent directory (e.g. not /run or /tmp) and make www-data (the user nginx runs as) the owner of it, as such:
$ sudo mkdir /var/uwsgi
$ sudo chown www-data:www-data /var/uwsgi
My nginx virtual host configuration
server {
listen 80;
server_name testserver1.com;
access_log /home/flybegins/log/python/testserver1.com/access.log;
error_log /home/flybegins/log/python/testserver1.com/error.log error;
location /static {
alias /home/flybegins/python/django/prd/static_files/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/var/uwsgi/prd.sock;
}
}

Related

Receiving permission denied error with Docker, nginx, uwsgi setup. I can manually write files inside the container

I'm trying to setup a flask application to run in production using docker, nginx, and uwsgi.
Docker file:
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /flask_app
RUN apt-get clean \
&& apt-get -y update
RUN apt-get -y install nginx \
&& apt-get -y install python3-dev \
&& apt-get -y install build-essential
EXPOSE 8080
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt --src /usr/local/src
COPY . .
COPY nginx.conf /etc/nginx
RUN chmod +x ./start.sh
CMD ["./start.sh"]
# CMD [ "python3", "-m" , "flask", "--app", "main", "run", "--host=0.0.0.0", "-p", "5001"]
uwsgi.ini
`[uwsgi]
module = main:app
uid = www-data
gid = www-data
master = true
processes = 5
#socket = /tmp/uwsgi.socket
socket = 127.0.0.1:8080
chmod-socket = 666
vacuum = true
die-on-term = true`
nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
access_log /dev/stdout;
error_log /dev/stdout;
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;
index index.html index.htm;
# Configuration containing list of application servers
upstream uwsgicluster {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
# .
}
server {
listen 8888 default_server;
listen [::]:8888 default_server;
server_name localhost;
root /var/www/html;
location / {
include uwsgi_params;
uwsgi_pass uwsgicluster;
uwsgi_read_timeout 1h;
uwsgi_send_timeout 1h;
proxy_send_timeout 1h;
proxy_read_timeout 1h;
}
}
}
The web application runs successfully until I try to upload an image to display. Saving it to an uploads folder under static/uploads returns this error:
PermissionError: [Errno 13] Permission denied: 'static/uploads/timyoutube.jpg'
I expect to not receive this error as it works using flask run in development mode.
The issue turned out to be a permissions issue with the user www-data not having write permissions.
I changed the owner of the workdir to www-data and that fixed the issue
RUN chown -R www-data:www-data /flask_app

Need to run django application in a domain name without specific port

I am newbie to Django recently I created a Django app and I uploaded to the server. I assigned a domain name to it. each time I run the server I need to type xyz.com:8000 to see my website. Is there any way to resolve this issue? Also, I have doubt. Do I need to type python manage.py runserver 0:8000 to launch the website or it's just run automatically like PHP.
You should set up a web server!
The web server is required for any site to work. Currently the most popular are Apache and NGINX. It is the web server that responds to user requests. We need to ensure the interaction of the web server and the python application. Most popular solutions:
uwsgi
gunicorn
Consider an example with Nginx and Gunicorn:
Let's start by installing the Gunicorn module in a virtual environment:
pip install gunicorn
Configure the gunicorn service settings for our project:
sudo nano /etc/systemd/system/gunicorn.service
/etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=my_user
Group=www-data
WorkingDirectory=/home/project_dir/project
ExecStart=/home/project_dir/project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/project_dir/project/project.sock project.wsgi
[Install]
WantedBy=multi-user.target
We enable and run the gunicorn service, check its status:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn
If all is well, install the nginx web server:
sudo apt install nginx
Configure the project site parameters:
sudo nano /etc/nginx/sites-available/project
/etc/nginx/sites-available/project:
server {
listen 80;
server_name <server IP or domain name>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/project_dir/project;
}
location /media/ {
root /home/project_dir/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/project_dir/project/project.sock;
}
}
We use Nginx as a proxy for the gunicorn Python server:
proxy_pass http://unix:/home/project_dir/project/project.sock;
Create a link in the allowed sites folder “/etc/nginx/sites-enabled”:
sudo ln -s /etc/nginx/sites-available/project/etc/nginx/sites-enabled
We restart the Nginx service and add permissions to the firewall:
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'
Done! You can check the operation of our site by typing the IP address of the server in the browser.
P.S. Sorry for my english! If you see an error in the text or code, please edit me.

Python Django+Nginx+uwsgi 502 Bad Gateway

Centos7,when I connect to my website, shows 502 Bad Gateway,
I test my website with command
uwsgi --ini
systemctl start nginx
And I cant figure out what's happened,please help me!
here's nginx.conf
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
charset utf-8;
include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
uwsgi_pass django;
}
location /static/ {
alias /usr/local/etc/dmp/static/;
}
}
and uwsgi setting
[uwsgi]
chdir = /usr/local/etc/dmp
module = DMP_python.wsgi
plugins = python3
socket = :8000
chmod-socket = 666
master = true
processes = 2
vacuum = true
You're using the wrong setting to tell uwsgi to use an HTTP port. You need http-socket rather than socket.
There can be multiple reasons for which an upstream might return invalid or even do not return any response
Verify if upstream uwsgi is actually running locally in the centos instance and can handle incoming requests
for this to verify run it as http-socket = :8000 in uwsgi.ini and then run uwsgi --ini uwsgi.ini
if uwsgi running fine on localhost, then change config back to socket = :8000
On centos 7.x SELinux package is enabled and runs in enforcing mode. So it won't allow nginx to write/connect to a socket.
verify if SELinux has the policy for nginx to write to sockets
check if read/connect/write to socket is allowed grep nginx /var/log/audit/audit.log | audit2allow -m nginx
do so by grep nginx /var/log/audit/audit.log | audit2allow -M nginx
and finally semodule -i nginx.pp
permission to connect to socket issue should be resolved by now
verify if nginx can do network connection with upstream. Check nginx error.log or getsebool -a | grep httpd
to allow it run setsebool httpd_can_network_connect on -P

Nginx not forwarding traffic to Gunicorn

I have a Flask app that I'm trying to host with Gunicorn and Nginx. I've followed several tutorials and I cannot seem to get Nginx to stop rendering the "Welcome to Nginx" page.
I've already confirmed that Supervisor successfully launches three Gunicorn workers, and Nginx is running in the background as well. SSL is confirmed working and Nginx picks up traffic on port 443. I've attempted to add Nginx to the same group as the user who owns the sock shared by Gunicorn and Nginx.
This is the bash script which Supervisor uses to start Gunicorn. This is confirmed working.
#!/bin/bash
NAME="Simon"
FLASKDIR=/var/www/Simon
SOCKFILE=/var/www/Simon/simon.sock
USER=glen
GROUP=glen
NUM_WORKERS=3
FLASK_SETTINGS_MODULE=config.py
FLASK_WSGI_MODULE=Simon.wsgi
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $FLASKDIR
source Simon/bin/activate
export FLASK_SETTINGS_MODULE=$FLASK_SETTINGS_MODULE
export PYTHONPATH=$FLASKDIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
exec gunicorn Simon:simon \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
This the site config:
upstream simon_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/var/www/Simon/simon.sock fail_timeout=0;
}
server {
listen 443 ssl;
server_name glencoverx.com;
client_max_body_size 4G;
access_log /var/www/Simon/logs/nginx-access.log;
error_log /var/www/Simon/logs/nginx-error.log;
location /static/ {
alias /var/www/Simon/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if ($scheme != "https") {
return 301 https://$host$request_uri;}
}
ssl_certificate /etc/letsencrypt/live/glencoverx.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/glencoverx.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
}
Weirdly enough, the logs show no problems. I've completely deleted the Nginx default site config and removed its symlink. I don't understand why Nginx will not communicate with Gunicorn.
first it would be helpful if you would do something like:
listen 443 default ssl;
This will make that site default.
If you haven't already, service nginx restart and make sure that your cache is purged. Try using telnet to do a get to make sure that it's still enabled.
Check your nginx/conf.d and sites-enabled dirs to see if the symlink or the default website is still there.

configuring nginx and uwsgi for python flask application

am trying to configure uwsgi and in the process it says on a tutorial that I must run
uwsgi -s /tmp/uwsgi.sock -w myapp:app
the problem is -w is an invalid option. Can anyone help me point out why or what should I do?
Thanks
maybe you are using debian-supplied packages. They are fully modular so you need to install/load the required plugins:
http://projects.unbit.it/uwsgi/wiki/Quickstart
My uwsgi app configuration looks like that
/etc/uwsgi/apps-enabled/mysite.ini
[uwsgi]
socket=/tmp/uwsgi_mysite.sock
chmod-socket=666
abstract-socket=false
master=true
workers=2
uid=altera
gid=altera
chdir=/home/altera/www/mysite ; Current dir
pp=/home/altera/www/mysite ; Python Path (to your application)
pyhome=/home/altera/vpy/mysite ; Path to virtual environment
plugins=python3
module=main ; *.py file name application starting from
post-buffering=8192
/etc/nginx/sites-available/mysite
server {
server_name mysite;
root /home/altera/www/mysite;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi_mysite.sock;
}
}

Categories