Last active
April 29, 2025 11:41
-
-
Save miketery/ed9d2d56e702325bf76ac83d358fc1b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source https://bradenmacdonald.com/blog/2015/uwsgi-emperor-multiple-python | |
Building a uWSGI Emperor for multiple python versisons | |
September 5, 2015 | |
This is a quick update to my tutorial on hosting Django Apps on Ubuntu with Nginx and uWSGI. I recently wanted to move to a mix of Python 3.4 and Python 2.7 web apps on one of my servers that was set up as described in that tutorial. Here's how to do that. | |
Normally, I just install uWSGI using pip. The big downside of that approach is that it only builds uWSGI for the version of Python with which you install it. So to get one emperor that can run apps (vassals) with different versions of Python, we need to compile uWSGI. Luckily it's really easy. | |
Here is an example of the required steps. Just run the following commands as root. | |
First download and extract uWSGI. Here's an example; check the website for the latest version and update the commands accordingly: | |
wget http://projects.unbit.it/downloads/uwsgi-2.0.11.1.tar.gz | |
tar zxvf uwsgi-2.0.11.1.tar.gz | |
cd uwsgi-2.0.11.1/ | |
One thing before we compile: we need to tell uWSGI where it's going to find the various python plugins. Edit buildconf/base.ini and set plugin_dir = /usr/local/lib/uwsgi/ . You are now ready to compile uWSGI. | |
We'll compile uWSGI without Python support, then separately compile plugins for each version of Python we want to support: | |
# may need to install python3-devel | |
sudo yum install python3-devel -y | |
make PROFILE=nolang | |
PYTHON=python3.4 ./uwsgi --build-plugin "plugins/python python34" | |
PYTHON=python2.7 ./uwsgi --build-plugin "plugins/python python27" | |
Now you could run uWSGI from this folder, but I prefer to install it: | |
mkdir /usr/local/lib/uwsgi/ | |
cp python*_plugin.so /usr/local/lib/uwsgi/ | |
cp uwsgi /usr/local/bin/uwsgi | |
Finally, you'll want an upstart or systemd file to boot uwsgi. Here's an upstart example (same as in my previous tutorial), which you would install as /etc/init/uwsgi.conf: | |
# Emperor uWSGI script | |
description "uWSGI Emperor" | |
start on runlevel [2345] | |
stop on runlevel [06] | |
exec uwsgi --die-on-term --emperor /etc/uwsgi --emperor-tyrant --logto /var/log/uwsgi.log | |
For each web app you want to run, create a config file in /etc/uwsgi that's owned by the user you want that app to run under. Include the setting plugin = python27 or plugin = python34 to indicate which python plugin you want to use for that app. | |
Example of an app config file in /etc/uwsgi/ (note %n mean "the filename without extension" which makes this sort of settings file really easy to re-use): | |
[uwsgi] | |
socket = /apps/%n/uwsgi/socket | |
chmod-socket = 664 | |
master = true | |
processes = 2 | |
virtualenv = /apps/%n/app/venv | |
pythonpath = /apps/%n/app/ | |
module = %n.wsgi | |
pidfile2 = /apps/%n/uwsgi/pid | |
daemonize = /apps/%n/uwsgi/log | |
plugin = python27 | |
You now have a shiny new uWSGI emperor, serving multiple python apps with support for python 2.7 and 3.4. | |
/// MORE | |
apt-get install build-essential python-dev | |
$ sudo apt install \ | |
python3.8 python3.8-dev python3-distutils uwsgi uwsgi-src uuid-dev libcap-dev libpcre3-dev | |
plugins-dir = /usr/local/lib/uwsgi | |
export PYTHON=python3.8 | |
uwsgi --build-plugin "/usr/src/uwsgi/plugins/python python38" | |
sudo mv python38_plugin.so /usr/lib/uwsgi/plugins/python38_plugin.so | |
sudo chmod 644 /usr/lib/uwsgi/plugins/python38_plugin.so | |
// test | |
uwsgi --plugin python38 -s :0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment