29

How do I get mod_wsgi for Apache2 that was compiled for Python 3.6.1?

(or any future Python version)

I am using a Python 3.6.1 virtual environment with Django 1.11 and Everything is working according to the Apache error log except that mod_wsgi for Apache 2.4 was compiled for Python/3.5.1+ and is using Python/3.5.2 so my Python 3.6.1 code is failing because I'm using new features not available in 3.5.2

All of the other configurations and installs involved in setting my system up seem to be fine (Running in daemon mode) though mod_wsgi doesn't seem to be using my Python 3.6.1 virtual environment (though it is trying to use it for Django according to the error log)...

I used: sudo apt-get install libapache2-mod-wsgi-py3 to install mod_wsgi for Apache 2.4

I used: ./configure --with-python=/usr/local/bin/python3.6 and make with make install to install mod_wsgi for Python 3.6

I must be doing something wrong - please correct me!

Here is my Apache Error Log (cleaned a bit) - and yes I know it fails on the f"" string line (python 3.6 feature not in 3.5)

[wsgi:warn] mod_wsgi: Compiled for Python/3.5.1+.
[wsgi:warn] mod_wsgi: Runtime using Python/3.5.2.
[wsgi:warn] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[wsgi:warn] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:error] mod_wsgi (pid=12963): Target WSGI script '/home/jamin/www/dev.tir.com/tir/tir/wsgi.py' cannot be loaded as Python module.
[wsgi:error] mod_wsgi (pid=12963): Exception occurred processing WSGI script '/home/jamin/www/dev.tir.com/tir/tir/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error]   File "/home/jamin/www/dev.tir.com/tir/tir/wsgi.py", line 21, in <module>
[wsgi:error]     application = get_wsgi_application()
[wsgi:error]   File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[wsgi:error]     django.setup(set_prefix=False)
[wsgi:error]   File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
[wsgi:error]     apps.populate(settings.INSTALLED_APPS)
[wsgi:error]   File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/django/apps/registry.py", line 116, in populate
[wsgi:error]     app_config.ready()
[wsgi:error]   File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 23, in ready
[wsgi:error]     self.module.autodiscover()
[wsgi:error]   File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
[wsgi:error]     autodiscover_modules('admin', register_to=site)
[wsgi:error]   File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
[wsgi:error]     import_module('%s.%s' % (app_config.name, module_to_search))
[wsgi:error]   File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[wsgi:error]     return _bootstrap._gcd_import(name[level:], package, level)
[wsgi:error]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[wsgi:error]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[wsgi:error]   File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
[wsgi:error]   File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
[wsgi:error]   File "<frozen importlib._bootstrap_external>", line 665, in exec_module
[wsgi:error]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[wsgi:error]   File "/home/jamin/www/dev.tir.com/tir/company/admin.py", line 13, in <module>
[wsgi:error]     from .forms import AdminInteractionForm
[wsgi:error]   File "/home/jamin/www/dev.tir.com/tir/company/forms.py", line 87
[wsgi:error]     resp = f"Hi {user.first_name}, you'll need to login to send any more suggestions. \\
[wsgi:error]                              \\n\\nFirst Time? Check your email/spam for login instructions from us."
[wsgi:error]                                                                                        
[wsgi:error]                                                                                                   ^
[wsgi:error] SyntaxError: invalid syntax

*also tir.com is shorthand for my actual site - i do not have anything to do with that domain

JxAxMxIxN
  • 1,711
  • 1
  • 17
  • 20

2 Answers2

45

Do the following.

Uninstall your system mod_wsgi package.

sudo apt-get remove libapache2-mod-wsgi-py3

Install mod_wsgi using pip, preferably into a Python virtual environment. Ensure pip is for the version of Python you want to use.

pip install mod_wsgi

Display the config to add to Apache configuration file to load this mod_wsgi by running:

mod_wsgi-express module-config

Take the output of above command to display config and add to Apache configuration.

Add configuration to Apache to load a hello world WSGI application to test it works.

See details for pip based install in:

bustawin
  • 684
  • 7
  • 11
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • 1
    I uninstalled libapache2-mod-wsgi-py3 then from my virtual environment `pip install mod_wsgi` gave me this error (there's more if you need it) `/usr/bin/ld: /usr/local/lib/libpython3.6m.a(abstract.o): relocation R_X86_64_32S against \`_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC /usr/local/lib/libpython3.6m.a: error adding symbols: Bad value collect2: error: ld returned 1 exit status error: command 'gcc' failed with exit status 1` – JxAxMxIxN Jul 05 '17 at 00:35
  • I'm going to try to recompile Python 3.6.1 with `--enable-shared` and will report back. – JxAxMxIxN Jul 05 '17 at 00:50
  • 1
    For general guidelines on how to build Python see http://blog.dscpl.com.au/2015/06/installing-custom-python-version-into.html It talks about Docker, but still relevant when installing direct to host. – Graham Dumpleton Jul 05 '17 at 00:54
  • [This](https://askubuntu.com/a/376226/707924) might be helpful for anyone having issues loading the module into apache on ubuntu – cardamom Jul 05 '17 at 13:12
  • Graham - Your solution worked (after I used your suggested Python compile technique at http://blog.dscpl.com.au/2015/06/installing-custom-python-version-into.html), and the reading material you supplied was great in helping me solve other issues as well. Though now I'm having issues with python not finding some libraries in PIL, and even builtin keywords like "hasattr" not resolving... I've even tried (from scratch) precompiled Python 3.6.1 versions. I don't think it's a wsgi issue here, but likely something I'm doing wrong again - I will figure it out... Thank you once again - you're a god!!! – JxAxMxIxN Jul 05 '17 at 19:41
  • For verifying stuff about the mod_wsgi install at least, also read http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html – Graham Dumpleton Jul 05 '17 at 21:11
  • This is very helpful! Thanks! – MadPhysicist Aug 16 '17 at 15:38
  • Not working for me. ```$ pip install mod-wsgi Traceback (most recent call last): File "", line 1, in File "/tmp/pip-install-kb2_eaz9/mod-wsgi/setup.py", line 168, in 'missing Apache httpd server packages.' % APXS) RuntimeError: The 'apxs' command appears not to be installed or is not executable. Please check the list of prerequisites in th e documentation for this package and install any missing Apache httpd server packages.``` – palewire Jun 29 '18 at 21:12
  • 3
    @palewire Create a new question. But if you look around, and also read the documentation on PyPi, you will find it is because you haven't installed the dev package for Apache. See https://pypi.org/project/mod_wsgi/ Without that ``apxs`` will not be found. The message even says as much. – Graham Dumpleton Jun 29 '18 at 21:20
  • I spent nearly a week trying to figure out why my mod_wsgi wasn't working. There was next to no useful trace logging to help. I finally realized it was my version of mod_wsgi.so . Followed these steps and resolved my problems. @GrahamDumpleton Thank you! – Nick Gotch Jul 20 '18 at 13:28
  • 1
    If you're getting the `missing Apache httpd server packages` error you may need to run `sudo apt-get install apache2-dev` – Braden Holt Oct 07 '18 at 21:25
21

When I have used the @Graham Dumpleton suggestion to install the mod_wsgi using pip, got this error:

RuntimeError: The 'apxs' command appears not to be installed or is not executable.
Please check the list of prerequisites in the documentation for this package and
install any missing Apache httpd server packages.

To successfully compile and install the mod_wsgi through pip, I need to install the apache2-dev package on Ubuntu:

sudo apt-get install -y apache2-dev

On CentOS/Red Hat:

sudo yum install httpd-devel

After that pip install mod_wsgi completes successfully.

Hope that might help others.

Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82