[Django]-Django/Apache & virtualenv on Ubuntu not using the right Python interpreter

3👍

I am not sure this will be helpful, but I found it to be tricky the first time I set up Apache with mod_wsgi under a custom Python environment (3.4). I also had a similar problem making sure that Apache used my specific Python version.

Here are the steps I went through to get Apache and mod_wsgi working with a custom Python version. They may be helpful.

1.On a clean install of Ubuntu, I installed apache2 and apache2-dev. Note: I did not install mod_wsgi from the repos.

2.I then downloaded this version of mod_wsgi* and compiled it against the specific Python3.4 I was using (which I had also previously built from source, but I don’t think that should be necessary).

3.From inside the directory where I downloaded and unpacked mod_wsgi, I did the usual package-building, as root, with the added step of specifying the Python executable mod_wsgi should be compiled against:

   $ ./configure --with-python=/opt/python3.4/bin/python3.4
   $ make
   $ make install

After that, I configured the following files:

4./etc/apache2/httpd.conf

LoadModule dir_module   /usr/lib/apache2/modules/mod_dir.so
LoadModule env_module   /usr/lib/apache2/modules/mod_env.so
LoadModule mime_module  /usr/lib/apache2/modules/mod_mime.so
LoadModule wsgi_module  /usr/lib/apache2/modules/mod_wsgi.so

ServerName localhost
WSGIPythonHome /location/of/virtualenv's/bin
WSGIRestrictEmbedded On
WSGILazyInitialization On

(That mod_wsgi above is the one that should have been built in the previous command.)

5./etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    Alias /media /var/www/django_media/
    Alias /static /var/www/django_static/

    <Directory /var/www/django_media>
        Options FollowSymLinks
        AllowOverride None
        Order Deny,Allow
        Allow from all
    </Directory>
    <Directory /var/www/django_static>
        Options FollowSymLinks
        AllowOverride None
        Order Deny,Allow
        Allow from all
    </Directory>

    WSGIApplicationGroup %{GLOBAL}
    KeepAlive Off
    WSGIDaemonProcess site-name.com processes=2 threads=6 python-path=/var/www/location/env/lib/python3.4/site-packages:/var/www/location/env/bin:/var/www/location/location
    WSGIProcessGroup site-name.com
    WSGIScriptAlias / /var/www/location/location/location/wsgi.py

    <Directory /var/www/location/location>
        <Files wsgi.py>
           Order allow,deny
           Allow from all
    </Files>
    </Directory>

    LogLevel info
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    CustomLog log/location.log combined
    ErrorLog log/error/location.log
</VirtualHost>

6.Finally, I created a symlink to the default above:

$ ln -s /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-enabled/000-default

This may be useful as you try to figure out why your Apache version is not using the right Python? Personally, I would guess that mod_wsgi is to blame.

👤erewok

0👍

To use python 3 interpreter with django use following steps:

1) check python 3 path in your system by using -> which python3
output will be like this  ->  /usr/bin/python3
2) then create a virtual environment-> virtualenv -p /usr/bin/python3 project
it will create a virtual environment name as project
3)to activate it run- > . project/bin/activate
👤Sunit

0👍

I got it working by following @GrahamDumpleton’s comment.

you don’t need to rebuild mod_wsgi as a py3 package is available. Here is what i did:

  • remove mod_wsgi package and dependencies:

    sudo apt-get remove libapache2-mod-wsgi
    sudo apt-get autoremove
    
  • install mod_wsgi for python 3:

    sudo apt-get install libapache2-mod-wsgi-py3
    
  • here is my apache configuration: /etc/apache2/sites-available/000-default.conf

    <VirtualHost *:80>
    WSGIDaemonProcess irmaweb python-path=/var/www/irmaweb/irmaweb:/var/www/irmaweb/env/lib/python3.4/site-packages
    WSGIProcessGroup irmaweb
    WSGIScriptAlias / /var/www/irmaweb/irmaweb/irmaweb/wsgi.py
    Alias /site_media/static/ /var/www/irmaweb/irmaweb/irmaweb/site_media/static/  
    ...
    
  • then, restart apache:

    sudo service apache2 restart
    

I also add to specify the full path of the db file in my settings.py, and made a chmod 664 on it.

👤Lionel

Leave a comment