要求:
    1. wordpress程序通过nfs共享给各个realserver
    2. 后端realserver中的nginx和php分离

网络结构图:

环境说明:
    OS:centos6.7 x64
    主机IP:
        LVS-DR(director): 192.168.2.10
        realsever1 web1 Nginx: 192.168.2.11
        realserver2 web2 Nginx: 192.168.2.12
        PHP server: 192.168.2.13
        MySQL server: 192.168.2.14
    主机名:
        lvs-dr: 192.168.2.10
        realserver1: 192.168.2.11
        realserver2: 192.168.2.12
        php-server: 192.168.2.13
        mysql-server: 192.168.2.14
    selinux: disabled
    内网iptables: off

一、安装nginx、MySQL

1. Nginx 安装:
    需要安装的主机:
        Nginx:192.168.2.11、192.168.2.12
        MySQL:192.168.2.14

192.168.2.11:

(1) 搭建本地yum源:

[root@realserver1 yum.repos.d]# yum install gcc* -y     # 安装gcc开发环境,为了编译安装nginx使用
[root@realserver1 yum.repos.d]# yum install ntpdate wget -y # ntpdate 时间同步,wget 下载nginx
[root@realserver1 yum.repos.d]# ntpdate -s tiger.sina.com.cn # 同步时间
[root@realserver1 src]# wget http://mirrors.sohu.com/nginx/nginx-1.9.9.tar.gz # 下载nginx-1.9.9包
[root@realserver1 src]# groupadd -g www
[root@realserver1 src]# useradd -u -g -s /sbin/nologin www # 创建nginx worker进程工作用户
[root@realserver1 nginx-1.9.]# yum install zlib zlib-devel pcre pcre-devel openssl openssl-devel -y
# pcre 支持正则表达式
# zlib 支持数据压缩
# openssl支持HTTPS
[root@realserver1 src]# tar xf nginx-1.9..tar.gz
[root@realserver1 src]# cd nginx-1.9.
[root@realserver1 nginx-1.9.]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre
# --with-http_ssl_module 启用HTTPS加密
# --with-http_stub_status_module 启用nginx状态监控
# --with-http_gzip_static_module 启用静态压缩
# --with-http_realip_module 做代理时获取客户端真实IP
[root@realserver1 nginx-1.9.]# make && make install [root@realserver1 nginx-1.9.]# vim /etc/init.d/nginx # 创建nginx服务脚本
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: -
# 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
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx start() {
[ -x $nginx ] || exit
[ -f $NGINX_CONF_FILE ] || exit
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq ] && touch $lockfile
return $retval
} stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq ] && rm -f $lockfile
return $retval
killall - nginx
} restart() {
configtest || return $?
stop
sleep
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 >&
} case "$1" in
start)
rh_status_q && exit
$
;;
stop)
rh_status_q || exit
$
;;
restart|configtest)
$
;;
reload)
rh_status_q || exit
$
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit
esac [root@realserver1 conf]# chmod +x /etc/init.d/nginx
[root@realserver1 nginx-1.9.]# cd /usr/local/nginx/conf/
[root@realserver1 conf]# vim nginx.conf
user www www;
[root@realserver1 conf]# vim /etc/profile.d/nginx.sh
[root@realserver1 conf]# source /etc/profile.d/nginx.sh
[root@realserver1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@realserver1 conf]# service nginx start
正在启动 nginx: [确定]
[root@realserver1 conf]# chkconfig --add nginx
[root@realserver1 conf]# chkconfig nginx on
[root@realserver1 conf]# chkconfig --list nginx
nginx :关闭 :关闭 :启用 :启用 :启用 :启用 :关闭 [root@realserver1 conf]# curl -I http://192.168.2.11 # 测试本地访问nginx服务
HTTP/1.1 OK
Server: nginx/1.9.
Date: Sun, Jun :: GMT
Content-Type: text/html
Content-Length:
Last-Modified: Sun, Jun :: GMT
Connection: keep-alive
ETag: "576635c4-264"
Accept-Ranges: bytes

测试客户端访问:

修改nginx配置文件如下:

行号
location / {
root /webapp;
index index.html index.htm;
} location ~ \.php$ {
root /webapp;
fastcgi_pass 192.168.2.13:;
fastcgi_index index.php;
include fastcgi.conf;
}
保存退出
[root@realserver1 conf]# mkdir /webapp
[root@realserver1 conf]# chown -R www:www /webapp/
[root@realserver1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@realserver1 conf]# service nginx reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重新载入 nginx: [确定]

realserver2 nginx配置如上同样进行配置,这里不在贴代码。

(2) MySQL: 192.168.2.14:

[root@mysql-server ~]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
[root@mysql-server ~]# tar xf mysql-5.6.-linux-glibc2.-x86_64.tar.gz -C /usr/local/
[root@mysql-server local]# groupadd -g mysql
[root@mysql-server local]# useradd -u -g -s /sbin/nologin mysql
[root@mysql-server local]# mkdir -pv /mydata/data
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[root@mysql-server local]# chown -R mysql:mysql /mydata/
[root@mysql-server local]# cd mysql
[root@mysql-server mysql]# chown -R root:mysql .
[root@mysql-server mysql]# yum install libaio -y
[root@mysql-server mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
[root@mysql-server mysql]# cp -a support-files/mysql.server /etc/init.d/mysqld
[root@mysql-server mysql]# chmod +x /etc/init.d/mysqld
[root@mysql-server mysql]# mv /etc/my.cnf /etc/my.cnf_old
[root@mysql-server mysql]# cp -a support-files/my-default.cnf /etc/my.cnf
[root@mysql-server mysql]# vim /etc/my.cnf
# 添加如下一条
datadir=/mydata/data
[root@mysql-server mysql]# ln -vs /usr/local/mysql/include /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include'
[root@mysql-server mysql]# vim /etc/ld.so.conf.d/mysql.conf
[root@mysql-server mysql]# ldconfig -v | less
[root@mysql-server mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@mysql-server mysql]# ss -ntl | grep
LISTEN ::: :::*

到此,nignx和mysql安装完毕。

(3) php 安装:

[root@php-server yum.repos.d]# yum install wget gcc* -y
# 添加epel源
[root@php-server ~]# vim /etc/yum.repos.d/epel-centos6.repo
[epel]
name=Extra Packages for Enterprise Linux - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=
gpgcheck=
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 [epel-debuginfo]
name=Extra Packages for Enterprise Linux - $basearch - Debug
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch
failovermethod=priority
enabled=
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck= [epel-source]
name=Extra Packages for Enterprise Linux - $basearch - Source
baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch
failovermethod=priority
enabled=
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=
保存退出 # 安装php源码包的依赖包
[root@php-server yum.repos.d]# yum install libmcrypt libmcrypt-devel mhash mhash-devel install libxml2-devel openssl openssl-devel bzip2-devel libcurl-devel gd -y
# gd-devel-2.0.-.el6.x86_64.rpm 从网上下载到的包安装,直接百度搜包名就有。
[root@php-server ~]# yum localinstall gd-devel-2.0.-.el6.x86_64.rpm -y
[root@php-server ~]# groupadd -g www
[root@php-server ~]# useradd -u -g -s /sbin/nologin www # 创建运行php worker进程用户
[root@php-server ~]# cd /usr/local/src/
[root@php-server src]# wget http://mirrors.sohu.com/php/php-5.6.7.tar.gz
[root@php-server php-5.6.]# cd php-5.6.
[root@php-server php-5.6.]# ./configure --prefix=/usr/local/php --enable-fpm --enable-ftp --enable-zip \
--enable-xml --enable-sockets --enable-bcmath --enable-pcntl --enable-shmop --enable-soap --enable-sysvsem \
--enable-mbstring --enable-mbregex --enable-inline-optimization --enable-maintainer-zts --enable-gd-native-ttf \
--with-fpm-user=www --with-fpm-group=www --with-mysql --with-mysqli --with-pdo-mysql --with-openssl --with-freetype-dir \
--with-iconv-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --with-curl --with-zlib --with-bz2 --with-xmlrpc \
--with-gd --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d
[root@php-server php-5.6.]# make && make install
# 编译时间比较长
[root@php-server php-5.6.]# cp -a sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php-server php-5.6.]# chmod +x /etc/init.d/php-fpm
[root@php-server php-5.6.]# cp -a php.ini-production /usr/local/php/etc/php.ini
[root@php-server php-5.6.]# cd /usr/local/php/etc/
[root@php-server etc]# cp -a php-fpm.conf.default php-fpm.conf
[root@php-server etc]# vim php-fpm.conf # 修改监听地址
listen = 192.168.2.13:
[root@php-server etc]# service php-fpm start
Starting php-fpm done
[root@php-server etc]# ss -ntl | grep
LISTEN 192.168.2.13: *:*
[root@php-server etc]# chkconfig --add php-fpm
[root@php-server etc]# chkconfig php-fpm on
[root@php-server etc]# iptables -F
[root@php-server etc]# iptables -X
[root@php-server etc]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@php-server etc]# iptables -A INPUT -p tcp -m multiport --sport , -m state --state NEW -j ACCEPT
[root@php-server etc]# iptables -P INPUT DROP
[root@php-server etc]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@php-server etc]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]

php 安装完毕。

(3) nginx 连接 php
nginx对于php的配置上面已经写过了:
修改nginx配置文件如下:

行号
location / {
root /webapp;
index index.html index.htm;
} location ~ \.php$ {
root /webapp;
fastcgi_pass 192.168.2.13:;
fastcgi_index index.php;
include fastcgi.conf;
}
保存退出

连接nfs共享web程序
php-sever 192.168.2.13 设置如下:

[root@php-server etc]# yum install nfs-utils -y     # 使用桌面虚拟机可能会碰到nfs需要重启下机器的情况
[root@php-server ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[root@php-server ~]# showmount -e 192.168.2.13
Export list for 192.168.2.13:
/webapp 192.168.2.0/ # 让两台nginx服务器挂载 192.168.2.11、192.168.2.12 [root@realserver1 ~]# mount -t nfs 192.168.2.13:/webapp/ /webapp/
[root@realserver2 ~]# mount -t nfs 192.168.2.13:/webapp/ /webapp/ # 下载wordpress并上传到服务器
[root@php-server ~]# unzip wordpress-4.5.-zh_CN.zip
[root@php-server ~]# cp -a wordpress/* /webapp/
[root@php-server ~]# chown -R www:www /webapp/

在数据库服务器(192.168.2.14)上建立wordpress数据库并赋予权限

[root@mysql-server mysql]# /usr/local/mysql/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6. MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE DATABASE wordpress;
Query OK, row affected (0.02 sec) mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'192.168.2.13' IDENTIFIED BY '';
Query OK, rows affected (0.02 sec) mysql> FLUSH PRIVILEGES;
Query OK, rows affected (0.00 sec)
mysql> Bye

通过页面访问安装wordpress

填写数据库信息

安装完毕。

realserver1 和 realserver2 都指向了一个站点,并能正常访问。

(4) lvs-dr搭建

vip: 192.168.2.200

director配置如下:

[root@lvs-dr ~]# yum install ipvsadm
[root@lvs-dr ~]# ifconfig eth0: 192.168.2.200/ broadcast 192.168.2.200 up
[root@lvs-dr ~]# route add -host 192.168.2.200 dev eth0:
[root@lvs-dr ~]# ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::::4f brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/ brd 192.168.2.255 scope global eth0
inet 192.168.2.200/ brd 192.168.2.200 scope global eth0:
inet6 fe80::20c:29ff:fe39:924f/ scope link
valid_lft forever preferred_lft forever

接下来给realserver 添加vip

net.ipv4.conf.all.arp_ignore =
net.ipv4.conf.all.arp_announce =
net.ipv4.conf.lo.arp_ignore =
net.ipv4.conf.lo.arp_announce =
[root@realserver1 ~]# sysctl -p
[root@realserver1 ~]# ifconfig lo: 192.168.2.200/ broadcast 192.168.2.200 up
[root@realserver1 ~]# route add -host 192.168.2.200 dev lo:

另一台realserver2 一样的操作

在lvs-dr主机添加规则如下:

[root@lvs-dr ~]# ipvsadm -A -t 192.168.2.200: -s rr
[root@lvs-dr ~]# ipvsadm -a -t 192.168.2.200: -r 192.168.2.11 -g -w
[root@lvs-dr ~]# ipvsadm -a -t 192.168.2.200: -r 192.168.2.12 -g -w
[root@lvs-dr ~]# ipvsadm -L -n
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.200: rr
-> 192.168.2.11: Route
-> 192.168.2.12: Route

浏览器访问http://192.168.2.200

访问成功。到此,本次实验完成。

由此引发的问题:
    1. lvs工作在四层,自身不具备后端realserver自动检测的功能,需要对lvs配备后端检测的功能
    2. 此架构不太完善,基于这种物理环境做简单调整会更好,如下图:

【 Linux 】lvs-dr模型实现HA,后端Nginx、PHP、MySQL分离 搭建wordpress站点的更多相关文章

  1. 【转】Nginx+php-fpm+MySQL分离部署详解

    转:http://www.linuxidc.com/Linux/2015-07/120580.htm Nginx+php-fpm+MySQL分离部署详解 [日期:2015-07-26] 来源:Linu ...

  2. nginx与php分离搭建

    nginx与php分离搭建 nginx配置 server { listen       80;      定义端口 server_name  www.a.org;    定义域名 location / ...

  3. LVS DR模型

    1,环境 VMWare10, CentOS6.3 2,LVS DR网络规划 所有机器都只需要一张网卡,给Director的eth0网卡起个别名eth0:1即VIP的值:给RealServer的lo网卡 ...

  4. lvs dr 模型配置详解

    前期准备: 两台服务器 note01(lvs服务器) note02(real sever) 1 首先在note01配置子网卡: ifconfig eth0: :2意思是eth0的子接口,随便一个数字就 ...

  5. nginx+php-fpm+mysql分离部署详解

    相信大家将这三者部署到同一台主机应该已经不陌生了,今天在这里,给大家演示一下如何将三者部署到三台主机上. 实验系统:CentOS 6.6_x86_64 实验前提:大部分软件使用编译安装,请提前准备好编 ...

  6. 烂泥:使用nginx利用虚拟主机搭建WordPress博客

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近开始打算学习nginx web服务器,既然是学习还是以实用为目的的.我们在此以搭建WordPress博客为例. 搭建WordPress博客,我们需要 ...

  7. linux LVS DR模式配置

    拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1.  安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...

  8. Linux lvs DR配置

    三台服务器,(1)做路由.(2)(3)做realserver IP为:192.168.196.121  (1) 192.168.196.122   (2) 192.168.196.123   (3) ...

  9. LVS DR模型RS端修改配置脚本

    #!/bin/bash vip=x.x.x.x in start) > /proc/sys/net/ipv4/conf/all/arp_ignore > /proc/sys/net/ipv ...

随机推荐

  1. 【读书笔记】2_增强学习中的Q-Learning

    本文为Thomas Simonini增强学习系列文章笔记或读后感,原文可以直接跳转到medium系列文章. 主要概念为: Q-Learning,探讨其概念以及用Numpy实现 我们可以将二维游戏想象成 ...

  2. JQuery Ajax执行过程AOP拦截

    JQuery Ajax过程AOP:用于在Ajax请求发送过程中执行必备操作,比如加载数据访问令牌. $.ajaxSetup({ type: "POST", error: funct ...

  3. gitbook生成的_book文件本地打开后链接失效问题

    Gitbook 生成本地 html 的问题 在本地用 gitbook-cli根据 Summary 生成目录 然后在每个 md 文件里书写内容 然后用 gitbook serve .生成本地 html ...

  4. 在easyUI开发中,出现jquery.easyui.min.js函数库问题

    easyUI是jquery的一个插件,是民间的插件.easyUI使用起来很方便,里面有网页制作的最重要的三大方块:javascript代码.html代码和Css样式.我们在导入easyUI库后,可以直 ...

  5. Linux yum安装MySQL5.7,及远程连接mysql(亲测有效!)

    一.安装配置MySQL的yum源 # 安装MySQL的yum源,下面是RHEL6系列的下载地址 rpm -Uvh http://dev.mysql.com/get/mysql-community-re ...

  6. 【Linux】- ls命令详解

    1 命令功能: 列出当前目录下或者指定目录下的所有文件和目录,ls是list的缩写. 2 命令语法: ls [选项] [目录名]     #注:[]中的内容为非必选项 3 命令选项: -a 列出目录下 ...

  7. JavaScript中的String对象详解

    1.属性 String对象最常用的属性是length,用于返回字符串对象的长度. 2.方法 CharAt(index)   返回字符串对象中指定索引号组成的字符串,位置的有效值为0到字符串的长度减1. ...

  8. springMVC笔记二

    第十四章 springmvc快速入门(注解版本) 1)springmvc快速入门(传统版) 步一:创建springmvc-day02这么一个web应用 步二:导入springioc,springweb ...

  9. 如何在数据访问层上提高js的执行效率

    本文讲到的是如何从数据访问层面上提高JS 代码的执行效率.总的来讲有以下几条原则: 函数中读写局部变量总是最快的,而全局变量的读取则是最慢的: 尽可能地少用with 语句,因为它会增加with 语句以 ...

  10. [hdu 1398]简单dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1398 看到网上的题解都是说母函数……为什么我觉得就是一个dp就好了,dp[i][j]表示只用前i种硬币 ...