如何将 Python 3 和 Django 与 Apache 结合使用?
我的目标是使用 Apache 设置 Python 3。我最大的问题实际上是获取 mod_python.so。在我的一生中,我只找到一个可以下载它的网站(http://www.modpython.org/),而我得到的是一堆构建和安装文件。
我找不到解释如何使用 Apache 设置 Python 3 的指南。因此,如果现在有人可以引导我完成它,互联网将最终包含解决方案!
My goal is to set up Python 3 with Apache. My biggest problem is actually acquiring mod_python.so. For the life of me I found only one site where it could be downloaded (http://www.modpython.org/) and what I got was a bunch of build and install files.
I can find no guide explaining how to set up Python 3 with Apache. So if someone could walk me through it now, the internet will finally contain the solution!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要将 Python 3 与 Apache 结合使用,需要 Django 1.6+ 和 mod_wsgi 3.4+。有关更多详细信息,请参阅 scot 的回答。
Django 1.6+ and mod_wsgi 3.4+ are required to use Python 3 with Apache. For more detail refer to scot's answer.
这些答案不再适用于 Django 1.6 - 它支持 python3。 mod_wsgi 页面说版本 3.4 支持 python 3。 https://code.google.com/p/modwsgi/
不要知道此时是否一切正常(当我发现时我会返回并编辑)!答案是肯定的,它有效!
我有一个运行 Python3、Django 1.5.6、Apache2.2 和 mod_wsgi 3.4 Python 3.3.4 的 AWS EC3 Ubuntu 实例
:
ppa:fkrull/deadsnakes 是一个 apt 存储库,有多个可用的 python 版本 - 请参阅 https://launchpad.net/~fkrull/+archive/deadsnakes
然后我使用 pip 页面上的说明添加了 pip ; http://pip.readthedocs.org/en/latest/installing.html 。 (记住,此时你的 python 可能在你的路径上为“python3.3”,普通的“python”将指向 python 2.x!)
之后,virtualenv。然后我对 python 安装进行了 virtualenv 处理。激活并将环境的 bin/ 目录添加到 $PATH 后,我现在有了一个干净的 python3.x 版本。
然后,在激活虚拟环境后,我执行了“pip Django”和所有其他必要的包(数量相当多)。我有 Django 版本 1.6.2 (我一直在开发这个版本并在我的 Mac 上的 python 3.3.3 下运行没有问题)。
我遇到的最大麻烦是安装 lxml,因为它需要使用 apt-get 安装 libxml2 和 libxslt (它是 C 代码的包装器),并且我尝试了几次才意识到它们尚未安装(lxml 编译失败)。
在折腾了我的 RDS 数据库实例启动、运行和可用之后(postgresql,小心 python3 下的 mysql,你会遇到很多 python 数据库驱动程序的痛苦!但我的大多数问题是由我试图了解 AWS 安全配置引起的),相对来说一帆风顺:
安装了 apache - 接下来你需要开发包。
在那一点上,我尝试使用 mod_wsgi 的 apt 包,但我认为最好的办法是按照此处的说明自行编译和安装它 - https://code.google.com/p/modwsgi/wiki/InstallationInstructions
我在配置、制作或安装时没有遇到任何问题。确保在 virtualenv 激活的环境中编译它。
您必须手动将配置添加到 Apache 的配置中:
并且以尽可能广泛的方式,这一切现在都可以正常工作。
These answers are no longer true of Django 1.6 - it supports python3. The mod_wsgi page says version 3.4 supports python 3. https://code.google.com/p/modwsgi/
Don't know if it all works at this point though (I will return and edit when I find out)!The answer is YES it works!
I have an AWS EC3 Ubuntu instance running Python3, Django 1.5.6, Apache2.2 and mod_wsgi 3.4
Python 3.3.4:
ppa:fkrull/deadsnakes is a apt repo has has multiple python versions available - see https://launchpad.net/~fkrull/+archive/deadsnakes
Then I added pip using the instructions at the pip page; http://pip.readthedocs.org/en/latest/installing.html. (remember your python is probably on your path as 'python3.3' at this point, plain 'python' will point at python 2.x!)
After that, virtualenv. Then I virtualenv'd the python installation. Upon activation and adding the environment's bin/ directory to the $PATH I've now got a clean python3.
Then, after I activated the virtual env, I did 'pip Django' and all my other necessary packages (which were quite a few). I have Django version 1.6.2 (I've been developing on this and running under python 3.3.3 on my Mac no problem).
The most trouble I had was installing lxml because it requires libxml2 and libxslt to be installed with apt-get (it is a wrapper around the C code) and it took me a couple of attempts to realise that they were not already installed (lxml compilation fails).
After tossing about getting my RDS database instance up and running and available (postgresql, beware mysql under python3, you'll get plenty of python db driver pain! but most of my issues were caused by me trying to understand the AWS security configuration), it was relatively plain sailing:
That installs apache - and you need the dev packages for the next bit.
And that point, I tried using the apt package for mod_wsgi but I decided that the best thing to do was to compile and install it myself, following the instructions here - https://code.google.com/p/modwsgi/wiki/InstallationInstructions
I had no problems with configure, make, or make install. Make sure you compile it in your virtualenv activated environment.
You have to manually add the configuration to Apache's configuration:
And in the broadest possible way, this is all now working.
你不能用 django 取消 python3。来自 Django 常见问题解答:
http://docs。 djangoproject.com/en/dev/faq/install/#can-i-use-django-with-python-3
我可以将 Django 与 Python 3 一起使用吗?
目前还没有。 Python 3.0 对 Python 语言引入了许多向后不兼容的更改,尽管这些更改对于 Python 的未来总体来说是一件好事,但大多数 Python 软件要赶上并能够在 Python 3.0 上运行还需要一段时间。对于像 Django 这样的大型基于 Python 的软件,过渡预计至少需要一两年时间(因为它涉及放弃对旧版 Python 版本的支持,因此必须逐步完成)。
与此同时,Python 开发团队将支持 Python 2.x 版本并提供错误修复和安全更新,因此在过渡期间继续使用 Python 2.x 版本不会带来任何风险。
You can't unser python3 with django. From Django FAQ:
http://docs.djangoproject.com/en/dev/faq/install/#can-i-use-django-with-python-3
Can I use Django with Python 3?
Not at the moment. Python 3.0 introduced a number of backwards-incompatible changes to the Python language, and although these changes are generally a good thing for Python’s future, it will be a while before most Python software catches up and is able to run on Python 3.0. For larger Python-based software like Django, the transition is expected to take at least a year or two (since it involves dropping support for older Python releases and so must be done gradually).
In the meantime, Python 2.x releases will be supported and provided with bug fixes and security updates by the Python development team, so continuing to use a Python 2.x release during the transition should not present any risk.
1)我们应该对新创建的项目文件做的第一件事是调整设置。使用文本编辑器打开设置文件:
gedit myproject/settings.py
在文件底部,我们将添加一行来配置此目录。 Django 使用 STATIC_ROOT 设置来确定这些文件应存放的目录。我们将使用一些 Python 来告诉它使用项目主目录中名为“static”的目录:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
2) 我们可以通过键入以下内容将所有静态内容收集到我们配置的目录位置:
./manage.pycollectstatic
您必须确认该操作。静态文件将放置在项目目录中名为 static 的目录中。
3)你需要mod-wsgi适配器来在Apache中配置Django
安装 wsgi lib 如下所示。
sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi
4)在文件底部 /etc/apache2/sites-available/000-default.conf
5)重新启动 apache2 服务器:
6)现在转到 本地主机:
宾果!您已准备好使用 Django 和 apache。
1) The first thing we should do with our newly created project files is adjust the settings. Open the settings file with your text editor:
gedit myproject/settings.py
At the bottom of the file, we will add a line to configure this directory. Django uses the STATIC_ROOT setting to determine the directory where these files should go. We'll use a bit of Python to tell it to use a directory called "static" in our project's main directory:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
2) We can collect all of the static content into the directory location we configured by typing:
./manage.py collectstatic
You will have to confirm the operation. The static files will be placed in a directory called static within your project directory.
3)You need mod-wsgi adapter to configure Django in Apache
install wsgi lib like below.
sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi
4)At the bottom of file, /etc/apache2/sites-available/000-default.conf
5)Restart the apache2 server:
6)Now go to Localhost:
Bingo! You are all set for Django with apache.
我遇到了类似的问题,这是我解决的方法:
安装 LAMP:
要使用 python3 和 apache 在 Ubuntu 14.04 LTS 服务器上部署 Django 1.7 应用程序:
在守护进程模式下配置虚拟主机,建议:
重新启动阿帕奇。
virtualhost配置文件中的目录必须存在于创建的虚拟环境中。
I faced a similar problem and here is how I solved it:
To Install LAMP:
To DEPLOY a Django 1.7 App on Ubuntu 14.04 LTS server with python3 and apache:
Configure your virtual host in Daemon mode which is recommended:
Restart Apache .
the directories in virtualhost configuration file must exist in the virtual environment created.