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
tar -xvzf uwsgi-2.0.12.tar.gz

2. Build uwsgi core
cd ~/uwsgi-2.0.12
python --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 /usr/lib/uwsgi/
sudo cp /usr/lib/uwsgi/

5. Install uwsgi binary system wide
cd ~/uwsgi-2.0.12
UWSGI_PROFILE=nolang python 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':
Description=uWSGI Emperor service

ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data


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