在Ubuntu + Apache2 + mod_python上安装Django

这是我最近开始设置的个人开发服务器。 尽pipePython在Python中可以像cgi和独立testing脚本一样运行良好,但Django似乎没有工作。 我看到的只是django文件浏览器中的目录列表。 任何帮助,将不胜感激。

我的虚拟主机:

<VirtualHost *:80> ServerAdmin webmaster@localhost #DocumentRoot /var/www DocumentRoot /home/aj/public_html <Directory /> Options FollowSymLinks AllowOverride None </Directory> #<Directory /var/www/> <Directory /home/aj/public_html/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all AddHandler mod_python .py #PythonHandler mod_python.publisher PythonHandler mod_python.cgihandler PythonDebug On </Directory> <Location "/home/aj/public_html/old/testing/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE testing.settings PythonOption django.root /testing PythonDebug On </Location> #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ #<Directory /usr/lib/cgi-bin/> #ScriptAlias /cgi-bin/ /var/www/cgi-bin/ #<Directory /var/www/cgi-bin/> ScriptAlias /bin/ /home/aj/public_html/ <Directory /home/aj/public_html/> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog /var/log/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel debug CustomLog /var/log/apache2/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost> 

我使用Django安装

 sudo apt-get install python-django 

以下是我想要做的事情:

  1. 确保你已经安装并启用了mod_python:

     $ sudo apt-get install libapache2-mod-python
     $ sudo a2enmod python
     $ sudo invoke-rc.d apache2重新启动
    

  2. 将您的站点特定的configuration项目放到单独的虚拟主机configuration文件中:

     $ cd / etc / apache2 / sites-available
     $ sudo cp默认python-site
     $ sudo a2ensite python-site
    

  3. 尝试注释掉CGI特有的设置,并留下非CGI设置,看看它们是否是
    1. 删除ScriptAlias /bin/ /home/aj/public_html/
    2. <Directory /home/aj/public_html/>部分删除+ExecCGI选项
    3. <Directory /home/aj/public_html/>部分删除AddHandler mod_python .py
    4. <Directory /home/aj/public_html/>部分删除PythonHandler mod_python.cgihandler
  4. 确保/home/aj/public_html/old在你的PYTHONPATH中,你可能想要添加如下:

     <Location "/home/aj/public_html/old/testing/"> SetHandler python-program PythonHandler django.core.handlers.modpython PythonPath "['/home/aj/public_html/old'] + sys.path" PythonOption django.root /testing PythonDebug On SetEnv DJANGO_SETTINGS_MODULE testing.settings </Location> 

  5. 不要忘记重新加载configuration使更改生效:

     $ sudo invoke-rc.d apache2重新加载
    

另外,正如topdog已经提到,我build议移动到mod_wsgi或fcgi,而不是mod_python,这被视为弃用。

更改:

 <Location "/home/aj/public_html/old/testing/"> 

至:

 <Location "/testing"> 

这意味着它被挂载的URL,而不是物理目录。

您还缺lessPythonPath指令,因此Python知道在哪里可以find您的Django站点。

您将访问的URL是“ http:// localhost / testing ”。 如果不是您正在运行浏览器的框,请将“localhost”replace为主机的名称。

您是否阅读过Django站点上的mod_wsgi部署的Django文档?