Isolated virtualenv and mod_wsgi - how do I do this on Apache

35 views
Skip to first unread message

Bruce Whealton

unread,
Jun 28, 2018, 11:46:42 AM6/28/18
to Django users
Hello all,
           For a  long time, this has perplexed me and I cannot find the answer.  It is a crucial recommendation for deploying Django (or any Python app for that matter) into production.
Here is the issue.  I have two web hosting accounts, one is VPS with ssh root access.  The other is shared hosting that specifically targets users of Python-based applications such as Django.  
It appears that the shared Python environment does not allow ssh access.  
            In all cases, a virtualenv is recommended for any Python deployment.  In both cases, I have Apache as the web server with mod_wsgi.  Obviously, I need the server, Apache, to activate the 
Django environment.  I can ssh to my VPS but what good is that.  I don't need my username to be activating the Python isolated environment.  As soon as I close the ssh session the activated environment
is no longer activated.  My best guess is that the Apache user must activate the virtual environment and keep it activated as an isolated virtual environment.  
            I have googled and googled for an answer to something so basic to deploying into production an isolated Python environment for Django (or any Python application) for that matter.  So, here is my best effort
to ask the question clearly.  How do I deploy into production a Django application following the best practice of using an isolated virtual environment that is activated even after I close my ssh session?

           Regarding the shared Python hosting, if I don't have ssh access I cannot even issue Pypi commands to install modules that I might need for my Django app.
Thanks,
Bruce

Gerardo Palazuelos Guerrero

unread,
Jun 28, 2018, 12:06:09 PM6/28/18
to django...@googlegroups.com

--
Gerardo Palazuelos Guerrero


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/78f2b728-ae20-4daf-9d38-221f7487af05%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kunal Grover

unread,
Jun 29, 2018, 7:48:40 AM6/29/18
to django...@googlegroups.com
You can specify your VirtualEnv using WSGIDaemonProcess: http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIPythonPath.html

Alternatively, you can use gunicorn to run your server behind Apache: https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/gunicorn/

My recommendation is to not go with Apache since you are just starting, and pick Nginx, It works much better on smaller servers with lower memory. There are a host of options when using Nginx for serving Django including Gunicorn, uWSGI. 

On Thu, Jun 28, 2018 at 9:35 PM Gerardo Palazuelos Guerrero <gerardo.p...@gmail.com> wrote:

--
Gerardo Palazuelos Guerrero


On Thu, Jun 28, 2018 at 9:46 AM, Bruce Whealton <futurewavewe...@gmail.com> wrote:
Hello all,
           For a  long time, this has perplexed me and I cannot find the answer.  It is a crucial recommendation for deploying Django (or any Python app for that matter) into production.
Here is the issue.  I have two web hosting accounts, one is VPS with ssh root access.  The other is shared hosting that specifically targets users of Python-based applications such as Django.  
It appears that the shared Python environment does not allow ssh access.  
            In all cases, a virtualenv is recommended for any Python deployment.  In both cases, I have Apache as the web server with mod_wsgi.  Obviously, I need the server, Apache, to activate the 
Django environment.  I can ssh to my VPS but what good is that.  I don't need my username to be activating the Python isolated environment.  As soon as I close the ssh session the activated environment
is no longer activated.  My best guess is that the Apache user must activate the virtual environment and keep it activated as an isolated virtual environment.  
            I have googled and googled for an answer to something so basic to deploying into production an isolated Python environment for Django (or any Python application) for that matter.  So, here is my best effort
to ask the question clearly.  How do I deploy into production a Django application following the best practice of using an isolated virtual environment that is activated even after I close my ssh session?

           Regarding the shared Python hosting, if I don't have ssh access I cannot even issue Pypi commands to install modules that I might need for my Django app.
Thanks,
Bruce

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jul 1, 2018, 8:38:56 AM7/1/18
to django...@googlegroups.com
On donderdag 28 juni 2018 17:46:42 CEST Bruce Whealton wrote:

> In all cases, a virtualenv is recommended for any Python
> deployment. In both cases, I have Apache as the web server with mod_wsgi.
> Obviously, I need the server, Apache, to activate the
> Django environment. I can ssh to my VPS but what good is that. I don't
> need my username to be activating the Python isolated environment. As soon
> as I close the ssh session the activated environment
> is no longer activated. My best guess is that the Apache user must
> activate the virtual environment and keep it activated as an isolated
> virtual environment.

Virtualenv "activation" is not magic. It simply adjusts PATH so that the
virtualenv's bin directory is first. This means that whenever "python" or "pip"
is typed into the shell it will not execute `/usr/bin/python` but
`your-virtualenv/bin/python`. Handing apache a sanitized PATH with the
virtualenv bin dir as first directory, will effectively do the same as
"activation".

(To be complete: activate also alters your shell prompt, this has no effect in
one-off application launches).

--
Melvyn Sopacua

Vinicius Assef

unread,
Jul 2, 2018, 8:29:56 AM7/2/18
to django...@googlegroups.com
I second Melvyn.

On top of what he wrote, Bruce, I work in an environment with exactly
this limitation: a shared hosting running Apache where I don't have
access
to ssh.

I build my environment (install libs, for that matter) locally and
transfer all my pack through ftp in one shot.

As Melvyn told us, "activate" a virtualenv means "put the path to your
libs first, in the PATH variable". But it can also be done inside a
Python program, too, manipulating `sys.path`.

You can see an example here:
http://blog.aprendapython.com.br/articles/setup-de-python-django-pyramid-dreamhost-1fr70/

In spite the blog post is in Brazilian Portuguese, Google Translator
is your friend and examples are in Python, afterall. ;-)

Cheers.
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2410185.edddNGrWiM%40fritzbook.

Vinicius Assef

unread,
Jul 2, 2018, 8:30:42 AM7/2/18
to django...@googlegroups.com
Maybe this other article helps you understand the essence of a
virtualenv: http://blog.aprendapython.com.br/articles/entendendo-virtualenv-1fr69/

Again, Google Translator is your friend.
Reply all
Reply to author
Forward
0 new messages