包安装利用 LNMP 实现 phpMyAdmin 的负载均衡并利用Redis实现会话保持nginx
https://i-blog.csdnimg.cn/direct/d42c681cc9ad4e19affb78dadea468d6.png142主机 安装设置MySQL
安装
apt install mysql-server 设置远程访问
vim /etc/mysql/mysql.conf.d/mysqld.cnf
#bind-address = 127.0.0.1
#mysqlx-bind-address = 127.0.0.1 https://i-blog.csdnimg.cn/direct/ef0abef1c77a4fd08c15b67a459921e1.png
#重启服务
systemctl restart mysql.service #创建用户制定插件(MySQL8.0)
mysql> create user admin@'192.168.138.%' identified with mysql_native_password by '123456';
#授权
mysql> grant all on *.* to admin@'192.168.138.%';
139主机 安装和设置 Nginx反向代理
安装
用root用户跑脚本
#!/bin/bash
#
#********************************************************************
#Author: wangxiaochun
#QQ: 29308620
#Date: 2020-12-01
#FileName: install_nginx.sh
#URL: http://www.wangxiaochun.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
NGINX_VERSION=1.27.2
#NGINX_VERSION=1.22.0
#NGINX_VERSION=1.20.2
#NGINX_VERSION=1.18.0
NGINX_FILE=nginx-${NGINX_VERSION}.tar.gz
NGINX_URL=https://nginx.org/download/
NGINX_INSTALL_DIR=/apps/nginx
SRC_DIR=/usr/local/src
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`
. /etc/os-release
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $"OK"
elif [ $2 = "failure" -o $2 = "1"] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
check () {
[ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
cd${SRC_DIR}
if [-e ${NGINX_FILE}${TAR} ];then
color "相关文件已准备好" 0
else
color '开始下载 nginx 源码包' 0
wget ${NGINX_URL}${NGINX_FILE}${TAR}
[ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; }
fi
}
install () {
color "开始安装 nginx" 0
if id nginx&> /dev/null;then
color "nginx 用户已存在" 1
else
useradd -s /sbin/nologin -rnginx
color "创建 nginx 用户" 0
fi
color "开始安装 nginx 依赖包" 0
if [ $ID == "centos" ] ;then
if [[ $VERSION_ID =~ ^7 ]];then
yum -yinstallgccmake pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
elif [[ $VERSION_ID =~ ^8 ]];then
yum -yinstall make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
else
color '不支持此系统!'1
exit
fi
elif [ $ID == "rocky"];then
yum -yinstall gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
else
apt update
apt -y install gcc makelibpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
fi
[ $? -ne 0 ] && { color "安装依赖包失败" 1; exit; }
cd $SRC_DIR
tar xf ${NGINX_FILE}
NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*).*/\1/p'`
cd ${NGINX_DIR}
./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
make -j $CPUS && make install
[ $? -eq 0 ] && color "nginx 编译安装成功" 0 ||{ color "nginx 编译安装失败,退出!" 1 ;exit; }
chown -R nginx.nginx ${NGINX_INSTALL_DIR}
ln -s ${NGINX_INSTALL_DIR}/sbin/nginx /usr/local/sbin/nginx
echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
cat > /lib/systemd/system/nginx.service <<EOF
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=100000
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx &> /dev/null
systemctl is-active nginx &> /dev/null ||{ color "nginx 启动失败,退出!" 1 ; exit; }
color "nginx 安装完成" 0
}
check
install
直接bash 文件名
设置
有些时候不知道nginx设置的文件路径可以用下面这个方法
ps -ef | grep nginx https://i-blog.csdnimg.cn/direct/baaaea00ceb24bcaaf1a48ea11a49bbf.png
/apps/nginx/sbin/nginx -t 就可以得到nginx的设置文件路径 /apps/nginx/conf/nginx.conf
https://i-blog.csdnimg.cn/direct/005012678b63434badcdf94c76299845.png
在设置文件里添加一下内容实现负载均衡
https://i-blog.csdnimg.cn/direct/69e0265348554e33bfa655a4eba46de5.png
测试
下面分别在140 / 141 主机把IP地址写入默认页面测试
https://i-blog.csdnimg.cn/direct/cee521ac34654ebd86671ee412600769.png
下面用客户端访问代理服务器139,就实现轮询的效果
https://i-blog.csdnimg.cn/direct/64119574bc4c48d4aa1fbf9a84e452b6.png
140 / 141主机 链接PHP
设置NGINX
在140 / 141主机安装设置NGINX
和139主机一样跑脚本安装NGINX,这里要加多一项要安装PHP
进入NGINX设置文件 /apps/nginx/conf/nginx.conf
新增以下内容实现与PHP链接
location ~ \.php$ { #实现php-fpm
root /data/php;#指定数据目录
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/(ping|pm_status)$ { #实现状态页
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name; 此配置也可以
} https://i-blog.csdnimg.cn/direct/a164d23947ef4eeb807eb93367af5377.png
修改设置文件可以
nginx -t 进行语法查抄
nginx -s reload 设置生效
#新建文件夹
mkdir /data/php -p
#新建一个测试文件
vim /data/php/test.php https://i-blog.csdnimg.cn/direct/d05a91df0c8748a1a6c5ea5171b36f27.png
安装PHP
下面就可以安装PHP和PHP与数据库连接的插件
apt install -y php8.3-fpm php-mysql 设置PHP
安装完成后进入PHP的设置文件
https://i-blog.csdnimg.cn/direct/aecc53a691344a15800af349e03ddbc8.png
默认当地访问,现在开启远程端口连接
https://i-blog.csdnimg.cn/direct/681682478c5545728bac96c50df94238.png
也可以修改主设置文件,设置上传文件巨细
vim /etc/php/8.3/fpm/php.ini
https://i-blog.csdnimg.cn/direct/d49423e2ab5a4a6a8d77e0411f3fedf7.png
https://i-blog.csdnimg.cn/direct/0752eea60f7846e1a15303572bfc9f7e.png
修改完成后记得重启PHP服务
systemctl restart php8.3-fpm.service 测试PHP
https://i-blog.csdnimg.cn/direct/ca69e05f4c6a409e9ef6774de1e08b6d.png
部署 phpMyAdmin 代码上线
PhpMyAdmin 是一个基于PHP的MySQL管理平台
在140 / 141主机安装设置PhpMyAdmin
#下载phpmyadmin
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
#解压缩
unzip phpMyAdmin-5.2.0-all-languages.zip
#移动到放PHP的文件夹]
mv phpMyAdmin-5.2.0-all-languages/* /data/php/
https://i-blog.csdnimg.cn/direct/19133794432f4f849d973c3bf2e160e2.png
https://i-blog.csdnimg.cn/direct/723c1d43c5a34c038912dfbce067a508.png
phpMyAdmin默认连接的是当地的数据库.以是我们要修改一下设置文件 在/data/php/ 里面
cp config.sample.inc.php config.inc.php
vim config.inc.php
https://i-blog.csdnimg.cn/direct/ee39c7d28ec7462e8365ad49ebc0c24c.png
https://i-blog.csdnimg.cn/direct/24767071ac0140d38e0ec3af132b8506.png
142主机 安装设置Redis
当两个服务器都打开的时候
https://i-blog.csdnimg.cn/direct/efcbc4d0798f4347ae607268e5a4dd9c.png
就会报下面的警告,因为主机默认把Session信息放在本机硬盘上的,导致呆板一轮询就找不到Session信息就登录不进去,以是把各自的Session信息放在会合的Redis服务器中,叫做Session会话服务器
https://i-blog.csdnimg.cn/direct/9e7ab9334a914bde9543fd64557e5b5e.png
下面就把Session信息同一存储到Redis里面
安装
apt install redis-server 设置文件打开远程连接
#Redis配置文件
vim /etc/redis/redis.conf
#重启服务
systemctl restart redis
改成0.0.0.0
https://i-blog.csdnimg.cn/direct/85540d2c246646908f8f8df347900a70.png
已经可以远程连接了
https://i-blog.csdnimg.cn/direct/94ef07acc81f4df49b9340aaf1d1ed62.png
因为140 / 141 主机PHP要连接Redis,以是分别要安装一个服务插件
apt install php-redis
还有因为Session都要存储在Redis中,以是要修改PHP的存储路径
#配置文件
vim /etc/php/8.3/fpm/pool.d/www.conf
#结尾写入
php_value = redis
php_value = "tcp://192.168.138.142:6379" https://i-blog.csdnimg.cn/direct/916dce54fe054133ab290c241c6aa3ef.png
修改完成后记得重启PHP服务
systemctl restart php8.3-fpm.service 如果出现不能连接的环境,查抄Redis的错误信息
(error) DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside
从错误信息可以看出,Redis 正在以受保护模式(protected mode)运行,而没有设置密码,这会限制连接,仅允许当地(loopback interface,即 127.0.0.1)访问。
方法 1:禁用受保护模式
1. 通过本地主机连接到 Redis:
redis-cli
2. 运行以下命令禁用受保护模式:
CONFIG SET protected-mode no
3.测试连接后,使用以下命令使更改永久生效:
CONFIG REWRITE https://i-blog.csdnimg.cn/direct/2f298a0fa6814208bf7767d6fc654581.png
测试
末了登录刷新,就可以发现访问的主机在变换,有轮询服务
https://i-blog.csdnimg.cn/direct/7518defa646d4018ac15867283addc28.png
https://i-blog.csdnimg.cn/direct/3404b69d9eb2475e8fb922a59769a5aa.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]