欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

apache基于mod_wsgi配置Django虚拟主机启动(针对不同端口)

程序员文章站 2022-05-19 17:16:21
...
apache基于mod_wsgi配置Django虚拟主机启动(针对不同端口)
(注:CentOS6.2和Windows7都适用)

1. 检查主机名
CentOS:
# hostname
   Windows:
          Doc窗口下执行:hostname

2. 如果主机名不符合要求,修改主机名:
(注:当执行# hostname, 返回的主机名,并不是我们预想的主机名,通俗讲,是我们提供给外网访问的资源地址, 对于局域网,通常使用的是ip访问,所以并不影响局域网)
CentOS6.2下:
步骤一:修改network文件
        # vim /etc/sysconfig/network
         修改HOSTNAME=yourhostname
        参考配置:
        NETWORKING=yes
NETWORKING_IPV6=no
#HOSTNAME=localhost.localdomain
HOSTNAME=yourhostname
步骤二:修改hosts文件
       # vim /etc/hosts
       修改 127.0.0.1 localhost.localdomain localhost
           为 127.0.0.1 localhost.localdomain localhost yourhostname
       参考配置:
           127.0.0.1 localhost.localdomain localhost yourhostname
::1 localhost6.localdomain6 localhost6
步骤三: 重启系统

3. 下载mod_wsgi
下载地址http://code.google.com/p/modwsgi/
注意下载的版本号
   下载后将文件改名为mod_wsgi.so
   并放到apache安装目录的modules目录下
   $APACH_PATH/modules
   例如40机器的目录在:/etc/httpd/modules/

4. 修改apache配置文件
例如40机器是在这里/etc/httpd/conf/httpd.conf
# vim /etc/httpd/conf/httpd.conf
文件末尾添加:
LoadModule wsgi_module modules/mod_wsgi.so
# /usr/bigbang/stargazerservice为应用的路径
Include "/usr/bigbang/stargazerservice/apache_port_django_wsgi.conf"
Include "/usr/bigbang/Unicorn/apache_port_django_wsgi.conf"
5. 进入相应的django应用的目录建立apache需要加载的文件
# cd /usr/bigbang/stargazerservice
# vim apache_port_django_wsgi.conf
文本内容:
Listen 8080
<VirtualHost *:8080>
ServerName data
WSGIScriptAlias / "/usr/bigbang/stargazerservice/django.wsgi"
Alias /site_media/ "/usr/bigbang/stargazerservice/site_media/"

<Directory /site_media>
        Order allow,deny
        Allow from all
</Directory>

<Directory "/usr/bigbang/stargazerservice/apache">
    Order allow,deny
    Allow from all
</Directory>
</VirtualHost>
   还需要新建django.wsgi文件:
   # vim django.wsgi
   文本内容:
   import os, sys

# Calculate the path based on the location of the WSGI script
apache_configuration = os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)

sys.path.append("/usr/bigbang/stargazerservice")      # add to python system path
sys.path.append("/usr/bigbang")             # add to python system path
os.chdir('/usr/bigbang/stargazerservice')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

同理Unicom这个应用的配置类似,具体的文本为:
apache_port_django_wsgi.conf文本如下:

Listen 8070
<VirtualHost *:8070>
ServerName data
WSGIScriptAlias / "/usr/bigbang/Unicorn/django.wsgi"
Alias /admin_media /usr/lib/python2.6/site-packages/django/contrib/admin/media

<Directory "/usr/bigbang/Unicorn/apache">
    Order allow,deny
    Allow from all
</Directory>
</VirtualHost>

django.wsgi文本内容如下:
import os, sys

# Calculate the path based on the location of the WSGI script
apache_configuration = os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)

sys.path.append("/usr/bigbang/Unicorn")         # add to python system path
sys.path.append("/usr/bigbang")
os.chdir('/usr/bigbang/Unicorn')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

6. 做完这步后,还需要修改应用的权限,可以让apache访问
# chown –R apache:apache myapplication_path

7. 重启apache
# service httpd restart


还存在的疑惑:
当我使用# /etc/init.d/httpd restart启动apache的时候,发现Unicom使用的环境好像是stargazer留下来的,所以无法访问,# service httpd restart启动一切正常

对应的这次的stargazerservice和unicom的部署,还需要修改相应的项目下的settings.py文件,将其中TEMPLATE_DIRS 处的“'./templates'”用
“os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),”代替,逗号一定不能忘记。