包安装利用 LNMP 实现 phpMyAdmin 的负载均衡并利用Redis实现会话保持nginx
142主机 安装配置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
#重启服务
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 -r nginx
color "创建 nginx 用户" 0
fi
color "开始安装 nginx 依赖包" 0
if [ $ID == "centos" ] ;then
if [[ $VERSION_ID =~ ^7 ]];then
yum -y install gcc make pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
elif [[ $VERSION_ID =~ ^8 ]];then
yum -y install 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 -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
else
apt update
apt -y install gcc make libpcre3 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/^(.*[0-9]).*/\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
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
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
[Install]
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
/apps/nginx/sbin/nginx -t
就可以得到nginx的配置文件路径 /apps/nginx/conf/nginx.conf
在配置文件里添加一下内容实现负载均衡
测试
下面分别在140 / 141 主机把IP地址写入默认页面测试
下面用客户端访问代理服务器139,就实现轮询的效果
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; 此配置也可以
}
修改配置文件可以
nginx -t 进行语法检查
nginx -s reload 配置生效
#新建文件夹
mkdir /data/php -p
#新建一个测试文件
vim /data/php/test.php
安装PHP
下面就可以安装PHP和PHP与数据库连接的插件
apt install -y php8.3-fpm php-mysql
配置PHP
安装完成后进入PHP的配置文件
默认本地访问,现在开启远程端口连接
也可以修改主配置文件,设置上传文件大小
vim /etc/php/8.3/fpm/php.ini
修改完成后记得重启PHP服务
systemctl restart php8.3-fpm.service
测试PHP
部署 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/
phpMyAdmin默认连接的是本地的数据库.所以我们要修改一下配置文件 在/data/php/ 里面
cp config.sample.inc.php config.inc.php
vim config.inc.php
142主机 安装配置Redis
当两个服务器都打开的时候
就会报下面的警告,因为主机默认把Session信息放在本机硬盘上的,导致机器一轮询就找不到Session信息就登录不进去,所以把各自的Session信息放在集中的Redis服务器中,叫做Session会话服务器
下面就把Session信息统一存储到Redis里面
安装
apt install redis-server
配置文件打开远程连接
#Redis配置文件
vim /etc/redis/redis.conf
#重启服务
systemctl restart redis
改成0.0.0.0
已经可以远程连接了
因为140 / 141 主机PHP要连接Redis,所以分别要安装一个服务插件
apt install php-redis
还有因为Session都要存储在Redis中,所以要修改PHP的存储路径
#配置文件
vim /etc/php/8.3/fpm/pool.d/www.conf
#结尾写入
php_value[session.save_handler] = redis
php_value[session.save_path] = "tcp://192.168.138.142:6379"
修改完成后记得重启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://blog.csdn.net/cj13106811623/article/details/145265935
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!