Search code examples
pythondjangoapache2mod-wsgihttpd.conf

Django on Apache2: Issue in.conf-file?


Good day to all of you,

together with our admin in our company, we're trying to deploy Django on Apache2 together with mod_wsgi, but we're facing some minor issues. I'm hoping, some of you might help and take a look at the following lines!?

Right now, the directory on our server is looking like this:

./var/www/
        |-- myproject
            |-- manage.py
            |-- project/
                |-- __init__.py
                |-- settings.py
                |-- urls.py
                |-- wsgi.py
            |-- statics
            |-- venv

When I'm trying to open the site, it just keeps on loading but nothings gonna happen! I'm wondering wether the data inside our .conf-file might be wrong!

<VirtualHost *:80>
. . .

Alias /static /var/www/myproject/static
<Directory /var/www/myproject/static>
    Require all granted
</Directory>

<Directory /var/www/myproject/project>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess myproject python-path=/var/www/myproject python-home=/var/www/myproject/venv
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/myproject/project/wsgi.py

</VirtualHost>

If I understood it correctly, the "python-home" inside the "WSGIDaemonProcess" should point onto my Virtual Environment to collect the necessary components and the "python-path" onto my project!?

import os 
import time 
import traceback 
import signal 
import sys 

from django.core.wsgi import get_wsgi_application 

sys.path.append('/var/www/myproject/project')
sys.path.append('/var/www/myproject/venv/lib/python3.7/site-packages') 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") 

try: 
    application = get_wsgi_application() 
except Exception: 
    # Error loading applications 
    if 'mod_wsgi' in sys.modules: 
        traceback.print_exc() 
        os.kill(os.getpid(), signal.SIGINT) 
        time.sleep(2.5) 

Does the second "sys.get.append" has to point to my venv or to the location where our admin installed the actutal python-version(3.7)?

I also noticed, that the current running python-version on the server is 2.7.6, although my admin installed version 3.7. The installed mod_wsgi is "libapache2-mod-wsgi-py3" for python 3x! Could this also be a problem?

Thanks and a great day to all of you!


Solution

  • i think you should activate first the virtual environment, that's why you got the default version 2.7.6 of python and not 3.7 as expected, to do so, you have to add those two lines in your wsgi.py file at the very top

    activate_this = '/var/www/myproject/venv/bin/activate_this.py'
    exec(open(activate_this).read(), dict(__file__=activate_this))
    [..]
    

    refer to this topic https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#daemon-mode-multiple-applications

    Note

    since you've chosen venv over mkviratualenv the easiest option is to download activate_this.py script from virtualenv repository :https://github.com/dcreager/virtualenv/blob/master/virtualenv_support/activate_this.py and put it under venv/bin (or venv/Scripts if you are on windows) since it's not shipped with (because it's not an official part of it)

    for more detailed infos refer to this thread How can I activate a pyvenv vitrualenv from within python? (activate_this.py was removed?)