lnmp拆分部署集群项目案例(博客项目与PHPmyadmin部署)(全)

一、博客搭建---过程详解
  • 1、首先在web01实现博客的基本搭建;
  • 2、搭建mysyql数据库
  • 3、扩展多台web02与web03------->搭建web集群
  • 4、实现静态资源共享nfs

1、首先在web01实现博客的基本搭建;`

  • 环境准备:保证虚拟机可以正常上网(空白的虚拟机)
  • 10.0.0.7(安装nginx,php,mariadb) 10.0.0.8(nginx,php) 10.0.0.9(nginx,php,)
  • 10.0.0.31(nfs) 10.0.0.51(mariadb)
​```准备环境:保证虚拟机可以正常上网(空白的虚拟机)
环境准备:配置php源和nginx源
[root@web01 ~]# cat /etc/yum.repos.d/php.repo 
[webtatic-php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
 
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
[root@web01 ~]# yum install mariadb mariadb-server -y
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# yum -y install nginx php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
[root@web01 ~]# yum install mariadb mariadb-server -y
......等待安装成功。。。。。
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl start php-fpm.service 
[root@web01 ~]# systemctl start mariadb

​```数据库配置
[root@web01 ~]# mysqladmin password oldxu.com
[root@web01 ~]# mysql -uroot -poldxu.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> Ctrl-C -- exit!
Aborted

​```编写配置文件
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 ~]# gzip default.conf 
[root@web01 conf.d]# ls
default.conf.gz
[root@web01 conf.d]# vim blog.oldxu.com.conf
server {
        listen 80;
        server_name blog.oldxu.com;
        root /code/wordpress;
        client_max_body_size 100m;
        location / {
        index index.php;
}
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
}
}
     
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# systemctl restart mariadb.service 
​```根据配置文件初始化环境
[root@web01 conf.d]# mkdir /code
[root@web01 conf.d]# cd /code/
[root@web01 code]# rz      #在浏览器搜索wordpress去官网下载至桌面,然后rz上传
[root@web01 code]# ls 
wordpress-5.2.3-zh_CN.tar.gz
[root@web01 code]# tar xf wordpress-5.2.3-zh_CN.tar.gz 
[root@web01 code]# ls
wordpress  wordpress-5.2.3-zh_CN.tar.gz
[root@web01 code]# mysql -uroot -poldxu.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.01 sec)

MariaDB [(none)]> exit
Bye
​```统一Nginx、php的权限为www
[root@web01 code]# groupadd www -g 666
[root@web01 code]# useradd -u666 -g666 www
[root@web01 code]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf 
[root@web01 code]# chown -R www.www /code/
[root@web01 code]# systemctl restart nginx
[root@web01 code]# sed -i '/^group/c user=www;' /etc/php-fpm.d/www.conf 
[root@web01 code]# systemctl restart php-fpm

host劫持:windows进入\c\windows\system32\drivers\etc\hosts
10.0.0.7 php.oldxu.com

进入浏览器访问blog.oldxu.com即可(推荐谷歌无痕模式)

2、然后将搭建好的mysyql进行独立拆分
1.备份172.16.1.7上的数据库信息
[root@web01 ~]# mysqldump -uroot -p'oldxu.com' --all-databases > mysql-all.sql
2.将172.16.1.7 上的数据推送至172.16.1.51
[root@web01 ~]# scp mysql-all.sql root@172.16.1.51:/tmp
3.登录172.16.1.51 恢复数据
[root@mysql ~]#  yum install mariadb mariadb-server -y
[root@mysql ~]# systemctl enable mariadb
[root@mysql ~]# systemctl start mariadb
读取sql文件至数据库中
[root@mysql ~]# mysql -uroot < /tmp/mysql-all.sql
[root@mysql ~]# systemctl restart mariadb

配置一个远程用户,允许其他服务器(10.0.0.7、10.0.0.8、10.0.0.9)能通过远程的方式连接
[root@mysql ~]# mysql -uroot -poldxu.com
MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by 'oldxu.com';
MariaDB [(none)]> flush privileges;
4.将 172.16.1.7 程序连接本地的数据库,修改为远程的数据库    ( 应用割接 )
[root@web01 ~]# systemctl disable mariadb
[root@web01 ~]# systemctl stop mariadb
5、修改数据名称
[root@web01 code]# cd /code/wordpress/
[root@web01 wordpress]# find ./ -type f  | xargs grep "oldxu.com"
	./wp-config.php:define( 'DB_PASSWORD', 'oldxu.com' );

/** MySQL数据库用户名 */
define( 'DB_USER', 'all' );

/** MySQL数据库密码 */
define( 'DB_PASSWORD', 'oldxu.com' );

/** MySQL主机 */
define( 'DB_HOST', '172.16.1.51' );

3、扩展多台web02、web03------->搭建web集群

​```确保web02、web03 上都安装了 Nginx与php

1.创建用户和用户组10.0.0.8与10.0.0.9
[root@web02 ~]# groupadd -g 666 www
[root@web02 ~]# useradd -u666 -g666 www
2.切到172.16.1.7 上执行如下操作
[root@web01 ~]# rsync -avz --delete  /etc/nginx root@172.16.1.8:/etc/
[root@web01 ~]# rsync -avz --delete  /etc/nginx root@172.16.1.9:/etc/
[root@web01 ~]# rsync -avz --delete  /etc/php.ini root@172.16.1.8:/etc/
[root@web01 ~]# rsync -avz --delete  /etc/php.ini root@172.16.1.9:/etc/
[root@web01 ~]# rsync -avz --delete  /etc/php-fpm.d root@172.16.1.8:/etc/
[root@web01 ~]# rsync -avz --delete  /etc/php-fpm.d root@172.16.1.9:/etc/
		
	3.打包代码
	[root@web01 ~]# tar czf code.tar.gz /code
		
	4.拷贝代码
	[root@web01 ~]# scp code.tar.gz root@172.16.1.8:/tmp
	[root@web01 ~]# scp code.tar.gz root@172.16.1.9:/tmp

3.回到172.16.1.8与172.16.1.9 然后解包  授权  重启服务,并加入开机自启
	[root@web02 ~]# tar xf /tmp/code.tar.gz -C /
	[root@web02 ~]# systemctl restart nginx php-fpm
	[root@web02 ~]# systemctl enable nginx php-fpm
	[root@web02 ~]# chown -R www.www /code/
4、实现静态资源共享nfs
1.准备172.16.1.31 nfs存储服务器
	(1) 安装
	[root@nfs ~]# yum install nfs-utils -y
	(2) 配置
	[root@nfs ~]# cat /etc/exports
	/data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
	3) 初始化环境
	[root@nfs ~]# mkdir -p /data/blog
	[root@nfs ~]# groupadd -g 666 www
	[root@nfs ~]# useradd -u666 -g666 www
	[root@nfs ~]# chown -R www.www /data/
	4) 启动
	[root@nfs ~]# systemctl enable nfs
	[root@nfs ~]# systemctl restart nfs

3.找到web存储的图片所在的路径 http://blog.oldxu.com/wp-content/uploads/2019/09/tt.jpeg
[root@web01 wp-content]# mv uploads/ uploads_bak
[root@web01 wp-content]# scp -rp uploads_bak/* root@172.16.1.31:/data/blog/
[root@web01 wp-content]# mkdir uploads

4.在 172.16.1.7 172.16.1.8 172.16.1.9 应用服务器上进行挂载
[root@web01 wp-content]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads
5、挂载完之后重启nginx
[root@web03 conf.d]# systemctl restart nginx
6、重启nfs
[root@nfs blog]# systemctl restart nfs
7、重启php
[root@web02 ~]# systemctl restart php-fpm
8、权限问题
[root@nfs blog]# chown -R www.www /data/

​```访问网站 测试------>成功

二、搭建phpmyadmin


基本思路:

  • 1、在web01和web02上面都安装phpmyadmin
  • 2、接入负载均衡proxy_域名、upstream
  • 3、实现流量的均摊,会话的问题得以保持--->redis
  • 4、测试访问---->搭建成功
  • 环境准备:保证虚拟机可以正常上网(空白的虚拟机)
  • 10.0.0.7(安装nginx,php,mariadb) 10.0.0.8(nginx,php) 10.0.0.9(nginx,php,)
  • 10.0.0.31(nfs) 10.0.0.51(mariadb、redis) 10.0.0.5(nginx)
1、在 172.16.1.8  和  172.16.1.7 安装 phpmyadmin     
		分别进行测试-->测试登录

	#1.安装phpmyadmin(web01和web02上都装)
	[root@web01 conf.d]# cd /code
	[root@web01 code]# wget https://files.phpmyadmin.net/phpMyAdmin/4.8.4/phpMyAdmin-4.8.4-all-languages.zip
	[root@web01 code]# unzip phpMyAdmin-4.8.4-all-languages.zip
	[root@web01 code]# ln -s /code/phpMyAdmin-4.9.0.1-all-languages /code/phpmyadmin
	
	
	#2.配置phpmyadmin连接远程的数据库
	[root@web01 code]# cd phpMyAdmin-4.8.4-all-languages/
	[root@web01 phpMyAdmin-4.8.4-all-languages]# cp config.sample.inc.php config.inc.php
	[root@web01 phpMyAdmin-4.8.4-all-languages]# vim config.inc.php
	/* Server parameters */
	$cfg['Servers'][$i]['host'] = '172.16.1.51';
	
	[root@web01 code]# vim /etc/nginx/conf.d/php.oldxu.com.conf
server {
        listen 80;
        server_name php.oldxu.com;
        root /code/phpmyadmin;

        location / {
                index index.php;
}
        location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}
}
	[root@web01 code]# nginx -t
	[root@web01 code]# systemctl restart nginx
	[root@web01 code]#chown -R www.www /var/lib/php/
	##host劫持测试
[root@web01 code]#scp /etc/nginx/conf.d/php.oldxu.com.conf root@10.0.0.8:/etc/nginx/conf.d
	[root@web01 code]#scp -rp /code/phpMyAdmin-4.9.0.1-all-languages root@172.16.1.8:/code/
	[root@web01 code]# systemctl start php-fpm.service
	[root@web01 code]# systemctl enable php-fpm.service
	然后在10.0.0.8上面创立软连接并给权限
    [root@web02 code]# ln -s /code/phpMyAdmin-4.9.0.1-all-languages/ /code/phpmyadmin
    [root@web02 code]# chown -R www.www /var/lib/php/
	[root@web02 code]# systemctl restart nginx
	[root@web02 code]# systemctl start php-fpm.service
	[root@web02 code]# systemctl enable php-fpm.service
	
	##host劫持测试10.0.0.8
--------------------------------------------------------------------------
2.接入负载均衡  ---> 代理至后端2台主机
#准备10.0.0.5的机器(配置nginx源,下载nginx)
1.安装官方仓库源
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
2.使用yum直接安装
[root@web01 ~]# yum install nginx -y
[root@lb01 conf.d]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# gzip default.conf 
[root@lb01 conf.d]# cat proxy_php.oldxu.com.conf 
upstream  php {
	server 172.16.1.7;
	server 172.16.1.8;
}

server {
	listen 80;
	server_name php.oldxu.com;
	location / {
		proxy_pass http://php;
		proxy_set_header Host $http_host;
	}
}

--------------------------------------------------------------------------
3.发现无法正常登陆
	1.解决方法:  (加入ip_hash;会话保持)
		在负载均衡上配置  ip_hash 会话保持   (  造成用户仅访问后端的某一台主机  )
[root@lb01 conf.d]# cat proxy_php.oldxu.com.conf 
upstream  php {
	ip_hash;
	server 172.16.1.7;
	server 172.16.1.8;
}

server {
	listen 80;
	server_name php.oldxu.com;
	location / {
		proxy_pass http://php;
		proxy_set_header Host $http_host;
	}
}

--------------------------------------------------------------------------
4.既希望能够实现流量的均摊,又希望会话的问题得以保持, 所以引入了redis(10.0.0.51)

1)安装redis
	[root@db01 ~]# yum install redis -y
2)配置redis
	[root@db01 ~]# sed -i '/^bind/c bind 127.0.0.1 172.16.1.51' /etc/redis.conf
3)启动redis
	[root@db01 ~]# systemctl enable redis
	[root@db01 ~]# systemctl start redis


4) 改造php, session写本地修改为写入redis中  (所有的web上都需要配置)
	前提:  已经安装过了redis的模块---> php71w-pecl-redis

	1.修改php存储session至redis中(10.0.0.7)
	[root@db01 ~]# vim /etc/php.ini
	session.save_handler = redis
	session.save_path = "tcp://172.16.1.51:6379?weight=1"
	2.修改php-fpm 注释默认存储session的位置
	[root@web01 ~]# vim /etc/php-fpm.d/www.conf
	;php_value[session.save_handler] = files
	;php_value[session.save_path]    = /var/lib/php/session

	3.将修改后的配置文件,推送至172.16.1.8
	[root@web01 ~]# scp /etc/php.ini root@172.16.1.8:/etc/  
	[root@web01 ~]# scp /etc/php-fpm.d/www.conf  root@172.16.1.8:/etc/php-fpm.d/www.conf
	
	4.重启172.16.1.7 172.16.1.8两台服务器的php-fpm
	[root@web02 conf.d]# systemctl restart php-fpm
	5、取消ip_hash
    [root@lb01 conf.d]# systemctl restart nginx 
    [root@lb01 conf.d]# vim proxy_php.oldxu.com.conf 
    [root@lb01 conf.d]# systemctl restart nginx 

5) 测试效果 ----->注释10.0.0.7--->打开10.0.0.5
	1.浏览器登录测试  (ok)
	
	2.查看redis的sessionID和    浏览器cookie中提交的sessionID是否一致
	[root@db01 ~]# redis-cli 
	127.0.0.1:6379> keys *
	1) "PHPREDIS_SESSION:38ecc8696c70a7252d943e7cb9b20f70"
	
搭建成功  ---->完成phpadmin的搭建


nfs
1.安装NFS  (PHPadmin不能直观体现,简单叙述)
[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -u666 -g666 www
[root@nfs ~]# yum install nfs-utils -y
[root@nfs ~]# cat /etc/exports
/data/zrlog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs ~]# mkdir /data/zrlog -p
[root@nfs ~]# systemctl restart nfs

2.找到图片资源   推送图片资源至NFS
[root@web01 ~]# scp -rp /code/zrlog/ROOT/attached/* root@172.16.1.31:/data/zrlog/
[root@nfs ~]# chown -R www.www /data/zrlog/			#重新授权

3.多节点挂载 (7和8均挂载)  
# mount -t nfs 172.16.1.31:/data/zrlog/ /code/zrlog/ROOT/attached/

负载均衡https的配置
[root@lb01 conf.d]# cat proxy_php.oldxu.com.conf 
upstream  php {
	server 172.16.1.7;
	server 172.16.1.8;
}

server {
	listen 443 ssl;
	ssl_certificate ssl_key/server.crt;
	ssl_certificate_key ssl_key/server.key;
	server_name php.oldxu.com;
	location / {
		proxy_pass http://php;
		proxy_set_header Host $http_host;
	}
}

server {
	listen 80;
	server_name php.oldxu.com;
	return 302 https://$http_host$request_uri;
}


高可用:


[root@lb01 ~]# scp -rp /etc/nginx/* root@172.16.1.6:/etc/nginx/
[root@lb01 ~]# scp -rp /etc/nginx/conf.d/* root@172.16.1.6:/etc/nginx/conf.d/
[root@lb2 conf.d]# rm -rf default.conf


1.安装
	yum install keepalived -y
	
2.配置 ( master backup )

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf 
global_defs {     
    router_id lb01   
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3
    }
}
--------------------------------------------------------------
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf global_defs {
    router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3
    }
}



3.启动
[root@lb01 ~]# systemctl start keepalived
[root@lb01 ~]# systemctl enable keepalived


4.验证
[root@lb01 ~]# ip addr|grep 10.0.0.3

posted @ 2019-11-08 22:21  老王教你学Linux  阅读(240)  评论(0编辑  收藏  举报