nginx+uwsgi+django emperor mode not working - python

I am doing multi-application nginx+uwsgi setup in django. I am following this documentation
to set up emperor mode for my project.
It works fine when I run "uwsgi --ini myproject_uwsgi.ini" but I am not able to deploy emperor mode following this documentation.
I have researched many docs but not able to find relevant information related to emperor mode setup.
My nginx.conf file:(angularJS frontend)
server {
listen 80;
server_name 192.168.xx.xx;
root /home/user/Myspace/workspace/Myproject_frontend/dist;
}
My "etc/uwsgi/vassals/uwsgi.ini" file looks like:
[uwsgi]
#Django-related settings
#Django's wsgi file
wsgi-file = /home/user/Myspace/workspace/myproject/myproject/wsgi.py
http = 192.168.xx.xx:8080
virtualenv = /home/user/Myspace/VENV_MYPROD
Also, i have created a uwsgi.service file in "/etc/systemd/system/uwsgi.service" to start emperor mode service and it looks like:
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals/
RuntimeDirectory= uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Please provide help on setting up uwsgi emperor mode.

Related

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.

Flask App with Gunicorn on Nginx Server Error On POST

I am new to Python . I hosted the Flask app which will be accepting a POST request with Gunicorn on Nginx Server on EC2 instance.
When I post on the route I am getting error as :
1578#0: *14 upstream prematurely closed connection while reading response header from upstream, client: myip, server: serverip, request: "POST /train HTTP/1.1", upstream: "http://unix:/home/ec2-user/myproject/myproject.sock:/save_data", host: "serverip"
The POST request is not from the same domain and will always be from some other domain. Do I need to add something on my nginx.conf file?
Everything works fine when I run app using the command python app.py
I was able to solve this issue. This wasn't related to my NGINX configuration (which I initially thought was the cause).
The problem resided in my Gunicorn configuration file.
In my Gunicorn config file (/etc/systemd/system/myproject.service), I added the following to my ExecStart line:
--timeout 600
The file now looks like this:
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target
[Service]
User=harrison
Group=www-data
WorkingDirectory=/home/harrison/myproject
Environment="PATH=/home/harrison/myproject/myprojectenv/bin"
ExecStart=/home/harrison/myproject/myprojectenv/bin/gunicorn --workers 3 --timeout 600 --bind unix:myproject.sock -m 007 wsgi:application
[Install]
WantedBy=multi-user.target
Furthermore, the reason why you do not experience the issue when launching your app using python app.py is because it is not being served by Gunicorn that way... it's using the Flask test development server. The development server does not have the same timeout duration as Gunicorn. By default, I believe the Gunicorn timeout defaults to 30 seconds. In the case of my application, this was far too low.

Setup multiple django gunicorn instances with nginx

Got one django site running with gunicorn & nginx need to setup another site also with the same. Nginx settings is straight forward, how to rework below to add another site /home/ubuntu/webapps/uganda_buzz/
relevant settings are /etc/init/gunicorn.conf
description "Gunicorn application server handling all projects"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid user
setgid www-data
chdir /home/ubuntu/webapps/kenyabuzz
exec /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
and /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=nginx
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz
ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
[Install]
WantedBy=multi-user.target
Try How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 14.04 LTS
or Configuring multiple domains and subdomains. DNS & Nginx issues abound!

uwsgi broken pipe when running it as systemd service

I am running a uwsgi/flask python app in a conda virtual environment using python 2.7.11.
I am moving from CentOS 6 to CentOS 7 and want to make use of systemd to run my app as a service. Everything (config and code) works fine if I manually call the start script for my app (sh start-foo.sh) but when I try to start it as a systemd service (sudo systemctl foo start) it starts the app but then fails right away with the following error:
WSGI app 0 (mountpoint='') ready in 8 seconds on interpreter 0x14c38d0 pid: 3504 (default app)
mountpoint already configured. skip.
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3504)
emperor_notify_ready()/write(): Broken pipe [core/emperor.c line 2463]
VACUUM: pidfile removed.
Here is my systemd Unit file:
[Unit]
Description=foo
[Service]
ExecStart=/bin/bash /app/foo/bin/start-foo.sh
ExecStop=/bin/bash /app/foo/bin/stop-foo.sh
[Install]
WantedBy=multi-user.target
Not sure if necessary, but here are my uwsgi emperor and vassal configs:
Emperor
[uwsgi]
emperor = /app/foo/conf/vassals/
daemonize = /var/log/foo/emperor.log
Vassal
[uwsgi]
http-timeout = 500
chdir = /app/foo/scripts
pidfile = /app/foo/scripts/foo.pid
#socket = /app/foo/scripts/foo.soc
http = :8888
wsgi-file = /app/foo/scripts/foo.py
master = 1
processes = %(%k * 2)
threads = 1
module = foo
callable = app
vacuum = True
daemonize = /var/log/foo/uwsgi.log
I tried to Google for this issue but can't seem to find anything related. I suspect this has something to do with running uwsgi in a virtual environment and using systemctl to start it. I'm a systemd n00b so let me know if I'm doing something wrong in my Unit file.
This is not a blocker because I can still start/stop my app by executing the scripts manually, but I would like to be able to run it as a service and automatically launch it on startup using systemd.
Following the instructions here at uwsgi's documentation regarding setting up a systemd service fixed the problem.
Here is what I changed:
Removed daemonize from both Emperor and Vassal configs.
Took the Unit file from the link above and modified slightly to work with my app
[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/app/foo/bin/uwsgi /app/foo/conf/emperor.ini
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target

supervisor and uWSGI will not work together along with nginx

I use supervisord for managing tornado with no issuse wnhile using nginx as a load balancer.
I am having severe issues with supervisor and uWSGI with nginx as a load blancer. I am using bottle as the framework.
when I run the below from the command line and I load a page in FF hitting nginx, all works great.
sudo command = /usr/local/bin/uwsgi --loop gevent --socket 127.0.0.1:8070 --wsgi-file /home/ubuntu/workspace/uwsgiServer.py -b 32768 --master --async 5 --enable-threads --listen 100 --uid root
If I place the command line in supervior then I get page not found.
uWSGI Error
Python application not found
[program:uwsgi]
#autostart=true
#autorestart=true
#process_name = uwsgi-%(process_num)s
command = /usr/local/bin/uwsgi --loop gevent --socket 127.0.0.1:8070 --wsgi-file /home/ubuntu/workspace//uwsgiServer.py -b 32768 --master --async 5 --enable-threads --listen 100 --uid root
#--port=%(process_num)s
#--log_file_prefix=%(here)s/logs/%(program_name)s-%(process_num)s.log
#numprocs = 1
#numprocs_start = 8070
Here are the relevant portions of the nginx.conf file:
upstream uwsgi_b {
server 127.0.0.1:8070;
}
location /u/ {
include uwsgi_params;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param Host $http_host;
uwsgi_pass uwsgi_b;
}
You might probably add some more params to supervisord config, like --pp (python path) to the uwsgi command, and maybe some environment variables:
[program:uwsgi]
command = /usr/local/bin/uwsgi
--loop gevent
--socket 127.0.0.1:8070
--wsgi-file uwsgiServer.py
--buffer-size 32768 --master --async 5 --enable-threads --listen 100 --uid root
--pp /home/ubuntu/workspace/
autostart=true
autorestart=true
environment=ENV_VAR='var'
user=root # or other
group=root # or other
directory=/home/ubuntu/workspace/
umask=022
EDIT: Removed Django-specific settings

Categories