使用 Python 3.6、Apache 2.4 和 Django 1.11 在 Ubuntu 上安装 mod_wsgi

Install mod_wsgi on Ubuntu with Python 3.6, Apache 2.4, and Django 1.11

如何为为 Python 3.6.1 编译的 Apache2 获取 mod_wsgi?

(或任何未来的 Python 版本)

我正在使用 Django 1.11 的 Python 3.6.1 虚拟环境,一切都根据 Apache 错误日志运行,除了 mod_wsgi for Apache 2.4 是为 Python/3 编译的.5.1+ 并且正在使用 Python/3.5.2 所以我的 Python 3.6.1 代码失败了,因为我正在使用 3.5.2

中不可用的新功能

设置我的系统所涉及的所有其他配置和安装似乎都很好(运行 在守护程序模式下)尽管 mod_wsgi 似乎没有使用我的 Python 3.6.1 虚拟环境(虽然根据错误日志它试图将它用于Django)...

我使用: sudo apt-get install libapache2-mod-wsgi-py3 安装 mod_wsgi for Apache 2.4

我使用: ./configure --with-python=/usr/local/bin/python3.6make 以及 make install 为 Python 3.6 安装 mod_wsgi

一定是我做错了什么,请指正!

这是我的 Apache 错误日志(稍作清理)- 是的,我知道它在 f"" 字符串行上失败(python 3.6 功能不在 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

*另外 tir.com 是 shorthand 对于我的实际站点 - 我与该域没有任何关系

执行以下操作。

卸载系统 mod_wsgi 软件包。

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

使用 pip 安装 mod_wsgi,最好安装到 Python 虚拟环境中。确保 pip 适用于您要使用的 Python 版本。

pip install mod_wsgi

显示要添加到 Apache 配置文件的配置以通过 运行 加载此 mod_wsgi:

mod_wsgi-express module-config

获取上述命令的输出以显示配置并添加到 Apache 配置中。

向 Apache 添加配置以加载 hello world WSGI 应用程序以测试其是否正常工作。

查看基于 pip 的安装的详细信息:

当我使用@Graham Dumpleton 建议使用 pip 安装 mod_wsgi 时,出现此错误:

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.

要通过 pip 成功编译和安装 mod_wsgi,我需要在 Ubuntu 上安装 apache2-dev 软件包:

sudo apt-get install -y apache2-dev

在 CentOS/Red 帽子上:

sudo yum install httpd-devel

之后 pip install mod_wsgi 成功完成。

希望对其他人有所帮助。