Lamp结合负载均衡拆分数据库

web脚本

#!/bin/bash
#1、配置yum源
cat >> /etc/yum.repos.d/yumrpm.repo << EOF
[all-rpm]
name=all  repo
baseurl=http://10.0.0.61:23456
enabled=1
gpgcheck=0
EOF
#2\清理缓存
yum clean all
#3\创建相应用户组
groupadd www -g  666
useradd www -s /sbin/nologin  -M -u 666 -g 666
#4\下载nginx\php服务
yum install nginx -y
yum install -y 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 php71w-json php71w-pecl-apcu php71w-pecl-apcu-devel
#5\修改php-fpm配置文件
sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 
sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf 
#6\启动服务,设置开机自启
systemctl start nginx
systemctl enable nginx

systemctl start php-fpm
systemctl enable php-fpm
#7\创建数据,看是否连通好
rm -rf /etc/nginx/conf.d/*

cat >/etc/nginx/conf.d/php.conf <<EOF
server {
	listen 80;
	server_name _;
	location / {
		root /php-code/; 
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;	
	}
}

EOF

mkdir -p /php-code/
chown -R www.www /php-code/

cat >/php-code/test-info.php <<EOF
<?php
	phpinfo();
?>
EOF

#重启
systemctl restart nginx

安装51数据库脚本

#!/bin/bash
#1、配置yum源
cat >> /etc/yum.repos.d/yumrpm.repo << EOF
[all-rpm]
name=all  repo
baseurl=http://10.0.0.61:23456
enabled=1
gpgcheck=0
EOF
#2\清理缓存
yum clean all
#3\安装数据库
yum install mariadb-server mariadb -y
#4\启动数据库,设置开机自启 
systemctl start mariadb
systemctl enable mariadb
#5\修改密码授予远程访问
mysqladmin  password '333333'
#创建非交互式远程连接用户
mysql -uroot -p333333  -e "grant all privileges on *.* to afei@'%' identified by '333333'"
#创建wordpress,wecenbter数据库
mysql -uroot -p333333 -e "create database wordpress"
mysql -uroot -p333333 -e "create database wecenter"

安装服务wordpress,wecenter

web-7

#/bin/bash
#1\创建数据目录
mkdir -p /code/wordpress
cd /code/wordpress
#2\下载与源码包
wget https://cn.wordpress.org/latest-zh_CN.zip
#3\解压
unzip latest-zh_CN.zip
#4\授权
chown -R www.www /code/
#5\创建wordpress虚拟机
cat >> /etc/nginx/conf.d/wordpress.conf << EOF
server{
    listen 80;
    server_name wordpress.afeitt.cn;

    root /code/wordpress;
    index index.php index.html;

    location ~* \.php$ {

        root /code/wordpress;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}
EOF
#重启nginx
systemctl restart nginx

xafei
222222

web-8

1\下载环境
#!/bin/bash
#1、配置yum源
cat >> /etc/yum.repos.d/yumrpm.repo << EOF
[all-rpm]
name=all  repo
baseurl=http://10.0.0.61:23456
enabled=1
gpgcheck=0
EOF
#2\清理缓存
yum clean all
#3\创建相应用户组
groupadd www -g  666
useradd www -s /sbin/nologin  -M -u 666 -g 666
#4\下载nginx\php服务
yum install nginx -y
yum install -y 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 php71w-json php71w-pecl-apcu php71w-pecl-apcu-devel
#5\修改php-fpm配置文件
sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 
sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf 
#6\启动服务,设置开机自启
systemctl start nginx
systemctl enable nginx

systemctl start php-fpm
systemctl enable php-fpm
#7\创建数据,看是否连通好
rm -rf /etc/nginx/conf.d/*

cat >/etc/nginx/conf.d/php.conf <<EOF
server {
	listen 80;
	server_name _;
	location / {
		root /php-code/; 
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;	
	}
}

EOF

mkdir -p /php-code/
chown -R www.www /php-code/

cat >/php-code/test-info.php <<EOF
<?php
	phpinfo();
?>
EOF

#重启
systemctl restart nginx

下载服务
mkdir -p /code/
cd /code/
wget https://cn.wordpress.org/latest-zh_CN.zip
unzip latest-zh_CN.zip
chown -R www.www /code/

2\将web7的数据发到web8

scp /code/wordpress/wp-config.php  root@172.16.1.8:/code/wordpress/
scp /etc/nginx/conf.d/wordpress.conf   root@172.16.1.8:/etc/nginx/conf.d/wordpress.conf

重启服务
systemctl  restart  nginx

wecenter服务

web-7
#/bin/bash
#1\创建数据目录
mkdir -p /code/wecenter
cd /code/wecenter
#2\下载与源码包
wget http://yuchaoit.cn/data/wecenter.zip
#3\解压
unzip wecenter.zip
#4\授权
chown -R www.www /code/
#5\创建wecenter虚拟机
cat >> /etc/nginx/conf.d/wecenter.conf << EOF
server{
    listen 80;
    server_name wecenter.afeitt.cn;

    root /code/wecenter;
    index index.php index.html;

    location ~* \.php$ {

        root /code/wecenter;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}
EOF
#重启nginx
systemctl restart nginx

删除表的命令
drop database wecenter;

web-8
mkdir -p /code/wecenter
cd /code/wecenter
wget http://yuchaoit.cn/data/wecenter.zip
unzip wecenter.zip
chown -R www.www /code/

2\将web7的数据发到web8

scp /code/wecenter/system/config/database.php  root@172.16.1.8:/code/wecenter/system/config
scp /etc/nginx/conf.d/wecenter.conf   root@172.16.1.8:/etc/nginx/conf.d/wecenter.conf

重启服务
systemctl  restart  nginx

NFS文件共享

31机器

下载服务
[root@nfs-31 ~]#yum install nfs-utils rpcbind -y
开机自启
[root@nfs-31 ~]#systemctl start nfs rpcbind
[root@nfs-31 ~]#systemctl enable nfs rpcbind


创建用户
[root@nfs-31 ~]#groupadd www -g 666
[root@nfs-31 ~]#useradd www -u 666 -g 666 -M -s /sbin/nologin

写入配置文件
[root@nfs-31 ~]#cat /etc/exports
/wordpress-uploads/  172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

创建共享目录
[root@nfs-31 ~]#mkdir /wordpress-uploads
[root@nfs-31 ~]#chown -R www.www /wordpress-uploads/
[root@nfs-31 ~]#systemctl restart nfs rpcbind

设置web7-8的共享

wordpass

下载服务
yum install nfs-utils rpcbind -y

showmount -e 172.16.1.31

挂载
[root@web-7 ~]#mount  -t nfs 172.16.1.31:/wordpress-uploads /code/wordpress/wp-content/uploads 

[root@web-8 ~]#mount  -t nfs 172.16.1.31:/wordpress-uploads /code/wordpress/wp-content/uploads 

wecenter

写入配置文件
[root@nfs-31 ~]#cat /etc/exports
/wecenter-uploads/  172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

创建共享目录
[root@nfs-31 ~]#mkdir /wecenter-uploads
[root@nfs-31 ~]#chown -R www.www /wecenter-uploads/
[root@nfs-31 ~]#systemctl restart nfs rpcbind

web7、8挂载

mount  -t nfs 172.16.1.31:/wecenter-uploads /code/wecenter/uploads/ 

设置5的负载均衡

yum install nginx -y

2\放入反向代理参数
[root@slb-5 ~]# cat /etc/nginx/proxy_params.conf 
proxy_set_header Host $http_host;
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;


[root@slb-5 /etc/nginx/conf.d]#cat proxy.conf 
upstream web-pools{
  server 172.16.1.7:80 weight=4;
  server 172.16.1.8:80 weight=1;

}

server{

  listen 80;
  server_name wordpress.laoliu.cc;
  location / {
    proxy_pass http://web-pools;
    include /etc/nginx/proxy_params.conf;

  }

}




重启
[root@slb-5 /etc/nginx/conf.d]#systemctl start nginx

tail -f /var/log/nginx/access.log
posted @ 2022-07-25 21:52  张开嘴  阅读(30)  评论(0编辑  收藏  举报