Apache-2.2

centos 6.4 - django 部署與 apache 和 mod_wsgi

  • April 16, 2013

我正在嘗試在帶有 centos 6.4 的 VM 上部署一個帶有 apache 和 mod_wsgi 的基本 django 項目。這是項目樹:

myproj/
├── manage.py
├── myapp
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── myproj
   ├── __init__.py
   ├── __init__.pyc
   ├── settings.py
   ├── settings.pyc
   ├── urls.py
   └── wsgi.py

這是 wsgi.py 文件(它是 django-admin.py startproject 生成的預設文件):

"""
WSGI config for myproj project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""
import os

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "myproj.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproj.settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

這是虛擬主機配置(/etc/httpd/conf/httpd.conf):

#WSGIPythonPath /home/www/myproj/myproj
<VirtualHost *:82>
    ServerAdmin webmaster@example.com
    WSGIScriptAlias /django /home/www/myproj/mypproj/wsgi.py
    WSGIDaemonProcess myproj user=apache threads=3
    <Directory />
       Options FollowSymLinks
       AllowOverride None
    </Directory>
    DocumentRoot /tmp
    ServerName www.example.com
    ServerAlias www.example.com
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>

雖然我可以訪問索引 apache 頁面(我的本地網路上的 192.168.0.15:80),但如果我嘗試將我的兄弟指向 192.168.0.15:82,我會得到“無法訪問目標”。知道配置中缺少什麼或有什麼問題嗎?在訪問或錯誤日誌中沒有活動痕跡…


這是修改後的虛擬主機配置:

<VirtualHost *:82>
    ServerAdmin webmaster@example.com
    WSGIScriptAlias /django /home/www/myproj/myproj/wsgi.py
    WSGIDaemonProcess myproj user=apache threads=3 python-path=/home/www/myproj/myproj/:/usr/lib/python2.6/site-packages
    WSGIProcessGroup myproj
    <Directory /home/www/myproj/myproj>
       <Files wsgi.py>
           Order allow,deny
           Allow from all
       </Files>
    </Directory>
    #DocumentRoot /tmp
    #ServerName www.example.com
    #ServerAlias www.example.com
    #Alias /media/ /home/www/myproj/
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>

…什麼也沒有變。在:

wget 192.168.0.15:82

我得到“連接被拒絕”。Debian 機器上的相同配置可以工作(在這種情況下映射到埠 80)。有什麼幫助嗎?

如果要直接訪問埠 82,則需要在伺服器的防火牆中打開相應的埠。您可以使用system-config-firewall-tui命令行工具執行此操作。

引用自:https://serverfault.com/questions/499289