Monday, December 5, 2016

How to build uwsgi from source and run as a service

As you might have already noticed, I always deploy python applications using uwsgi and nginx. But I didn't write yet how to install uwsgi. Probably there is some version in repository, but I don't really care about it. I know it's old and not what I actually need.

I need to build uwsgi with plugin support, which would allow me to work both with python 3 and 2. Then I will create service config file, so that you can use it as service. Sounds good, isn't it? Let's do it then.

1. Download uwsgi and extract it
cd ~
curl -O http://projects.unbit.it/downloads/uwsgi-2.0.12.tar.gz
tar -xvzf uwsgi-2.0.12.tar.gz


2. Build uwsgi core
cd ~/uwsgi-2.0.12
python uwsgiconfig.py --build nolang

3. Build uwsgi python plugins (2 and 3)
cd ~/uwsgi-2.0.12
PYTHON=python2.7 ./uwsgi --build-plugin "plugins/python python27
PYTHON=python3.4 ./uwsgi --build-plugin "plugins/python python34

4. Copy plugins to system directories:
sudo mkdir -p /usr/lib/uwsgi
sudo cp python27_plugin.so /usr/lib/uwsgi/
sudo cp python34_plugin.so /usr/lib/uwsgi/

5. Install uwsgi binary system wide
cd ~/uwsgi-2.0.12
UWSGI_PROFILE=nolang python setup.py install

6. Create vassals directory
sudo mkdir -p /etc/uwsgi/vassals/

7. Create service config (replace 'www-data' with user that uwsgi should be run as) and save it to the file '/etc/systemd/system/uwsgi.service':
[Unit]
Description=uWSGI Emperor service

[Service]
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

8. Enable and start uwsgi service
sudo systemctl enable uwsgi
sudo systemctl start uwsgi

Now you can start using uwsgi. As an example of uwsgi config file, you can check this article, which I wrote last time about trac deployment. There you can find 'plugins-dir' and 'plugin' parameters, that's why we've built it with plugin support. Now you can easily change python27 to python34 in some of your other projects.

No comments:

Post a Comment