09.Apache 2.4 基于端口配置多个Diango项目

08-06 12:16:00   来源:   评论: 点击:

写在前边的话:基于多端口配置普通的web项目是非常简单的,大家上网搜索就能得到很多答案,而且不会出错,然而

写在前边的话:

基于多端口配置普通的web项目是非常简单的,大家上网搜索就能得到很多答案,而且不会出错,然而在配置Django的多端口时,却出现了各种bug,苦苦的折腾了一晚上
环境 说明:
服务器 Windows Server 2008
Apache 2.4.4
Python 2.7.10
Mysql 5.7
Mod_Wsgi 3.4
在进行配置之前,首先要保证你的环境是Ok的,这样才能保证配置过程中,不会因为环境缺少问题而出错,这里假设你所需要的软件都已安装完毕

1:加载 mod_wsgi

修改Apache目录下的conf/httpd.conf 文件,加入下面这行

<span style="font-size:14px;">    LoadModule wsgi_module modules/mod_wsgi.so</span><span style="font-size:18px;"></span>

2:打开端口虚拟映射

和1中的文件相同,找到如下
# Virtual hosts    # Include conf/extra/httpd-vhosts.conf
将第二行的#去掉,即允许Apache进行端口映射

3:添加端口

还是httpd.cond 文件,大约在五十多行吧,添加你的端口,我这里设置为8080和8081
#Listen 12.34.56.78:80
Listen 8080
Listen 8081

4:打开 conf/extra/httpd-vhosts.conf

原来是这样的

<VirtualHost *:80>    ServerAdmin webmaster@dummy-host.example.com    DocumentRoot "c:/Apache24/docs/dummy-host.example.com"    ServerName dummy-host.example.com    ServerAlias www.dummy-host.example.com    ErrorLog "logs/dummy-host.example.com-error.log"    CustomLog "logs/dummy-host.example.com-access.log" common</VirtualHost><VirtualHost *:80>    ServerAdmin webmaster@dummy-host2.example.com    DocumentRoot "c:/Apache24/docs/dummy-host2.example.com"    ServerName dummy-host2.example.com    ErrorLog "logs/dummy-host2.example.com-error.log"    CustomLog "logs/dummy-host2.example.com-access.log" common</VirtualHost>
修改端口号,加载Django的配置文件,配置之后是这样的

<VirtualHost *:8080>  ServerName blog_analysic  WSGIScriptAlias / D:/WWW/blog_analysic/blog_analysic/wsgi.py  <Directory D:/WWW/blog_analysic/blog_analysic/>       <Files wsgi.py>         Require all granted       </Files>    </Directory>     Alias /static D:/WWW/blog_analysic/static  <Directory D:/WWW/blog_analysic/static>       Require all granted    </Directory>     DocumentRoot "D:/WWW/blog_analysic"  <Directory "D:/WWW/blog_analysic">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted  </Directory></virtualHost><VirtualHost *:8081>  ServerName CyanScikit  WSGIScriptAlias / D:/WWW/CyanScikit/CyanScikit/wsgi.py  <Directory D:/WWW/CyanScikit/CyanScikit/>       <Files wsgi.py>         Require all granted       </Files>    </Directory>     Alias /static D:/WWW/CyanScikit/static    <Directory D:/WWW/CyanScikit/static>       Require all granted    </Directory>        DocumentRoot "D:/WWW/CyanScikit/templates"     <Directory "D:/WWW/CyanScikit/templates">         Options Indexes FollowSymLinks         AllowOverride None         Require all granted     </Directory></virtualHost>
分别监控8080和8081端口,且我的项目目录在D盘根目录的WWW文件夹下,其中包含两个项目 blog_analysic 和CyanScikit

PS:网上查的资料都是基本配置到这里就完事了,就可以重启Apache服务器了,这样的现象是服务器重启OK,但是访问我们 的web项目时会报错如下:



这时候查看服务器日志 logs/error文件

mod_wsgi (pid=3548): Target WSGI script 'D:/WWW/blog_analysic/blog_analysic/wsgi.py' cannot be loaded as Python module. mod_wsgi (pid=3548): Exception occurred processing WSGI script 'D:/WWW/blog_analysic/blog_analysic/wsgi.py'. Traceback (most recent call last):   File "D:/WWW/blog_analysic/blog_analysic/wsgi.py", line 16, in <module>     application = get_wsgi_application()   File "D:\Program Files\Python27\lib\site-packages\django-1.8.8-py2.7.egg\django\core\wsgi.py", line 14, in get_wsgi_application     django.setup()   File "D:\Program Files\Python27\lib\site-packages\django-1.8.8-py2.7.egg\django\__init__.py", line 17, in setup     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)   File "D:\Program Files\Python27\lib\site-packages\django-1.8.8-py2.7.egg\django\conf\__init__.py", line 48, in __getattr__     self._setup(name)   File "D:\Program Files\Python27\lib\site-packages\django-1.8.8-py2.7.egg\django\conf\__init__.py", line 44, in _setup     self._wrapped = Settings(settings_module)   File "D:\Program Files\Python27\lib\site-packages\django-1.8.8-py2.7.egg\django\conf\__init__.py", line 92, in __init__     mod = importlib.import_module(self.SETTINGS_MODULE)   File "D:\Program Files\Python27\Lib\importlib\__init__.py", line 37, in import_module     __import__(name) ImportError: No module named blog_analysic.settings
这个问题是因为找不到项目的路径,这时候就要修改项目的wsgi文件了

5:修改项目的wsgi.py

打开文件,将我们的项目目录加进去
import sys    sys.path.append('D:/WWW/blog_analysic/')

同理修改另一个项目的wsgi.py即可
这时候再去重启我们的服务器,访问web目录
http://115.28.17.62:8080



http://115.28.17.62:8081/



OK

附:项目的地址: 博客统计分析系统 CyanScikit科技网站

相关热词搜索:

上一篇:Caused by: org.apache.ibatis.builder.BuilderExcepti 下一篇:文件上传之Apache commons fileupload使用
分享到: 收藏