Windows环境下在Apache24上部署多个django项目(多端口)

来源:互联网 发布:淘宝网兼职 编辑:程序博客网 时间:2024/06/05 20:39

Windows环境下在Apache24上部署多个django项目(多端口)

        由于二期项目与一期一样,都采用Django框架,为了便于管理,需要同时部署在同一台服务器上,采用Apache24管理,查阅资料,经过多次尝试,终于成功在Apache上同时运行两个Django项目,以此类推,部署多个项目的方法也是一样的。


安装Apache24

在cmd中cd到httpd.exe所在目录,执行httpd–k install安装Apache,httpd –k start 可启动Apache,httpd–k stop 可停止,如有更改参数:httpd –k restart可重新启动生效。也可使用Apache自带的监视器进行管理。

若需要重新安装,可先卸载以前的版本:

关闭apache服务;然后运行命令行程序,输入 sc deleteapache即可卸载

 

小插曲:在Apache运行中,由于在连接数据库函数中加入了print语句,如下图,死活连接不上外部的数据库,搞了半天没搞定,师兄一把搞定,把连接数据库中函数中的所有print注释掉就行了。


2.部署多个django

网上有介绍多个IP、同一IP不同端口、同一IP同一端口不同域名的设置方式,根据项目的实际环境,这里选用了同一IP不同端口的方式。

参考网站:

http://httpd.apache.org/docs/current/vhosts/examples.html

http://blog.csdn.net/gamer_gyt/article/details/52135349

主要步骤如下:

修改Apache目录下的conf/httpd.conf文件内容

1.添加Apache根目录:

DefineSRVROOT "D:/Apache24"

ServerRoot"${SRVROOT}"

2.添加Python路径:

WSGIPythonHome "C:/Python27"

3. 添加mod_wsgi.so模块:

LoadModule wsgi_module modules/mod_wsgi.so

4.找到如下语句,将第二行注释掉,允许Apache进行端口映射

#Virtual hosts 

#Include conf/extra/httpd-vhosts.conf 

5.添加监听端口,这里要部署两个django,所以设置两个端口:

Listen8080

Listen8081

6. 打开conf/extra/httpd-vhosts.conf文件,添加对每个项目的参数设置,跟单个项目设置差不多。

<VirtualHost127.0.0.1:8086> 

    ServerName manageindex.com

#设置wsgi路径

    WSGIScriptAlias /D:/CJh_resourceV16.5/CJh_resource/wsgi.py 

    <Directory D:/CJh_resourceV16.5/CJh_resource>   

        <Files wsgi.py>   

            Require all granted   

        </Files>   

    </Directory> 

#设置静态文件路径

   Alias /staticD:/CJh_resourceV16.5/collected_static 

    <DirectoryD:/CJh_resourceV16.5/collected_static>    

        Require all granted   

    </Directory>  

#设置项目的根目录

   DocumentRoot"D:/CJh_resourceV16.5" 

    <Directory"D:/CJh_resourceV16.5"> 

        Options Indexes FollowSymLinks 

        AllowOverride None 

        Require all granted 

    </Directory> 

#设置日志记录位置

    ErrorLog"logs/error_CJh_resource.log"

    CustomLog"logs/access_CJh_resource.log" common

</virtualHost> 

 

 

 

<VirtualHost192.168.71.98:8080> 

    ServerName waterindex.com

    WSGIScriptAlias / D:/water_index/water_index/wsgi.py 

    <DirectoryD:/water_index/water_index>   

       <Files wsgi.py>   

            Require all granted   

       </Files>   

    </Directory>  

        Alias /staticD:/water_index/collected_static 

    <Directory D:/water_index/collected_static>    

        Require all granted   

    </Directory>   

      DocumentRoot"D:/water_index" 

    <Directory"D:/water_index"> 

        Options Indexes FollowSymLinks 

        AllowOverride None 

        Require all granted 

    </Directory> 

    ErrorLog"logs/error_water_index.log"

    CustomLog"logs/access_water_index.log" common

</virtualHost> 

7.分别用8080和8086两个端口监听两个项目。重启服务器,

如果有出现以下错误

(1)如下图


说明找不到项目的路径,需要分别修改两个项目的wsgi.py文件,加入以下语句:

importos

importsys 

root_path= os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))

sys.path.insert(0,root_path)

同理,另一个项目的wsgi.py文件也修改,重启Apache即可正常访问两个项目。

(2)部署后,在本地服务器上可以访问,在局域网内无法访问,请查看电脑的防火墙是否允许Apache通信,若不允许,点击允许运行另一程序添加Apache。如果仍然不行检查局域网内是否允许使用你所设置的端口,若不允许请更换可用的端口或找网络管理员解决。


阅读全文
0 0