实现负载均衡的方式有很多种,DNS、反向代理、LVS负载均衡器(软件实现)、F5(负载均衡器,硬件,非常昂贵)这里我们只提到基于DNS,以及反向代理的方式来实现负载均衡Web服务

     DNS服务器实现负载均衡的原理是基于轮询的方式实现的,直接由DNS解析系统进行随机分配。除了性能分配不均之外,还有更可怕的就是如果一台服务器down了,DNS服务器因为是基于轮询方式的负载均的,所以它并不能够探测到或者没有对付这种情况的动作,所以会继续向这台服务器分配流量,导致访问超时,给用户带来不便。并且基于DNS服务实现负载均衡web集群,需要3个公网IP,代价可想而知。现在IPv4的资源及其紧张,铺张浪费不是一个好的运维工程师,你懂得.

     如果说使用nginx实现负载均衡的话

如图所示:内网有三台web server(两台Apache,一台Nginx),管理员可以根据服务器的性能,设置权重,来分配服务器的使用几率。如果某台服务器反回超时或者挂掉了,Nginx反向代理会基于max_fails这样的机制,使其不再把用户分配到那台服务器,这样比DNS服务器直接进行随机分配好的多。可靠性和利用率大大提高。你懂得.

    并且基于nginx反向代理:只要有一个IP地址来绑定就可以实现nginx负载均衡,大大节省购买IP地址时的代价。当用户访问公司域名时,请求直接被发送到Nginx Server上,Nginx Server根据weight值和fail_timeout来进行合理分配服务器资源。注释:

更加人性化。反向代理还经常被称为网关服务器,因为它能够防止了公司内网Web server直接暴露在外网的环境下,在一定程度上保证了web server的安全。

   NFS 全称:Network file system NFS由SUN公司开发,目前已经成为文件服务的一种标准。我们在这里,通过NFS实现存储.

   NFS 跟PHP-FPM服务器为同一台都为(172.16.251.215)

一、配置Nginx反向代理

1、创建用户和组

2、编译安装Nginx

# groupadd nginx# useradd -g nginx -s /bin/false -M nginx./configure \  --prefix=/usr \  --sbin-path=/usr/sbin/nginx \  --conf-path=/etc/nginx/nginx.conf \  --error-log-path=/var/log/nginx/error.log \  --http-log-path=/var/log/nginx/access.log \  --pid-path=/var/run/nginx/nginx.pid  \  --lock-path=/var/lock/nginx.lock \  --user=nginx \  --group=nginx \  --with-http_ssl_module \  --with-http_flv_module \  --with-http_stub_status_module \  --with-http_gzip_static_module \  --http-client-body-temp-path=/var/tmp/nginx/client/ \  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \  --with-pcremake && make install

3、提供Nginx服务脚本

#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig:   - 85 15# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \#               proxy and IMAP/POP3 proxy server# processname: nginx# config:      /etc/nginx/nginx.conf# config:      /etc/sysconfig/nginx# pidfile:     /var/run/nginx.pid                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 # Source function library.. /etc/rc.d/init.d/functions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 # Source networking configuration.. /etc/sysconfig/network                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 # Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 nginx="/usr/sbin/nginx"prog=$(basename $nginx)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NGINX_CONF_FILE="/etc/nginx/nginx.conf"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 lockfile=/var/lock/subsys/nginx                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 make_dirs() {   # make required directories   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`   options=`$nginx -V 2>&1 | grep 'configure arguments:'`   for opt in $options; do       if [ `echo $opt | grep '.*-temp-path'` ]; then           value=`echo $opt | cut -d "=" -f 2`           if [ ! -d "$value" ]; then               # echo "creating" $value               mkdir -p $value && chown -R $user $value           fi       fi   done}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 start() {    [ -x $nginx ] || exit 5    [ -f $NGINX_CONF_FILE ] || exit 6    make_dirs    echo -n $"Starting $prog: "    daemon $nginx -c $NGINX_CONF_FILE    retval=$?    echo    [ $retval -eq 0 ] && touch $lockfile    return $retval}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 stop() {    echo -n $"Stopping $prog: "    killproc $prog -QUIT    retval=$?    echo    [ $retval -eq 0 ] && rm -f $lockfile    return $retval}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 restart() {    configtest || return $?    stop    sleep 1    start}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 reload() {    configtest || return $?    echo -n $"Reloading $prog: "    killproc $nginx -HUP    RETVAL=$?    echo}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 force_reload() {    restart}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 configtest() {  $nginx -t -c $NGINX_CONF_FILE}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 rh_status() {    status $prog}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 rh_status_q() {    rh_status >/dev/null 2>&1}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 case "$1" in    start)        rh_status_q && exit 0        $1        ;;    stop)        rh_status_q || exit 0        $1        ;;    restart|configtest)        $1        ;;    reload)        rh_status_q || exit 7        $1        ;;    force-reload)        force_reload        ;;    status)        rh_status        ;;    condrestart|try-restart)        rh_status_q || exit 0            ;;    *)        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"        exit 2esac

4、配置Nginx实现反向代理

#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    #proxy_cache_path /nginx/cache levels=1:2 keys_zone=mycache:16m#           inactive=24h max_size=1g;    upstream backend {    server 172.16.251.253  weight=1 max_fails=2  fail_timeout=30s;    server 172.16.251.244  weight=1 max_fails=2  fail_timeout=30s;    server 172.16.251.208  weight=1 max_fails=2  fail_timeout=30s;}    server {        listen       80;    server_name www.nginx.com  172.16.251.187;    location /  {    proxy_pass http://backend;    proxy_set_header host www.nginx.com;    #proxy_cache mycache;    #proxy_cache_valid 200 301 1d;    #proxy_cache_valid 404 1m;}#        location / {#            root   html;#            index   index.php index.html index.htm;#        }        error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   172.16.251.215:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443;    #    server_name  localhost;    #    ssl                  on;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_timeout  5m;    #    ssl_protocols  SSLv2 SSLv3 TLSv1;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers   on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

二、安装web服务,编译安装Httpd     Web Server:172.16.251.208                  

Web Server: 172.16.251.244

1、编译安装http2.4.9,依赖于更高版本的aprapr-utilapr全称为apache portable runtime

#tar xf apr-1.5.0.tar.bz2#cd apr-1.5.0#./configure --prefix=/usr/local/apr#make && make install

2、编译安装apr-util-1.5.2

#tar xf apr-util-1.5.2.tar.bz2#cd apr-util-1.5.2#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/#make && make install

3、编译安装http2.4.9

# tar xf httpd-2.4.9.tar.bz2# cd httpd-2.4.9# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event# make && make install

4、后续的配置:

#ln -sv /usr/local/apache/include/usr/include/httpd#vim /etc/profile.d/httpd.shexportPATH=/usr/local/apache/bin:$PATH#vim /etc/man.conf#MANPATH  /usr/local/apache/man

5、3、修改httpd的主配置文件,设置其Pid文件的路径 vim /etc/httpd24/httpd.conf

PidFile  "/var/run/httpd.pid"

6、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:

#!/bin/bash## httpd        Startup script for the Apache HTTP Server## chkconfig: - 85 15# description: Apache is a World Wide Web server.  It is used to serve \#        HTML files and CGI.# processname: httpd# config: /etc/httpd/conf/httpd.conf# config: /etc/sysconfig/httpd# pidfile: /var/run/httpd.pid# Source function library.. /etc/rc.d/init.d/functionsif [ -f /etc/sysconfig/httpd ]; then        . /etc/sysconfig/httpdfi# Start httpd in the C locale by default.HTTPD_LANG=${HTTPD_LANG-"C"}# This will prevent initlog from swallowing up a pass-phrase prompt if# mod_ssl needs a pass-phrase from the user.INITLOG_ARGS=""# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server# with the thread-based "worker" MPM; BE WARNED that some modules may not# work correctly with a thread-based MPM; notably PHP will refuse to start.# Path to the apachectl script, server binary, and short-form for messages.apachectl=/usr/local/apache/bin/apachectlhttpd=${HTTPD-/usr/local/apache/bin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0start() {        echo -n $"Starting $prog: "        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS        RETVAL=$?        echo        [ $RETVAL = 0 ] && touch ${lockfile}        return $RETVAL}stop() {  echo -n $"Stopping $prog: "  killproc -p ${pidfile} -d 10 $httpd  RETVAL=$?  echo  [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}}reload() {    echo -n $"Reloading $prog: "    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then        RETVAL=$?        echo $"not reloading due to configuration syntax error"        failure $"not reloading $httpd due to configuration syntax error"    else        killproc -p ${pidfile} $httpd -HUP        RETVAL=$?    fi    echo}# See how we were called.case "$1" in  start)  start  ;;  stop)  stop  ;;  status)        status -p ${pidfile} $httpd  RETVAL=$?  ;;  restart)  stop  start  ;;  condrestart)  if [ -f ${pidfile} ] ; then    stop    start  fi  ;;  reload)        reload  ;;  graceful|help|configtest|fullstatus)  $apachectl $@  RETVAL=$?  ;;  *)  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"  exit 1esacexit $RETVAL

7、而后为此脚本赋予执行权限:并且加入服务列表:

# chmod +x /etc/rc.d/init.d/httpd# chkconfig --add httpd

8、配置Apache能够调用后端的PHP-FPM服务器(fastCIG服务器),因为默认编译安装的Httpd有许多模块都是被注释掉的,要启用之,去掉注释即可:并且在这里Apache是作为反向代理服务,代理PHP-FPM(fastCGI)服务器工作之.所以在这里需要启动代理模块

LoadModule proxy_module modules/mod_proxy.so#LoadModule proxy_connect_module modules/mod_proxy_connect.so#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so#LoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

9、根据标准配置我们还应该启用虚拟主机,在Httpd2.4.9中配置文件被拆分了多个子配置文件,在/etc/httpd24/extra/此目录下众多的配置文件都是httpd的,前面说到要想启用虚拟主机也要去掉注释,表示将虚拟主机的配置文件包含之,实现的方法都是如此

460 # User home directories461 #Include /etc/httpd24/extra/httpd-userdir.conf462463 # Real-time info on requests and configuration464 #Include /etc/httpd24/extra/httpd-info.conf465466 # Virtual hosts467 Include /etc/httpd24/extra/httpd-vhosts.conf468469 # Local access to the Apache HTTP Server Manual470 #Include /etc/httpd24/extra/httpd-manual.conf471472 # Distributed authoring and versioning (WebDAV)473 #Include /etc/httpd24/extra/httpd-dav.conf474475 # Various default settings476 #Include /etc/httpd24/extra/httpd-default.conf477478 # Configure mod_proxy_html to understand HTML4/XHTML1

10、配置虚拟主机,在httpd2.4.9中虚拟主机需要配置控制才能够访问,默认是拒绝访问的.DocumentRoot:定义虚拟主机网站的根目录(这里的根目录是NFS通过网络共享存储)我们将其挂载到本地.

# vim /etc/httpd24/extra/httpd-vhosts.conf
# The first VirtualHost section is used for all requests that do not# match a ServerName or ServerAlias in any 
block.#
ServerAdmin Admin@aaa.com DocumentRoot "/usr/html/" ServerName www.aaa.com Errorlog "logs/access.log" CustomLog "logs/error.log" combined
Options None Require all granted
ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.251.215:9000/usr/html/$1

这里我们已经将其挂载,使用mount命令可查看之;

#mount -t nfs ServerIP:/ShareDirectory  /LocalDirectory#mount -t nfs 172.16.251.215/usr/html   /usr/html#mount#172.16.251.215:/usr/html on /usr/html type nfs (rw,vers=4,addr=172.16.251.215,clientaddr=172.16.251.208)

另外一台web服务器配置如上

三、安装mysql DB  MysqlDB Server 172.16.251.243

1、准备数据存放的文件系统,新建一个逻 辑卷,并将其挂载至特定目录即可。这里不再给出过程。(实际生产环境下都是如此)这里假设其逻辑卷的挂载目录为/mydata

2、新建用户以安全方式运行进程:

# groupadd -r mysql# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql# chown -R mysql:mysql /mydata/data

3、安装mysql DB-5.5.33

# tar xf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local# cd /usr/local/# ln -sv mysql-5.5.33-linux2.6-i686  mysql# cd mysql
# chown -R mysql:mysql  .# scripts/mysql_install_db --user=mysql --datadir=/mydata/data# chown -R root  .

4、为mysql提供主配置文件:

# cd /usr/local/mysql# cp support-files/my-large.cnf  /etc/my.cnf

并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行,另外还需要添加如下行指定mysql数据文件的存放位置:

#thread_concurrency = 2#datadir = /mydata

5、为mysql提供sysv服务脚本:

# cd /usr/local/mysql# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld# chmod +x /etc/rc.d/init.d/mysqld

6、添加至服务列表:

# chkconfig --add mysqld# chkconfig mysqld on

为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:

7、输出mysql的man手册至man命令的查找路径: 编辑/etc/man.config,添加如下行即可:

MANPATH  /usr/local/mysql/man

8、输出mysql的头文件至系统头文件路径/usr/include:这可以通过简单的创建链接实现:

# ln -sv /usr/local/mysql/include  /usr/include/mysql

9、修改PATH环境变量,让系统可以直接使用mysql的相关命令。

# vim /etc/profile.d/mysql.sh# export PATH=/usr/local/mysql/bin:$PATH# .  /etc/profile.d/mysql.sh# echo $PATH

(PHP-FPM)Application Server IP:172.16.251.215  NFS Server IP:172.16.251.215

四、编译安装php-5.4.26

# yum -y groupinstall "Desktop Platform Development"# yum -y install bzip2-devel libmcrypt-devel

如果想让编译的php支持mcrypt、mhash扩展和libevent

另外,也可以根据需要安装libevent,系统一般会自带libevent,但版本有些低。因此可以编译安装之

说明:libevent是一个异步事件通知库文件,其API提供了在某文件描述上发生某事件时或其超时时执行回调函数的机制,它主要用来替换事件驱动的网络服务器上的event loop机制。目前来说, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。

1、libevent

2
3
4
5
# tar zxvf libevent-1.4.14b-stable.tar.gz
# cd libevent-1.4.14b-stable
# ./configure
# make && make install
# make verify

2、libiconv

# tar zxvf libiconv-1.13.1.tar.gz# cd libiconv-1.13.1# ./configure# make && make install

3、libmcrypt

1
2
3
4
5
6
7
8
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure
# make && make install
# ldconfig -v
# cd libltdl
# ./configure --with-gmetad --enable-gexec
# make && make install

4、mhash

# tar jxvf mhash-0.9.9.9.tar.bz2# cd mhash-0.9.9.9# ./configure# make && make install# ln -sv /usr/local/lib/libmcrypt* /usr/lib/# ln -sv /usr/local/lib/libmhash.* /usr/lib/

5、编译安装php5.4.26;

# tar xf php-5.4.4.tar.bz2# cd php-5.4.4#  ./configure --prefix=/usr/local/php  --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd   --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl# make# make test# make intall

说明:如果前面第1步解决依赖关系时安装mcrypt相关的两个rpm包,此./configure命令还可以带上--with-mcrypt选项以让php支持mycrpt扩展。--with-snmp选项则用于实现php的SNMP扩展,但此功能要求提前安装net-snmp相关软件包

为php提供配置文件:

1
# cp php.ini-production /etc/php.ini

为php-fpm提供Sysv init脚本,并将其添加至服务列表:

1
2
3
4
# cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on

为php-fpm提供配置文件:

1
2
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# vim /usr/local/php/etc/php-fpm.conf

编辑php-fpm的配置文件:

1
# vim /usr/local/php/etc/php-fpm.conf

配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):注释:因为php-fpm默认只监听127.10.0.1:9000上;

1
2
3
4
5
6
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid =
/usr/local/php/var/run/php-fpm
.pid
listen = 0.0.0.0:9000

接下来就可以启动php-fpm了:

1
# service php-fpm start

使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):

1
# ps aux | grep php-fpm

默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验正其是否已经监听在相应的套接字。

# netstat -tnlp | grep php-fpmtcp        0      0  0.0.0.0:9000              0.0.0.0:*                   LISTEN      689/php-fpm

五、NFS服务在Centos或者redhat系列发行版linux中默认已安装过啦!这里我们只要修改配置文件启用之就行啦!

# vim /etc/exports#/usr/html          172.16.0.0/16(rw,no_root_squash)

六 、整合nginx和php5 ,因为在公司内部还有一台Nginx的webSercer

1、编辑/etc/nginx/nginx.conf,启用如下选项:

1
2
3
4
5
6
7
location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  
/scripts
$fastcgi_script_name;
include        fastcgi_params;
}

2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fastcgi_param  GATEWAY_INTERFACE  CGI
/1
.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

并在所支持的主页面格式中添加php格式的主页,类似如下:

1
2
3
4
location / {
root   html;
index  index.php index.html index.htm;
}

而后重新载入nginx的配置文件:

1
# service nginx reload

3、挂在共享存储

#mount -t nfs ServerIP:/ShareDirectory  /LocalDirectory#mount -t nfs 172.16.251.215/usr/html   /usr/html#mount

4、在/usr/html新建index.php的测试页面,测试php是否能正常工作:

1
2
3
4
# cat > /usr/html/index.php << EOF
<?php
phpinfo();

?>

七、安装xcache,为php加速:

1、安装

1
2
3
4
5
# tar xf xcache-2.0.0.tar.gz
# cd xcache-2.0.0
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install

安装结束时,会出现类似如下行:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

2、编辑php.ini,整合php和xcache:

首先将xcache提供的样例配置导入php.ini

1
2
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d

说明:xcache.ini文件在xcache的源码目录中。

接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:

1
zend_extension =
/usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache
.so

注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。

3、重新启动php-fpm

1
# service php-fpm restart

八、搭建论坛

1、安装wordpress#Unzip  wordpress-3.0.5-zh_CN.zip# mv wordpress /www/htdoc2/mybbs# cp wp-config-sample.php wp-config.php#chmod  a+x wp-config.php# vim wp-config.php# mysql -uroot -pzhangxiaocen –hlocalhostmysql> create database wpdb18 define('DB_NAME', 'wpdb');                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             21 define('DB_USER', 'root');                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             24 define('DB_PASSWORD', 'zhangxiaocen');27 define('DB_HOST', 'localhost');                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              30 define('DB_CHARSET', 'utf8');

2、 安装Discuz_X2.5_SC_GBK# unzip Discuz_X2.5_SC_GBK.zip# mv upload    /www/htdoc3/bbs# chown -R daemon:daemon  ./*#mysql>create database discuz;#grant all on discuz.* to discuz@'172.16.251.243' identified by 'zhangxiaocen';

通过网页登上论坛发表帖子,并通过其他web服务器登录,查看帖子!这里就不再演示。负载均衡的话,写上三个不同的网页页面测试之.

9、基于DNS实现负载均衡

# yum -y install bind

# vim /etc/named.rfc1912.zoneszone "nginx.com" IN {        type master;        file "nginx.zone";        allow-update { none; };};

$TTL  664@       IN SOA   DNS.nginx.com.         admin.nginx.com. (                                        0       ; serial                                        1D      ; refresh                                        1H      ; retry                                        1W      ; expire                                        3H )    ; minimum            IN          NS              DNS.nginx.com.DNS         IN          A               172.16.251.198www         IN          A               172.16.251.253www         IN          A               172.16.251.244www         IN          A               172.16.251.208