I've recently deployed an application online using DigitalOcean single-click droplet setup which setup Django on Ubuntu with nginx and gunicorn. It came with a default django project which I've managed to change to my own. However, the default project doesn't use a virtualenv, it uses a system-wide install. So, the app only works if all the dependencies are installed on the system. I know this because if I uninstall django, it gives me an internal server error.
I would like to use the python in my virtualenv as the interpreter. And refer to the site-packages in that environment. I've tried fiddling with the PYTHONPATH and adding sys.path.append('/home/env/projectname') to the wsgi file but this doesn't work.
How can I achieve this?
/etc/init/gunicorn.conf:
setuid django
setgid django
chdir /home/env/projectname
exec gunicorn \
--name=prj \
--pythonpath=prj \
--bind=127.0.0.1:9000 \
--config /etc/gunicorn.d/gunicorn.py \
prj.wsgi:application
Now, gunicorn is a Python program. It doesn't run your Django project; it imports your Django project. So you can't possibly run gunicorn and your Django project in different environments, because they are actually one program. The way to run your Django project in a specific isolated environment is to start gunicorn in that isolated environment:
For this to work, you must either have installed gunicorn in the virtualenv, or you must have installed gunicorn system-wide and created the virtualenv with
--system-site-packages
.See also this article of mine.
Try installing the gunicorn into virtualenv and run it from there setting up the working directory and passing the wsgi:application. This tutorial may help you. This tutorial may also be useful.