失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Nginx反向代理 实现Web负载均衡

Nginx反向代理 实现Web负载均衡

时间:2023-04-30 09:28:03

相关推荐

Nginx反向代理 实现Web负载均衡

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

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

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

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

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

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

NFS全称:NetworkfilesystemNFS由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服务脚本

1 #!/bin/sh 2 # 3 # nginx - this script starts and stops the nginx daemon 4 # 5 # chkconfig: - 85 15 6 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 7 #proxy and IMAP/POP3 proxy server 8 # processname: nginx 9 # config:/etc/nginx/nginx.conf 10 # config:/etc/sysconfig/nginx 11 # pidfile:/var/run/nginx.pid 12 13 # Source function library. 14 . /etc/rc.d/init.d/functions 15 16 # Source networking configuration. 17 . /etc/sysconfig/network 18 19 # Check that networking is up. 20 [ "$NETWORKING" = "no" ] && exit 0 21 22 nginx="/usr/sbin/nginx" 23 prog=$(basename $nginx) 24 25 NGINX_CONF_FILE="/etc/nginx/nginx.conf" 26 27 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 28 29 lockfile=/var/lock/subsys/nginx 30 31 make_dirs() { 32 # make required directories 33 user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 34 options=`$nginx -V 2>&1 | grep 'configure arguments:'` 35 for opt in $options; do 36 if [ `echo $opt | grep '.*-temp-path'` ]; then 37 value=`echo $opt | cut -d "=" -f 2` 38 if [ ! -d "$value" ]; then 39# echo "creating" $value 40mkdir -p $value && chown -R $user $value 41 fi 42 fi 43 done 44 } 45 46 start() { 47[ -x $nginx ] || exit 5 48[ -f $NGINX_CONF_FILE ] || exit 6 49make_dirs 50echo -n $"Starting $prog: " 51daemon $nginx -c $NGINX_CONF_FILE 52retval=$? 53echo 54[ $retval -eq 0 ] && touch $lockfile 55return $retval 56 } 57 58 stop() { 59echo -n $"Stopping $prog: " 60killproc $prog -QUIT 61retval=$? 62echo 63[ $retval -eq 0 ] && rm -f $lockfile 64return $retval 65 } 66 67 restart() { 68configtest || return $? 69stop 70sleep 1 71start 72 } 73 74 reload() { 75configtest || return $? 76echo -n $"Reloading $prog: " 77killproc $nginx -HUP 78RETVAL=$? 79echo 80 } 81 82 force_reload() { 83restart 84 } 85 86 configtest() { 87 $nginx -t -c $NGINX_CONF_FILE 88 } 89 90 rh_status() { 91status $prog 92 } 93 94 rh_status_q() { 95rh_status >/dev/null 2>&1 96 } 97 98 case "$1" in 99start)100 rh_status_q && exit 0101 $1102 ;;103stop)104 rh_status_q || exit 0105 $1106 ;;107restart|configtest)108 $1109 ;;110reload)111 rh_status_q || exit 7112 $1113 ;;114force-reload)115 force_reload116 ;;117status)118 rh_status119 ;;120condrestart|try-restart)121 rh_status_q || exit 0122 ;;123*)124 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"125 exit 2126 esac

4、配置Nginx实现反向代理

#使用的用户和组#user nobody;#指定工作衍生进程数(一般等于CPU的总核数或者总和数)worker_processes 1;#指定错误日志存放的路径,错误日志记录级别可选项[debug|info|notice|warn|error|crit]#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#指定pid存放的路径#pid logs/nginx.pid;events {#默认使用的网络I/O模型,Linux系统推荐采用epoll模型,FreeBSD推荐采用kqueue模型#use epoll;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"';#通过$remote_addr:可以获取Nginx反向代理服务器的IP地址#X-Forwarded-For:用以记录原有的客户端ip和原来客户端请求的服务器地址.#$remote_user:用于记录远程客户端用户名称#$time_local:用于记录访问时间与时区#$request:用于记录请求URL与http协议#$status:用于记录请求状态,列如成功状态为200,,页面找不到时状态404#$body_bytes_sent:用于记录发送客户端的文件主体内容的大小;#$http_referer:用于记录是从哪个页面链接访问过来的;#$http_usr_agent:用于记录客户端浏览器的相关信息.#采用日志格式combined记录的日志的格式是非常广泛和流行的access_log path [format [buffer=size | off]]access_log logs/access.log main;sendfile on;#tcp_nopushon;#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 backendserver 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;}#upstream:该指令用于设置可以再proxy_pass和fastcgi_pass指令中使用的代理服务器#weight:设置服务器的权重,权重数值越高,被分配到的客户端请求数越多.默认为1#max_fails:指定的时间内对后端服务器请求失败的次数,如果检测到后端服务器无法连接及发生服务器错误(404错误除外),则标记为失败.默认为1,设为数值0将关闭这项检测#fail_timeout:在经历参数max_fails设置的失败次数后,暂停的时间.#down:标记服务器为永久离线状态,用于ip_hash指令#backup:仅仅非在backup服务器全部繁忙的时候才会启用.server {listen 80;server_name 220.2.2.2;#server_name 绑定域名,以及IP地址location / {proxy_pass http://backend; proxy_set_header host ;#proxy_cache mycache;#proxy_cache_valid 200 301 1d;#proxy_cache_valid 404 1m;}#proxy_pass:指定后端被代理的服务器#proxy_connect_timeout:跟后端服务器连接的超时时间_发起握手等候响应超时时间#proxy_read_timeout:连接成功后_等待后端服务器响应时间_其实已经进入后端的排队之中等候处理。#proxy_send_timeout:后端服务器数据回传时间_就是规定时间内后端服务器必须传完所有的数据#proxy_buffer_size:代理请求缓存_这个缓存区间会保存用户的头部信息以供Nginx进行规则处理_一般只能保存下头信息即可#proxy_buffers:同上 告诉nginx保存单个用的几个buffer最大可用空间#proxy_busy_buffers_size:如果系统很忙的时候可以申请更大的Proxy_buffers#proxy_temp_file_write_size:缓存临时文件的大小;# 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_certificatecert.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服务,编译安装HttpdWebServer:172.16.251.208

WebServer:172.16.251.244

1、编译安装http2.4.9,依赖于更高版本的apr和apr-util。apr全称为apacheportableruntime

#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

1 # tar xf httpd-2.4.9.tar.bz22 # cd httpd-2.4.93 # ./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=event4 # make && make install

4、后续的配置:

1 #ln -sv /usr/local/apache/include/usr/include/httpd2 #vim /etc/profile.d/httpd.sh3 exportPATH=/usr/local/apache/bin:$PATH4 #vim /etc/man.conf5 #MANPATH /usr/local/apache/man

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

PidFile "/var/run/httpd.pid"

6、补充:

(1)构建MPM为静态模块

在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本时,使用参数--with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用./httpd-l来确定选择的MPM。此命令会列出编译到服务器程序中的所有模块,包括MPM。

(2)构建MPM为动态模块

在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。构建MPM为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。

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

1 #!/bin/bash 2 # 3 # httpd Startup script for the Apache HTTP Server 4 # 5 # chkconfig: - 85 15 6 # description: Apache is a World Wide Web server. It is used to serve \ 7 # HTML files and CGI. 8 # processname: httpd 9 # config: /etc/httpd/conf/httpd.conf10 # config: /etc/sysconfig/httpd11 # pidfile: /var/run/httpd.pid12 # Source function library.13 . /etc/rc.d/init.d/functions14 if [ -f /etc/sysconfig/httpd ]; then15 . /etc/sysconfig/httpd16 fi17 # Start httpd in the C locale by default.18 HTTPD_LANG=${HTTPD_LANG-"C"}19 # This will prevent initlog from swallowing up a pass-phrase prompt if20 # mod_ssl needs a pass-phrase from the user.21 INITLOG_ARGS=""22 # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server23 # with the thread-based "worker" MPM; BE WARNED that some modules may not24 # work correctly with a thread-based MPM; notably PHP will refuse to start.25 # Path to the apachectl script, server binary, and short-form for messages.26 apachectl=/usr/local/apache/bin/apachectl27 httpd=${HTTPD-/usr/local/apache/bin/httpd}28 prog=httpd29 pidfile=${PIDFILE-/var/run/httpd.pid}30 lockfile=${LOCKFILE-/var/lock/subsys/httpd}31 RETVAL=032 start() {33 echo -n $"Starting $prog: "34 LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS35 RETVAL=$?36 echo37 [ $RETVAL = 0 ] && touch ${lockfile}38 return $RETVAL39 }40 stop() {41 echo -n $"Stopping $prog: "42 killproc -p ${pidfile} -d 10 $httpd43 RETVAL=$?44 echo45 [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}46 }47 reload() {48echo -n $"Reloading $prog: "49if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then50 RETVAL=$?51 echo $"not reloading due to configuration syntax error"52 failure $"not reloading $httpd due to configuration syntax error"53else54 killproc -p ${pidfile} $httpd -HUP55 RETVAL=$?56fi57echo58 }59 # See how we were called.60 case "$1" in61 start)62 start63 ;;64 stop)65 stop66 ;;67 status)68 status -p ${pidfile} $httpd69 RETVAL=$?70 ;;71 restart)72 stop73 start74 ;;75 condrestart)76 if [ -f ${pidfile} ] ; then77stop78start79 fi80 ;;81 reload)82 reload83 ;;84 graceful|help|configtest|fullstatus)85 $apachectl $@86 RETVAL=$?87 ;;88 *)89 echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"90 exit 191 esac92 exit $RETVAL

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

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

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

1 LoadModule proxy_module modules/mod_proxy.so2 #LoadModule proxy_connect_module modules/mod_proxy_connect.so3 #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so4 #LoadModule proxy_http_module modules/mod_proxy_http.so5 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

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

1 460 # User home directories 2 461 #Include /etc/httpd24/extra/httpd-userdir.conf 3 462 4 463 # Real-time info on requests and configuration 5 464 #Include /etc/httpd24/extra/httpd-info.conf 6 465 7 466 # Virtual hosts 8 467 Include /etc/httpd24/extra/httpd-vhosts.conf 9 46810 469 # Local access to the Apache HTTP Server Manual11 470 #Include /etc/httpd24/extra/httpd-manual.conf12 47113 472 # Distributed authoring and versioning (WebDAV)14 473 #Include /etc/httpd24/extra/httpd-dav.conf15 47416 475 # Various default settings17 476 #Include /etc/httpd24/extra/httpd-default.conf18 477

11、配置虚拟主机,在httpd2.4.9中虚拟主机需要配置控制才能够访问,默认是拒绝访问的.DocumentRoot:定义虚拟主机网站的根目录(这里的根目录是NFS通过网络共享存储)我们将其挂载到本地,以及后端PHP Server的ip地址,Apache现在的角色是反向代理PHP-FPM应用成服务器

# vim /etc/httpd24/extra/httpd-vhosts.conf

1 ​# The first VirtualHost section is used for all requests that do not 2 # match a ServerName or ServerAlias in any <VirtualHost> block. 3 # 4 <VirtualHost *:80> 5ServerAdmin Admin@ 6DocumentRoot "/usr/html/" 7ServerName 8Errorlog "logs/access.log" 9CustomLog "logs/error.log" combined10 <Directory "/usr/html/">11Options None12Require all granted13 </Directory>14ProxyRequests Off15 ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.251.215:9000/usr/html/$116 </VirtualHost>

这里我们已经将其挂载之(NFS共享存储的文件系统),使用mount命令可查看之;

1 #mount -t nfs ServerIP:/ShareDirectory /LocalDirectory2 #mount -t nfs 172.16.251.215/usr/html /usr/html3 #mount4 #172.16.251.215:/usr/html on /usr/html type nfs (rw,vers=4,addr=172.16.251.215

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

三、安装mysqlDBMysqlDBServer172.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、安装mysqlDB-5.5.33

1 # tar xf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local2 # cd /usr/local/3 # ln -sv mysql-5.5.33-linux2.6-i686 mysql4 # 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-f /etc/f

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

1 #thread_concurrency = 22 #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,添加如下行即可:

1 MANPATH /usr/local/mysql/man

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

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

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

1 # vim /etc/profile.d/mysql.sh2 # export PATH=/usr/local/mysql/bin:$PATH3 # . /etc/profile.d/mysql.sh4 # echo $PATH

(PHP-FPM)ApplicationServerIP:172.16.251.215NFSServerIP:172.16.251.215

四、编译安装php-5.4.26

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

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

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

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

1、libevent

1 # tar zxvf libevent-1.4.14b-stable.tar.gz2 # cd libevent-1.4.14b-stable3 # ./configure4 # make && make install5 # make verify

2、libiconv

1 # tar zxvf libiconv-1.13.1.tar.gz2 # cd libiconv-1.13.13 # ./configure4 # make && make install

3、libmcrypt

1 # tar zxvf libmcrypt-2.5.8.tar.gz2 # cd libmcrypt-2.5.83 # ./configure4 # make && make install5 # ldconfig -v6 # cd libltdl7 # ./configure --with-gmetad --enable-gexec8 # make && make install

4、mhash

1 # tar jxvf mhash-0.9.9.9.tar.bz22 # cd mhash-0.9.9.93 # ./configure4 # make && make install5 # ln -sv /usr/local/lib/libmcrypt* /usr/lib/6 # 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提供Sysvinit脚本,并将其添加至服务列表:

1 # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm2 # chmod +x /etc/rc.d/init.d/php-fpm3 # chkconfig --add php-fpm4 # chkconfig php-fpm on

编辑php-fpm的配置文件:

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

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

1 pm.max_children = 1502 pm.start_servers = 83 pm.min_spare_servers = 54 pm.max_spare_servers = 105 pid = /usr/local/php/var/run/php-fpm.pid6 listen = 0.0.0.0:9000

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

# service php-fpm start

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

1 # ps aux | grep php-fpm

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

1 # netstat -tnlp | grep php-fpm2 tcp 00 0.0.0.0:9000 0.0.0.0:* LISTEN689/php-fpm

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

1 # vim /etc/exports2 #/usr/html172.16.0.0/16(rw,no_root_squash)

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

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

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

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

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

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

1 # service nginx reload

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

1 # cat > /usr/html/index.php << EOF2 <?php3 phpinfo();4 ?>

七、安装xcache,为php加速:

1、安装

1 # tar xf xcache-2.0.0.tar.gz2 # cd xcache-2.0.03 # /usr/local/php/bin/phpize4 # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config5 # make && make install

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

Installingsharedextensions:/usr/local/php/lib/php/extensions/no-debug-zts-0525/

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

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

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

注意:如果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实现负载均衡

1 # yum -y install bind

$TTL 664@ IN SOA . . (0 ; serial1D; refresh1H; retry1W; expire3H ) ; minimumINNS .DNS INA172.16.251.198www INA172.16.251.253www INA172.16.251.244www INA172.16.251.208

如果觉得《Nginx反向代理 实现Web负载均衡》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。