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

29

如何获取编译自Python 3.6.1的mod_wsgi适用于Apache2?

(或任何未来的Python版本)

我正在使用Python 3.6.1虚拟环境,Django 1.11,根据Apache错误日志,除了为Python/3.5.1+编译的Apache 2.4的mod_wsgi使用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 安装了适用于Apache 2.4的mod_wsgi

我使用以下命令: ./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是我实际网站的速记方式 - 我与该域名无关


你从哪里获取到 Python 3.5 的 .so 文件? - David Torrey
2个回答

45

请按照以下步骤操作。

卸载您的系统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的安装详细信息,请参见:


1
我卸载了libapache2-mod-wsgi-py3,然后在我的虚拟环境中运行pip install mod_wsgi时出现了以下错误(如果需要更多信息,请告诉我): /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
1
有关如何构建Python的一般指南,请参阅http://blog.dscpl.com.au/2015/06/installing-custom-python-version-into.html。它谈到了Docker,但在直接安装到主机时仍然相关。 - Graham Dumpleton
为了验证有关mod_wsgi安装的信息,还需阅读http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html。 - Graham Dumpleton
3
请创建一个新问题。如果您环顾四周,并阅读PyPi的文档,就会发现问题是因为您没有安装Apache的开发包。请参见 https://pypi.org/project/mod_wsgi/。若没有该包,“apxs”将无法被找到。提示信息已经明确说明了这一点。 - Graham Dumpleton
1
如果你遇到了“缺少 Apache httpd 服务器软件包”的错误,可能需要运行sudo apt-get install apache2-dev命令。 - Braden Holt
显示剩余6条评论

22

当我使用 @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 Hat 上:

sudo yum install httpd-devel

在那之后,pip install mod_wsgi 成功完成。

希望这可以帮到其他人。


1
在CentOS上,执行yum install httpd-devel命令。 - Afshin Mehrabani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接