uWSGI won't reload, restart or let me run service
Asked Answered
L

2

28

I have a fairly big issue.

I am very new to uwsgi and am not 100% sure on how to debug this issue but I will give you information on where I am at.

  • I have previously had sites working on this configuration and suddenly it isn't working.
  • I am running Emperor mode.
  • My ini files are ok when I use command line to run them but it seems they wont automatically start

When I run uwsgi reload

sudo service uwsgi reload

I get this error

* Reloading app server(s) uwsgi
...fail!

Thats it. I get nothing else.

I have been looking for hours on stack overflow and haven't found anything that outlines this problem exactly, I found a lot to do with peoples .ini files but I know that is NOT my issue because when running my site manually via uwsgi --ini MYINI.ini then accessing it it runs perfectly fine, the issue is in uWSGI and I don't know how to find the solution to this one. I have looked in the documents and can't find anything on this particular error.

If this interests anyone here is my uwsgi-server.conf file

description     "uWSGI Emperor"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

env LOGTO=/var/log/uwsgi.log
env BINPATH=/usr/local/bin/uwsgi

exec $BINPATH --emperor /etc/uwsgi/vassals --logto $LOGTO

Any insight would be appreciated. I feel like I am missing something but being so new with uWSGI I cant even guess as to what it may be, To me this all looks ok as per the documentation.

If you need any more information on my setup please just ask.

Longo answered 15/4, 2014 at 2:46 Comment(3)
have you tried running /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals to check if it works well ?Rockbound
What version of uwsgi?Ani
@Rockbound yes it works fine @ Louis 1.9.xLongo
F
85

Use uwsgi the right way

Using uwsgi to deply django site on ubuntu server is quite easy, but there are still something you need to know before making mistakes.

install

You have two ways to install uwsgi on ubuntu: apt-get or pip

apt-get

if you use apt-get, you need to install the python plugin:

sudo apt-get install uwsgi-plugin-python
sudo apt-get install uwsgi

And, in your uwsgi ini file for your site, you need to add this:

plugins=python

pip

if you use pip, you need to install python-dev first:

sudo apt-get install python-dev
sudo pip install uwsgi

And, you don't need the plugins=python in ini file anymore.

See the sudo before pip? Yes, uwsgi should be installed in global system. If you miss the sudo here, you may install it in your virtualenv. It's meaningless and you may have trouble running it.

daemonize uwsgi

Daemonize means make uwsgi run on system boot and in the background. According to how you install uwsgi, you have two ways.

apt-get

When you apt-get install uwsgi on ubuntu, it's installed as a service automatically. The magic lies in this file:

/etc/init.d/uwsgi

Files in /etc/init.d will be loaded by sysvinit. Then you can manage your uwsgi service like this:

sudo /etc/init.d/uwsgi start|stop|restart|reload

or:

sudo service uwsgi start|stop|restart|reload

the service command can find the service managed by sysvinit

pip

If you uwsgi is installed by pip, you only have the executable file in /usr/local/bin/uwsgi, you need to daemonize it yourself.

When you open some of the files in /etc/init.d/, you may feel sad: I just want to register uwsgi as a service, why I need to write such long a script which looks similar to the others? It doesn't make sense.

Good news is that it is quite simple with the help of Upstart, which is an alternative to sysvinit. It use /etc/init/ instead of /etc/init.d/.

Just create a file /etc/init/uwsgi.conf with following content:

description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals/ --logto /var/log/uwsgi.log

and then, you can manage your uwsgi process like this:

sudo initctl start|stop|restart|reload| uwsgi

or, still this:

sudo service uwsgi start|stop|restart|reload

Yes, as you can see, the service command is smart, it can manage service from both sysvinit and Upstart, with the same command.

And, if you have both /etc/init.d/uwsgi and /etc/init/uwsgi.conf, when you say:

sudo service uwsgi restart

It will restart the Upstart file /etc/init/uwsgi.conf. The sysvinit one will be ignored, or something similar.

uwsgi config for your site

I recommend everyone to use the pip and Upstart way, it's much better then the apt-get way.

If so, you are using the emperor mode of uwsgi, which is very handy and powerful.

Now, you can create a ini file in /etc/uwsgi/vassals/ like this:

[uwsgi]
virtualenv=/path/to/venv/
chdir=/path/to/proj/root
module=wsgi:application
env=DJANGO_SETTINGS_MODULE=settings
master=True
vacuum=True
socket=/tmp/%n.sock
pidfile=/tmp/%n.pid
daemonize=/var/log/uwsgi/%n.log

The %n means your file name. For example, my project name is 'example', I create a example.ini file for it. Then the %n means 'example'. You don't need to replace it with real name. uwsgi will do this for you.

And then restart or reload uwsgi:

sudo service uwsgi restart

Check your socket file:

ll /tmp/*.sock

If it's there, you are successful with uwsgi now:)

nginx config for your site

Take domain example.com for example:

server {
    listen          80;
    server_name     www.example.com;
    return          301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    charset utf-8;
    server_name example.com;

    location  /static/ {
        alias  /path/to/static/;
    }

    location  /media/ {
        alias /path/to/media/;
    }

    location / {
        try_files $uri @django;
    }

    location @django {
       uwsgi_pass unix:///tmp/example.sock;
       include uwsgi_params;
    }
}

restart nginx, you will see your site!

answer to you question

Your config file for uwsgi is /etc/init/uwsgi-server.conf So, the name you should use is uwsgi-server, not uwsgi

you need to restart your uwsgi emperor instance like this:

sudo initctl restart uwsgi-server

or:

sudo service uwsgi-server restart

That's all!

Frankhouse answered 16/4, 2014 at 0:58 Comment(5)
Using pip, 2 files installed: /usr/local/bin/uwsgi and /usr/local/lib/python2.7/dist-packages/uwsgidecorators.py (if we ignore the five meta data files for the egg package). Using apt-get app 220 files installed :) In a sense I like that apt-get takes care of so much, on the other hand the pip setup is much more transparent and it's possible to understand how it works.Mychael
Hi. I wanted to ask one thing, related to installs. Maybe you can help. When uwsgi is installed using pip for some reason it does not see default python paths in system path on ubuntu. So in order for it to work you need manually copy all those default paths which are in sys.path to PYTHONPATH. Otherwise you get import error. But when installed by apt-get there is no such problem. Any idea how to fix this without dirty tricks like adding all those paths to PYTHONPATH through .bashrc?@FrankhouseVernievernier
Why is it 'wrong' to have uwsgi runing in venv? And could you elaborate a bit more on emperor mode vs venv? ThanksBoner
Your idea of "uwsgi in venv is bad" is not correct. In fact, running inside venv is recommended and makes the venv truly virtual. Correctly configuring upstart or systemd is the key.Tyrannosaur
I've rolled back your edit that removed all content. There are better ways to handle the situation, especially with such a highly upvoted post.Friederike
K
0

tool for uwsgi restart based on sample app stack: Flask/uWSGI/NGINX from tutorial:

https://iotbytes.wordpress.com/python-flask-web-application-on-raspberry-pi-with-nginx-and-uwsgi/

GIT: https://github.com/mmossak/uwsgi_restart.git

Saves some time and makes the task fairly easy. Just make sure that you customize script with your application set up(especially starting uwsgi). It only restarts uWSGI.

App used for my Raspbery Pi project interacting with Temperature Sensor which is able to send GET requests to raspberry.

Changing Ini files, touch, sudo service uwsgi restart methods etc. won't help with this. uWSGI in my case isn't a service. It is daemonised process.

You need to kill uwsgi master proces and start again to be able to see changes in application.

Kerbela answered 1/1, 2022 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.