缓存-反向代理-负载均衡@Plone+squid(黑色技术)

来源:互联网 发布:java开发工程师笔试题 编辑:程序博客网 时间:2024/06/05 11:55
 

架设 python + zope + plone + squid + iredir 网站随笔

架设 python + zope + plone + squid + iredir 网站随笔

首先介绍一下这三个名词

  • python

Python对大型的程式来说,它比起shell能提供更多的结构性及支援。另外一方面,它也提供了比C语言更多的错误检查。由于Python是一个非常高阶的语言,所以它有许多内建的资料型态像是有弹性的阵列及字典(dictionary)等等,如果用C来做的话得花上你大半天的时间。正是因为Python有较为一般性的资料型态,Python可以应用的范围比起awk甚或是Perl要广的很多,最起码,Python跟这些语言一样容易开发。

  • zope

zope是一个灵活、多用途的web应用平台,它是用python写成的

  • plone

plone是运行在zope之上的开放源代码的企业内容管理系统。

  • Squid

鼎鼎大名的代理软件,来实现代理,缓存功能

  • Iredir

是一个反向代理软件

熟悉了这些名词好了我们该开始安装了。

注:我们安装的所有都是基于linux系统的

第一步:python的安装:

下载地址:http://www.python.org/ftp/python/2.3.5/Python-2.3.5.tgz

解压缩 #tar xvzf Python-2.3.5.tgz

进目录 # cd Python-2.3.5

编译 #./configure –prefix=/usr/python 指定安装目录
#make

安装 #make install

这样python就安装完了。

第二步:zope的安装(步骤比较烦琐)

&。下载地址:http://www.zope.org/Products/Zope/2.8.6/Zope-2.8.6-final.tgz

解压缩 tar xvzf Zope-2.8.6-final

进目录 cd Zope-2.8.6-final

编译 ./configure –prefix=/usr/zope

make

安装 make install

&。注:还要安装如下插件:

Imaging-1.1.5,这个是zope支持图片功能

cjkcodecs插件是支持中文编码的

MySQL-python插件支持mysql数据库

&。Imaging-1.1.5下载地址:http://www.pythonware.net/storage/Imaging-1.1.5.tar.gz

安装步骤#tar xvzf Imaging-1.1.5.tar.gz

#cd Imaging-1.1.5

# python setup.py build_ext –i

{color: blue}
====================
PIL 1.1.5 BUILD SUMMARY

----------------------------------------------------------------

*** TKINTER support not available (Tcl/Tk 8.3 libraries needed)

--- JPEG support ok

[size=12]--- ZLIB (PNG/ZIP) support ok

[size=12]--- FREETYPE support ok
====================

出现这个信息提示,JPEG support ok就说明已经测试通过

#python setup.py install 开始安装

&。cjkcodecs 下载地址:http://download.berlios.de/cjkpython/cjkcodecs-1.1.1.tar.gz

安装步骤:#tar xvzf cjkcodecs-1.1.1.tar.gz

#cd cjkcodecs-1.1.1

#python setup.py install 进行安装

&。MySQL-python下载地址:

http://www.zope.org/Members/adustman/Products/MySQLdb/1.2.0/MySQL-python...

安装步骤:#tar xvzf MySQL-python-1.2.0.tar.gz

#cd MySQL-python-1.2.0

# python setup.py install 进行安装

到这里zope的安装基本结束下面就是安装实例了

&。ZEO就是我们的访问中心,实现负载分配功能,zeo1、zeo2就是zope的client端,我们实现的就是用户访问到服务器时,可以分配到zeo1、zeo2访问,实现了负载平衡。

&。现在来建立zope环境,我们使用/tmp/Zope-2.8.6-final/bin/ mkzeoinstance.py脚本

操作#cd /tmp/Zope-2.8.6-final/bin

#./mkzeoinstance.py /var/zeo 后面指的是zeo的安装路径
&。下面来创建zope client

我们使用/tmp/Zope-2.8.6-final/bin/ mkzopeinstance.py脚本来创建

步骤# cd /tmp/Zope-2.8.6-final/bin

#./mkzopeinstance.py 进行安装下面是交互式的
{color: blue}
====================
Please choose a directory in which you'd like to install

Zope "instance home" files such as database files, configuration

files, etc.

Directory::/var/zeo1

Password: ******
====================

这样就在/var/下面创建了zeo1(图示的zope client1)的目录,用同样的方法继续创建zeo2(图示的zope client2)

&。下面开始配置zeo和zeo1、zeo2

在 /var/zeo/etc/zeo.conf 里面设置zeo与zeo1、zeo2联系的端口

步骤:#vi /var/zeo/etc/zeo.conf

{color: blue}
====================
zeo

address 2000 设置zeo的端口

read-only false

invalidation-queue-size 100

# pid-filename $INSTANCE/var/ZEO.pid

# monitor-address PORT

# transaction-timeout SECONDS

/zeo
====================

上面的2000端口就是zeo于zeo1、zeo2联系的端口,设好之后就开始设置zeo1、zeo2

在 /var/zeo1/etc/zope.conf 里面设置如下:

步骤:#vi /var/zeo1/etc/zeo.conf

1. 设置使用zope的用户,这里使用zope

{color: blue}
====================
# Directive: effective-user

#

  1. Description:
  1. If you intend to run Zope as the "root" user, you must supply this
  1. directive with an effective username or userid number to which Zope
  1. will 'suid' after the server ports are bound. This directive only
  1. has effect under UNIX and if Zope is started as the root user.

#

  1. Default: unset

#

  1. Example:

effective-user zope 设置用户zope
====================

2.设置zeo1对外的端口
{color: blue}
====================

http-server

# valid keys are "address" and "force-connection close"

address 8080 设置zeo01的http端口

# force-connection-close on

/http-server

icp-server

address 3030

/icp-server

====================

3.使用mkzopeinstance.py建立的zope执行目录,要将他改成ZEO client的话,要改按照如下改正将原本使用的屏蔽,保留区段,开启zeo client storage的设定,端口设置成zeo设置的2000端口如下图:

{color: blue}
====================
#zodb_db main

  1. Main FileStorage database
  1. filestorage
  1. path $INSTANCE/var/Data.fs 这些区域为屏蔽区
  1. /filestorage
  1. mount-point /

#/zodb_db

zodb_db temporary

  1. Temporary storage database (for sessions)

temporarystorage

name temporary storage for sessioning 这段是保留区

/temporarystorage

mount-point /temp_folder

container-class Products.TemporaryFolder.TemporaryContainer

/zodb_db

  1. Other storage examples
  1. ZEO client storage:

zodb_db main

mount-point /

  1. # ZODB cache, in number of objects

cache-size 5000

zeoclient

server localhost:2000

storage 1

name zeostorage 这段为开启区域,端口为2000

var $INSTANCE/var

  1. # ZEO client cache, in bytes

cache-size 20MB

  1. # Uncomment to have a persistent disk cache
  1. #client zeo1

/zeoclient

====================

4.用同样的方法设置zeo2,不同的是上面第二步的http端口要改成8081,

5.要把 /var/zeo/

/var/zeo1/

/var/zeo2/

的所属用户组都要改成zope用户,具体步骤

#chown –R zope.zope /var/zeo

#chown –R zope.zope /var/zeo1

#chown –R zope.zope /var/zeo2

6.要在iptables 中开启8080,8081端口否则就无法打开网页:

步骤在#vi /etc/sysconfig/iptables

加入两条语句

{color: blue}
====================
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 –j ACCEPT

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
====================

#service iptables restart 重新启动防火墙生效

第三步:plone的安装

plone下载地址:

安装步骤:#tar xvzf Plone-2.1.2.tgz

#mv –R Plone-2.1.2/* /usr/Zope/lib/python/Products

第四部:也就是最复杂的一部就是配置squid+iredir利用它实现反向代理、缓存功能

squid下载地址:

wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0-PRE3-20060306.tar.b...

iredir下载地址:wget http://longsleep.org/projects/iredir/files/iredir-1.3.tar.gz

安装步骤:1.#bunzip2 squid-3.0-PRE3-20060306.tar.bz2

#tar xvzf squid-3.0-PRE3-20060306.tar.

#cd squid-3.0-PRE3-20060306

# ./configure --prefix=/usr/squid3 --enable-gnuregex --with-pthreads --enable-storeio=ufs,aufs --with-aufs-threads=10 --enable-useragent-log --enable-referer-log -enable-ssl --enable-x-accelerator-vary --with-dl 进行编译

#make

#make install 进行安装

# mv iredir-1.3.tar.gz /usr/

#cd /usr

#tar xvzf iredir-1.3.tar.gz iredir的安装很简单

2. 进行复杂的配置了

squid的配置如下所示:

{color: blue}========================

http_port 90 vhost 指定squid的代理端口

icp_port 3130 指定icp端口

cache_mem 256 MB 设置缓存大小

maximum_object_size 4096 KB 开启就可以

cache_replacement_policy lru 开启就可以

memory_replacement_policy lru 开启就可以

cache_dir ufs /var/squid/cache 1024 16 256 一级、二级缓存目个数

access_log /usr/squid3/logs/access.log combined 开启就可以

redirect_program /usr/iredir/iRedirector.py 指定iredir反向代理的脚本位置

redirect_children 10 设置开启缓存个数

redirect_rewrites_host_header off

cache_peer z1 parent 8081 3031 no-digest no-netdb-exchange round-robin

cache_peer z0 parent 8080 3030 no-digest no-netdb-exchange round-robin这两个起到了负载均衡的作用,意思是

acl in_backendpool dstdomain backendpool 设置访问列表,域名

cache_peer_access z1 allow in_backendpool 允许in_backendp001访问z1

cache_peer_access z1 deny all 拒绝所有

cache_peer_access z0 allow in_backendpool 允许in_backendp001访问z2

cache_peer_access z0 deny all 拒绝所有

http_access allow all http允许所有访问

cache_mgr admin@epad.cn 设置管理员邮箱

visible_hostname server122 设置虚拟主机名称
====================

上面的z0、z1是两个域名在/etc/hosts中有映射,如下:
{color: blue}
====================
# Do not remove the following line, or various programs

  1. that require network functionality will fail.

127.0.0.1 server122 localhost.localdomain localhost

140.211.166.46 drupal.org

83.151.28.155 longsleep.org

127.0.0.1 z1 z0

127.0.0.1 peer2.epad.cn

127.0.0.1 epad.cn

127.0.0.1 www.epad.cn 将z0、z1映射到本地
====================

iredir的配置,把iRedirector.py脚本里面的python路径写上,设置反向代理如下:

{color: blue}
====================
sitemap = {
(10, 'www.epad.cn'): 'backendpool/VirtualHostBase/http/$netloc$/epad/VirtualHostRoot'
} 这里当用户请求www.epad.cn时会通过squid中backendpool 域名转向到squid设定的规则
====================

到此网站就设置完成了,你可以用浏览器进行浏览了
方法:地址栏输入:http://www.epad.cn:90就可以了看到成效了,随后就可以通过plone管理网站了,但是现在还有一个问题就是用户访问时输入http://www.epad.cn:90肯定是不方便啊,所以要用到apache提供的rewrite也就是从新定向到http://www.epad.cn,具体就是更改/etc/httpd/conf/httpd.conf
在文件末尾加如下:
{color: blue}
====================
Virtual Host 61.135.16.122:80
ServerName www.epad.cn
ServerAlias epad.cn
RewriteRule ^/(.*) http://www.epad.cn:90/$1 [L,P]
Virtual Host
====================

这样从新起动apache就一切ok了,

原创粉丝点击