uWSGI Emperor configuration - python

I have been struggling to deploy a flask app to my server using uWSGI in
Emperor mode all day and am at a loss why it is not working. I have read all the other uWSGI Emperor mode questions here & asked on #uwsgi & the mailing list with no reply.
My setup is nginx (1.4.6-1ubuntu3.2), uWSGI==2.0.10 installed via pip
into a virtualenv, Flask==0.10.1, and Python 3.4.0. supervisord is
responsible for launching the Emperor process.
The flask app itself works fine when I start it manually bound to a TCP port, and when I manually run uwsgi like:
uwsgi -s /tmp/oauthsvc.sock -w wsgi --stats /tmp/oauthsvc-stats.sock
I do notice that even though I think I configured the vassal to use a
unix socket, it reports on startup that it is listening to http://127.0.0.1:5000/
I verified with strace that nginx is opening the socket, and permissions
of that socket are 777:
srwxrwxrwx 1 oauthsvc oauthsvc 0 Jun 3 18:34 /tmp/oauthsvc.sock
srwxrwxrwx 1 oauthsvc oauthsvc 0 Jun 3 18:08 /tmp/oauthsvc-stats.sock
But the connection eventually just times out.
My configs & some log output follows.
The nginx config is as simple as can be:
server {
listen 80;
server_name default;
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/oauthsvc.sock;
}
}
supervisor launches the emporer process with this command:
[program:oauthsvc-emperor]
command=/services/oauthsvc/services/oauthsvc/bin/uwsgi --emperor /services/oauthsvc/etc/uwsgi/vassals --die-on-term --uid 1005 --gid 1005 --logto /services/oauthsvc/var/log/uwsgi/emperor.log
user=oauthsvc
autostart=true
autorestart=true
redirect_stderr=true
The vassal's ini file looks like:
[uwsgi]
# do not turn on master mode when spawned by emperor
# http://stackoverflow.com/questions/15055002/uwsgi-master-with-emperor-spawns-two-emperors
#master = true
protocol = uwsgi
socket = /tmp/oauthsvc.sock
stats = /tmp/oauthsvc-stats.sock
daemonize = /services/oauthsvc/var/log/uwsgi/oauthsvc.log
chdir = /services/oauthsvc/services/oauthsvc/oauthsvc-server
wsgi-file = wsgi.py
callable = app
chmod-socket = 777
uid = 1005
gid = 1005
virtualenv = /services/oauthsvc/services/oauthsvc
# PYTHONHOME
home = /services/oauthsvc/services/oauthsvc
#env =
processes = 4
vacuum = true
# If you start uWSGI without threads, the Python GIL will not be enabled, so
# threads generated by your application will never run unless you include:
enable-threads = true
harakiri = 30
And finally, wsgi.py:
#!/usr/bin/env python3
from oauthsvc.flask.server import parse_args, configured_app
args = parse_args()
app = configured_app(args.config, debug=args.debug)
app.run()
The emperor log:
*** Starting uWSGI 2.0.10 (64bit) on [Wed Jun 3 18:34:25 2015] ***
compiled with version: 4.8.2 on 03 June 2015 18:32:12
os: Linux-3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
nodename: ip-10-0-1-224
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /
detected binary path: /services/oauthsvc/src/bs-oauth-svc.2015-06-03.183113/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 30038
your memory page size is 4096 bytes
detected max file descriptor number: 1024
*** starting uWSGI Emperor ***
*** has_emperor mode detected (fd: 6) ***
[uWSGI] getting INI configuration from oauthsvc.ini
The vassal log:
*** Starting uWSGI 2.0.10 (64bit) on [Wed Jun 3 18:34:25 2015] ***
compiled with version: 4.8.2 on 03 June 2015 18:32:12
os: Linux-3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
nodename: ip-10-0-1-224
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /services/oauthsvc/etc/uwsgi/vassals
detected binary path: /services/oauthsvc/src/bs-oauth-svc.2015-06-03.183113/bin/uwsgi
chdir() to /services/oauthsvc/services/oauthsvc/oauthsvc-server
your processes number limit is 30038
your memory page size is 4096 bytes
*** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers ***
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 /tmp/oauthsvc.sock fd 3
Python version: 3.4.0 (default, Apr 11 2014, 13:08:40) [GCC 4.8.2]
Set PythonHome to /services/oauthsvc/services/oauthsvc
Python main interpreter initialized at 0x135d4d0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 363840 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Brian on the uWSGI mailing list solved this for me. The answer is: don't call app.run() in the wsgi.py file! I knew it would be something embarrassing.

Related

Attempting to deploy a python pyramid application with uWSGI

I'm attempting to deploy a pyramid application using uWSGI.
The application works fine when served with the included pyramid development server.
Also, I have set this up before, and I swear it worked at one time.
However, putting in the magic phrases right now is resulting in "This webpage is not available"
I'm trying to keep all of the configuration parameters as similar as possible to what I have currently so I don't have to worry about firewall issues.
uWSGI section in development.ini looks like this (from: Setup uWSGI as webserver with pyramid (no NGINX)):
[uwsgi]
socket = localhost:8080
virtualenv = /var/www/finance/finance-env
die-on-term = 1
master = 1
#logto = /var/log/wsgi/uwsgi.log
enable-threads = true
offload-threads = N
py-autoreload = 1
wsgi-file = /var/www/finance/wsgi.py
wsgy.py looks like this:
from pyramid.paster import get_app, setup_logging
ini_path = '/var/www/finance/corefinance/development.ini'
setup_logging(ini_path)
application = get_app(ini_path, 'main')
Here's the output right now. Everything seems to be listening just fine on port 8080.
user1#finance1:~$ sudo /var/www/finance/finance-env/bin/uwsgi --ini-paste-logg /var/www/finance/corefinance/development.ini
[uWSGI] getting INI configuration from /var/www/finance/corefinance/development.ini
*** Starting uWSGI 2.0.11.2 (64bit) on [Fri Jan 15 21:13:31 2016] ***
compiled with version: 4.7.2 on 16 November 2015 20:13:35
os: Linux-4.1.5-x86_64-linode61 #7 SMP Mon Aug 24 13:46:31 EDT 2015
nodename: finance1
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/user1
detected binary path: /var/www/finance/finance-env/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) ***
your processes number limit is 3934
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 TCP address localhost:8080 fd 3
Python version: 3.2.3 (default, Feb 20 2013, 14:49:46) [GCC 4.7.2]
Set PythonHome to /var/www/finance/finance-env
Python main interpreter initialized at 0xfd0a10
python threads support enabled
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 1 seconds on interpreter 0xfd0a10 pid: 6275 (default app)
mountpoint already configured. skip.
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 6275)
spawned uWSGI worker 1 (pid: 6282, cores: 1)
Python auto-reloader enabled
Unless you are behind a proxy such as nginx, you need to use the internal http routing support in uwsgi, change
socket = localhost:8080
to
http = 0.0.0.0:8080
Here are the uwsgi http support docs

uwsgi Exception: not correct domain name /

I have some application written in python.
I'm trying to run python application under uwsgi.
My app.ini:
[uwsgi]
chdir = /var/www/myapp
wsgi-file = /var/www/myapp/app.wsgi
uid = apache
gid = apache
master = true
processes = 1
socket = :8080
vacuum = true
Start uwsgi:
uwsgi --ini app.ini
[uWSGI] getting INI configuration from app.ini
*** Starting uWSGI 2.0.10 (64bit) on [Sat Mar 21 16:59:09 2015] ***
compiled with version: 4.8.2 20140120 (Red Hat 4.8.2-16) on 21 March 2015 16:55:47
os: Linux-3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014
nodename: lw_site
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /var/www/myapp
detected binary path: /usr/bin/uwsgi
chdir() to /var/www/myapp
your processes number limit is 4096
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 TCP address :8080 fd 3
Python version: 2.7.5 (default, Jun 17 2014, 18:11:42) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x2644f70
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145520 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x2644f70 pid: 39838 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 39838)
spawned uWSGI worker 1 (pid: 39839, cores: 1)
This is
WSGI app 0 (mountpoint='')
confuse me.
I get this error:
Custom Error: Traceback (most recent call last):
File "/var/www/myapp/app/manager.py", line 53, in app
contrller, args = router.mapping(request, city_model)
File "/var/www/myapp/app/router.py", line 478, in mapping
raise Exception('not correct domain name '+str(rurl))
Exception: not correct domain name /
Where is I made a mistake ?
I have found the error. It's problem in code. It get domain name from SERVER_NAME variable. I set in nginx:
uwsgi_param SERVER_NAME $host;

502 Bad Gateway connecting Nginx to django via socket

I'm trying to follow the tutorial at http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html. I've gotten everything working down to http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#running-the-django-application-with-uwsgi-and-nginx. I am working with an ubuntu 14.4 instance on amazon EC2:
My django project is called tp. Here's a screenshot:
Following the instructions I have done:
(env1)ubuntu#ip-172-31-28-196:~$ uwsgi --socket /tmp/mysite.sock --module /home/ubuntu/tproxy/tp/tp
/wsgi.py --chmod-socket=664
*** Starting uWSGI 2.0.9 (64bit) on [Thu Mar 5 16:50:02 2015] ***
compiled with version: 4.8.2 on 03 March 2015 02:58:28
os: Linux-3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
nodename: ip-172-31-28-196
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu
detected binary path: /home/ubuntu/.virtualenvs/env1/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7862
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 /tmp/mysite.sock fd 3
Python version: 3.4.0 (default, Apr 11 2014, 13:08:40) [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1fe5320
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
ImportError: No module named '/home/ubuntu/tproxy/tp/tp/wsgi'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 24393, cores: 1)
Obviously you can see:
ImportError: No module named '/home/ubuntu/tproxy/tp/tp/wsgi'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
How do I set the path to the wsgi.py file?
edit 1: here's my ini file:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/tproxy/tp
# Django's wsgi file
module = uwsgi
# the virtualenv (full path)
home = /home/ubuntu/.virtualenvs/env1
wsgi-file = /home/ubuntu/tproxy/tp/tp/wsgi.py
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 3
# the socket (use the full path to be safe
socket = /tmp/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true
I ran:
(env1)ubuntu#ip-172-31-28-196:~$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
but still getting a 502 error.
Logs:
2015/03/05 18:10:00 [crit] 1828#0: *51 connect() to unix:///tmp/mysite.sock failed (13: Permission denied) while connecting to upstream, client: 107.0.193.131, server: 52.10.**-**, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///tmp/mysite.sock:", host: "52.10.**-**:8000"
edit 2 : is home the directory for virtualenv? I set:
home = /home/ubuntu/tproxy/
Now:
(env1)ubuntu#ip-172-31-28-196:~$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
[uWSGI] getting INI configuration from /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Thu Mar 5 18:59:27 2015] ***
compiled with version: 4.8.2 on 03 March 2015 02:58:28
os: Linux-3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
nodename: ip-172-31-28-196
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu
detected binary path: /home/ubuntu/.virtualenvs/env1/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/ubuntu/tproxy/tp
your processes number limit is 7862
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 /tmp/mysite.sock fd 3
Python version: 3.4.0 (default, Apr 11 2014, 13:08:40) [GCC 4.8.2]
Set PythonHome to /home/ubuntu/tproxy/
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted (core dumped)
You put it in your uwsgi ini file. Here is an example one:
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /var/www/virtualenv/project
# Django's wsgi file
module = uwsgi
# the virtualenv (full path)
home = /var/www/virtualenv/
wsgi-file = /var/www/virtualenv/project/projectsettings/wsgi.py
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /tmp/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true
This is my ini file minus my path's to my projects. I also ran into this same issue when installing it myself but was able find the appropriate answer from looking at other configs on google.

Flask + uWSGI + Nginx + Fedora 20 setup issues

I'm trying to deploy a flask app through uWSGI onto Nginx, with Fedora 20 as the OS, and i'm running into some issues.
I have the following configuration files:
NGINX - Default config with following edit:
location / { try_files $uri #yourapplication; }
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:/tmp/sjudson_app_uswgi.sock;
}
uWSGI:
[uwsgi]
socket = /tmp/%n.sock
wsgi-file = sjudson_app/sjudson_app.py
callable = sjudson
master = true
chmod-socket = 666
logto = /home/server/logs/uwsgi.log
First of all, when I attempt to run:
uwsgi --ini sjudson_app_uwsgi.ini
I just get:
[uWSGI] getting INI configuration from sjudson_app_uwsgi.ini
Indefinetely. Secondly, when I run it directly from the command line:
uwsgi --socket /tmp/sjudson_app_uwsgi.sock --wsgi-file sjudson_app/sjudson_app.py --callable sjudson --master --chmod-socket=666
I get:
*** Starting uWSGI 2.0.3 (32bit) on [Tue Mar 25 17:58:44 2014] ***
compiled with version: 4.8.2 20131212 (Red Hat 4.8.2-7) on 25 March 2014 16:48:01
os: Linux-3.13.6-200.fc20.i686+PAE #1 SMP Fri Mar 7 17:17:53 UTC 2014
nodename: new-host-6
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/server
detected binary path: /usr/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 1024
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 /tmp/sjudson_app_uwsgi.sock fd 3
Python version: 2.7.5 (default, Feb 19 2014, 13:47:40) [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x881cc40
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 127952 bytes (124 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x881cc40 pid: 1131 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1131)
spawned uWSGI worker 1 (pid: 1136, cores: 1)
But when I try to connect to the server I get a 502 error. I noticed this stackoverflow post: Flask, nginx, and uwsgi with a similar issue, for which the upvoted answer concerned the socket permissions not being correct. However, I do have the chmod-socket=666 argument, and looking at /tmp:
srw-rw-rw-. 1 server server 0 Mar 25 17:53 sjudson_app_uwsgi.sock
So it doesn't look to me as though that's the issue.
EDIT:
Realized I forgot to include the Nginx error message:
2014/03/26 07:58:38 [crit] 792#0: *15 connect() to unix:/tmp/sjudson_app_uswgi.sock
failed (2: No such file or directory) while connecting to upstream, client:
173.79.242.54, server: localhost, request: "GET / HTTP/1.1", upstream:
"uwsgi://unix:/tmp/sjudson_app_uswgi.sock:", host: "173.79.242.54"
I'm not sure what's meant by the "no such file or directory" in this case. I know the .sock file exists, so what is it struggling to find?
I had almost the exact same setup and same error message. To fix it I had to do:
change the socket statement in my uwsgi.ini file to:
socket = 127.0.0.1:8081
add the following to my nginx.conf file (outside of the server statement):
upstream uwsgi { server 127.0.0.1:8081; }
change the location statement in my nginx.conf file to:
location / { try_files $uri #uwsgi; }
location #uwsgi {
include uwsgi_params;
uwsgi_pass uwsgi;
}
This then enabled nginx and uwsgi to communicate. These instructions were taken from http://blog.djcentric.com/setting-up-uswgi-nginx-what-you-need-to-know/ .
Please change string in nginx configuration, from
uwsgi_pass unix:/tmp/sjudson_app_uswgi.sock;
to
uwsgi_pass unix:///tmp/sjudson_app_uswgi.sock;

Flask, nginx, and uwsgi

My flask app looks like this...
myapp.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run('0.0.0.0')
My nginx setup
server {
root /home/admin.jeremylspencer.com;
server_name admin.jeremylspencer.com;
location / { try_files $uri #yourapplication; }
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
#error_page 404 /404.html;
#error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location ~ /\.ht {
allow all;
}
}
Then finally I restart nginx and run this:
sudo uwsgi -s /tmp/uwsgi.sock --module myapp --callable app
And this is the output
*** Starting uWSGI 1.4.3 (64bit) on [Mon Dec 10 15:41:00 2012] ***
compiled with version: 4.6.3 on 10 December 2012 13:06:15
os: Linux-3.2.0-32-generic #51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012
nodename: jeremylspencer.com
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /home/admin.jeremylspencer.com
detected binary path: /usr/local/bin/uwsgi
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 31285
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Python version: 2.7.3 (default, Aug 1 2012, 05:25:23) [GCC 4.6.3]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1dfa790
your server socket listen backlog is limited to 100 connections
mapped 72392 bytes (70 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1dfa790 pid: 13645 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 13645, cores: 1)
But yet all I get is a 502 error... any how can i fix this?
unix sockets are filesystem objects, so nginx need write permissions to /tmp/uwsgi.sock
You are running uWSGI as root (why ???) so /tmp/uwsgi.sock will be owned by root while nginx generally runs as nobody or www-data.
If you do not want to take in account permissions just use TCP sockets, but obviously do not run your app as root.
You can have a look on this: Python flask with Nginx and uWSGI
as the repo provides the exact case / scenario on how to use Flask in production mode.

Categories

Resources