Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uwsgi specify python binary

On our production server we are running Ubuntu 12.04, to make sure that our application is running in a predefined consistent environment we use Pythonbrew to compile a custom Python. I have created a user that is going to be running our API server, the user has his own Python 2.7.2 environment created with Pythonbrew, with the necessary packages installed using pip.

I've also installed uWSGI on the system, it's going to wrap the API application and make it available to Apache via mod_wsgi. The problem is that I can't get uWSGI to use the correct python binary. The current config looks like this:

[uwsgi]
socket = 127.0.0.1:3031
processes = 4
chdir = /home/app/
pythonpath = /home/app
gid = 1002
uid = 1002
module = api
callable = APP
enable-threads = true

When I try to run this config on the terminal with:

uwsgi --ini app.ini

It fails to import some module. This module is installed only in the Pythonbrew environment that the API user is using. The problem is that uWSGI is using the default python binary in /usr/bin.

One solution is to use a separate uWSGI installed using pip in the API user's environment, but I really want to use the system's uWSGI because it integrates better with the OS.

Is there any way to specify which Python binary uWSGI should use, other then by installing a separate one? If I have to install a separate instance of uWSGI, what is the best way to have it start on system boot?

Edit: just realised that it is probably not using the python binary, but just lining against it's library, so there is no way for me to use the default installation of wsgi, and the non-default python. But the question still remains, what is the best way to integrate a custom built uWSGI into the system.

like image 958
Blubber Avatar asked Nov 03 '22 14:11

Blubber


2 Answers

uWSGI (as well as mod_wsgi) does not use the python binary, but the python shared library, so you have to link the uWSGI python plugin with the specific library of the pythonbrew user.

There are a lot of tricks to accomplish that (expecially if the pythonbrew python version matches the system version), the first one popping into my mind is specifying the pythonbrew user's prefix in its config file with

env = PYTHONHOME=path

By the way, is not better to use virtualenvs instead of full python installations for your users ?

like image 128
roberto Avatar answered Nov 06 '22 03:11

roberto


I've run into this issue as well. It seems that uWSGI binds the Python 3 runtime during install. Once you've pip installed a version, it uses the cached wheel (hence cached Python version).

I've found this will fix it by forcing a rebuild of the binary, at least on Ubuntu:

$ pip uninstall uwsgi
$ pip install uwsgi --no-cache
$ COMMAND_TO_RESTART_UWSGI
like image 33
Michael Kennedy Avatar answered Nov 06 '22 05:11

Michael Kennedy