如何将 Python 3 和 Django 与 Apache 结合使用?

发布于 2024-11-05 19:28:52 字数 218 浏览 0 评论 0原文

我的目标是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦过后 2024-11-12 19:28:52

要将 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.

时光无声 2024-11-12 19:28:52

这些答案不再适用于 Django 1.6 - 它支持 python3。 mod_wsgi 页面说版本 3.4 支持 python 3https://code.google.com/p/modwsgi/

不要知道此时是否一切正常(当我发现时我会返回并编辑)!

答案是肯定的,它有效!

我有一个运行 Python3、Django 1.5.6、Apache2.2 和 mod_wsgi 3.4 Python 3.3.4 的 AWS EC3 Ubuntu 实例

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get install python3.3
sudo apt-get install python3.3-dev python3.3-doc idle-python3.3

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 安全配置引起的),相对来说一帆风顺:

sudo apt-get install apache2 apache2-threaded-dev

安装了 apache - 接下来你需要开发包。

在那一点上,我尝试使用 mod_wsgi 的 apt 包,但我认为最好的办法是按照此处的说明自行编译和安装它 - https://code.google.com/p/modwsgi/wiki/InstallationInstructions

我在配置、制作或安装时没有遇到任何问题。确保在 virtualenv 激活的环境中编译它。

您必须手动将配置添加到 Apache 的配置中:

# wsgi module
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
# now configure it
<Directory /my/app/path>
 <Files wsgi.py>
  Order deny,allow
  Allow from all
 </Files>
</Directory>
WSGIScriptAlias / /my/app/path/wsgi.py
WSGIPythonPath /my/app:/path/to/the/virtual/env/lib/python3.3/site-packages

并且以尽可能广泛的方式,这一切现在都可以正常工作。

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:

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get install python3.3
sudo apt-get install python3.3-dev python3.3-doc idle-python3.3

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:

sudo apt-get install apache2 apache2-threaded-dev

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:

# wsgi module
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
# now configure it
<Directory /my/app/path>
 <Files wsgi.py>
  Order deny,allow
  Allow from all
 </Files>
</Directory>
WSGIScriptAlias / /my/app/path/wsgi.py
WSGIPythonPath /my/app:/path/to/the/virtual/env/lib/python3.3/site-packages

And in the broadest possible way, this is all now working.

送舟行 2024-11-12 19:28:52

你不能用 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.

我家小可爱 2024-11-12 19:28:52

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

WSGIPythonPath /var/www/html/myproject/ 
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py


  Alias /static /var/www/html/myproject/static/

<Directory /var/www/html/myproject/static>
    Require all granted
</Directory>

<Directory /var/www/html/myproject/myproject>
    <Files wsgi.py>
         Order deny,allow
        Require all granted
    </Files>
</Directory>

Alias /media/ /var/www/html/myproject/media/
<Directory /var/www/html/myproject/media>
Require all granted
</Directory> 
WSGIDaemonProcess myproject python-path=/var/www/html/myproject/

5)重新启动 apache2 服务器:

sudo service apache2 restart

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

WSGIPythonPath /var/www/html/myproject/ 
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py


  Alias /static /var/www/html/myproject/static/

<Directory /var/www/html/myproject/static>
    Require all granted
</Directory>

<Directory /var/www/html/myproject/myproject>
    <Files wsgi.py>
         Order deny,allow
        Require all granted
    </Files>
</Directory>

Alias /media/ /var/www/html/myproject/media/
<Directory /var/www/html/myproject/media>
Require all granted
</Directory> 
WSGIDaemonProcess myproject python-path=/var/www/html/myproject/

5)Restart the apache2 server:

sudo service apache2 restart

6)Now go to Localhost:

Bingo! You are all set for Django with apache.

゛清羽墨安 2024-11-12 19:28:52

我遇到了类似的问题,这是我解决的方法:
安装 LAMP:

   sudo apt-get install lamp-server^
 // then do more from here:
   //https://help.ubuntu.com/community/ApacheMySQLPHP
 // If you will run python services then : 
       apt-get install python-mysqldb 
//and for python 3 install: 
   sudo apt-get install python3-pip python-dev build-essential
   pip3.4 install mysqlclient  // for mysql       connection 
   sudo apt-get install build-essential python-dev libmysqlclient-dev

   pip3 install virtualenvwrapper
   sudo nano ~/.bash_profile
  //Set location of virtualenvs by pasting this in above: 
   export WORKON_HOME=$HOME/.virtualenvs 
   source /usr/local/bin/virtualenvwrapper.sh
//Reload startup file:
  source ~/.bash_profile
   mkvirtualenv -p /usr/bin/python3 myprojectenv
   workon myprojectenv
//then you can install packages you want using pip
//and copy myproject here

要使用 python3 和 apache 在 Ubuntu 14.04 LTS 服务器上部署 Django 1.7 应用程序:

   sudo apt-get install libapache2-mod-wsgi-py3
 //then configure your apache virtualhosts( here our project is   //myproject    and environment is myprojectenv):

在守护进程模式下配置虚拟主机,建议:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAdmin [email protected]

    DocumentRoot /home/user/.virtualenvs/myprojectenv/public_html


    <Directory /home/user/.virtualenvs/myprojectenv/myproject/myproject>

     <Files wsgi.py>
     Require all granted
    </Files>
    </Directory>

    ErrorLog /home/user/.virtualenvs/myprojectenv/logs/error.log
    CustomLog /home/user/.virtualenvs/myprojectenv/logs/access.log  combined
    WSGIScriptAlias /  /home/user/.virtualenvs/myprojectenv/myproject/myproject/wsgi.py

    WSGIDaemonProcess myproject python- path=/home/user/.virtualenvs/myprojectenv/prisec:/home/user/.virtualenvs/m yprojectenv/lib/python3.4/site-packages

    WSGIProcessGroup myproject
    Alias /static /home/user/.virtualenvs/myprojectenv/public_html/static

    <Directory /home/user/.virtualenvs/myprojectenv/public_html/static>
     Require all granted
    </Directory>
     Alias /media
  /home/user/.virtualenvs/myprojectenv/public_html/media
       <Directory        /home/user/.virtualenvs/myprojectenv/public_html/media>
     Require all granted
    </Directory>

重新启动阿帕奇。
virtualhost配置文件中的目录必须存在于创建的虚拟环境中。

I faced a similar problem and here is how I solved it:
To Install LAMP:

   sudo apt-get install lamp-server^
 // then do more from here:
   //https://help.ubuntu.com/community/ApacheMySQLPHP
 // If you will run python services then : 
       apt-get install python-mysqldb 
//and for python 3 install: 
   sudo apt-get install python3-pip python-dev build-essential
   pip3.4 install mysqlclient  // for mysql       connection 
   sudo apt-get install build-essential python-dev libmysqlclient-dev

   pip3 install virtualenvwrapper
   sudo nano ~/.bash_profile
  //Set location of virtualenvs by pasting this in above: 
   export WORKON_HOME=$HOME/.virtualenvs 
   source /usr/local/bin/virtualenvwrapper.sh
//Reload startup file:
  source ~/.bash_profile
   mkvirtualenv -p /usr/bin/python3 myprojectenv
   workon myprojectenv
//then you can install packages you want using pip
//and copy myproject here

To DEPLOY a Django 1.7 App on Ubuntu 14.04 LTS server with python3 and apache:

   sudo apt-get install libapache2-mod-wsgi-py3
 //then configure your apache virtualhosts( here our project is   //myproject    and environment is myprojectenv):

Configure your virtual host in Daemon mode which is recommended:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAdmin [email protected]

    DocumentRoot /home/user/.virtualenvs/myprojectenv/public_html


    <Directory /home/user/.virtualenvs/myprojectenv/myproject/myproject>

     <Files wsgi.py>
     Require all granted
    </Files>
    </Directory>

    ErrorLog /home/user/.virtualenvs/myprojectenv/logs/error.log
    CustomLog /home/user/.virtualenvs/myprojectenv/logs/access.log  combined
    WSGIScriptAlias /  /home/user/.virtualenvs/myprojectenv/myproject/myproject/wsgi.py

    WSGIDaemonProcess myproject python- path=/home/user/.virtualenvs/myprojectenv/prisec:/home/user/.virtualenvs/m yprojectenv/lib/python3.4/site-packages

    WSGIProcessGroup myproject
    Alias /static /home/user/.virtualenvs/myprojectenv/public_html/static

    <Directory /home/user/.virtualenvs/myprojectenv/public_html/static>
     Require all granted
    </Directory>
     Alias /media
  /home/user/.virtualenvs/myprojectenv/public_html/media
       <Directory        /home/user/.virtualenvs/myprojectenv/public_html/media>
     Require all granted
    </Directory>

Restart Apache .
the directories in virtualhost configuration file must exist in the virtual environment created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文