Nginx负载均衡实现动静分离

Nginx负载均衡实现动静分离

实验拓扑

原本应该有5台主机,2台放动态资源,2台放静态资源,一台做nginx负载均衡器,但是我们资源有限,九江4台资源机变成了2台,但是结果不受影响

实验环境:

系统 主机名 需要安装的服务 IP
Centos8 Nginx-LB Nginx 192.168.169.139
Centos8 LNMP Nginx+MySQL+PHP 192.168.169.140
Centos8 LAMP Httpd+MySQL+PHP 192.168.169.142

1. 前置实验环境准备

1.1 关闭防火墙和selinux

//三台主机关闭防火墙和selinux
systemctl disable --now firewalld
sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config 
reboot

1.2 修改主机名

//Nginx-LB
[root@localhost ~]# hostnamectl set-hostname Nginx-LB
[root@localhost ~]# bash
[root@Nginx-LB ~]#

//LNMP
[root@localhost ~]# hostnamectl set-hostname LNMP
[root@localhost ~]# bash
[root@LNMP ~]#

//Apache
[root@localhost ~]# hostnamectl set-hostname LAMP
[root@localhost ~]# bash
[root@LAMP ~]#

1.3 配置网络源

//三台主机配置网络源
rm -f /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
dnf clean all
dnf makecache

2. 在Nginx-LB主机上安装nginx

//安装所需工具和依赖包
[root@Nginx-LB ~]# dnf -y install gcc gcc-c++ make wget vim pcre-devel openssl openssl-devel gd-devel


//创建nginx用户
[root@Nginx-LB ~]# useradd -rMs /sbin/nologin nginx

//下载nginx1.22安装包
[root@Nginx-LB ~]# cd /usr/src
[root@Nginx-LB src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz

//解压nginx安装包
[root@Nginx-LB src]# tar xf nginx-1.22.0.tar.gz
[root@Nginx-LB src]# cd nginx-1.22.0

//编译nginx
[root@Nginx-LB nginx-1.22.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module
[root@Nginx-LB nginx-1.22.0]# make && make install
[root@Nginx-LB nginx-1.22.0]# cd /usr/local/nginx/
[root@Nginx-LB nginx]# ls
conf  html  logs  sbin

//配置nginx全局环境变量
[root@Nginx-LB nginx]# echo "export PATH=\$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@Nginx-LB nginx]# source /etc/profile.d/nginx.sh 
[root@Nginx-LB nginx]# which nginx
/usr/local/nginx/sbin/nginx

//使用systemd风格控制nginx服务
[root@Nginx-LB nginx]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=Nginx server daemon
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target 
[root@Nginx-LB nginx]# systemctl daemon-reload 
[root@Nginx-LB nginx]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

访问测试

3. 在LNMP主机上部署LNMP

3.1 安装工具包和依赖包

[root@LNMP ~]# dnf -y install epel-release
[root@LNMP ~]# dnf -y install gcc gcc-c++ make wget vim pcre-devel openssl openssl-devel gd-devel ncurses-compat-libs libxml2 libxml2-devel  bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm libzip-devel

3.2 安装nginx

//创建nginx用户
[root@LNMP ~]# useradd -rMs /sbin/nologin nginx

//下载nginx1.22安装包
[root@LNMP ~]# cd /usr/src
[root@LNMP src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz

//解压nginx
[root@LNMP src]# tar xf nginx-1.22.0.tar.gz 
[root@LNMP src]# cd nginx-1.22.0/

//编译nginx
[root@LNMP nginx-1.22.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module
[root@LNMP nginx-1.22.0]# make && make install
[root@LNMP nginx-1.22.0]# cd /usr/local/nginx/
[root@LNMP nginx]# ls
conf  html  logs  sbin

//配置nginx全局环境变量
[root@LNMP nginx]# echo "export PATH=\$PATH:/usr/local/nginx/sbin/" > /etc/profile.d/nginx.sh
[root@LNMP nginx]# source /etc/profile.d/nginx.sh 
[root@LNMP nginx]# which nginx 
/usr/local/nginx/sbin/nginx

//使用systemd风格管理nginx
[root@LNMP nginx]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=Nginx server daemon
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target 
[root@LNMP nginx]# systemctl daemon-reload 
[root@LNMP nginx]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

访问测试

3.3 安装mysql

//下载mysql8.0安装包
[root@LNMP nginx]# cd /usr/src/
[root@LNMP src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

//创建mysql用户
[root@LNMP src]# useradd -rMs /sbin/nologin mysql

//创建数据存放目录
[root@LNMP src]# mkdir /opt/data
[root@LNMP src]# chown -R mysql.mysql /opt/data


//安装mysql
[root@LNMP src]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
//文件较大,解压时间较长
[root@LNMP src]# cd /usr/local/
[root@LNMP local]# ln -s mysql-8.0.28-linux-glibc2.12-x86_64 mysql
[root@LNMP local]# chown -R mysql.mysql mysql*
[root@LNMP local]# ll -d mysql*
lrwxrwxrwx 1 mysql mysql  35 Oct 19 15:31 mysql -> mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Oct 19 15:29 mysql-8.0.28-linux-glibc2.12-x86_64
[root@LNMP local]# cd mysql
[root@LNMP mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files

//配置mysql全局环境变量
[root@LNMP mysql]# echo "export PATH=\$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh 
[root@LNMP mysql]# source /etc/profile.d/mysql.sh 
[root@LNMP mysql]# which mysql
/usr/local/mysql/bin/mysql

//配置mysql库文件
[root@LNMP mysql]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf 
[root@LNMP mysql]# ldconfig

//配置mysql头文件
[root@LNMP mysql]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@LNMP mysql]# ll /usr/include/mysql
lrwxrwxrwx 1 root root 24 Oct 19 15:39 /usr/include/mysql -> /usr/local/mysql/include

//配置mysql man文档
[root@LNMP mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/mysql/man

//初始化mysql
[root@LNMP mysql]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data/ 
2022-10-19T07:40:54.581530Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 35019
2022-10-19T07:40:54.596720Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-19T07:40:55.397137Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-19T07:40:56.374710Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _i<g-I&/>7fS

//root@localhost: 后面的就是mysql初始化密码,要保存好
[root@LNMP mysql]# echo "_i<g-I&/>7fS" > /tmp/pass 
[root@LNMP mysql]# cat /tmp/pass 
_i<g-I&/>7fS

//生成mysql配置文件
[root@LNMP mysql]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

//启动mysql
[root@LNMP mysql]# support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/LNMP.err'.
. SUCCESS!
//停止mysql
[root@LNMP mysql]# support-files/mysql.server stop
Shutting down MySQL. SUCCESS! 

//使用systemd风格管理mysql服务
[root@LNMP mysql]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL server daemon
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target
[root@LNMP mysql]# systemctl daemon-reload 
[root@LNMP mysql]# systemctl enable --now mysqld.service 
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

//初始化mysql密码
//先登录到mysql
[root@LNMP mysql]# mysql -uroot -p'_i<g-I&/>7fS'

//更改密码
mysql> alter user root@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

//利用新密码进行登录
[root@LNMP mysql]# mysql -uroot -p123456

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

3.4 安装php

//下载php源码包
[root@LNMP mysql]# cd /usr/src
[root@LNMP src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz

//解压php
[root@LNMP src]# tar xf php-8.1.11.tar.gz 
[root@LNMP src]# cd php-8.1.11/

//编译php
[root@LNMP php-8.1.11]# ./configure --prefix=/usr/local/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
…………
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
[root@LNMP php-8.1.11]# make && make install
//编译安装时间较久
[root@LNMP php-8.1.11]# cd /usr/local/php8/
[root@LNMP php8]# ls
bin  etc  include  lib  php  sbin  var

//配置php全局环境变量
[root@LNMP php8]# echo "export PATH=\$PATH:/usr/local/php8/bin" > /etc/profile.d/php.sh
[root@LNMP php8]# source /etc/profile.d/php.sh

//配置php头文件
[root@LNMP php8]# ln -s /usr/local/php8/include /usr/include/php
[root@LNMP php8]# ll /usr/include/php
lrwxrwxrwx 1 root root 23 Oct 19 16:18 /usr/include/php -> /usr/local/php8/include

//配置php库文件
[root@LNMP php8]# echo "/usr/local/php8/lib" > /etc/ld.so.conf.d/php.conf
[root@LNMP php8]# ldconfig 

//配置php-fpm
[root@LNMP php8]# cp /usr/src/php-8.1.11/php.ini-production /etc/php.ini 
cp: overwrite '/etc/php.ini'? y
[root@LNMP php8]# cp /usr/src/php-8.1.11/sapi/fpm/init.d.php-fpm /usr/local/php8/bin/php-fpm
[root@LNMP php8]# chmod +x /usr/local/php8/bin/php-fpm
[root@LNMP php8]# cd /usr/local/php8/etc/
[root@LNMP etc]# cp php-fpm.conf.default php-fpm.conf
[root@LNMP etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf

//使用systemd风格管理php-fpm
[root@LNMP php8]# vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/php8/bin/php-fpm start
ExecStop=/usr/local/php8/bin/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target
[root@LNMP php8]# systemctl daemon-reload 
[root@LNMP php8]# systemctl enable --now php-fpm.service 
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

3.5 配置nginx

[root@LNMP php8]# cd /usr/local/nginx/

//生成php测试页面
[root@LNMP nginx]# cat > html/index.php <<EOF
<?php
     phpinfo();
?>
EOF

//修改nginx主配置文件
[root@LNMP nginx]# vim conf/nginx.conf
location / {
            root   html;
            index  index.html index.htm index.php; //添加index.php
        }
//取消如下内容的注释
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
//进行修改,如下
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

//重启nginx
[root@LNMP nginx]# systemctl restart nginx.service

访问测试

4. 在LAMP主机上部署LAMP

4.1 安装工具包和依赖包

[root@LAMP ~]# dnf -y install epel-release
[root@LAMP ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make ncurses-devel  openssl cmake ncurses-compat-libs libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel  http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

4.2 安装httpd

//创建apache用户
[root@LAMP ~]# useradd -rMs /sbin/nologin apache

//下载apr,apr-util,httpd源码包
[root@LAMP ~]# cd /usr/src
[root@LAMP src]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@LAMP src]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@LAMP src]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz

//编译apr
[root@LAMP src]# tar xf apr-1.7.0.tar.gz 
[root@LAMP src]# cd apr-1.7.0/
[root@LAMP apr-1.7.0]# vim configure
	cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    #$RM "$cfgfile"	 	将这行注释掉或者删掉
[root@LAMP apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@LAMP apr-1.7.0]# make && make install

//编译apr-util
[root@LAMP apr-1.7.0]# cd /usr/src
[root@LAMP src]# tar apr-util-1.6.1.tar.gz
[root@LAMP src]# cd apr-util-1.6.1/
[root@LAMP apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@LAMP apr-util-1.6.1]# make && make install

//编译httpd
[root@LAMP apr-util-1.6.1]# cd /usr/src/
[root@LAMP src]# tar xf httpd-2.4.54.tar.gz 
[root@LAMP src]# cd httpd-2.4.54/
[root@LAMP httpd-2.4.5]# ./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@LAMP httpd-2.4.5]# make && make install
[root@LAMP httpd-2.4.54]# cd /usr/local/apache/
[root@LAMP apache]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules

//配置apache全局环境变量
[root@LAMP apache]# echo "export PATH=\$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
[root@LAMP apache]# source /etc/profile.d/httpd.sh 
[root@LAMP apache]# which apachectl
/usr/local/apache/bin/apachectl

//配置头文件
[root@LAMP apache]# ln -s /usr/local/apache/include /usr/include/httpd
[root@LAMP apache]# ll /usr/include/httpd
lrwxrwxrwx 1 root root 25 Oct 19 20:33 /usr/include/httpd -> /usr/local/apache/include

//配置man 文档
[root@LAMP apache]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/apache/man

//将主配置文件里的'#ServerName'行的注释去掉
[root@LAMP apache]#  sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf

//使用systemd风格管理apache
[root@LAMP apache]# vim /usr/lib/systemd/system/httpd.service
[Unit]
Description=web server daemon
Documentation=man:httpd(5)
After=network.target 
 
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop 
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target
[root@LAMP apache]# systemctl daemon-reload 
[root@LAMP apache]# systemctl enable --now httpd.service 
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

访问测试

4.3 安装mysql

//下载mysql安装包
[root@LAMP apache]# cd /usr/src/
[root@LAMP src]#wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

//创建mysql用户
[root@LAMP src]# useradd -rMs /sbin/nologin mysql

//创建数据存放目录
[root@LAMP src]# mkdir /opt/data
[root@LAMP src]# chown -R mysql.mysql /opt/data

//安装mysql
[root@LAMP src]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@LAMP src]# cd /usr/local/
[root@LAMP local]# ln -s mysql-8.0.28-linux-glibc2.12-x86_64 mysql
[root@LAMP local]# chown -R mysql.mysql mysql*
[root@LAMP local]# ll -d mysql*
lrwxrwxrwx 1 mysql mysql  35 Oct 19 20:41 mysql -> mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Oct 19 20:41 mysql-8.0.28-linux-glibc2.12-x86_64
[root@LAMP local]# cd mysql
[root@LAMP mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files

//配置全局环境变量
[root@LAMP mysql]# echo "export PATH=\$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh 
[root@LAMP mysql]# source /etc/profile.d/mysql.sh 
[root@LAMP mysql]# which mysql
/usr/local/mysql/bin/mysql

//配置mysql库文件
[root@LAMP mysql]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf 
[root@LAMP mysql]# ldconfig

//配置mysql头文件
[root@LAMP mysql]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@LAMP mysql]# ll /usr/include/mysql
lrwxrwxrwx. 1 root root 24 Sep  7 15:21 /usr/include/mysql -> /usr/local/mysql/include

//配置mysql man文档
[root@LAMP mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/mysql/man

//初始化mysql
[root@LAMP mysql]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data/
2022-10-19T12:44:51.639295Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 44706
2022-10-19T12:44:51.659424Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-19T12:44:52.447609Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-19T12:44:53.668287Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: bt/XF)uTU6Vg

//root@localhost: 后面的就是mysql初始化密码,要保存好
[root@LAMP mysql]# echo "bt/XF)uTU6Vg" > /tmp/pass
[root@LAMP mysql]# cat /tmp/pass 
bt/XF)uTU6Vg

//生成mysql配置文件
[root@LAMP mysql]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

//使用systemd风格管理mysql服务
[root@LAMP mysql]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL server daemon
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target
[root@LAMP mysql]# systemctl daemon-reload
[root@LAMP mysql]# systemctl enable --now mysqld

//初始化mysql密码
//先登录到mysql
[root@LAMP mysql]# mysql -uroot -p'bt/XF)uTU6Vg'

//更改密码
mysql> alter user root@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

//利用新密码进行登录
[root@LAMP mysql]# mysql -uroot -p123456

4.4 安装php

//下载php源码包
[root@LAMP mysql]# cd /usr/src
[root@LAMP src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz

//解压php
[root@LAMP src]# tar xf php-8.1.11.tar.gz
[root@LAMP src]# cd php-8.1.11/

//编译php
[root@LAMP php-8.1.11]# ./configure --prefix=/usr/local/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
…………
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
[root@LAMP php-8.1.11]# make && make install
[root@LAMP php-8.1.11]# cd /usr/local/php8/
[root@LAMP php8]# ls
bin  etc  include  lib  php  sbin  var

//配置php全局环境变量
[root@LAMP php8]# echo "export PATH=\$PATH:/usr/local/php8/bin" > /etc/profile.d/php.sh
[root@LAMP php8]# source /etc/profile.d/php.sh

//配置php头文件
[root@LAMP php8]# ln -s /usr/local/php8/include /usr/include/php
[root@LAMP php8]# ll /usr/include/php
lrwxrwxrwx 1 root root 23 Oct 19 21:02 /usr/include/php -> /usr/local/php8/include

//配置php库文件
[root@LAMP php8]# echo "/usr/local/php8/lib" > /etc/ld.so.conf.d/php.conf
[root@LAMP php8]# ldconfig

//配置php-fpm
[root@LAMP php8]# cp /usr/src/php-8.1.11/php.ini-production /etc/php.ini 
cp: overwrite '/etc/php.ini'? y
[root@LAMP php8]# cp /usr/src/php-8.1.11/sapi/fpm/init.d.php-fpm /usr/local/php8/bin/php-fpm
[root@LAMP php8]# chmod +x /usr/local/php8/bin/php-fpm
[root@LAMP php8]# cd /usr/local/php8/etc/
[root@LAMP etc]# cp php-fpm.conf.default php-fpm.conf
[root@LAMP etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf

//使用systemd风格管理php-fpm
[root@LAMP etc]# vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/php8/bin/php-fpm start
ExecStop=/usr/local/php8/bin/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target
[root@LAMP etc]# systemctl daemon-reload 
[root@LAMP etc]# systemctl enable --now php-fpm.service

4.5 配置httpd

//启动代理模块
[root@LAMP etc]# cd /usr/local/apache/
[root@LAMP apache]# vim conf/httpd.conf
#取消如下内容的注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

//配置虚拟主机,让php页面监听在82端口
listen 82
<VirtualHost *:82>
    DocumentRoot "/usr/local/apache/htdocs/php"
    ServerName php.example.com
    ErrorLog "logs/php.example.com-error_log"
    CustomLog "logs/php.example.com-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/php/$1
    <Directory "/usr/local/apache/htdocs/php">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>、

//创建虚拟主机的目录并生成php测试页面
[root@LAMP apache]# mkdir /usr/local/apache/htdocs/php
[root@LAMP apache]# cd /usr/local/apache/htdocs/php
[root@LAMP php]# cat > index.php <<EOF
<?php
    phpinfo();
?>
EOF

[root@LAMP php]#  chown -R apache.apache /usr/local/apache/htdocs/

[root@LAMP apache]# vim /usr/local/apache/conf/httpd.conf
#取消此行注释
Include conf/extra/httpd-vhosts.conf
#搜索AddType,加上以下内容
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php       #添加此行   
AddType application/x-httpd-php-source .phps   #添加此行
#在'DirectoryIndex index.html'后面添加index.php
DirectoryIndex index.html index.php

//重启httpd
[root@LAMP php]# systemctl restart httpd.service

访问测试

5. 在Nginx-LB主机上开负载均衡和反向代理

[root@Nginx-LB ~]# cd /usr/local/nginx/
[root@Nginx-LB nginx]#
#在http段配置如下内容
upstream dynamic {
        server 192.168.169.140;		//动态资源放在哪里就写那台主机的IP+端口
        server 192.168.169.142:82;
    }

upstream static {
        server 192.168.169.140;		//静态资源放在哪里就写哪台主机的IP+端口
        server 192.168.169.142;
    }

#在server段进行如下配置
location / {
            proxy_pass http://static; //因为负载均衡器不需要主页,直接修改默认的location段
        }
//取消如下内容的注释
location ~ \.php$ {
            proxy_pass   http://127.0.0.1;
        }
//进行修改
location ~ \.php$ {
            proxy_pass   http://dynamic;
        }

//重启nginx
[root@Nginx-LB nginx]# systemctl restart nginx.service

6. 进行验证

访问Nginx-LB主机的IP

此时我们访问到的是静态网页,并且nginx和apache做轮询

刷新,重新加载

访问Nginx-LB主机的IP+index.php

现在我们访问到的是php动态网页,同样也是LNMP和LAMP两台主机轮询

刷新,重新加载

posted @ 2022-10-19 17:31  Zic师傅  阅读(92)  评论(0编辑  收藏  举报