CentOS 6安装配置KVM

上一篇文章介绍过CentOS 6安装Xen,有点麻烦,因为自CentOS 6开始,虚拟化平台已经从xen转向KVM,所以在CentOS 6安装KVM还是非常的简单的。

1、安装KVM

  1. yum groupinstall Virtualization ‘Virtualization Client’

2、安装api支持

  1. yum install libvirt
  2. service libvirtd start

3、验证是否已经载入KVM模块

  1. $ lsmod | grep kvm
  2. kvm
  3. kvm_intel

4、使用virt-manager安装guest

virt-manager是一个api gui工具,可以很方便的管理虚拟机。下面简单介绍使用virt-manager安装虚拟机系统。
1)打开virt-manager,Add-connection,hypervisor选择QEMU/KVM,点击connect。
2)在连接localhost(QEMU)右键-NEW,输入guest名称,还有选择安装方式,我这里选择iso文件安装,点击forword。
3)选择iso文件路径,点击forword。
4)定义分配内存及cpu个数。
5)定义磁盘映像大小。
6)下一步就是常规的系统安装了。

5、使用virt-install安装guest

当你的CentOS没有桌面环境时,可以使用virt-install命令安装,如:
1、输入虚拟机名称
2、分配多少内存
3、定义虚拟机磁盘映像的位置
4、此步可以直接输入iso的位置或是url
5、进行系统常规安装

Windows XP下硬盘安装CentOS 6.2

我们现在要实现的是硬盘安装linux系统,这里以CentOS 6.2为例,简单介绍一下安装过程,不会很详细,只是给个思路。

必备工具

CentOS 6.2 ISO文件
下载地址:http://mirrors.163.com/centos/6.2/isos/
Paragon-Partition-Manager:用于在xp下ext2或ext3的分区
下载地址:http://115.com/file/e732imz8#Paragon-Partition-Manager-10.0-Server.7z
grub4dos:用于引导linux系统
下载地址:http://115.com/file/dpdswtcm#grub4dos-0.4.4-2009-01-11.zip
Ext2Fsd:用于windows下能读写ext2或ext3分区
下载地址:http://115.com/file/be6ajmwc#Ext2Fsd-0.51.7z

安装步骤

ext3分区

使用Paragon-Partition-Manager分出一个ext3的分区,这个分区是用来存在iso文件的,大小根据iso文件确定,分区之后,硬盘还必须有未分区的空间,因为需要给安装CentOS留下。分区的时候顺便分配盘符。

使用Ext2Fsd访问ext3分区

安装打开ext2fsd软件,在刚才分好的ext3分区上右键,选择“配置文件系统”,点击“启用”,之后“更改并退出”。这时后就可以打开我的电脑,并看见已经多了一个磁盘分区,比如F。接着把iso文件复制到F分区的根目录,

用grub4dos软件制作引导菜单

打开我的电脑C盘,工具–文件夹选项–查看,在“隐藏受保护的操作系统文件(推荐)”前面的勾去掉,并选中“显示所有文件和文件夹”,再把“隐藏已经文件类型的扩展名”前面的勾去掉,最后点击应用,确定。
右键单击C盘根目录下的boot.ini,选择“属性”,把“只读”前面的勾去掉。接着,用记事本打开boot.ini文件,在最后一行添加如下内容:
C:GRLDR=”Grub”
解压grub4dos-0.4.4,把文件夹里面的GRLDR复制到C盘根目录。然后在C盘根目录新建boot文件夹,在boot文件夹中再建grub文件夹,把grub4dos-0.4.4文件夹里面的menu.lst复制到C:\boot\grub下。

然后解压挂载或解压iso文件,把里面的isolinux文件夹复制到F盘的根目录下面。

引导CentOS启动

重启电脑,进入引导界面,选择Grub,按下”C”键进入命令行模式。
输入“root (hd0,”(双引号不用输),这时按下”Tab”键,会在下面出现硬个硬盘的所有分区,假如我们看到 “5”对应之前的ext3分区,那就继续输入”5)”,完整的命令是:root (hd0,5)。
现在按下回车键,继续输入kernel /isolinux/vmlinuz,再按下回车,输入initd /isolinux/initrd.img,按下回车,继续输入boot,按下回车,这时grub已经能够引导centos进入安装界面。

CentOS系统安装

这里不多说,需要注意的有几点:
1、在要求选择CentOS image文件所在的分区时,一般选择最后一个分区。
2、这步一定要小心,不然会导致windows系统丢失。在提示“您要进行哪种类型的安装”时,选择“创建自定义布局”进行自定义分区,然后在未分区的空间上新建ext4分区,也可以使用LVM管理分区,不过boot必须是主物理分区。
3、其它的默认就好

CentOS 6安装配置Xen

centos 6安装xen并不像centos 5那样轻松,因为在centos 6中,官方源已经去除了xen的rpm包,只能使用第三方源或自行编译,这里推荐使用第三方源,编译安装要解决的问题比较多。还有一个包libvirt,这个是管理xen的api,官方的这个包已经不支持xen,并且是0.9版的需要更高版本的iptables支持,所以libvirt需要编译安装。

下面是在CentOS 6 64位系统进行安装配置Xen。

1、安装Xen及内核

  1. rpm -Uvh http://www.crc.id.au/repo/x86_64/kernel-xen-release-6-3.noarch.rpm //导入第三方源安装Xen
  2. yum install bridge-utils //安装网桥设置工具
  3. yum install kernel-xen xen //安装xen及内核

2、配置grub引导xen内核

xen内核安装后,会自动插入引导xen内核代码到/boot/grub/grub.conf文件,但还需要进行相应的修改。

修改后引导xen内核的代码如下:

  1. title CentOS (2.6.32.56-1.el6xen.x86_64)
  2. root (hd0,7)
  3. kernel /xen.gz dom0_mem=1024M loglvl=all guest_loglvl=all
  4. module /vmlinuz-2.6.32.56-1.el6xen.x86_64 ro root=/dev/mapper/VolGroup-LogVol00 rd_LVM_LV=VolGroup/LogVol00 nomodeset
  5. module /initramfs-2.6.32.56-1.el6xen.x86_64.img

这段代码仅够参考,不一定适用于你的配置。

3、关闭selinux

这是必须关闭的,要不会因为selinux的安全机制导致xen无法正常工作。

编辑selinux配置文件

  1. vi /etc/sysconfig/selinux

修改为如下:

  1. # This file controls the state of SELinux on the system.
  2. # SELINUX= can take one of these three values:
  3. # enforcing – SELinux security policy is enforced.
  4. # permissive – SELinux prints warnings instead of enforcing.
  5. # disabled – No SELinux policy is loaded.
  6. SELINUX=disabled
  7. # SELINUXTYPE= can take one of these two values:
  8. # targeted – Targeted processes are protected,
  9. # mls – Multi Level Security protection.
  10. SELINUXTYPE=targeted

还有一点配置也很重要,就是禁止netmanager接管网络,因为这个工具不支持网桥管理。
在/etc/sysconfig/network-script/ifcfg-eth0中加入:

  1. NM_CONTROLLED=no

4、重启引导进入xen内核

引导后执行xm info测试:

  1. [root@localhost ~]# xm info
  2. host                   : localhost.localdomain
  3. release                : 2.6.32.56-1.el6xen.x86_64
  4. version                : #1 SMP Mon Feb 20 13:03:03 EST 2012
  5. machine                : x86_64
  6. nr_cpus                : 4
  7. nr_nodes               : 1
  8. cores_per_socket       : 2
  9. threads_per_core       : 2
  10. cpu_mhz                : 2394
  11. hw_caps                : bfebfbff:28100800:00000000:00003f40:009ae3bd:00000000:00000001:00000000
  12. virt_caps              : hvm
  13. total_memory           : 1972
  14. free_memory            : 412
  15. free_cpus              : 0
  16. xen_major              : 4
  17. xen_minor              : 1
  18. xen_extra              : .2
  19. xen_caps               : xen-3.0-x86_64 xen-3.0-x86_32p hvm-3.0-x86_32 hvm-3.0-x86_32p hvm-3.0-x86_64
  20. xen_scheduler          : credit
  21. xen_pagesize           : 4096
  22. platform_params        : virt_start=0xffff800000000000
  23. xen_changeset          : unavailable
  24. xen_commandline        : dom0_mem=1024M loglvl=all guest_loglvl=all
  25. cc_compiler            : gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
  26. cc_compile_by          : mockbuild
  27. cc_compile_domain      : crc.id.au
  28. cc_compile_date        : Mon Feb 20 12:52:37 EST 2012
  29. xend_config_format     : 4

5、安装api管理工具

  1. yum install virt-install virt-viewer

6、编译安装libvirt

因为rpm安装的libvirt不支持xen连接,所以我们使用编译安装。还有版本也不能选择高的,不然可能会因为iptables的版本低而不能使用libvirt。

  1. yum install gcc xen-devel libxml2-devel gnutls-devel device-mapper-devel libnl-devel make
  2. cd /tmp
  3. wget http://libvirt.org/sources/libvirt-0.8.1.tar.gz
  4. tar xzf libvirt-0.8.1.tar.gz
  5. cd libvirt-0.8.1
  6. ./configure –with-xen –with-xen-inotify –with-libvirtd
  7. make && make install

然后打开/usr/local/etc/libvirt/libvirtd.conf文件,删除unix_sock_dir = “/var/run/libvirt”前面的注释,并创建/var/run/libvirt目录。
之后启动libvirtd,并设置开机启动

  1. libvirtd -d
  2. echo "/usr/local/sbin/libvirtd -d" >>/etc/rc.d/rc.local

7、安装xen guest

我们这里使用virt-install工具进行guest的安装,当然你也可以使用xm命令安装,不过相对麻烦点。

可以执行virt-install –help学习这工具的使用方法。
下面是安装guest的示例

  1. virt-install -n CentOSVM1 -r 512 -f /xen/CentOSVM1.img  -l http://www/  –network=network:default -s 10 –nographics –vcpus=2

或者使用交互式安装

  1. virt-install –prompt

-n CentOSVM1 设置虚拟服务器名称
-r 512 设置内存大小
-f /xen/CentOSVM1.img  虚拟磁盘文件的保存路径,如果有重名可以使用–force参数强制重建。
-l http://www/ 安装文件的访问方式,支持nfs http ftp smb等多种方式。如果你对你的带宽比较自信,可以使用http://mirrors.163.com/centos/5.5/os/i386/网易的镜像站
–network=network:default 网络连接方式,我选择的是route中的default
-s 10磁盘文件的大小,单位是G
 –nographics 不使用图形界面,可以不加此参数,然后加–vnc看看图形效果
 –vcpus=2 虚拟CPU的个数
 在执行virt-install命令安装系统之前,先确定你的安装源,支持nfs http ftp smb等多种方式,宽带大的话,可以使用http直接连接远程安装源,比如mirror.163.com。
 我这里adsl上网的,本地已经下载有iso文件了,所以在本地架设个http服务器进行安装。
1、挂载iso文件到/iso目录,这个目录必须存在。

  1. mount -o loop CentOS-6.2-x86_64-minimal.iso /iso

2、安装httpd服务器,我这里安装apache

  1. yum install httpd
  2. service httpd start

3、创建软链接或直接复制文件到apache根目录/var/www/html,如果你的centos 6.2是完整版,只需要做个软链接,如果下载的是精简版,也就是minimal,需要复制全部文件到根目录,因为.treeinfo这个文件需要相应的修改。

centos 6.2完整版:

  1. ln -s /iso /var/www/html

centos 6.2精简版:

  1. cp -R /iso  /var/www/html

修改.treeinfo文件,如:

  1. [general]
  2. family = CentOS
  3. timestamp = 1323560005.81
  4. variant =
  5. totaldiscs = 1
  6. version = 6.2
  7. discnum = 1
  8. packagedir =
  9. arch = x86_64
  10.  
  11. [images-x86_64]
  12. kernel = isolinux/vmlinuz
  13. initrd = isolinux/initrd.img
  14.  
  15. [images-xen]
  16. kernel = isolinux/vmlinuz
  17. initrd = isolinux/initrd.img
  18.  
  19. [stage2]
  20. mainimage = images/install.img

4、在安装之前,需要暂时关闭iptables,否则安装过程中会无法取得相应文件。

  1. service iptables stop

5、开始使用virt-install安装

  1. virt-install -n centos6 -r 512 -f /xen/CentOSVM1.img -l http://192.168.1.100/iso –network=network:default -s 4 –vcpus=2

之后会自动调用virt-viewer工具显示安装界面。
6、启动guest centos6
安装完成后会要求重启,这时guest关闭之后不会自动启动,需要使用xm start命令启动:

  1. xm start centos6

7、开启iptables

之前为了连接安装源,暂时关闭了iptables,现在需要启动iptables,否则guest无法连接外网。

  1. service iptables start

8、使用virt-viewer管理guest

  1. virt-viewer centos6

执行这条命令即可连接虚拟机centos6进行管理了,当然也可以直接使用ssh连接更简单。

另外,我安装过virt-manager来安装guest,但到创建域时就出现KeyError错误,不知道如何解决,有懂的告诉一声。

nginx配置反向代理

nginx配置反向代理很简单,只需要在nginx.conf配置文件加入server区块。

  1. server {
  2.         listen       80;
  3.         server_name  devops.webres.wang;
  4.  
  5. location / {
  6.                 proxy_pass              http://1.2.3.4; //后端ip地址
  7.                 proxy_redirect          off;  //关闭后端返回的header修改
  8.                  proxy_set_header       Host            $host;  //修改发送到后端的header的host
  9.                 proxy_set_header        X-Real-IP       $remote_addr; //设置真实ip
  10.                 proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  11.                 }
  12.         }

 

varnish下使用acl限制ip地址访问

第1步:定义ACL,我们使用一个外部文件存储IP地址

  1. acl forbidden {
  2. include "/etc/varnish/chinaip.dat";
  3. }
  4. ############chinaip.data#########
  5.  
  6. "192.168.1.0"/24;
  7. "10.0.0.0"/24;

 

第2步:在vcl_recv中定义策略,放到最前面。

  1. if (client.ip ~ forbidden) {
  2. error 505 "Forbidden";
  3. }

 

第3步(可选):自定义错误页面
#根据不同的错误代码,执行不同的操作
#将错误代码为750的,重定向google,将错误代码为505的,直接返回错误代码。

  1. sub vcl_error {
  2. set obj.http.Content-Type = "text/html; charset=utf-8";
  3. if (obj.status == 750) {
  4. set obj.http.Location = "http://www.google.com/";
  5. set obj.status = 302;
  6. deliver;
  7. }
  8. else {
  9. synthetic {"
  10. <!–?xml version="1.0" encoding="utf-8"?–>
  11.  
  12.  
  13.  
  14. "} obj.status " " obj.response {"
  15. <h1>Error "} obj.status " " obj.response {"</h1>
  16. "} obj.response {"
  17.  
  18. "};
  19. }
  20.  
  21. return (deliver);
  22. }

 

第4步:验证配置是否正确

  1. varnishd -d -f /etc/varnish/my.vcl

 

第5步:重启varnish

  1. service varnish restart

 

第6步:测试

忘记说第0步了,就是先备份你的配置文件,很重要。
转自:http://blog.poesylife.com/

Iftop-网络流量实时监控工具

iftop介绍

Iftop 主要用来显示本机网络流量情况及各相互通信的流量集合,如单独同那台机器间的流量大小,非常适合于代理服务器和iptables服务器使用。

iftop安装

1、安装依赖

  1. yum install libpcap-devel ncurses-devel

2、开始安装

  1. wget http://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gz
  2. tar xzf iftop-0.17.tar.gz
  3. cd iftop-0.17
  4. ./configure
  5. make && make install

如何使用

直接执行iftop即可运行查看流量情况。
执行iftop -h得到帮助信息

  1. [root@www ~]#iftop -h
  2. iftop: display bandwidth usage on an interface by host
  3.  
  4. Synopsis: iftop -h | [-npbBP] [-i interface] [-f filter coolcode] [-N net/mask]
  5.  
  6.    -h                  display this message
  7.    -n                  don’t do hostname lookups
  8.    -N                  don’t convert port numbers to services
  9.    -p                  run in promiscuous mode (show traffic between other
  10.                        hosts on the same network segment)
  11.    -b                  don’t display a bar graph of traffic
  12.    -B                  Display bandwidth in bytes
  13.    -i interface        listen on named interface
  14.    -f filter coolcode      use filter coolcode to select packets to count
  15.                        (default: none, but only IP packets are counted)
  16.    -F net/mask         show traffic flows in/out of network
  17.    -P                  show ports as well as hosts
  18.    -m limit            sets the upper limit for the bandwidth scale
  19.    -c config file      specifies an alternative configuration file

官方网站:http://www.ex-parrot.com/pdw/iftop/

双机热备+负载均衡线上方案(Heartbeat+DRBD+NFS+Keepalived+Lnmp)

我们下面来实现一个架构,heartbeat+drbd+nfs实现mysql和网站数据的同步,keepalived实现nginx的高可用,而用nginx和dns轮询实现负载均衡。

架构说明

目录规划

/usr/local/src/lnmp:用来存放源码工具等等
/data:用来存放所有数据和NFS以及DRBD的挂载
/data/shell:用来存放所有管理脚本
/data/mysql:用来挂载DRBD的mysql资源,以供mysql存放数据库
/data/wwwnfs:用来挂载DRBD生成的www资源,以供两个节点挂载到各个节点的/data/www目录,以供论坛等程序数据使用
/data/www:用来挂载NFS资源,用来存放论坛(网站)等程序数据

拓扑工作原理

内网:
1,DRBD网络存储创建出两个资源,一个mysql给mysql数据库同步用,一个www给web(论坛)数据NFS共享挂载用,虚拟出两个虚拟IP,一个是 192.168.1.100,用来连接数据库,一个是192.168.1.200,用来给节点挂载NFS
注意:NFS底下挂载了三次:DRBD挂载一次,文件系统挂载一次,客户端挂载一次
2,Heartbeat来实现DRBD的HA,同时虚拟出两个内网IP,并管理NFS,MySQL的启动和关闭

外网:
1,两个节点都用Nginx做均衡器,通过内网调度负载两个节点,实现内部均衡
2,DNS配置双IP对应一个域名的方式来实现DNS轮询,实现外网均衡
3,Keepalived使用双主(master)配置虚拟出两个虚拟IP:节点一 12.12.12.100和节点二 12.12.12.200,同时共外网访问,两个节点互为主从关系,当某个节点挂掉的时候,另外一个节点将同时是两个资源的master,同时拥有两个虚拟IP,实现资源转移。

我们知道DNS的缺点就是生效慢,分配资源不合理,理论上有可能把所有的请求都发送给同一节点,导致均衡不合理导致所有资源不可用,这里我们由于有了NGINX内部负载,就不怕DNS轮询不均衡了,因为NGINX内部有严谨的调度方式,不管那台请求有多少,在内部都能实现理想的调度,这样就能把DNS负载均衡和NGINX完美结合,是硬件资源得到合理的利用,然后利用keepalive保证了每个节点的可靠性,几乎完美!
拓扑图如下:
高可用/集群

架构实现

LNMP架构配置

配置LNMp架构需要注意两点:
注意一:这里MYSQL都不要初始化,不要启动!后面有专门的配置的
注意二:nginx所有端口都改成 8080,因为一会还要安装nginx来做均衡器并对外提供服务,所以不要用默认的80
注意三、nginx和php-fpm运行的用户都是www。

安装配置NFS

1、安装NFS

  1. yum install nfs-utils nfs4-acl-tools portmap

2、配置/etc/exports

  1. /data/wwwnfs 192.168.1.0/24(rw,,no_root_squash,sync,anonuid=502,anongid=502)

注意:
/data/wwwnfs:就是给两个节点挂载的目录,所有网站程序都放在这里,实现论坛程序等数据的共享(同步)
anonuid=502,anongid=502:这个表示客户端上任何用户进入到挂载目录都以uid=502和gid=502身份,我这里这个代表的是www用户
3、启动

  1. service portmap start
  2. service nfs start

切忌,必须先启动portmap

  1. chkconfig  nfs off
  2. chkconfig  portmap on

注意:portmap服务器必须常驻,且不收heartbeat管理;而nfs这必须要用heartbeat来管理他的启动和关闭,所以这里要关闭nfs开机自动启动

同时要启动锁机制,因为同时有两个节点要使用同一份数据,所以需要有总裁,这个尤其是在NFS给mysql用的时候是必须要用的,对于论坛或网站,要看情况,如果存在对同一文件同时修改的时候必须要启动NFS锁机制,如果没有这种情况,那么建议不要启动,启动了会降低NFS的性能:

  1. /sbin/rpc.lockd
  2. echo "/sbin/rpc.lockd" >>/etc/rc.local

4、开机自动挂载

  1. echo "sleep 20" >>/etc/rc.local
  2. echo "/bin/mount -t nfs 192.168.1.200:/data/wwwnfs /data/www" >>/etc/rc.local

为什么为延迟20秒再挂载nfs?因为如果不等待立即挂载,会发现挂载不上,这是由于heartbeat启动用的vip还没设置好的原因。
立即挂载:

  1. mount -a

安装配置DRBD

安装方法见:http://devops.webres.wang/2012/02/drbd-compile-install-deploy/

配置文件

DRBD有三种配置文件:
/usr/local/drbd/etc/drbd.conf
/usr/local/drbd/etc/drbd.d/global_common.conf
/usr/local/drbd/etc/drbd.d/*.res
1、drbd.conf

  1. include "drbd.d/global_common.conf";
  2. include "drbd.d/*.res";

2、global_common.conf

  1. global {
  2.   usage-count yes;
  3. }
  4. common {
  5.   net {
  6.     protocol C;
  7.   }
  8. }

3、mysql.res和www.res
mysql.res:

  1. vi /usr/local/drbd/etc/drbd.d/mysql.res
  1. #资源组的名称
  2. resource mysql{
  3.  
  4. #定义主服务器资源
  5.         on node1{
  6. #建立块设备文件
  7.         device /dev/drbd1;
  8. #要用于复制的分区
  9.         disk /dev/sdb1;
  10. #定义侦听IP和端口
  11.         address 192.168.1.10:7788;
  12. #meta data信息存放的方式,这里为内部存储,即和真实数据放在一起存储
  13.         meta-disk internal;
  14.                    }
  15.  
  16. #定义备服务器资源
  17.         on node2{
  18.         device /dev/drbd1;
  19.         disk /dev/sdb1;
  20.         address 192.168.1.20:7788;
  21.         meta-disk internal;
  22.                    }
  23.  
  24.                 }

www.res:

  1. vi /usr/local/drbd/etc/drbd.d/www.res
  1. #资源组的名称
  2. resource www{
  3.  
  4. #定义主服务器资源
  5.         on node2{
  6. #建立块设备文件
  7.         device /dev/drbd2;
  8. #要用于复制的分区
  9.         disk /dev/sdb2;
  10. #定义侦听IP和端口
  11.         address 192.168.1.20:7789;
  12. #meta data信息存放的方式,这里为内部存储,即和真实数据放在一起存储
  13.         meta-disk internal;
  14.                    }
  15.  
  16. #定义备服务器资源
  17.         on node1{
  18.         device /dev/drbd2;
  19.         disk /dev/sdb2;
  20.         address 192.168.1.10:7789;
  21.         meta-disk internal;
  22.                    }
  23.  
  24.                 }

最后复制这些文件到node2。

初始化DRBD资源

1)在各个节点启用资源mysql和www

  1. modprobe drbd
  2. dd if=/dev/zero of=/dev/sdb1 bs=1M count=10
  3. dd if=/dev/zero of=/dev/sdb2 bs=1M count=10
  4. drbdadm create-md mysql
  5. drbdadm create-md www
  6. drbdadm up mysql
  7. drbdadm up www

2),提升各个节点上的主
在node1上:

  1. drbdadm primary –force mysql

在node2上:

  1. drbdadm primary –force www

3)格式化drbd块设备
在node1上

  1. mkfs.ext3 /dev/drbd1

在node2上

  1. mkfs.ext3 /dev/drbd2

4)挂载分区
在node1上

  1. mount /dev/drbd1 /data/mysql

在node2上

  1. mount /dev/drbd2 /data/wwwnfs

安装配置heartbeat

1、安装heartbeat

  1. yum install heartbeat

安装完后会自动建立用户hacluster和组haclient
确保两个节点上hacluster用户的的UID和GID相同
2、同步两台节点的时间

  1. rm -rf /etc/localtime
  2. cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  3. yum install -y ntp
  4. ntpdate -d cn.pool.ntp.org

3、配置/etc/ha.d/ha.cf

  1. debugfile /var/log/ha-debug                             #打开错误日志报告
  2. keepalive 2                                             #两秒检测一次心跳线连接
  3. deadtime 10                                             #10 秒测试不到主服务器心跳线为有问题出现
  4. warntime 6                                              #警告时间(最好在 2 ~ 10 之间)
  5. initdead 120                                            #初始化启动时 120 秒无连接视为正常,或指定heartbeat
  6.                                                         #在启动时,需要等待120秒才去启动任何资源。
  7.  
  8. udpport 694                                             #用 udp 的 694 端口连接
  9. ucast eth0 192.168.1.20                                #单播方式连接(主从都写对方的 ip 进行连接)
  10. node   node1                                           #声明主服(注意是主机名uname -n不是域名)
  11. node   node2                                           #声明备服(注意是主机名uname -n不是域名)
  12. auto_failback on                                        #自动切换(主服恢复后可自动切换回来)这个不要开启
  13. respawn hacluster /usr/lib/heartbeat/ipfail           #监控ipfail进程是否挂掉,如果挂掉就重启它

4、/etc/ha.d/authkeys

  1. auth 1
  2. 1 crc

5、/etc/ha.d/haresources

  1. node1 IPaddr::192.168.1.100/24/eth0 drbddisk::mysql Filesystem::/dev/drbd1::/data/mysql::ext3 mysqld portmap
  2. node2 IPaddr::192.168.1.200/24/eth0 drbddisk::www Filesystem::/dev/drbd2::/data/wwwnfs::ext3 portmap nfs

6、创建nfs管理脚本

  1. vi /etc/ha.d/resource.d/nfs

写入:

  1. #!/bin/bash
  2.  
  3. NFSD=/etc/rc.d/init.d/nfs
  4. NFSDPID=`/sbin/pidof nfsd`
  5. case $1 in
  6. start)
  7. $NFSD start;
  8. ;;
  9. stop)
  10. $NFSD stop;
  11.         if [ "$NFSDPID" != " " ];then
  12.                 for NFSPID in $NFSDPID
  13.                 do /bin/kill -9 $NFSPID;
  14.                 done
  15.         fi
  16. ;;
  17. *)
  18. echo "Syntax incorrect. You need one of {start|stop }"
  19. ;;
  20. esac

先启动node1的heartbeat,再启动node2的heartbeat
启动成功后,这里有几项需要检查
node1:
1、执行ip a,检查是否已经设置有虚拟ip 192.168.1.100
2、执行cat /proc/drbd检查状态是否正常
3、执行df -h查看/dev/drbd1是否已经挂载到/data/mysql
4、执行service mysqld status查看mysql是否已经启动
node2:
1、执行ip a查看是否已经设置虚拟ip 192.168.1.200
2、执行cat /proc/drbd检查状态是否正常
3、执行df -h查看/dev/drbd2是否已经挂载到/data/wwwnfs和192.168.1.200:/data/wwwnfs是否已经挂载到/data/www

nginx均衡器配置

  1. user  www;
  2. worker_processes  1;
  3.  
  4. error_log  /var/log/nginx/error.log warn;
  5. pid        /var/run/nginx.pid;
  6.  
  7.  
  8. events {
  9.     worker_connections  1024;
  10. }
  11.  
  12.  
  13. http {
  14.     include       /etc/nginx/mime.types;
  15.     default_type  application/octet-stream;
  16.  
  17.     log_format  main  ‘$remote_addr – $remote_user [$time_local] "$request" ‘
  18.                       ‘$status $body_bytes_sent "$http_referer" ‘
  19.                       ‘"$http_user_agent" "$http_x_forwarded_for"’;
  20.  
  21.     access_log  /var/log/nginx/access.log  main;
  22.  
  23.     sendfile        on;
  24.     #tcp_nopush     on;
  25.  
  26.     keepalive_timeout  65;
  27.  
  28.     #gzip  on;
  29.  upstream devops.webres.wang_server
  30.   {
  31.   server 192.168.1.10:8080 weight=3 max_fails=2 fail_timeout=30s;
  32.   server 192.168.1.20:8080 weight=9 max_fails=2 fail_timeout=30s;
  33.   }
  34.   server
  35.   {
  36.     listen       80;
  37.     server_name  devops.webres.wang;
  38.     location / {
  39.     root /data/www/devops.webres.wang;
  40.     index index.php index.htm index.html;
  41.     proxy_redirect off;
  42.     proxy_set_header Host $host;
  43.     proxy_set_header X-Real-IP $remote_addr;
  44.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  45.     proxy_pass http://devops.webres.wang_server;
  46.     }
  47.     access_log  off;
  48.   }
  49. server
  50.   {
  51.     listen       8080;
  52.     server_name  devops.webres.wang;
  53.     index index.html index.htm index.php;
  54.     root  /data/www/devops.webres.wang;
  55.     #limit_conn   crawler  20;
  56.  location ~ .php$ {
  57.         root           /data/www/devops.webres.wang;
  58.         fastcgi_pass   127.0.0.1:9000;
  59.         fastcgi_index  index.php;
  60.         fastcgi_param  SCRIPT_FILENAME  /data/www/devops.webres.wang/$fastcgi_script_name;
  61.         include        fastcgi_params;
  62.     }
  63.     location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
  64.     {
  65.       expires      30d;
  66.     }
  67.     location ~ .*.(js|css)?$
  68.     {
  69.       expires      1h;
  70.     }
  71.     access_log  off;
  72.       }
  73. }

这里定义了两台用于负载均衡的机子,分别是192.168.1.10:8080和192.168.1.20:8080,通过proxy_pass http://devops.webres.wang_server代理循询转发到这两台机,达到负载均衡的作用。
你可以建立index.php,里面写入:

  1. <?php
  2. echo $_SERVER[‘SERVER_ADDR’];
  3. ?>

如果连续刷新几次,得到不同的IP,证明已经均衡负载到不同的服务器。

Keepalived实现nginx和php的HA

1、keepalived安装
安装方法见:http://devops.webres.wang/2012/02/nginx-keepalived-high-availability/
2、配置
节点一node1配置如下:

  1. global_defs {
  2.    notification_email {
  3.      [email protected]
  4.    }
  5.    notification_email_from [email protected]
  6.    smtp_server 127.0.0.1
  7.    smtp_connect_timeout 30
  8.    router_id LVS_DEVEL
  9. }
  10. vrrp_instance VI_1 {
  11.     state MASTER        ############ 辅机为 BACKUP
  12.     interface eth0
  13.     virtual_router_id 100
  14.     mcast_src_ip 192.168.1.10  ########### 本机IP
  15.     priority 102                  ########### 权值要比 back 高
  16.     advert_int 1
  17.     authentication {
  18.         auth_type PASS
  19.         auth_pass 1111
  20.     }
  21.     virtual_ipaddress {
  22.        12.12.12.100
  23.     }
  24. }
  25.  
  26. vrrp_instance VI_1 {
  27.     state BACKUP
  28.     interface eth0
  29.     virtual_router_id 200
  30.     mcast_src_ip 192.168.1.101 ########### 本机IP
  31.     priority 101              ##########权值 要比 master 低。。
  32.     advert_int 1
  33.     authentication {
  34.         auth_type PASS
  35.         auth_pass 1111
  36.     }
  37.     virtual_ipaddress {
  38.        12.12.12.200
  39.     }
  40. }

节点二配置:

  1. global_defs {
  2.    notification_email {
  3.      [email protected]
  4.    }
  5.    notification_email_from [email protected]
  6.    smtp_server 127.0.0.1
  7.    smtp_connect_timeout 30
  8.    router_id LVS_DEVEL
  9. }
  10. vrrp_instance VI_1 {
  11.     state BACKUP
  12.     interface eth0
  13.     virtual_router_id 100
  14.     mcast_src_ip 192.168.1.20 ########### 本机IP
  15.     priority 101              ##########权值 要比 master 低。。
  16.     advert_int 1
  17.     authentication {
  18.         auth_type PASS
  19.         auth_pass 1111
  20.     }
  21.     virtual_ipaddress {
  22.        12.12.12.100
  23.     }
  24. }
  25.  
  26. vrrp_instance VI_1 {
  27.     state MASTER        ############ 辅机为 BACKUP
  28.     interface eth0
  29.     virtual_router_id 200
  30.     mcast_src_ip 192.168.1.103  ########### 本机IP
  31.     priority 102                  ########### 权值要比 back 高
  32.     advert_int 1
  33.     authentication {
  34.         auth_type PASS
  35.         auth_pass 1111
  36.     }
  37.     virtual_ipaddress {
  38.        12.12.12.200
  39.     }
  40. }

3、创建监控脚本
node1监控脚本:

  1. vi /opt/check.sh
  1. #!/bin/bash
  2. while  :
  3. do
  4. mysqlcheck=`/usr/bin/mysqladmin -uroot ping 2>&1`
  5. mysqlcode=`echo $?`
  6. heartbeat=`ps -C heartbeat –no-header | wc -l`
  7. if [ $mysqlcode -ne 0 ] ;then
  8.  if [ $heartbeat-ne 0 ];then
  9. service heartbeat stop
  10. fi
  11. fi
  12. phpcheck=`ps -C php-fpm –no-header | wc -l`
  13. nginxcheck=`ps -C nginx –no-header | wc -l`
  14. keepalivedcheck=`ps -C keepalived –no-header | wc -l`
  15. if [ $nginxcheck -eq 0 ]|| [ $phpcheck -eq 0 ];then
  16.                 if [ $keepalivedcheck -ne 0 ];then
  17.                    killall -TERM keepalived
  18.                 else
  19.                    echo "keepalived is stoped"
  20.                 fi
  21.         else
  22.                 if [ $keepalivedcheck -eq 0 ];then
  23.                    /etc/init.d/keepalived start
  24.                 else
  25.                    echo "keepalived is running"
  26.                 fi
  27. fi
  28. sleep 5
  29. done

node2监控脚本:

  1. #!/bin/bash
  2. while  :
  3. do
  4. phpcheck=`ps -C php-cgi –no-header | wc -l`
  5. nginxcheck=`ps -C nginx –no-header | wc -l`
  6. keepalivedcheck=`ps -C keepalived –no-header | wc -l`
  7. if [ $nginxcheck -eq 0 ]|| [ $phpcheck -eq 0 ];then
  8.                 if [ $keepalivedcheck -ne 0 ];then
  9.                    killall -TERM keepalived
  10.                 else
  11.                    echo "keepalived is stoped"
  12.                 fi
  13.         else
  14.                 if [ $keepalivedcheck -eq 0 ];then
  15.                    /etc/init.d/keepalived start
  16.                 else
  17.                    echo "keepalived is running"
  18.                 fi
  19. fi
  20. sleep 5
  21. done

这个监控代码实现了mysql,nginx,php-fpm的HA。
加上权限,并执行

  1. chmod +x /opt/check.sh
  2. nohup sh /opt/check.sh &

设置开机启动:
echo “nohup sh /opt/check.sh &” >> /etc/rc.local

4、测试keepalived
分别启动keepalived

  1. service keepalived start

1)执行ip a检查node1和node2是否已经存在vip:12.12.12.100和12.12.12.200
2)测试nginx和php-fpm的HA。在node1执行service nginx stop或者service php-fpm stop停止nginx或php-fpm,过几秒钟后你会发现node2已经接管了vip 12.12.12.100,并且使用vip 12.12.12.100或12.12.12.200浏览nginx网页你会发现网页显示的IP一直是192.168.1.20,表明keepalived已经成功接管node1的vip和nginx或php-fpm服务。
3)测试mysql HA。在node1执行service mysqld stop停止mysql服务,几秒后在node2查看,发现node2已经接管vip 192.168.1.100,并且已经启动mysql服务。
注意:在恢复mysql或nginx,php-fpm时,先停止监控脚本,要不heartbeat或keepalived还没实现接管又被停止。
参考:http://bbs.ywlm.net/thread-965-1-1.html

NFS常见错误

错误一:Cannot register service: RPC

service nfs restart

Shutting down NFS mountd: [ OK ]

Shutting down NFS daemon: [ OK ]

Shutting down NFS quotas: [ OK ]

Shutting down NFS services: [ OK ]

Starting NFS services: [ OK ]

Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno = Connection refused

rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp).

[FAILED]

#解决方法:

service portmap start

#先启动portmap才行

错误二:Address already in use

tail -f /var/log/message

Apr :: bogon nfsd[]: nfssvc: Setting version failed: errno (Device or resource busy)

Apr :: bogon nfsd[]: nfssvc: unable to bind UPD socket: errno (Address already in use)

Apr :: bogon nfsd[]: nfssvc: Setting version failed: errno (Device or resource busy)

Apr :: bogon nfsd[]: nfssvc: unable to bind UPD socket: errno (Address already in use)

Apr :: bogon nfsd[]: nfssvc: Setting version failed: errno (Device or resource busy)

#解决方法:

ps aux | grep nfs

#然后用kill干掉这些进程

错误三:mount: …:/nfsdata failed, reason given by server: Permission denied

#解决方法:

a.把该客户端的ip加入服务端的/etc/exports

b.服务端的和客户端规则要统一,要么都使用主机名(注意每台机器的hosts文件),要么都使用IP

错误四:客户端挂载超时

tail -f /var/log/message

Apr :: localhost kernel: portmap: server localhost not responding, timed out

Apr :: localhost kernel: RPC: failed to contact portmap (errno -).

Apr :: localhost kernel: RPC: failed to contact portmap (errno -).

Apr :: localhost kernel: lockd_up: makesock failed, error=-

Apr :: localhost kernel: RPC: failed to contact portmap (errno -).

#解决方法:

service portmap restart

service nfs restart

错误五:Error: RPC MTAB does not exist.

service nfs start

Starting NFS services: [ OK ]

Starting NFS quotas: [ OK ]

Starting NFS daemon: [ OK ]

Starting NFS mountd: [ OK ]

Starting RPC idmapd: Error: RPC MTAB does not exist.

#解决方法:

#手动执行

mount -t rpc_pipefs sunrpc /var/lib/nfs/rpc_pipefs/

#需要时加入开机启动时,加入下面两行到/etc/fstab

rpc_pipefs /var/lib/nfs/rpc_pipefs rpc_pipefs defaults

nfsd /proc/fs/nfsd nfsd defaults

PHP环境安全性能检查

PHP在Linux环境下安全配置是一个复杂的过程,其中涉及到很多的细节设置,在这里发出来一个脚本,通过这个脚本来检测你的PHP环境是否存在安全隐患,从而针对这些对你的PHP环境进行加固。
功能:

  • 1.检测PHP环境安全配置
  • 2.应禁用的功能。
  • 3.危险的设置,可能会导致本地或远程文件包含。
  • 4.错误处理。
  • 5.在编译时定义的常量。
  • 安装PHP环境后,将此三个文件脚本放在网站web目录下(audit.php php.xml style.css )进行浏览器查看,他将在你配置的基础中通过XML文件中匹配规则检测出可能存在的配置错误,存在问题的选项它会用红色突出的颜色显示。当然还有一些东西可以根据你的要求更改。
    效果如下:
    服务器安全
    audit.php

    1. <?php
    2. /**
    3.  * PHP Security Auditor
    4.  */
    5. class Audit {
    6.  
    7. static private $rules;
    8. static private $constants;
    9. static private $phpVer;
    10.  
    11. static public $report;
    12.  
    13. /**
    14. * Converts settings such as 1M 1G 1K to their byte equivilent values
    15. *
    16. * @param string $n
    17. * @return string
    18. */
    19. static private function convertToBytes($n) {
    20.  
    21. // If n is -1 then there is no limit
    22.      if ($n == -1)
    23.      return PHP_INT_MAX;
    24.  
    25.      switch (substr($n, -1)) {
    26.                     case "B": return substr($n,0,-1);
    27.       case "K": return substr($n,0,-1) * 1024;
    28.                     case "M": return substr($n,0,-1) * 1024 * 1024;
    29.                     case "G": return substr($n,0,-1) * 1024 * 1024 * 1024;
    30.             }
    31.             return $n;
    32.      }
    33.  
    34. static private function MakeReport($type, $title) {
    35.  
    36. ksort(self::$report[$type]);
    37.  
    38.      $html = ‘<h1>’ . $title . ‘</h1><table><tr class="h"><th>Setting</th><th>Current</th><th>Recomended</th><th>Description</th></tr>’;
    39.     foreach(self::$report[$type] as $key => $values)
    40.     {
    41.     if ($values[‘p’] == 1) $class="r";
    42.     else $class="v";
    43.  
    44. $html .= ‘<tr><td class="e">’ . htmlentities($key) . ‘</td>’ .
    45. ‘<td class="’. $class .’">’ . htmlentities($values[‘c’]) . ‘</td>’ .
    46. ‘<td class="’. $class .’">’ . htmlentities($values[‘r’]) . ‘</td>’ .
    47. ‘<td class="’. $class .’">’ . htmlentities($values[‘d’]) . ‘</td></tr>’;
    48.     }
    49.     $html .= ‘</table>’;
    50.  
    51. return $html;
    52. }
    53.  
    54.  
    55.     static public function HTMLReport()
    56.      {
    57.      $class = "";
    58.  
    59.      $html = ‘<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">’ .
    60. ‘<html><head>’ .
    61.      ‘<link rel="stylesheet" type="text/css" media="all" href="style.css"/>’ .
    62.      ‘</head><body>’;
    63.  
    64.      $html .= self::MakeReport("ini", "PHP INI");
    65.      $html .= self::MakeReport("disabled", "PHP Disabled Functions");
    66.      $html .= self::MakeReport("const", "PHP CONST");
    67.  
    68.     $html .= ‘</html>’;
    69.  
    70.     echo($html . "n");
    71. }
    72.  
    73.    /**
    74.    * Adds an item to the reporting array.
    75.    *
    76.    * @param string $type – the type (ini or const)
    77.    * @param string $key – the name of the variable
    78.    * @param string $currentValue – the current ini or const value
    79.    * @param string $recomended – the recomended value
    80.    * @param string $desc – a description of the issue
    81.    * @param boolean $problem – true if not complaint, false if compliant
    82.    */
    83. static private function Report($type, $key, $currentValue, $recomended, $desc, $problem)
    84. {
    85. if (isset(self::$report[$type][$key]))
    86. if ((self::$report[$type][$key][‘r’] < $recomended)
    87. && (self::$report[$type][$key[‘p’]] == 1))
    88. return;
    89.  
    90. self::$report[$type][$key] = array(
    91. "c" => $currentValue,
    92. "r" => $recomended,
    93. "d" => $desc,
    94. "p" => $problem
    95. );
    96. }
    97.  
    98. /**
    99. * Loads the rules from an XML file
    100. *
    101. * @param string $file
    102. */
    103. static public function LoadRules($file = "php.xml")
    104. {
    105.  
    106. if (!defined(‘PHP_VERSION_ID’))
    107. {
    108. $version = explode(".", PHP_VERSION);
    109. self::$phpVer =  ($version[0] * 10000 + $version[1] * 100 + $version[2]);
    110. } else
    111. self::$phpVer = PHP_VERSION_ID;
    112.  
    113. self::$constants = get_defined_constants();
    114. self::$rules = simplexml_load_file($file);
    115. }
    116.  
    117. /**
    118. * Processes the XML ruleset against const and ini values found in PHP
    119. *
    120. */
    121. static public function ProcessXML() {
    122.  
    123. foreach(self::$rules as $null => $entry) {
    124. $ruleID = $entry->attributes()->id;
    125.  
    126. // Check the version of PHP the rule applies to
    127.  
    128. $version = (string)$entry->version;
    129.  
    130. if ($version != "") {
    131.  
    132. $op = (string)$entry->version->attributes()->op;
    133.  
    134. switch ($op) {
    135. case ‘before’:
    136. if ($version < self::$phpVer)
    137. continue 2;
    138. break;
    139. }
    140. }
    141.  
    142. // Evaluate the rule as we are sure it applys to the version of PHP running
    143.  
    144. switch((string)$entry->type)
    145. {
    146. // Look at CONST values in PHP
    147. case "const":
    148.  
    149. $key = (string)$entry->key; // e.g LIBXML_NOENT
    150. $cValue = self::$constants[$key]; // The current value
    151. $rValue = (string)$entry->value; // The recomended value
    152. $desc = (string)$entry->description; // Description
    153.  
    154. switch((string)$entry->value->attributes()->op)
    155. {
    156. case "eq":
    157. self::Report("const", $key, $cValue, $rValue, $desc, ($cValue == $rValue) ? 0 : 1);
    158. break;
    159. }
    160.  
    161. break;
    162.  
    163. // Check the list of functions that should be restricted
    164.  
    165. case "disable_functions":
    166.  
    167. $disabled = ini_get("disable_functions");
    168. $list = explode(",", $disabled);
    169.  
    170. $xmlList = (array)($entry->list);
    171. $xmlList = $xmlList[‘function’];
    172.  
    173. foreach($xmlList as $null => $function) {
    174. $de = array_search($function, $list);
    175. self::Report("disabled", $function, (($de == 0) ? "enabled" : "disabled"), "disabled", "", (($de == 0) ? 1 : 0));
    176. }
    177.  
    178. break;
    179.  
    180. // Look at values defined within the INI files
    181.  
    182. case "ini":
    183.  
    184. $key = (string)$entry->key; // e.g. display_errors
    185. $cValue = trim(self::convertToBytes(ini_get($key))); // Current value
    186. $rValue = (string)$entry->value; // Recomended value
    187. $desc = (string)$entry->description; // Description
    188.  
    189. if (is_numeric($rValue) && $cValue == "") $cValue = "0";
    190.  
    191. // Deals with where one value should be compared to another
    192.  
    193. if ((string)$entry->value->attributes()->type == "key")
    194. $rValue = self::convertToBytes(ini_get((string)$entry->value));
    195.  
    196. switch((string)$entry->value->attributes()->op)
    197. {
    198. // Equal to
    199. case "eq":
    200. self::Report("ini", $key, $cValue, $rValue, $desc, ($cValue == $rValue) ? 0 : 1);
    201. break;
    202.  
    203. // Less than or equal to
    204. case "lt":
    205. self::Report("ini", $key, $cValue, "< $rValue", $desc, ($cValue <= $rValue) ? 0 : 1);
    206. break;
    207.  
    208. // Greater than or equal to
    209. case "gt":
    210. self::Report("ini", $key, $cValue, "> $rValue", $desc, ($cValue >= $rValue) ? 0 : 1);
    211. break;
    212.  
    213. // Not equal to
    214. case "ne":
    215. $neValue  = (string)$entry->value->attributes()->net;
    216. $notBlank = (string)$entry->value->attributes()->notblank;
    217.  
    218.  
    219. if ($notBlank == "true") {
    220. self::Report("ini", $key, $cValue, $rValue, $desc, ($cValue != "") ? 0 : 1);
    221. break;
    222. }
    223.  
    224. self::Report("ini", $key, $cValue, $rValue, $desc, ($cValue != $neValue) ? 0 : 1);
    225. break;
    226.  
    227. }
    228.  
    229. break;
    230. }
    231.  
    232. }
    233.  
    234. }
    235.  
    236.  
    237. }
    238.  
    239. Audit::LoadRules();
    240. Audit::ProcessXML();
    241. Audit::HTMLReport();

    php.xml代码如下:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <rules>
    3. <entry id="1">
    4. <type>ini</type>
    5. <key>upload_max_filesize</key>
    6. <value op="lt">4194304</value>
    7. <description>Sets the maximum size of an uploaded file. Reduce this to mitigate the risk of DOS attacks.</description>
    8. </entry>
    9. <entry id="29">
    10. <type>ini</type>
    11. <key>upload_max_filesize</key>
    12. <value op="lt" type="key">memory_limit</value>
    13. <description>The maximum size of an uploaded file should be able to fit within the avaliable memory limit.</description>
    14. </entry>
    15. <entry id="30">
    16. <type>ini</type>
    17. <key>post_max_size</key>
    18. <value op="lt" type="key">memory_limit</value>
    19. <description>The maximum post size of data posted to the server should be within the avaliable memory limit.</description>
    20. </entry>
    21. <entry id="32">
    22. <type>ini</type>
    23. <key>always_populate_raw_post_data</key>
    24. <value op="eq">0</value>
    25. <description>This does not need to be used. The preferred method for accessing the raw POST data is php://input.</description>
    26. </entry>
    27. <entry id="33">
    28. <type>ini</type>
    29. <key>magic_quotes_gpc</key>
    30. <value op="eq">0</value>
    31. <description>Sets magic_quotes state for GPC (GET PUT COOKIE) data.  Relying on this feature is highly discouraged.</description>
    32. <version op="before">50300</version>
    33. <url>http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc</url>
    34. </entry>
    35. <entry id="34">
    36. <type>ini</type>
    37. <key>magic_quotes_runtime</key>
    38. <value op="eq">0</value>
    39. <description>Sets magic_quotes state for data from external sources.  Relying on this feature is highly discouraged.</description>
    40. <version op="before">50300</version>
    41. <url>http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-runtime</url>
    42. </entry>
    43. <entry id="35">
    44. <type>ini</type>
    45. <key>safe_mode</key>
    46. <value op="eq">0</value>
    47. <description>This feature has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.</description>
    48. <version op="before">50300</version>
    49. </entry>
    50. <entry id="36">
    51. <type>ini</type>
    52. <key>memory_limit</key>
    53. <value op="lt">16777216</value>
    54. <description>The maximum memory limit for each script should be 16M or less.</description>
    55. </entry>
    56. <entry id="5">
    57. <type>ini</type>
    58. <key>upload_max_filesize</key>
    59. <value op="lt" type="key">post_max_size</value>
    60. <description>The maximum upload file size should be less than or equal to the maximum post size.</description>
    61. </entry>
    62. <entry id="2">
    63. <type>ini</type>
    64. <key>max_file_uploads</key>
    65. <value op="lt">10</value>
    66. <description>The maximum mumber of files that can be uploaded in 1 go.</description>
    67. </entry>
    68. <entry id="3">
    69. <type>ini</type>
    70. <key>file_uploads</key>
    71. <value op="eq">0</value>
    72. <description>This may be impractical but if not needed file uploading should be disabled.</description>
    73. </entry>
    74. <entry id="4">
    75. <type>ini</type>
    76. <key>post_max_size</key>
    77. <value op="lt">4194304</value>
    78. <description>The maximum post size should as small as reasonably possible to mitigate the risk of DOS attacks.</description>
    79. </entry>
    80. <entry id="6">
    81. <type>ini</type>
    82. <key>register_long_arrays</key>
    83. <value op="eq">0</value>
    84. <description>Populates HTTP_*_VARS which should no longer be used.</description>
    85. <version op="before">50300</version>
    86. </entry>
    87. <entry id="7">
    88. <type>ini</type>
    89. <key>register_globals</key>
    90. <value op="eq">0</value>
    91. <description>Highly dangerous feature enabling variables to be defined in scripts from the GPC paramaters. This should be always be turned off.</description>
    92. <version op="before">50300</version>
    93. </entry>
    94. <entry id="8">
    95. <type>ini</type>
    96. <key>session.hash_function</key>
    97. <value op="eq">1</value>
    98. <description>MD5 should be replaced with SHA-160 as it is a more complex and secure hashing algorithm.</description>
    99. <version op="after">50000</version>
    100. </entry>
    101. <entry id="9">
    102. <type>ini</type>
    103. <key>session.hash_bits_per_character</key>
    104. <value op="gt">5</value>
    105. <description>The number of bits encoded per character of the session key.</description>
    106. <version op="after">50000</version>
    107. </entry>
    108. <entry id="10">
    109. <type>ini</type>
    110. <key>session.entropy_file</key>
    111. <value op="ne" net="">/dev/random</value>
    112. <description>Provides a random seed for generating the session.</description>
    113. </entry>
    114. <entry id="11">
    115. <type>ini</type>
    116. <key>session.entropy_length</key>
    117. <value op="gt">32</value>
    118. <description>The number of bytes to read for gathering entropy for session generation.</description>
    119. </entry>
    120. <entry id="12">
    121. <type>ini</type>
    122. <key>session.name</key>
    123. <value op="ne" net="PHPSESSID">Custom String</value>
    124. <description>The name given to the PHP Session. It is recomended this be changed from the default.</description>
    125. </entry>
    126. <entry id="14">
    127. <type>ini</type>
    128. <key>session.save_path</key>
    129. <value op="ne" net="/tmp" notblank="true">/custom/location</value>
    130. <description>The save path for the session should be changed from the default /tmp.</description>
    131. </entry>
    132. <entry id="15">
    133. <type>ini</type>
    134. <key>session.use_trans_sid</key>
    135. <value op="eq">0</value>
    136. <description>Sessions should not be allowed in GET paramaters.</description>
    137. </entry>
    138. <entry id="18">
    139. <type>ini</type>
    140. <key>display_errors</key>
    141. <value op="eq">0</value>
    142. <description>Error messages should be suppressed</description>
    143. </entry>
    144. <entry id="19">
    145. <type>ini</type>
    146. <key>allow_url_fopen</key>
    147. <value op="eq">0</value>
    148. <description>Remote files should not be accessable using fopen.</description>
    149. </entry>
    150. <entry id="20">
    151. <type>ini</type>
    152. <key>allow_url_include</key>
    153. <value op="eq">0</value>
    154. <description>You should not be able to include remote scripts using include.</description>
    155. </entry>
    156. <entry id="31">
    157. <type>ini</type>
    158. <key>session.cookie_httponly</key>
    159. <value op="eq">1</value>
    160. <description>Cookies must be httponly by default</description>
    161. <version op="after">50200</version>
    162. </entry>
    163. <entry id="20">
    164. <type>ini</type>
    165. <key>open_basedir</key>
    166. <value op="ne" net="/" notblank="true">/the/webroot</value>
    167. <description>Limit the files that can be opened by PHP to the webroot.</description>
    168. </entry>
    169. <entry id="32">
    170. <type>ini</type>
    171. <key>upload_tmp_dir</key>
    172. <value op="ne" net="/tmp" notblank="true">/custom/location</value>
    173. <description>Change the location of where files are initally uploaded to</description>
    174. </entry>
    175. <entry id="21">
    176. <type>ini</type>
    177. <key>max_execution_time</key>
    178. <value op="lt">20</value>
    179. <description>Execution time should be limited to 20 seconds or less.</description>
    180. </entry>
    181. <entry id="22">
    182. <type>ini</type>
    183. <key>max_input_nesting_level</key>
    184. <value op="lt">32</value>
    185. <description>Maximum level of nesting of objects 32 is sufficent.</description>
    186. </entry>
    187. <entry id="23">
    188. <type>ini</type>
    189. <key>enable_dl</key>
    190. <value op="eq">0</value>
    191. <description>Disable loading of dynamic extensions.</description>
    192. </entry>
    193. <entry id="24">
    194. <type>ini</type>
    195. <key>display_startup_errors</key>
    196. <value op="eq">0</value>
    197. <description>Startup errors should be suppressed.</description>
    198. </entry>
    199. <entry id="25">
    200. <type>ini</type>
    201. <key>log_errors</key>
    202. <value op="eq">1</value>
    203. <description>All errors generated by PHP should be logged to a file.</description>
    204. </entry>
    205. <entry id="26">
    206. <type>ini</type>
    207. <key>log_errors_max_len</key>
    208. <value op="gt">2048</value>
    209. <description>At least 2048 characters of the error message should be stored in the error log.</description>
    210. </entry>
    211. <entry id="27">
    212. <type>ini</type>
    213. <key>error_log</key>
    214. <value op="ne" net="">/custom/location</value>
    215. <description>Should be set to the location of the php error log.</description>
    216. </entry>
    217. <entry id="28">
    218. <type>const</type>
    219. <key>LIBXML_NOENT</key>
    220. <value op="eq">0</value>
    221. <description>External entities should be disabled for XML parsing</description>
    222. </entry>
    223. <entry id="37">
    224. <type>ini</type>
    225. <key>session.use_only_cookies</key>
    226. <value op="eq">1</value>
    227. <description>Session variables should only be passed in cookies.</description>
    228. </entry>
    229. <entry id="29">
    230. <type>const</type>
    231. <key>LIBXML_NONET</key>
    232. <value op="eq">0</value>
    233. <description>Network access for XML parsers should be disabled.</description>
    234. </entry>
    235. <entry id="38">
    236. <type>disable_functions</type>
    237. <list>
    238. <function>fsocket_open</function>
    239. <function>pack</function>
    240. <function>escapeshellarg</function>
    241. <function>escapeshellcmd</function>
    242. <function>exec</function>
    243. <function>passthru</function>
    244. <function>proc_close</function>
    245. <function>php_uname</function>
    246. <function>getmyuid</function>
    247. <function>getmypid</function>
    248. <function>passthru</function>
    249. <function>leak</function>
    250. <function>listen</function>
    251. <function>diskfreespace</function>
    252. <function>tmpfile</function>
    253. <function>link</function>
    254. <function>ignore_user_abort</function>
    255. <function>set_time_limit</function>
    256. <function>limit</function>
    257. <function>exec</function>
    258. <function>highlight_file</function>
    259. <function>show_source</function>
    260. <function>fpaththru</function>
    261. <function>virtual</function>
    262. <function>posix_ctermid</function>
    263. <function>posix_getcwd</function>
    264. <function>posix_getegid</function>
    265. <function>posix_geteuid</function>
    266. <function>posix_getgid</function>
    267. <function>posix_getgrgid</function>
    268. <function>posix_getgrnam</function>
    269. <function>posix_getgroups</function>
    270. <function>posix_getlogin</function>
    271. <function>posix_getpgid</function>
    272. <function>posix_getpgrp</function>
    273. <function>posix_getpid</function>
    274. <function>posix</function>
    275. <function>posix_getpwnam</function>
    276. <function>posix_getpwuid</function>
    277. <function>posix_getrlimit</function>
    278. <function>posix_getsid</function>
    279. <function>posix_getuid</function>
    280. <function>posix_isatty</function>
    281. <function>posix_kill</function>
    282. <function>posix_mkfifo</function>
    283. <function>posix_setegid</function>
    284. <function>posix_seteuid</function>
    285. <function>posix_setgid</function>
    286. <function>posix_setpgid</function>
    287. <function>posix_setsid</function>
    288. <function>posix_setuid</function>
    289. <function>posix_times</function>
    290. <function>posix_ttyname</function>
    291. <function>posix_uname</function>
    292. <function>proc_open</function>
    293. <function>proc_close</function>
    294. <function>proc_get_status</function>
    295. <function>proc_nice</function>
    296. <function>proc_terminate</function>
    297. <function>phpinfo</function>
    298. <function>proc_open</function>
    299. <function>shell_exec</function>
    300. <function>system</function>
    301. <function>set_time_limit</function>
    302. <function>ini_alter</function>
    303. <function>dl</function>
    304. <function>popen</function>
    305. <function>parse_ini_file</function>
    306. </list>
    307. </entry>
    308. </rules>

    style.css代码如下:

    1. @CHARSET "UTF-8";
    2.  
    3. body {background-color: #ffffff; color: #000000;}
    4. body, td, th, h1, h2 {font-family: sans-serif;}
    5. pre {margin: 0px; font-family: monospace;}
    6. table {border-collapse: collapse;}
    7. td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;  padding-left:5px; padding-right:5px;}
    8. h1 {font-size: 150%;}
    9. h2 {font-size: 125%;}
    10. .p {text-align: left;}
    11. .e {background-color: #ccccff; font-weight: bold; color: #000000;}
    12. .h {background-color: #9999cc; font-weight: bold; color: #000000;}
    13. .v {background-color: #cccccc; color: #000000; padding-left:5px;}
    14. .r {background-color: #c50000; color: #000000;  padding-left:5px;}

    三个文件已经打包:php-security-check.zip
    转自:http://lanlan611.sinaapp.com/?p=112

    lvs负载均衡及高可用(heartbeat+ldirectord)集群配置

    lvs是一个开源免费的负载均衡软件,能实现多台服务器之间的负载均衡,搭配heartbeat和ldirectord的使用,就能配置成高可用的集群。

    服务器环境说明

    下面说明本次测试配置的服务器环境。
    系统:CentOS-5 32 内核2.6.18-238.el5
    因为机器只有两台,所以lvs负载器和后端服务器在同一机器。
    node1 192.168.79.130
    node2 192.168.79.131
    VIP 192.168.79.135
    当node1出现故障时,lvs负载器和web服务器转移到node2。
    如果机器充足,还是建议lvs负载器和web服务器分开。

    软件安装

    1. yum -y install heartbeat heartbeat-ldirectord ipvsadm

    配置

    主要的配置文件有以下几个:
    Authkeys
    ha.cf
    ldirectord.cf
    haresources

    authkeys

    1. vi /etc/ha.d/authkeys

    代码:

    1. auth 1
    2. 1 crc

    ha.cf

    1. vi /etc/ha.d/ha.cf
    1. debugfile /var/log/ha-debug
    2. logfile /var/log/ha-log
    3. logfacility local0
    4. keepalive 8
    5. deadtime 60
    6. warntime 60
    7. initdead 120
    8. udpport 694
    9. ucast eth0 192.168.79.131
    10. auto_failback on
    11. node node1
    12. node node2
    13. respawn hacluster /usr/lib/heartbeat/ipfail
    14. apiauth ipfail gid=haclient uid=hacluster

    node2唯一不同是ucast eth0 192.168.79.131,把IP改成node1的IP。

    haresources

    1. vi /etc/ha.d/haresources

    填入:

    1. node1 lvs IPaddr::192.168.79.135/24/eth0:0 ldirectord

    这段代码的意思是双机启动heartbeat时,启动node1的lvs脚本,接着配置vip 192.168.79.135/24/eth0:0,然后启动ldirectord来设置node1成lvs负载器并监控80端口。如果node1出故障,node1的heartbeat将从右到左停止服务,如先停止ldirectord,取消vip等。接着node2将接管node1的所有服务,如vip,web服务等。

    ldirectord.cf

    1. vi /etc/ha.d/ldirectord.cf
    1. checktimeout=10
    2. checkinterval=8
    3. autoreload=yes
    4. logfile="/var/log/ldirectord.log"
    5. logfile="local0"
    6. quiescent=no
    7.  
    8. virtual=192.168.79.135:80
    9. real=192.168.79.130:80 gate
    10. real=192.168.79.131:80 gate
    11. service=http
    12. request="test.html"
    13. receive="Test Page"
    14. scheduler=wrr
    15. persistent=30
    16. protocol=tcp
    17. checktype=negotiate
    18. checkport=80

    node2配置这文件时,需要把real=192.168.79.130:80 gate删除,因为当lvs负载器转移到node2时,不能把故障机node1添加到虚拟机。

    test.html

    在网站根目录建立test.html,并写入Test Page字段,这个用来监控web服务器的健康情况。假设根目录为/var/www/html:

    1. echo "Test Page" > /var/www/html/test.html

    lvs启动脚本

    1. vi /etc/init.d/lvs

    node1上的lvs启动脚本:

    1. #!/bin/bash
    2. /sbin/ipvsadm –set 10 10 10

    node1上的lvs启动脚本:

    1. #!/bin/bash
    2. VIP=192.168.79.135
    3. /etc/rc.d/init.d/functions
    4. /sbin/ipvsadm –set 10 10 10
    5. case "$1" in
    6. start)
    7. /sbin/ifconfig lo:0 down
    8. /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
    9. /sbin/route add -host $VIP dev eth0:0
    10. ;;
    11. stop)
    12. /sbin/ifconfig eth0:0 down
    13. /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
    14. /sbin/route add -host $VIP dev lo:0
    15. ;;
    16. *)
    17. echo "Usage: $0 {start|stop}"
    18. exit 1
    19. esac

    最后加上执行权限:

    1. chmod +x /etc/init.d/lvs

    主机名及hosts配置

    1、对两台机分别设置对应的主机名
    192.168.79.130 为 node1
    192.168.79.131 为 node2
    2、添加主机名解析

    1. vi /etc/hosts
    1. 192.168.79.130 node1
    2. 192.168.79.131 node2

    解决arp问题

    1. vi /etc/sysctl.conf
    1. net.ipv4.ip_forward = 1
    2. net.ipv4.conf.lo.arp_ignore = 1
    3. net.ipv4.conf.lo.arp_announce = 2
    4. net.ipv4.conf.all.arp_ignore = 1
    5. net.ipv4.conf.all.arp_announce = 2

    立即使内核参数生效:

    1. sysctl -p

    lvs测试

    测试负载均衡:可以在两台机放入不同的首页内容,在不同的客户端测试是否显示不一样的内容
    测试高可用:关掉node1 heartbeat,在node2执行ip a查看是否已经接管vip。
    测试ldirectord:ldirectord可以实时监控指定的服务是否可用,如果发现不可用,就会使用ipvsadm把这台故障的机从虚拟机中删除。