I'm not able to run uwsgi with upstart/supervisord, I used pyenv to setup python with virtualenv myenv, myenv has python3.4.3 and system's python pointing to python2.7.6. I changed pyenv global to 3.4.3.
Right now as my_user, which python => /home/my_user/.pyenv/shims/python and python --version => Python 3.4.3.
When I do with sudo, I get sudo which python => /usr/bin/python and sudo python --version => Python 2.7.6.
/etc/init/uwsgi_servers.conf has
description "uWSGI instance to serve authservice in production"
start on runlevel [2345]
stop on runlevel [!2345]
exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/sites/ --uid my_user --gid my_user
and /etc/uwsgi/sites/uwsgi.ini has
[uwsgi]
# variables
projectname = accounts
base = /home/my_user/django/accounts/
for-readline = /home/my_user/conf/django/accounts/env_var
env = %(_)
endfor =
# config
chdir = /home/my_user/django/accounts/
master = True
processes = 5
protocol = uwsgi
env = in_accounts.settings.production
module = in_accounts.wsgi
socket = /tmp/uwsgi_%(projectname).sock
chmod-socket = 666
home = /home/my_user/.virtualenvs/accounts_production
logto = %(base)/logs/uwsgi.log
daemonize = /var/log/uwsgi/%(projectname).log
vacuum = True
pidfile = /tmp/%(projectname).pid
die-on-term = true
When I run sudo service uwsgi_servers start, I get error from log
*** Starting uWSGI 2.0.11.2 (64bit) on [Tue Oct 13 09:32:18 2015] ***
compiled with version: 4.8.4 on 12 October 2015 11:59:31
os: Linux-3.16.0-43-generic #58~14.04.1-Ubuntu SMP Mon Jun 22 10:21:20 UTC 2015
nodename: st-auths-web1
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/sites
writing pidfile to /tmp/accounts.pid
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/my_user/django/accounts/current
your processes number limit is 13731
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/uwsgi_accounts.sock fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]
Set PythonHome to /home/my_user/.virtualenvs/accounts_production
ImportError: No module named site
VACUUM: pidfile removed.
VACUUM: unix socket /tmp/uwsgi_accounts.sock removed.
From log I can see that starting uwsgi using upstart try to use python2.7.6 since I use sudo to run upstart service, but It should have used python3.4.3, this might be the reason why server isn't running. Though I use uid and gid I get the above error. I also tried setuid and setgid, but no luck.
NOTE: I can able to run uwsgi server successfully without upstart as uwsgi --emperor /etc/uwsgi/sites/. I tried use supervisord, but no use, getting same error.
To make this work you will have to tell wsgi which virtualenv to use that's with the -H option. If your virtualenv is in your home folder, upstart will not be able to access it. So move it to a readable place and try
uwsgi -H /usr/local/virtualenv
or something like that with the correct path to the virtualenv in your upstart file.
Related
I am trying to run nginx and uwsgi.
When I try to run uwsgi with below command in my proj virtual environment with proj user I get an error. When I run uwsgi, I see the proj.sock is created in /run/uwsgi/proj.sock
I changed the /run/uwsgi permissions to drwxrwxrwx to see if the problem is permission related. But problem still appears. I also checked the /etc/passwd and there is no uwsgi user.
The directory info for /run/uwsgi is :
drwxrwxrwx 2 root root 60 Jul 16 10:53 uwsgi
When I try to change ownership of sock file with proj user to proj:nginx in proj virtual nvironment I get the error below.
chown: changing ownership of ‘/run/uwsgi/proj.sock’: Operation not permitted
When I check /run/uwsgi/proj.sock file it is proj:proj but it should be changed to proj:nginx. This operation can be done manually with root. But it cannot be done with proj user.
Run below command:
uwsgi --ini /etc/uwsgi/sites/proj.ini
Error :
[uWSGI] getting INI configuration from /etc/uwsgi/sites/proj.ini<br>
*** Starting uWSGI 2.0.15 (64bit) on [Mon Jul 16 10:53:02 2018] ***<br>
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 13 July 2018 17:12:50<br>
os: Linux-3.10.0-862.6.3.el7.x86_64 #1 SMP Tue Jun 26 16:32:21 UTC 2018<br>
nodename: localhost.localdomain<br>
machine: x86_64<br>
clock source: unix<br>
detected number of CPU cores: 2<br>
current working directory: /home/proj<br>
detected binary path: /home/proj/Env/proj/bin/uwsgi<br>
!!! no internal routing support, rebuild with pcre support !!!<br>
chdir() to /home/proj/work/proj<br>
your processes number limit is 4096<br>
your memory page size is 4096 bytes<br>
detected max file descriptor number: 1024<br>
lock engine: pthread robust mutexes<br>
thunder lock: disabled (you can enable it with --thunder-lock)<br>
chown(): Operation not permitted [core/utils.c line 2623]<br>
VACUUM WARNING: unix socket /run/uwsgi/proj.sock changed inode. Skip removal
My proj.ini file for uwsgi ini configuration is :
[uwsgi]
py-autoreload = 2
project = proj
username = proj
base = /home/%(username)
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = %k
uid = %(username)
socket = /run/uwsgi/%(project).sock
chown-socket = %(username):nginx
chmod-socket = 666
vacuum = true
#logto = /var/log/uwsgi/%(project).log
A user can only set the group of a file to a group he is in. In this case your user proj is not in the nginx group, so it is not allowed to make that change. If you add the user to the nginx group (and relogin) then it should work.
Also, setting the directory permissions to 777 allows any user to create and delete files in that directory, and 666 for the socket allows any user to access it. You probably don't want that. More reasonable would be to have /run/uwsgi owned by root:nginx with mode 770, and chmod-socket = 660 in your project ini.
I solved the problem.
The socket files were in /run/uwsgi directory.
I gave the related project user right to write read execute on this folder.
setfacl -m u:projuser:rwx /run/uwsgi/
As suggested by "ivbtar", I just removed following lines from uwsgi.ini file and it started working,
chown-socket = %(username):nginx
chmod-socket = 666
Environment: Ubuntu 16.04 (with system Python at 2.7.12) running in Vagrant/Virtualbox on Windows 10 host
Python Setup: System python verified by doing python -V with no virtualenv's activated. Python 3.5 is also installed, and I've done pipenv --three to create the virtualenv for this project. Doing python -V within the activated virtualenv (pipenv shell to activate) shows Python 3.5.2.
Additional Background: I'm developing a Wagtail 2 app. Wagtail 2 requires Django 2 which, of course, requires Python 3. I have other Django apps on this machine that were developed in Django 1.11/Python 2.7 and are served by Apache. We are moving to Django 2/Python 3 for development going forward and are moving to nginx/uWSGI for serving the apps.
I have gone through many tutorials/many iterations. All Vagrant port mapping is set up fine with nginx serving media/static files and passing requests upstream to the Django app on a unix socket, but this is giving a 502 Gateway not found error because uWSGI will not run correctly. Therefore, right now I'm simply running the following from the command line to try to get uWSGI to run: uwsgi --ini /etc/uwsgi/sites/my_site.com.ini. This file contains:
[uwsgi]
uid = www-data
gid = www-data
plugin = python35
# Django-related settings
# the base directory (full path)
chdir=/var/sites/my_site
# Django's wsgi file
wsgi-file = my_site.wsgi
# the virtualenv (full path)
virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID
# process-related settings
# master
master = True
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe)
socket = /tmp/my_site.sock
# clear environment on exit
vacuum = True
I've tried installing uWSGI in the following ways:
system-wide with pip install uwsgi as well as pip3 install uwsgi
using apt-get install uwsgi uwsgi-plugin-python3
I've ensured that only one install is in place at a time by uninstalling any previous uwsgi installs. The latter install method places uwsgi-core in usr/bin and also places in usr/bin shortcuts to uwsgi, uwsgi_python3, and uwsgi_python35.
In the .ini file I've also tried plugin = python3. I've also tried from the command line:
uwsgi_python3 --ini /etc/uwsgi/sites/my_site.com.ini
uwsgi_python35 --ini /etc/uwsgi/sites/my_site.com.ini
I've tried executing the uwsgi ... .ini commands from both within the activated virtual environment and with the virtualenv deactivated. Each of the three command line uwsgi ... .ini executions (uwsgi ..., uwsgi_python3 ... and uwsgi_python35 ...) DO cause the .ini file to be executed, but each time I'm getting the following error (the last two lines of the following statements):
[uwsgi] implicit plugin requested python35
[uWSGI] getting INI configuration from /etc/uwsgi/sites/my_site.com.ini
*** Starting uWSGI 2.0.12-debian (64bit) on [Wed Mar 7 03:54:44 2018] ***
compiled with version: 5.4.0 20160609 on 31 August 2017 21:02:04
os: Linux-4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018
nodename: vagrant-ubuntu-trusty-64
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /vagrant/my_site
detected binary path: /usr/bin/uwsgi-core
setgid() to 33
setuid() to 33
chdir() to /var/sites/my_site
your processes number limit is 7743
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/my_site.sock fd 3
Python version: 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
Set PythonHome to /root/.local/share/virtualenvs/my_site-gmmiTMID
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
If I go into the Python command line within the activated virtualenv and do import encodings, it imports fine (no message - just comes back to command line). A search for this particular error has turned up nothing of use. Any idea why the ImportError: No module named 'encodings' is coming up?
UPDATE - PROBLEM STILL OCCURRING
I'm using pipenv, and it stores individual virtualenvs in the /home/username/.local/share/virtualenvs folder. Though I was able to start uWSGI from the command line by executing the uWSGI config file as the vagrant user (see comment below), I have still not been able start the service with /home/vagrant/.local/share/virtualenvs/my_venv in the uWSGI config file. I tried adding the vagrant user to the www-data group and adding the www-data user to the vagrant group. I also put both read and execute permission for the world on the whole path (including the individual venv), but the uWSGI service will still not start.
A HACKISH WORKAROUND
I did finally get the uWSGI service to start by copying the venv to /opt/virtualenvs/my_venv. I was then able to start the service with sudo service uwsgi start. Ownership of that whole path is root:root.
STILL LOOKING FOR A SOLUTION...
This solution is not optimal since I am now executing from a virtualenv that will have to be updated when the default virtualenv is updated since this location is not the default for pipenv, so I'm still looking for answers. Perhaps it is a Ubuntu permissions error, but I just can't find the problem.
It might be the problem with your virtual environment. Try the following
rm -rf /root/.local/share/virtualenvs/my_site-gmmiTMID
virtualenv -p python3 /root/.local/share/virtualenvs/my_site-gmmiTMID
source /root/.local/share/virtualenvs/my_site-gmmiTMID/bin/activate
pip install -r requirements.txt
and in uwsgi conf try to change
virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID
to
home=/root/.local/share/virtualenvs/my_site-gmmiTMID
Reference:
http://uwsgi-docs.readthedocs.io/en/latest/Options.html#virtualenv
I'm trying to run a Flask application using a ubuntu 12.04 server with nginx and uWSGI however after starting the server, specifying the wsgi.ini file and then starting nginx, I try to access the page but I get a 502 bad gateway error with nginx. I know that it is an issue with the flask application and uWSGI. I checked the uWSGI logs and found this:
*** Starting uWSGI 2.0.11.1 (64bit) on [Thu Sep 17 19:38:03 2015] ***
compiled with version: 4.8.4 on 17 September 2015 15:58:04
os: Linux-3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
nodename: ip-172-31-30-153
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu/myapp
writing pidfile to /home/ubuntu/myapp/myapp.pid
detected binary path: /usr/local/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) ***
*** 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 TCP address 127.0.0.1:5000 fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]
Set PythonHome to /home/ubuntu/myapp
ImportError: No module named site
I found this question's top answer but saw received the same error. If it is something to do with PYTHONHOME, I'm following this guide and it states:
The $PYTHONPATH variable seems to get unset randomly, and causes
errors for later steps. To avoid this, add the following line to the
top of your ~/.bashrc file:
export
PYTHONPATH='/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat'
So what could be the issue? Is it PYTHONHOME or another factor affecting it? Thanks.
Edit:
My uwsgi.ini:
[uwsgi]
home = /home/ubuntu/myapp
wsgi-file = %(home)/run.py
socket = 127.0.0.1:5000
callable = app
module = app
pythonpath = %(home)
daemonize = %(home)/myapp.log
pidfile = %(home)/myapp.pid
To run it, I use uwsgi uwsgi.ini.
I am having trouble getting my flask app to run on a new server running CentOS 6.5.using uWSGI. (I have a similar CentOS box that it runs fine on)
I installed uWSGI within a virtual environment. I was getting the following error (in stdout) when calling uwsgi binary from the virtualenv
$ /path/to/venv/bin/uwsgi --emperor /etc/uwsgi/vassals
*** Starting uWSGI 2.0.8 (64bit) on [Mon Nov 17 21:18:13 2014] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 16 November 2014 22:19:09
os: Linux-2.6.32-431.1.2.0.1.el6.x86_64 #1 SMP Fri Dec 13 13:06:13 UTC 2013
nodename: boxname
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/username
detected binary path: /home/username/mydevelopment/venvs/ecodev_do/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 1024
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 ers_portal_uwsgi.ini
Mon Nov 17 21:18:14 2014 - [emperor] curse the uwsgi instance ers_portal_uwsgi.ini (pid: 22724)
Mon Nov 17 21:18:17 2014 - [emperor] removed uwsgi instance ers_portal_uwsgi.ini
The uwsgi.log file shows:
*** Starting uWSGI 2.0.8 (64bit) on [Mon Nov 17 21:25:40 2014] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 16 November 2014 22:19:09
os: Linux-2.6.32-431.1.2.0.1.el6.x86_64 #1 SMP Fri Dec 13 13:06:13 UTC 2013
nodename: boxname
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/vassals
detected binary path: /home/username/mydevelopment/venvs/ecodev_do/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/username/mydevelopment/git/ers_portal
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 /home/username/mydevelopment/git/ers_portal/ers_portal_uwsgi.sock fd 3
Python version: 2.7.8 (default, Nov 12 2014, 23:36:57) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
Set PythonHome to /home/username/mydevelopment/venvs/ecovapor_dev
ImportError: No module named site
Then, reading the official uWSGI documentation they describe a scenario where uwsgi is installed in a venv, but then it has to be installed system-wide after that. So, I used
pip2.7 install uwsgi (which created the binary in /usr/local/bin/uwsgi)
I have tried all of the following with no change in the error or the output (with and without venv activated)..
$ /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals
$ /home/username/mydevelopment/venvs/ecodev_do/bin/uwsgi --emperor /etc/uwsgi/vassals
$ /home/username/mydevelopment/venvs/ecodev_do/bin/uwsgi --no-site
--emperor /etc/uwsgi/vassals
$ uwsgi --emperor /etc/uwsgi/vassals
$ uwsgi --no-site --emperor /etc/uwsgi/vassals
$ /path/to/venv/bin/uwsgi ~/mydevelopment/git/ers_portal/ers_portal_uwsgi.ini (the .ini file is symlinked in /etc/uwsgi/vassals )
Here is emperor.ini;
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = username
gid = username
logto = /home/username/mydevelopment/git/ers_portal/logs/uwsgiEmperor.log
Here is ers_portal_uwsgi.ini;
[uwsgi]
master = true
#user info
uid = username
gid = username
#application's base folder
base = /home/username/mydevelopment/git/ers_portal
#chdir to this folder when starting
chdir = /home/username/mydevelopment/git/ers_portal
#python module to import
app = run_web
module = %(app)
home = /home/username/mydevelopment/venvs/ecovapor_dev
pythonpath = %(base)
#socket file's location
socket = /home/username/mydevelopment/git/ers_portal/%n.sock
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
#uwsgi varible only, does not relate to your flask application
callable = app
#location of log files
logto = /home/username/mydevelopment/git/ers_portal/logs/%n.log
This helped me.
Type it in your terminal.
unset PYTHONHOME
unset PYTHONPATH
In case someone uses python 3.x, installing uwsgi with pip3 solved this problem for me:
sudo pip3 install uwsgi
The virtualenv in question and uwsgi must be using the same python version.
In case you want to use multiple python versions on the same system, you can get the uwsgi source and follow the instructions here: http://projects.unbit.it/uwsgi/wiki/MultiPython
Then add a "plugin=/path/to/plugin" to the .ini file
Take note that plugins dont work for the packaged Ubuntu versions (built monolithic), but you need to compile the source directly for a fully modular binary
I am trying to run my sample django application using uWSGI and nginx. But I am getting import error, no module named py. I am not sure where should I add the python path.
I am running with the following command.
sudo uwsgi --socket mysite.socket --module wsgi.py --chmod-socket=666.
I even tried passing path in the parameter --pythonpath, still the same error.
This is how my wsgi.py looks like
enter code here
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
When I try running, I get the following messages
ubuntu#ubuntu1204desktopi386:/usr/local/lib/python2.7/site-packages/mysite/mysite
$ sudo uwsgi --socket mysite.socket --module wsgi.py --chmod-socket=666
*** Starting uWSGI 1.9.18.1 (32bit) on [Mon Oct 14 13:15:19 2013] ***
compiled with version: 4.6.3 on 13 October 2013 02:53:51
os: Linux-3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:15:33 UTC 2013
nodename: ubuntu1204desktopi386
machine: i686
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /usr/local/lib/python2.7/site-packages/mysite/mysite
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 3846
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 mysite.socket fd 3
Python version: 2.7.3 (default, Sep 26 2013, 20:26:19) [GCC 4.6.3]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x9644d68
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 64024 bytes (62 KB) for 1 cores
*** Operational MODE: single process ***
ImportError: No module named py
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: 2311, cores: 1)
The --module argument is presumably meant to be a Python module, not a file. So you probably just need --module=wsgi.
i guess you can also use , --wsgi-file wsgi.py like this
$ uwsgi --socket 127.0.0.1:8000 --wsgi-file my_proj/wsgi.py
Make sure you run command from same folder
If python manage.py runserver 0.0.0.0:8024 can work well, then
This also can work(run it on the same directory as above command):
uwsgi --http :8024 --module mysite.wsgi
mysite is the name of this django project
uwsgi version is 2.0.14;
django version is 1.10.
ref: https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html