how to specify python3 for wsgihandler

665 views
Skip to first unread message

David Zejda

unread,
Nov 11, 2019, 5:42:56 PM11/11/19
to web2py-users
Hello to all.

I have a web2py with several apps on a production server behind Apache accessed via wsgihandler. So far, all the apps have been developed for python2.

To support python3 apps (preferred for new projects), I set-up a separate web2py directory and adjusted the Apache configs respectively.

The question is - how to set this new web2py instance to run on python3?

For compatibility reasons, /usr/bin/env python still translates to python 2 interpreter. 

Do I have to change all the #!/usr/bin/env python lines in web2py sources to point to python3 instead?
Or where else can I specify the python version?

Thank you very much for an answer in advance!

David

David Zejda

unread,
Nov 22, 2019, 12:39:01 PM11/22/19
to web2py-users
Hi again.

Well, I found out that one Apache instance can't handle applications with different python interpreters via mod_wsgi.

So, I decided to set up a virtual environment for a web2py instance running python3 applications with its own Apache and mod_wsgi.

Since it is virtual environment, I decided if favour of pip instead of the operating system-specific packaging system.

Apache+mod_wsgi and the script mod_wsgi-express (refer to http://blog.dscpl.com.au/2015/04/introducing-modwsgi-express.html) makes the process easier:

Provided that the web2py for python3 apps is in /opt/web2py3, my steps were roughly:

sudo apt-get install python3-pip virtualenvwrapper
pip3 install --user --upgrade virtualenvwrapper
cd ~/.local/bin
mkvirtualenv -p /usr/bin/python3 /opt/python3env/web2py
cd /opt/python3env/web2py/bin
./pip3 install mod_wsgi-httpd
./pip3 install mod_wsgi
cd /opt/web2py3
/opt/python3env/web2py/bin/mod_wsgi-express \
  --user www-data \
  --group www-data \
  --port 8830 \
  --working-directory /opt/web2py3
  --server-root /opt/python3env/web2py/etc
  --log-directory /opt/python3env/web2py/log
  --access-log
  --startup-log
  --rotate-logs
  start-server wsgihandler.py

I will probably use one public IP address for web2py with python2-based apps served by the legacy Apache and a different IP address for python3-based apps served by the Apache from the virtual environment (configured as a bridge on the same network interface), so I can stick to default ports for HTTP and HTTPS, which seems a better solution than proxying from one Apache instance to the other.

David

David Zejda

unread,
Dec 4, 2019, 11:31:48 AM12/4/19
to web2py-users
I needed SSL with per-domain certificates. Because mod_wsgi-express, though easy to install, becomes inflexible if virtual hosts settings need to be adjusted eventually, I switched to a second instance of Apache from the system-specific packaging. The steps like this:

cd /usr/share/doc/apache2/examples/
./setup-instance python3

The new instance can be launched as:

systemctl start apa...@python3.service

To get WSGI support for python3 it was not possible to simply install the relevant mod-wsgi module package (libapache2-mod-wsgi-py3) alongside with its python2 version (libapache2-mod-wsgi), because they are in conflict.

So, I had to install the module from source, such as:

aptitude install apache2-dev
cd /opt
tar -xvzf 4.6.5.tar.gz
cd mod_wsgi-4.6.5
which python3.7
./configure --with-python=/usr/bin/python3.7

add and enable the mod_wsgi.so (located in my case in /opt/mod_wsgi-4.6.5/src/server/.libs/mod_wsgi.so) in the apache-python3 instance instead of the python2 version. As long as the correct module is loaded, no need to specify the interpreter in Apache configs; at least in my case.

I hope it helps someone ...

David

Reply all
Reply to author
Forward
0 new messages