LNMP基础架构搭建(详细)

LNMP准备环境:

nginx1:192.168.1.8
nginx: 192.168.1.9
mysql: 192.168.1.11
php:192.168.1.12
lb1:192.168.1.13
lb2:192.168.1.17
nfs:192.168.1.18

1.nginx1服务器配置

  1. 关闭防火墙:
    systemctl stop firewalld (关闭防火墙)
    systemctl disable firewalld (开机自动关闭防火墙)

  2. 关闭selilnux
    setenforce 0

  3. 配置nginx官方源:
    vim /etc/yum.repo.d/nginx.repo
    #添加
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    保存退出

  4. yum安装nginx
    yum -y install nginx

  5. 启动nginx
    systemctl start nginx

  6. 开机自动启动nginx
    systemctl enable nginx

2.nginx2服务器配置

  1. 关闭防火墙:
    systemctl stop firewalld (关闭防火墙)
    systemctl disable firewalld (开机自动关闭防火墙)

  2. 关闭selilnux
    setenforce 0

  3. 配置nginx官方源:
    vim /etc/yum.repo.d/nginx.repo
    #添加
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    保存退出

  4. yum安装nginx
    yum -y install nginx

  5. 启动nginx
    systemctl start nginx

  6. 开机自动启动nginx
    systemctl enable nginx

3.mysql的配置

  1. 下载mysql包到mysql5.6.rpm文件夹
    mysql包下载

  2. 进入到mysql5.6.rpm文件夹,安装mysql
    cd mysql5.6.rpm/
    yum -y localinstall *.rpm

  3. 启动mysql
    systemctl start mysqld

  4. 开机自动启动mysql
    systemctl enable mysqld

4.PHP配置:

  1. 下载PHP包到php文件夹中
    php包下载.
  2. 进入到php文件夹中,安装php
    cd php/
    yum -y localinstall *.rpm
  3. 启动php
    systemctl start php-fpm
  4. 开机自动启动php
    systemctl enable php-fpm

5.nginx1中安装wordpress

  1. 下载wordpress源码包
    wordpress源码包
  2. 复制wordpress安装包,到/下,解压,并赋权
    unzip wordpress-4.9.4-zh_CN.zip
    chmod -R 777 /wordpress
  3. 创建虚拟主机配置文件
vim /etc/nginx/conf.d/blog.conf
 #添加
   server {
        listen 80;
        server_name blog.benet.com;
        root /wordpress;
        index index.php index.html;

        location ~ \.php$ {
                root /wordpress;
                fastcgi_pass 192.168.1.12:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
	}
  1. 重启nginx
    systemctl restart nginx
  2. 在mysql服务器上创建blog数据库和用户
    登录数据库:mysql -u root
    创建数据库:create database blog;
    创建远程管理用户: grant all on blog.* to lisi@"%" identified by ‘123.com’;
    注意:这里的引号是英文下的引号,因此要修改一下
  3. 在nginx1复制wordpress目录到php的根目录
    scp -rp /wordpress root@192.168.1.12:/
  4. 修改php服务器的配置文件
    vim /etc/php-fpm.d/www.conf
    定位20行和48行并修改为:
    listen = 192.168.1.12:9000
    listen.allowed_clients = 192.168.1.8,192.168.1.9
    保存退出
  5. 重启php:
    systemctl restart php-fpm

6.nginx1下安装zh

  1. 下载wecenter源码包
    Wecenter源码包
  2. 复制wecenter安装包,到虚拟机/zh目录下,赋权
    mkdir /zh
    cd /zh
    unzip Wecenter_3-3-4.zip
    chmod -R 777 /zh
  3. 创建虚拟主机配置文件
vim /etc/nginx/conf.d/zh.conf
	#添加:
	server {
        listen 80;
        server_name zh.benet.com;
        root /zh;
        index index.php index.html;

        location ~ \.php$ {
                root /zh;
                fastcgi_pass 192.168.1.12:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
	}
  1. . 重启nginx
    systemctl restart nginx
  2. 在mysql服务器上创建zh数据库和管理用户
    登录数据库:mysql -u root
    创建数据库:create database zh;
    设置管理用户及密码:grant all on zh.* to wangwu@’%’ identified by ‘123.com’;
  3. 在nginx1复制wordpress目录到php的根目录
    scp -rp /zh root@192.168.1.12:/zh
  4. 在客户端上配置域名解析
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.9 blog.benet.com zh.benet.com

  1. 客户端通过域名访问zh,安装并配置
    blog.benet.com
    zh.benet.com __blog.benet.com__
    zh,benet.com
  2. 配置需要注意的:
    blog.benet.com
    数据库名称:blog;
    用户名:lisi
    密码:123.com
    数据库主机:192.168.1.11
    zh.benet
    数据库主机:192.168.1.11
    数据库账号:wangwu
    数据库密码:123.com
    数据库名称:zh

7. nginx2服务器配置:

  1. 复制nginx1下的/etc/nginx/到nginx2中/etc/nginx下
    scp -rp root@192.168.1.8:/etc/nginx/
    /etc/nginx/

  2. 复制nginx1下的/wordpress/到nginx2/wordpress下
    scp -rp root@192.168.1.8:/wordpress/
    /wordpress

  3. 复制nginx1下的/zh/到nginx2/zh下
    scp -rp root@192.168.1.8:/zh/
    /zh

  4. 重启nginx
    systemctl restart nginx
    ####### 6.配置负载均衡服务器lb1:

  5. 关闭防火墙和selinux
    systemctl stop firewalld
    systemctl disable firewalld
    setenforce 0

  6. 添加nginx官方源

vim /etc/yum.repos.d/nginx.repo
#添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
  1. 安装naginx
    yum -y install nginx
  2. 启动nginx,并开机自启
    systemctl start nginx
    systemctl enable nginx
    5.优化nginx
vim /etc/nginx/nginx_params
#添加:
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
  1. 创建lb配置文件:
vim /etc/nginx/conf.d/lb1.conf
#添加:
	upstream web_cluster {
        	server 192.168.1.8:80;
        	server 192.168.1.9:80;
	}

	server {
        listen 80;
        server_name blog.benet.com;

        location / {
                proxy_pass http://web_cluster;
                include nginx_params;
        }
	}
	server {
        listen 80;
        server_name zh.benet.com;

        location / {
                proxy_pass http://web_cluster;
                include nginx_params;
     	   }
	}
  1. 重启nginx:
    systemctl restart nginx

8.lb2负载均衡服务器配置:

关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

  1. 添加nginx官方源
vim /etc/yum.repos.d/nginx.repo
#添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
  1. 安装naginx
    yum -y install nginx
  2. 启动nginx,并开机自启
    systemctl start nginx
    systemctl enable nginx
  3. 复制b1服务器/etc/nginx/复制到lb2中
    __scp -rp root@192.168.1.13:/etc/nginx/
    /etc/nginx/__
  4. 修改/etc/nginx/conf.d/lb1.conf修改为lb2.conf
    __mv /etc/nginx/conf.d/lb1.conf /etc/nginx/conf.d/lb2.conf __
  5. 修改客户端的域名解析配置:
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.13 blog.benet.com zh.benet.com

  1. 再次访问blog.benet.com, zh.benet.com

9.配置lb1负载均衡服务器高可用

  1. 安装keepalived
    yum -y install keepalived
  2. 修改keepalived的配置文件
vim /etc/keepalived/keepalived.conf
修改为:
global_defs {
  router_id lb1
}

vrrp_instance VI_1 {
   state MASTER
   interface ens33
   virtual_router_id 51
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       192.168.1.254
   }
}
  1. 启动keepalived
    systemctl start keepalived

10.配置lb2负载均衡服务器高可用

    1. 安装keepalived
      yum -y install keepalived
  1. 修改keepalived的配置文件
vim /etc/keepalived/keepalived.conf
修改为:
global_defs {
  router_id lb2
}

vrrp_instance VI_1 {
   state BACKUP
   interface ens33
   virtual_router_id 51
   priority 50
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       192.168.1.254
   }
}
  1. 启动keepalived
    systemctl start keepalived
  2. 在lb1上查看漂移地址
    ip a show dev ens33
  3. 修改客户端的域名解析配置:
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.254 blog.benet.com zh.benet.com

11.lb1配置解决nginx故障造成群集无法工作

  1. 编辑nginx监控脚本
vim /sh/check_nginx_proxy.sh
#!/bin/bash
killall  -0  nginx
if  [ $? -ne 0 ];then
  systemctl stop keepalived
fi
  1. 添加脚本追踪模块到keepalived配置文件
vim /etc/keepalived/keepalived.conf
global_defs {
   router_id lb1
}
#增加以下字段
vrrp_script check_nginx_proxy {
        script "/sh/check_nginx_proxy.sh"
        interval 2
        weight 5
        }      
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.254
    }
    #增加以下字段
    track_script {
        check_nginx_proxy
    }
}
  1. 重启keepalived
    systemctl restart keepalived
  2. lb2配置:
vim /etc/keepalived/keepalived.conf
global_defs {
   router_id lb2
}
#增加以下字段
vrrp_script check_nginx_proxy {
        script "/sh/check_nginx_proxy.sh"
        interval 2
        weight 5
        }      
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.254
    }
    #增加以下字段:
    track_script {
        check_nginx_proxy
    }
}
  1. 重启keepalived
    systmctl restart keepalived

13.配置nfs共享

  1. 安装nfs-utils、rpcbind:
    yum -y install nfs-utils rpcbind
  2. 创建挂载点
    mkdir -p /nfs/{blog,zh}
  3. 发布共享目录
vim /etc/exports
	添加:
	/nfs/blog       192.168.1.0/24(rw,sync,no_root_squash)
	/nfs/zh         192.168.1.0/24(rw,sync,no_root_squash)
  1. 启动nfs服务
    systemctl start rpcbind
    systemctl start nfs
  2. 在nginx1服务器上查看nfs共享目录
    showmount -e 192.168.1.18
  3. 把wordpress的内容目录挂载到nfs
    cd /wordpress
    cp -rp wp-content/ wp-contentbak
    mount -t nfs 192.168.1.116:/nfs/blog wp-content
    cp -rp wp-contentbak/* wp-content/
  4. 设置永久挂载
vim /etc/fstab
	#末尾添加:
	192.168.1.18:/nfs/log  /wordpress/wp-content   nfs     defaults        0 0

14.验证:

  1. 在nfs上验证
    cd /nfs/blog/ 进入到nfs/blog下
    ls 查看
  2. 关闭lb1上的keepalived看是否可以访问到论坛
    10.关闭lb1上的nginx看是否可以访问到论坛:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值