包安装利用 LNMP 实现 phpMyAdmin 的负载均衡并利用Redis实现会话保持nginx ...

宁睿  论坛元老 | 2025-1-23 03:35:53 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1083|帖子 1083|积分 3259

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x

142主机 安装设置MySQL

安装

  1. apt install mysql-server
复制代码
 设置远程访问

  1. vim /etc/mysql/mysql.conf.d/mysqld.cnf
  2. #bind-address = 127.0.0.1
  3. #mysqlx-bind-address = 127.0.0.1
复制代码

  1. #重启服务
  2. systemctl restart mysql.service
复制代码
  1. #创建用户制定插件(MySQL8.0)
  2. mysql> create user admin@'192.168.138.%' identified with mysql_native_password by '123456';
  3. #授权
  4. mysql> grant all on *.* to admin@'192.168.138.%';
复制代码

139主机 安装和设置 Nginx反向代理

安装 

用root用户跑脚本
  1. #!/bin/bash
  2. #
  3. #********************************************************************
  4. #Author:            wangxiaochun
  5. #QQ:                29308620
  6. #Date:              2020-12-01
  7. #FileName:          install_nginx.sh
  8. #URL:               http://www.wangxiaochun.com
  9. #Description:       The test script
  10. #Copyright (C):     2020 All rights reserved
  11. #********************************************************************
  12. NGINX_VERSION=1.27.2
  13. #NGINX_VERSION=1.22.0
  14. #NGINX_VERSION=1.20.2
  15. #NGINX_VERSION=1.18.0
  16. NGINX_FILE=nginx-${NGINX_VERSION}.tar.gz
  17. NGINX_URL=https://nginx.org/download/
  18. NGINX_INSTALL_DIR=/apps/nginx
  19. SRC_DIR=/usr/local/src
  20. CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`
  21. . /etc/os-release
  22. color () {
  23.     RES_COL=60
  24.     MOVE_TO_COL="echo -en \\033[${RES_COL}G"
  25.     SETCOLOR_SUCCESS="echo -en \\033[1;32m"
  26.     SETCOLOR_FAILURE="echo -en \\033[1;31m"
  27.     SETCOLOR_WARNING="echo -en \\033[1;33m"
  28.     SETCOLOR_NORMAL="echo -en \E[0m"
  29.     echo -n "$1" && $MOVE_TO_COL
  30.     echo -n "["
  31.     if [ $2 = "success" -o $2 = "0" ] ;then
  32.         ${SETCOLOR_SUCCESS}
  33.         echo -n $"  OK  "   
  34.     elif [ $2 = "failure" -o $2 = "1"  ] ;then
  35.         ${SETCOLOR_FAILURE}
  36.         echo -n $"FAILED"
  37.     else
  38.         ${SETCOLOR_WARNING}
  39.         echo -n $"WARNING"
  40.     fi
  41.     ${SETCOLOR_NORMAL}
  42.     echo -n "]"
  43.     echo
  44. }
  45. check () {
  46.     [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
  47.     cd  ${SRC_DIR}
  48.     if [  -e ${NGINX_FILE}${TAR} ];then
  49.         color "相关文件已准备好" 0
  50.     else
  51.         color '开始下载 nginx 源码包' 0
  52.         wget ${NGINX_URL}${NGINX_FILE}${TAR}
  53.         [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; }
  54.     fi
  55. }
  56. install () {
  57.     color "开始安装 nginx" 0
  58.     if id nginx  &> /dev/null;then
  59.         color "nginx 用户已存在" 1
  60.     else
  61.         useradd -s /sbin/nologin -r  nginx
  62.         color "创建 nginx 用户" 0
  63.     fi
  64.     color "开始安装 nginx 依赖包" 0
  65.     if [ $ID == "centos" ] ;then
  66.             if [[ $VERSION_ID =~ ^7 ]];then
  67.             yum -y  install  gcc  make pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
  68.                 elif [[ $VERSION_ID =~ ^8 ]];then
  69.             yum -y  install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
  70.                 else
  71.             color '不支持此系统!'  1
  72.             exit
  73.         fi
  74.      elif [ $ID == "rocky"  ];then
  75.             yum -y  install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
  76.      else
  77.         apt update
  78.         apt -y install gcc make  libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
  79.      fi
  80.      [ $? -ne 0 ] && { color "安装依赖包失败" 1; exit; }
  81.      cd $SRC_DIR
  82.      tar xf ${NGINX_FILE}
  83.      NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
  84.      cd ${NGINX_DIR}
  85.      ./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
  86.      make -j $CPUS && make install
  87.      [ $? -eq 0 ] && color "nginx 编译安装成功" 0 ||  { color "nginx 编译安装失败,退出!" 1 ;exit; }
  88.          chown -R nginx.nginx ${NGINX_INSTALL_DIR}
  89.      ln -s ${NGINX_INSTALL_DIR}/sbin/nginx /usr/local/sbin/nginx
  90.      echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
  91.      cat > /lib/systemd/system/nginx.service <<EOF
  92. [Unit]
  93. Description=The nginx HTTP and reverse proxy server
  94. After=network.target remote-fs.target nss-lookup.target
  95. [Service]
  96. Type=forking
  97. PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
  98. ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
  99. ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
  100. ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
  101. ExecReload=/bin/kill -s HUP \$MAINPID
  102. KillSignal=SIGQUIT
  103. TimeoutStopSec=5
  104. KillMode=process
  105. PrivateTmp=true
  106. LimitNOFILE=100000
  107. [Install]
  108. WantedBy=multi-user.target
  109. EOF
  110.      systemctl daemon-reload
  111.      systemctl enable --now nginx &> /dev/null
  112.      systemctl is-active nginx &> /dev/null ||  { color "nginx 启动失败,退出!" 1 ; exit; }
  113.      color "nginx 安装完成" 0
  114. }
  115. check
  116. install
复制代码
直接bash 文件名
设置

有些时候不知道nginx设置的文件路径可以用下面这个方法
  1. ps -ef | grep nginx
复制代码

  1. /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链接
  1.     location ~ \.php$ { #实现php-fpm
  2.     root /data/php;  #指定数据目录
  3.     fastcgi_pass 127.0.0.1:9000;
  4.     fastcgi_index index.php;
  5.     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6.     include fastcgi_params;
  7.     }
  8.     location ~ ^/(ping|pm_status)$ { #实现状态页
  9.     include fastcgi_params;
  10.     fastcgi_pass 127.0.0.1:9000;
  11.     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  12.     #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name; 此配置也可以
  13.     }
复制代码

修改设置文件可以
nginx -t                 进行语法查抄 
nginx -s reload    设置生效
  1. #新建文件夹
  2. mkdir /data/php -p
  3. #新建一个测试文件
  4. vim /data/php/test.php
复制代码

安装PHP 

下面就可以安装PHP和PHP与数据库连接的插件
  1. apt install -y php8.3-fpm php-mysql
复制代码
设置PHP 

安装完成后进入PHP的设置文件 

 默认当地访问,现在开启远程端口连接

 也可以修改主设置文件,设置上传文件巨细
  1. vim /etc/php/8.3/fpm/php.ini
复制代码
 


修改完成后记得重启PHP服务
  1. systemctl restart php8.3-fpm.service
复制代码
测试PHP 


部署 phpMyAdmin 代码上线
PhpMyAdmin 是一个基于PHP的MySQL管理平台
在140 / 141主机安装设置PhpMyAdmin
  1. #下载phpmyadmin
  2. wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
  3. #解压缩
  4. unzip phpMyAdmin-5.2.0-all-languages.zip
  5. #移动到放PHP的文件夹]
  6. mv phpMyAdmin-5.2.0-all-languages/* /data/php/
复制代码



phpMyAdmin默认连接的是当地的数据库.以是我们要修改一下设置文件 在/data/php/ 里面 
  1. cp config.sample.inc.php config.inc.php
  2. vim config.inc.php
复制代码



142主机 安装设置Redis

当两个服务器都打开的时候

就会报下面的警告,因为主机默认把Session信息放在本机硬盘上的,导致呆板一轮询就找不到Session信息就登录不进去,以是把各自的Session信息放在会合的Redis服务器中,叫做Session会话服务器

下面就把Session信息同一存储到Redis里面 
安装

  1. apt install redis-server
复制代码
设置文件打开远程连接

  1. #Redis配置文件
  2. vim /etc/redis/redis.conf
  3. #重启服务
  4. systemctl restart redis
复制代码
改成0.0.0.0

已经可以远程连接了

因为140 / 141 主机PHP要连接Redis,以是分别要安装一个服务插件
  1. apt install php-redis
复制代码
还有因为Session都要存储在Redis中,以是要修改PHP的存储路径
  1. #配置文件
  2. vim /etc/php/8.3/fpm/pool.d/www.conf
  3. #结尾写入
  4. php_value[session.save_handler] = redis
  5. php_value[session.save_path] = "tcp://192.168.138.142:6379"
复制代码

 修改完成后记得重启PHP服务
  1. 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. 1. 通过本地主机连接到 Redis:
  2. redis-cli
  3. 2. 运行以下命令禁用受保护模式:
  4. CONFIG SET protected-mode no
  5. 3.测试连接后,使用以下命令使更改永久生效:
  6. CONFIG REWRITE
复制代码

测试

末了登录刷新,就可以发现访问的主机在变换,有轮询服务



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宁睿

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表