欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

程序员文章站 2023-11-06 11:04:58
1 实验拓扑 2 需求 RS 01和RS 02对外提供WEB服务。 RS 01搭建LAMP,PHP通过http模块方式提供。 RS 02搭建LAMP,PHP通过fpm方式提供。 RS 01和RS 02之间的关系。 RS 01对外提供NFS服务,作为两个LAMP环境的共享存储,负责存储用户上传的资源, ......

1 实验拓扑

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

2 需求

  • rs-01和rs-02对外提供web服务。
  • rs-01搭建lamp,php通过http模块方式提供。
  • rs-02搭建lamp,php通过fpm方式提供。
  • rs-01和rs-02之间的关系。
    • rs-01对外提供nfs服务,作为两个lamp环境的共享存储,负责存储用户上传的资源,例如:图片、文档等。
    • rs-02对外提供mariadb服务,作为两台lamp环境的共享数据库,负责存储用户的帖子、文字等。
  • 对外提供的域名为www.example.com
    建议现将下面的文章全部浏览完成,再做实验。

3 软件版本

本次使用root用户进行安装,软件全部在每台设备的/root/目录下
apr-1.6.3.tar.bz2
apr-util-1.6.1.tar.bz2
discuz_x3.1_sc_utf8.zip
httpd-2.4.6.tar.bz2
mariadb-5.5.46-linux-x86_64.tar.gz
phpmyadmin-4.0.10.20-all-languages.zip
wordpress-4.7.4-zh_cn.tar.gz
php-5.3.27.tar.gz

4 vs配置

一共部署了3个软件,wordpress、discuz、phpmyadmin;
wordpress和discuz工作在rr模式下也可以正常的访问网页不会出现session超时的现象,可以访问速度非常慢;如果将模式改为sh或者删除一个rs主机,速度就快起来了;
phpmyadmin工作在rr模式下会提示session超时;

sysctl -w net.ipv4.ip_forward=1
yum -y install ipvsadm
ipvsadm -a -t 10.207.51.113:80 -s rr
ipvsadm -a -t 10.207.51.113:80 -r 10.0.0.101:80 -m
ipvsadm -a -t 10.207.51.113:80 -r 10.0.0.102:80 -m

5 rs_01部署nfs

因为nfs是共享存储,rs-01和rs-02都需要使用,所以提前部署。

yum install -y rpcbind nfs-utils &&\
echo '/nfsdata/wordpress    10.0.0.0/24(rw,sync,root_squash,all_squash,anonuid=48,anongid=48)' >/etc/exports &&\
echo '/nfsdata/discuz    10.0.0.0/24(rw,sync,root_squash,all_squash,anonuid=48,anongid=48)' >>/etc/exports &&\
mkdir -p /nfsdata/wordpress &&\
mkdir -p /nfsdata/discuz &&\
systemctl start rpcbind &&\
systemctl start nfs &&\
systemctl enable rpcbind &&\
systemctl enable nfs

[root@rs_01 ~]# showmount -e
export list for rs_01:
/nfsdata/discuz    10.0.0.0/24
/nfsdata/wordpress 10.0.0.0/24

注意:要确保rs-01和rs-02上的apache用户的uid和gid都是48

6 rs_02部署mariadb

因为mariadb是共享数据库,rs-01和rs-02都需要使用,所以提前部署。

tar -xf mariadb-5.5.46-linux-x86_64.tar.gz -c /usr/local/ &&\
cd /usr/local/ &&\
useradd -r -m -s /sbin/nologin mysql &&\
ln -sv mariadb-5.5.46-linux-x86_64 mysql &&\
chown -r mysql.mysql  mysql/ &&\
mkdir /databases &&\
cd mysql/ &&\
\cp support-files/my-huge.cnf /etc/my.cnf &&\
sed -i "/\[mysqld\]/a datadir = /databases\ninnodb_file_per_table = on\nskip_name_resolve = on" /etc/my.cnf &&\
./scripts/mysql_install_db --user=mysql --datadir=/databases --basedir=/usr/local/mysql &&\
touch /var/log/mysqld.log &&\
chown mysql:mysql /var/log/mysqld.log &&\
echo 'path=$path:/usr/local/mysql/bin' >  /etc/profile.d/mariadb.sh &&\
source /etc/profile.d/mariadb.sh &&\
echo 'manpath_map /usr/local/apache2/bin    /usr/local/apache2/man' >>/etc/man_db.conf && \
manpath &&\
ln -sv /usr/local/mysql/include /usr/include/mysql &&\
echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mariadb.conf &&\
ldconfig &&\
\cp support-files/mysql.server /etc/init.d/mysqld &&\
chkconfig --add mysqld &&\
chkconfig --list mysqld &&\
sleep 3 &&\
service mysqld start

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

mysql -h127.0.0.1 -uroot
create database wordpress;
create database discuz;
set password for 'root'@'127.0.0.1' =  password('123123');
set password for 'root'@'localhost' =  password('123123');
create user 'wordpress'@'10.0.0.%' identified by '123123';
create user 'wordpress'@'127.0.0.1' identified by '123123';
grant all on wordpress.* to 'wordpress'@'127.0.0.1';
grant all on wordpress.* to 'wordpress'@'10.0.0.%';
create user 'discuz'@'10.0.0.%' identified by '123123';
create user 'discuz'@'127.0.0.1' identified by '123123';
grant all on discuz.* to 'discuz'@'127.0.0.1';
grant all on discuz.* to 'discuz'@'10.0.0.%';
exit

7 rs_01

7.1 部署http

vim /etc/sysconfig/httpd
# configuration file for the httpd service.

#
# the default processing model (mpm) is the process-based
# 'prefork' model.  a thread-based model, 'worker', is also
# available, but does not work with some modules (such as php).
# the service must be stopped before changing this variable.
#
httpd=/usr/local/apache2/bin/httpd

#
# to pass additional options (for instance, -d definitions) to the
# httpd binary at startup, set options here.
#
#options=

#
# by default, the httpd process is started in the c locale; to
# change the locale in which the server runs, the httpd_lang
# variable can be set.
#
#httpd_lang=c

#
# by default, the httpd process will create the file
# /usr/local/apache2/logs/httpd.pid in which it records its process
# identification number when it starts.  if an alternate location is
# specified in httpd.conf (via the pidfile directive), the new
# location needs to be reported in the pidfile.
#
#pidfile=/usr/local/apache2/logs/httpd.pid


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


vim /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd        startup script for the apache http server
#
# chkconfig: - 85 15
# description: the apache http server is an efficient and extensible  \
#           server implementing the current http standards.
# processname: httpd
# config: /etc/httpd/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /usr/local/apache2/logs/httpd.pid
#
### begin init info
# provides: httpd
# required-start: $local_fs $remote_fs $network $named
# required-stop: $local_fs $remote_fs $network
# should-start: distcache
# short-description: start and stop apache http server
# description: the apache http server is an extensible server
#  implementing the current http standards.
### end init info

# source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# 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/apache2/bin/apachectl
httpd=${httpd-/usr/local/apache2/bin/httpd}
prog=httpd
pidfile=${pidfile-/usr/local/apache2/logs/httpd.pid}
lockfile=${lockfile-/var/lock/subsys/httpd}
retval=0
stop_timeout=${stop_timeout-10}

# the semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  so we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"starting $prog: "
        lang=$httpd_lang daemon --pidfile=${pidfile} $httpd $options
        retval=$?
        echo
        [ $retval = 0 ] && touch ${lockfile}
        return $retval
}

# when stopping httpd, a delay (of default 10 second) is required
# before sigkilling the httpd parent; this gives enough time for the
# httpd parent to sigkill any errant children.
stop() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"stopping $prog: "
        killproc -p ${pidfile} -d ${stop_timeout} $httpd
    else
        echo -n $"stopping $prog: "
        success
    fi
    retval=$?
    echo
    [ $retval = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"reloading $prog: "
    if ! lang=$httpd_lang $httpd $options -t >&/dev/null; then
        retval=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # force lsb behaviour from killproc
        lsb=1 killproc -p ${pidfile} $httpd -hup
        retval=$?
        if [ $retval -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# see how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    retval=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p ${pidfile} $httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    retval=$?
    ;;
  *)
    echo $"usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    retval=2
esac

exit $retval


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


chmod +x /etc/init.d/httpd &&\
yum groupinstall -y "development tools" && \
yum install -y expat-devel openssl-devel pcre-devel && \
tar -xf apr-1.6.3.tar.bz2 && \
tar -xf apr-util-1.6.1.tar.bz2 && \
tar -xf httpd-2.4.6.tar.bz2 && \
cd apr-1.6.3/ && \
./configure -prefix=/usr/local/apr-1.6.3 && \
make && make install && \
cd ../apr-util-1.6.1/ && \
./configure -prefix=/usr/local/apr-unil-1.6.1 --with-apr=/usr/local/apr-1.6.3 && \
make && make install && \
cd ../httpd-2.4.6/ && \
cp -r ../apr-1.6.3 ./srclib/apr && \
cp -r ../apr-util-1.6.1 ./srclib/apr-util && \
./configure  --prefix=/usr/local/apache2 \
--with-apr=/usr/local/apr-1.6.3 \
--with-apr-util=/usr/local/apr-unil-1.6.1 \
--with-included-apr \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-mpms-shared=all \
--enable-mods-shared=all \
--with-mpm=prefork && \
make && \
make install && \
chmod 755 /etc/init.d/httpd && \
chkconfig --add httpd && \
chkconfig --level 3 httpd on && \
echo "export path=$path:/usr/local/apache2/bin" > /etc/profile.d/apache2.sh && \
source /etc/profile.d/apache2.sh && \
sed -i -e "/\/opt\/sbin/a manpath_map /usr/local/apache2/bin    /usr/local/apache2/man" /etc/man_db.conf && \
manpath && \
ln -sv /usr/local/apache2/include /usr/include/httpd && \
echo "/usr/local/apache2/lib" > /etc/ld.so.conf.d/httpd.conf && \
ldconfig && \
sed -i "s#user daemon#user apache#g" /etc/httpd/httpd.conf && \
sed -i "s#group daemon#group apache#g" /etc/httpd/httpd.conf && \
sed -ri "s#^\#(servername www.example.com:80)#\1#g" /etc/httpd/httpd.conf &&\
groupadd -g 48 apache && \
useradd -u 48 -g 48 -m -s /sbin/nologin apache && \
sleep 3 && \
service httpd start

7.2 php

yum install -y zlib-devel libxml2-devel libjpeg-devel  libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel openssl-devel libmcrypt-devel  libtool-ltdl-devel gcc gcc-c++ && \
tar -xf php-5.3.27.tar.gz && \
cd php-5.3.27 && \
useradd -r -m -s /sbin/nologin php && \
./configure --prefix=/usr/local/php \
--sysconfdir=/etc \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/usr/local/libiconv \
--with-libxml-dir=/usr \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-openssl \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--enable-ftp \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d && \
make && \
make install && \
echo 'path=$path:/usr/local/php/bin' >  /etc/profile.d/php.sh && \
source /etc/profile.d/php.sh && \
sed -i -e "/\/opt\/sbin/a manpath_map     /usr/local/php/bin      /usr/local/php/man" /etc/man_db.conf && \
manpath && \
ln -sv /usr/local/php/include  /usr/include/php && \
cp php.ini-production /etc/php.ini && \
sed -i -r 's#^;(date.timezone =)#\1 asia/shanghai#g' /etc/php.ini && \
chown -r php:php /etc/php.ini && \
chown -r php:php /usr/local/php && \
sed -i 's#directoryindex index.html#directoryindex index.html index.php#g' /etc/httpd/httpd.conf && \
sed -i "/<ifmodule mime_module>/a \ \ \ \ addtype application/x-httpd-php .php\n    addtype applicaiton/x-httpd-php-source .phps" /etc/httpd/httpd.conf && \
sed -i "s#/usr/local/apache2/htdocs#/data/www#g" /etc/httpd/httpd.conf &&\
sleep 3 &&\
service httpd restart

7.3 wordpress

mkdir -p /data/www/  &&\
tar -xf wordpress-4.7.4-zh_cn.tar.gz -c /data/www/  &&\
cp -r /data/www/wordpress/wp-content/* /nfsdata/wordpress/  &&\
mv /data/www/wordpress/wp-content /data/www/wordpress/wp-content.bak  &&\
mkdir /data/www/wordpress/wp-content  &&\
chown -r apache: /data/www/wordpress/  &&\
mount --bind /nfsdata/wordpress /data/www/wordpress/wp-content  &&\
cd /data/www/  &&\
cp wordpress/wp-config-sample.php  wordpress/wp-config.php  &&\
sed -i 's#database_name_here#wordpress#g' wordpress/wp-config.php  &&\
sed -i 's#username_here#wordpress#g' wordpress/wp-config.php  &&\
sed -i 's#password_here#123123#g' wordpress/wp-config.php  &&\
sed -i 's#localhost#10.0.0.102#g' wordpress/wp-config.php  &&\
chown -r apache:apache /data/www/wordpress/  &&\
service httpd restart

在浏览器中输入下面的链接安装wordpress
http://10.0.0.101/wordpress/wp-admin/setup-config.php

在本次环境中,使用上面这个链接安装是不对的,要使用最后绑定给vip的域名(www.example.com)来安装wordpress,不要直接使用rs-01的ip来安装,具体原因见下文,此处假定使用了上面的安装方式;
正确安装方式:将www.example.com临时解析为10.0.0.101;然后执行安装http://www.example.com/wordpress/wp-admin/setup-config.php

8 rs-02

8.1 http

vim /etc/sysconfig/httpd
# configuration file for the httpd service.

#
# the default processing model (mpm) is the process-based
# 'prefork' model.  a thread-based model, 'worker', is also
# available, but does not work with some modules (such as php).
# the service must be stopped before changing this variable.
#
httpd=/usr/local/apache2/bin/httpd

#
# to pass additional options (for instance, -d definitions) to the
# httpd binary at startup, set options here.
#
#options=

#
# by default, the httpd process is started in the c locale; to
# change the locale in which the server runs, the httpd_lang
# variable can be set.
#
#httpd_lang=c

#
# by default, the httpd process will create the file
# /usr/local/apache2/logs/httpd.pid in which it records its process
# identification number when it starts.  if an alternate location is
# specified in httpd.conf (via the pidfile directive), the new
# location needs to be reported in the pidfile.
#
#pidfile=/usr/local/apache2/logs/httpd.pid


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


vim /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd        startup script for the apache http server
#
# chkconfig: - 85 15
# description: the apache http server is an efficient and extensible  \
#           server implementing the current http standards.
# processname: httpd
# config: /etc/httpd/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /usr/local/apache2/logs/httpd.pid
#
### begin init info
# provides: httpd
# required-start: $local_fs $remote_fs $network $named
# required-stop: $local_fs $remote_fs $network
# should-start: distcache
# short-description: start and stop apache http server
# description: the apache http server is an extensible server
#  implementing the current http standards.
### end init info

# source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# 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/apache2/bin/apachectl
httpd=${httpd-/usr/local/apache2/bin/httpd}
prog=httpd
pidfile=${pidfile-/usr/local/apache2/logs/httpd.pid}
lockfile=${lockfile-/var/lock/subsys/httpd}
retval=0
stop_timeout=${stop_timeout-10}

# the semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  so we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"starting $prog: "
        lang=$httpd_lang daemon --pidfile=${pidfile} $httpd $options
        retval=$?
        echo
        [ $retval = 0 ] && touch ${lockfile}
        return $retval
}

# when stopping httpd, a delay (of default 10 second) is required
# before sigkilling the httpd parent; this gives enough time for the
# httpd parent to sigkill any errant children.
stop() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"stopping $prog: "
        killproc -p ${pidfile} -d ${stop_timeout} $httpd
    else
        echo -n $"stopping $prog: "
        success
    fi
    retval=$?
    echo
    [ $retval = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"reloading $prog: "
    if ! lang=$httpd_lang $httpd $options -t >&/dev/null; then
        retval=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # force lsb behaviour from killproc
        lsb=1 killproc -p ${pidfile} $httpd -hup
        retval=$?
        if [ $retval -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# see how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    retval=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p ${pidfile} $httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    retval=$?
    ;;
  *)
    echo $"usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    retval=2
esac

exit $retval


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


chmod +x /etc/init.d/httpd &&\
yum groupinstall -y "development tools" && \
yum install -y expat-devel openssl-devel pcre-devel && \
tar -xf apr-1.6.3.tar.bz2 && \
tar -xf apr-util-1.6.1.tar.bz2 && \
tar -xf httpd-2.4.6.tar.bz2 && \
cd apr-1.6.3/ && \
./configure -prefix=/usr/local/apr-1.6.3 && \
make && make install && \
cd ../apr-util-1.6.1/ && \
./configure -prefix=/usr/local/apr-unil-1.6.1 --with-apr=/usr/local/apr-1.6.3 && \
make && make install && \
cd ../httpd-2.4.6/ && \
cp -r ../apr-1.6.3 ./srclib/apr && \
cp -r ../apr-util-1.6.1 ./srclib/apr-util && \
./configure  --prefix=/usr/local/apache2 \
--with-apr=/usr/local/apr-1.6.3 \
--with-apr-util=/usr/local/apr-unil-1.6.1 \
--with-included-apr \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-mpms-shared=all \
--enable-mods-shared=all \
--with-mpm=prefork && \
make && \
make install && \
chmod 755 /etc/init.d/httpd && \
chkconfig --add httpd && \
chkconfig --level 3 httpd on && \
echo "export path=$path:/usr/local/apache2/bin" > /etc/profile.d/apache2.sh && \
source /etc/profile.d/apache2.sh && \
sed -i -e "/\/opt\/sbin/a manpath_map /usr/local/apache2/bin    /usr/local/apache2/man" /etc/man_db.conf && \
manpath && \
ln -sv /usr/local/apache2/include /usr/include/httpd && \
echo "/usr/local/apache2/lib" > /etc/ld.so.conf.d/httpd.conf && \
ldconfig && \
sed -i "s#user daemon#user apache#g" /etc/httpd/httpd.conf && \
sed -i "s#group daemon#group apache#g" /etc/httpd/httpd.conf && \
sed -ri "s#^\#(servername www.example.com:80)#\1#g" /etc/httpd/httpd.conf &&\
groupadd -g 48 apache && \
useradd -u 48 -g 48 -m -s /sbin/nologin apache && \
sleep 3 && \
service httpd start

8.2 php

yum install -y zlib-devel libxml2-devel libjpeg-devel  libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel  libcurl-devel libxslt-devel libxslt-devel openssl-devel libmcrypt-devel  libtool-ltdl-devel gcc gcc-c++ &&\
useradd -r -m -s /sbin/nologin php &&\
tar -xf php-5.3.27.tar.gz &&\
cd php-5.3.27 &&\
./configure \
--prefix=/usr/local/php \
--sysconfdir=/etc/ \
--with-mysql=/usr/local/mysql \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--with-openssl \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=php \
--with-fpm-group=php \
--enable-ftp \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d &&\
make &&\
make install &&\
echo 'path=$path:/usr/local/php/bin' >  /etc/profile.d/php.sh &&\
source /etc/profile.d/php.sh &&\
sed -i "/\/opt\/sbin/a manpath_map     /usr/local/php/bin      /usr/local/php/man" /etc/man_db.conf &&\
manpath &&\
ln -sv /usr/local/php/include /usr/include/php &&\
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm &&\
chmod +x /etc/init.d/php-fpm &&\
chkconfig --add php-fpm &&\
cp php.ini-production /etc/php.ini &&\
sed -i -r 's#^(pdo_mysql.default_socket=)#\1/usr/local/mysql/mysql.sock#g' /etc/php.ini && \
sed -i -r 's#^;(date.timezone =)#\1 asia/shanghai#g' /etc/php.ini &&\
mv /etc/php-fpm.conf.default /etc/php-fpm.conf &&\
chown php:php /etc/php.ini &&\
chown -r php:php /usr/local/php &&\
sed -i "s#/usr/local/apache2/htdocs#/data/www#g" /etc/httpd/httpd.conf &&\
sed -i 's#directoryindex index.html#directoryindex index.html index.php#g' /etc/httpd/httpd.conf &&\
sed -i 's#^\#loadmodule proxy_module modules/mod_proxy.so#loadmodule proxy_module modules/mod_proxy.so#g' /etc/httpd/httpd.conf &&\
sed -i 's#^\#loadmodule proxy_fcgi_module modules/mod_proxy_fcgi.so#loadmodule proxy_fcgi_module modules/mod_proxy_fcgi.so#g' /etc/httpd/httpd.conf &&\
sed -i "/<ifmodule mime_module>/a \ \ \ \ addtype application/x-httpd-php .php\n    addtype applicaiton/x-httpd-php-source .phps" /etc/httpd/httpd.conf &&\
echo -e 'proxyrequests off\nproxypassmatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/www/$1' >> /etc/httpd/httpd.conf &&\
sleep 3 &&\
service php-fpm start
service httpd restart

8.3 wordpress

其实将rs-01的wordpress目录复制过来就行,然后为/data/wordpress/wp-content并挂在共享存储,最后更改一下属主属组为apache,
如果挂在不上需要查看网络原因,可以先安装nfs-utils,然后使用showmount -e 10.0.0.101命令查看可挂载的目录

yum install -y rpcbind &&\
systemctl start rpcbind &&\
systemctl enable rpcbind &&\
mkdir -p /data/www/  &&\
tar -xf wordpress-4.7.4-zh_cn.tar.gz -c /data/www/  &&\
mv /data/www/wordpress/wp-content /data/www/wordpress/wp-content.bak  &&\
mkdir /data/www/wordpress/wp-content  &&\
chown -r apache: /data/www/wordpress/  &&\
mount -t nfs -o bg,timeo=300,soft,retrans=50,rsize=180000,wsize=180000,rw,noexec,sync 10.0.0.101:/nfsdata/wordpress /data/www/wordpress/wp-content/  &&\
cd /data/www/  &&\
cp wordpress/wp-config-sample.php  wordpress/wp-config.php  &&\
sed -i 's#database_name_here#wordpress#g' wordpress/wp-config.php  &&\
sed -i 's#username_here#wordpress#g' wordpress/wp-config.php  &&\
sed -i 's#password_here#123123#g' wordpress/wp-config.php  &&\
sed -i 's#localhost#10.0.0.102#g' wordpress/wp-config.php  &&\
chown -r apache:apache /data/www/wordpress/

9 故障解决

部署完rs-01的lamp环境之后,挂载了共享存储作为wordpress的用户上传文件的目录,并且安装了wordpress,然后测试登录,发布文章,发现没问题。
部署完rs-02的lamp环境之后,挂载了共享存储作为wordpress的用户上传文件的目录,并且安装了wordpress(和rs-01使用的是一个mariadb,rs-02不用再次创建表,支持读取rs-01创建的表),测试登录,发现总是直接跳转到rs-01上(在浏览器里输入10.0.0.102,只要一点击登录或者浏览文章,url就跳转到了10.0.0.101),而且通过抓包发现了下面这种情况,而且通过查看数据库,发现wordpress数据库中很多表的字段里面都写了rs-01的地址。所以无法实现最初的需求;

下面是解决问题过程中,测试先安装rs-02,然后安装rs-01时抓的报文,这里作为示例

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)


最后解决的方法

上一次部署的时候,我是输入http://10.0.0.101/wordpress/wp-admin/setup-config.php进行安装的。

这回我是输入http://www.example.com/wordpress/wp-admin/setup-config.php进行安装的。
此时www.example.com 对应rs-01的地址10.0.0.101。
部署完成后,发布文档,上传文件。没有问题

将www.example.com对应rs-02的地址10.0.0.102。
查看之前发布的文章,查看之前上传的文件。发布新的文章,都没有问题。

最后配置lvs-nat。将www.example.com对应lvs的vip地址,10.207.51.113,调度方式选择成rr(没有选择sh)。
访问www.example.com登录wordpress。发布文章,查看之前的文章,上传的文件。没有问题。
并且session没有问题(这个不清楚为什么,选择的是rr,竟然没有出问题,但是访问速度非常的慢;一旦将模式改为sh或者删除一台主机就访问速度就快了;)

此时收的到的报文
LVS-NAT负载均衡PHP应用(Wordpress、Discuz)


通过查看wordpress创建的表,发现在很多表的字段中,wordpress程序会绑定安装的时候使用的url,之前绑定了成了http://10.0.0.101/wordpress所以会出现跳转的情况

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)


10 phpmyadmin

配置比较简单,参考我之前的文章即可;
之后又部署了phpmyadmin之后,使用lvs-nat模式,调度方式使用rr,发现每点击几次个页面,或者刷新几次,就会出现出现session过期的情况,更换调度方式为sh后解决问题;
因为connection: keep-alive的原因,所以不是刷新一次就切换一台服务器,如果connection: closed,就会变成刷新一次换一个服务器了;

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

11 discuz

配置比较简单,此处略
最后安装了discuz,这个软件就不存在wordpres绑定url的情况;而且这个软件也是在调度模式为rr的时候没有问题,担仍然是很慢,换成sh模式就快了;

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

LVS-NAT负载均衡PHP应用(Wordpress、Discuz)