主机名 | IP地址 |
---|---|
proxy | 192.168.99.5 |
web1 | 192.168.99.100 |
web2 | 192.168.99.200 |
1.1 设置防火墙永久关闭
- [root@proxy ~]$ systemctl disable firewalld --now
- Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
- Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
- [root@proxy ~]$ systemctl status firewalld
- ● firewalld.service - firewalld - dynamic firewall daemon
- Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
- Active: inactive (dead)
- Docs: man:firewalld(1)
-
1.2 设置 selinux 为宽容模式,并修改配置文件为禁用
- [root@proxy ~]$ setenforce 0
- [root@proxy ~]$ getenforce
- Permissive
- [root@proxy ~]$ sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
-
2.1 挂载光盘到 /dvd 目录
- [root@proxy ~]$ mkdir /dvd
- [root@proxy ~]$ mount /dev/cdrom /dvd/
- mount: /dev/sr0 写保护,将以只读方式挂载
- [root@proxy ~]$ ls /dvd/
- AppStream BaseOS EFI images isolinux LICENSE media.repo TRANS.TBL
-
2.2 将光盘设置为开机自动挂载
- [root@proxy ~]$ vim /etc/fstab
- /dev/cdrom /dvd iso9660 defaults 0 0
- [root@proxy ~]$ mount -a
- [root@proxy ~]$ df -h /dvd/
- 文件系统 容量 已用 可用 已用% 挂载点
- /dev/sr0 4.4G 4.4G 0 100% /dvd
-
2.3 编写本地 yum 源文件
- [root@proxy ~]$ ls /etc/yum.repos.d/
- CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
- CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo CentOS-x86_64-kernel.repo
- [root@proxy ~]$ rm -rf /etc/yum.repos.d/*
- [root@proxy ~]$ vim /etc/yum.repos.d/dvd.repo
- [App]
- name=AppStream
- baseurl=file:///dvd/AppStream
- enabled=1
- gpgcheck=0
- [Base]
- name=BaseOS
- baseurl=file:///dvd/BaseOS
- enabled=1
- gpgcheck=0
-
2.4 验证 yum 是否可用
- [root@web1 ~]$ yum makecache
- AppStream 64 MB/s | 7.8 MB 00:00
- BaseOS 49 MB/s | 2.6 MB 00:00
- 上次元数据过期检查:0:00:02 前,执行于 2024年03月25日 星期一 10时57分43秒。
- 元数据缓存已建立。
-
3.1 安装 gcc 编译器
- [root@proxy ~]$ yum -y install gcc
- 已加载插件:fastestmirror, langpacks
- Loading mirror speeds from cached hostfile
- 软件包 gcc-4.8.5-44.el7.x86_64 已安装并且是最新版本
- 无须任何处理
- [root@proxy ~]$ rpm -qa gcc
- gcc-4.8.5-44.el7.x86_64
-
3.2 安装 Memcached 服务
- [root@proxy ~]$ yum -y install memcached
- [root@proxy ~]$ rpm -qa memcached
- memcached-1.4.15-10.el7_3.1.x86_64
-
3.3 查看 Memcached 服务的服务单元文件
- [root@proxy ~]$ cat /usr/lib/systemd/system/memcached.service
- [Unit]
- Description=Memcached #描述了该服务单元的简要描述,即 Memcached
- Before=httpd.service #指定在启动 httpd.service 之前启动 Memcached 服务
- After=network.target #指定在网络服务加载完成后再启动 Memcached 服务
- [Service]
- Type=simple #指定服务的类型为简单类型,即服务不会进入后台,直到服务运行完成
- EnvironmentFile=-/etc/sysconfig/memcached #指定从 /etc/sysconfig/memcached 文件中读取环境变量
- ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS #指定启动 Memcached 服务时要执行的命令,其中 $USER、$PORT、$CACHESIZE、$MAXCONN 和 $OPTIONS 是从 /etc/sysconfig/memcached 文件中读取的环境变量
- [Install]
- WantedBy=multi-user.target #指定将该服务添加到 multi-user.target,表示在多用户模式下启用该服务
-
3.4 查看 Memcached 服务的配置文件
- [root@proxy ~]$ cat /etc/sysconfig/memcached
- PORT="11211" #指定 Memcached 服务监听的端口为 11211
- USER="memcached" #指定 Memcached 服务以 memcached 用户的身份运行
- MAXCONN="1024" #设置 Memcached 服务的最大连接数为 1024
- CACHESIZE="64" #将 Memcached 分配的内存缓存大小设置为 64MB
- OPTIONS="" #未提供其他特定选项的额外参数
-
3.5 启动 Memcached 服务
- [root@proxy ~]$ systemctl enable memcached.service --now
- Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /usr/lib/systemd/system/memcached.service.
- [root@proxy ~]$ systemctl status memcached.service
- ● memcached.service - Memcached
- Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
- Active: active (running) since 五 2024-03-22 15:53:04 CST; 8s ago
-
3.6 查看 Memcached 服务使用的端口信息
- [root@proxy ~]$ ss -anput | grep memcached
- udp UNCONN 0 0 *:11211 *:* users:(("memcached",pid=61363,fd=28))
- udp UNCONN 0 0 [::]:11211 [::]:* users:(("memcached",pid=61363,fd=29))
- tcp LISTEN 0 128 *:11211 *:* users:(("memcached",pid=61363,fd=26))
- tcp LISTEN 0 128 [::]:11211 [::]:* users:(("memcached",pid=61363,fd=27))
-
3.7 下载 telnet 远程登录服务
- [root@proxy ~]$ yum -y install telnet
- [root@proxy ~]$ rpm -qa telnet
- telnet-0.17-65.el7_8.x86_64
-
3.8 使用 telnet 远程登录连接 Memcached 服务的端口进行增删改查等功能测试
- [root@proxy ~]$ telnet 192.168.99.5 11211
- Trying 192.168.99.5...
- Connected to 192.168.99.5.
- Escape character is '^]'.
- #3.8.1 设置数据(增加/更新)
- #set key flags exptime bytes
- #value
- #key: 要设置的键名。
- #flags: 自定义标记,可用于存储额外信息。
- #exptime: 数据的过期时间,以秒为单位。
- #bytes: 数据的字节数。
- #value: 要存储的值。
- set name 0 60 3
- zdr
- STORED
- #3.8.2 获取数据(查询)
- #get key
- #key: 要获取值的键名。
- get name
- VALUE name 0 3
- zdr
- END
- #3.8.3 替换数据
- #replace key flags exptime bytes
- #value
- #与 set 命令类似,但只有在键存在时才会替换
- replace name 0 60 5
- zdrnb
- STORED
- get name
- VALUE name 0 5
- zdrnb
- END
- #3.8.4 删除数据
- #delete key
- #key: 要删除的键名
- delete name
- DELETED
- get name
- END
- #3.8.5 自增
- #incr key value
- #incr: 将键对应的值递增
- set num 0 500 1
- 1
- STORED
- get num
- VALUE num 0 1
- 1
- END
- incr num 1
- 2
- get num
- VALUE num 0 1
- 2
- END
- #3.8.6 自减
- #decr key value
- #decr: 将键对应的值递减。
- decr num 2
- 0
- get num
- VALUE num 0 1
- 0
- END
- #3.8.7 删除所有数据
- #flush_all
- #OK
- flush_all
- OK
- get num
- END
- get name
- END
-
4.1 下载安装 Nginx 会用到的依赖
- [root@proxy ~]$ yum -y install make pcre-devel openssl-devel
- [root@proxy ~]$ rpm -qa make pcre-devel openssl-devel
- make-3.82-24.el7.x86_64
- pcre-devel-8.32-17.el7.x86_64
- openssl-devel-1.0.2k-19.el7.x86_64
-
4.2 解压准备好的压缩包,并解压其中的 Nginx 压缩包
压缩包自行提取
链接:https://pan.baidu.com/s/16RQj0ueO7woF5ZExwIZhlg?pwd=iosx
提取码:iosx
- [root@proxy ~]$ ls
- lnmp_soft.tar.gz
- [root@proxy ~]$ tar -zxf lnmp_soft.tar.gz
- [root@proxy ~]$ cd lnmp_soft/
- [root@proxy lnmp_soft]$ tar -zxf nginx-1.17.6.tar.gz
-
4.3 进入 Nginx 目录,源码编译安装 Nginx
- [root@proxy lnmp_soft]$ cd nginx-1.23.2/
- [root@proxy nginx-1.23.2]$ ./configure && make && make install
-
4.4 启动 Nginx 查看 Nginx 端口信息
- [root@proxy nginx-1.23.2]$ /usr/local/nginx/sbin/nginx
- [root@proxy nginx-1.23.2]$ netstat -nplt | grep nginx
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 59474/nginx: master
-
4.5 修改 Nginx 配置文件,使 Nginx 能处理 php 脚本
- [root@proxy nginx-1.23.2]$ vim /usr/local/nginx/conf/nginx.conf
- 45 index index.php index.html index.htm; #指定了在该 location 下的默认索引文件顺序,即当访问一个目录时,Nginx 会按照 index.php -> index.html -> index.htm 的顺序寻找默认文件进行展示
- 65 location ~ \.php$ { #使用正则表达式匹配以 .php 结尾的 URL
- 66 root html; #指定 PHP 文件所在的根目录为 html
- 67 fastcgi_pass 127.0.0.1:9000; #定义请求转发给 127.0.0.1:9000 服务器处理
- 68 fastcgi_index index.php; #指定 FastCGI 服务器应该执行的 PHP 脚本的名称,默认是 index.php
- 69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- 70 include fastcgi.conf; #使用 include 指令引入 fastcgi.conf 文件,这个文件包含了关于 FastCGI 的一些常用配置
- 71 }
-
5.1 设置防火墙永久关闭
- [root@web1 ~]$ systemctl disable firewalld --now
- Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
- Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
- [root@proxy ~]$ systemctl status firewalld
- ● firewalld.service - firewalld - dynamic firewall daemon
- Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
- Active: inactive (dead)
- Docs: man:firewalld(1)
-
5.2 设置 selinux 为宽容模式,并修改配置文件为禁用
- [root@web1 ~]$ setenforce 0
- [root@web1 ~]$ getenforce
- Permissive
- [root@web1 ~]$ sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
-
6.1 挂载光盘到 /dvd 目录
- [root@web1 ~]$ mkdir /dvd
- [root@web1 ~]$ mount /dev/cdrom /dvd/
- mount: /dvd: WARNING: device write-protected, mounted read-only.
-
6.2 将光盘设置为开机自动挂载
- [root@web1 ~]$ vim /etc/fstab
- /dev/cdrom /dvd iso9660 defaults 0 0
- [root@web1 ~]$ mount -a
- [root@web1 ~]$ df -h /dvd/
- 文件系统 容量 已用 可用 已用% 挂载点
- /dev/sr0 11G 11G 0 100% /dvd
-
6.3 将 proxy 主机的 yum 源文件拷贝到本地
- [root@web1 ~]$ rm -rf /etc/yum.repos.d/*
- [root@web1 ~]$ scp root@192.168.99.5:/etc/yum.repos.d/dvd.repo /etc/yum.repos.d/dvd.repo
-
6.4 验证 yum 是否可用
- [root@web1 ~]$ yum makecache
- AppStream 64 MB/s | 7.8 MB 00:00
- BaseOS 49 MB/s | 2.6 MB 00:00
- 上次元数据过期检查:0:00:02 前,执行于 2024年03月25日 星期一 15时57分43秒。
- 元数据缓存已建立。
-
7.1 安装 Nginx
7.1.1 安装后续操作所需要的依赖环境
- [root@web1 ~]$ yum -y install gcc make pcre-devel openssl-devel
- [root@web1 ~]$ rpm -qa gcc make pcre-devel openssl-devel
- openssl-devel-1.1.1k-6.el8_5.x86_64
- gcc-8.5.0-10.el8.x86_64
- make-4.2.1-11.el8.x86_64
- pcre-devel-8.42-6.el8.x86_64
-
7.1.2 将 proxy 主机上解压好的文件夹拷贝到本机,源码编译安装 Nginx
- [root@web1 ~]$ scp -r root@192.168.99.5:/root/lnmp_soft lnmp_soft
- [root@web1 ~]$ cd lnmp_soft/nginx-1.23.2/
- [root@web1 nginx-1.23.2]$ ./configure && make && make install
-
7.1.3 启动 Nginx 查看 Nginx 端口信息
- [root@web1 nginx-1.23.2]$ /usr/local/nginx/sbin/nginx
- [root@web1 nginx-1.23.2]$ netstat -anplut | grep nginx
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45057/nginx: master
-
7.1.4 将 proxy 主机的 Nginx 配置文件拷贝至本机,替换原本的 Nginx 配置文件
- [root@web1 nginx-1.23.2]$ scp root@192.168.99.5:/usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
-
7.1.5 重启 Nginx 重新加载配置文件
- [root@web1 nginx-1.23.2]$ /usr/local/nginx/sbin/nginx -s reload
-
7.2 安装 Mariadb
7.2.1 安装 Mariadb 及相关所需服务环境
- [root@web1 nginx-1.23.2]$ yum -y install mariadb mariadb-server mariadb-devel
- [root@web1 nginx-1.23.2]$ rpm -qa install mariadb mariadb-server mariadb-devel
- mariadb-server-10.3.32-2.module+el8.5.0+777+18007c86.x86_64
- mariadb-10.3.32-2.module+el8.5.0+777+18007c86.x86_64
- mariadb-devel-10.3.32-2.module+el8.5.0+777+18007c86.x86_64
-
7.2.2 设置 Mariadb 开机自启动,并启动服务
- [root@web1 nginx-1.23.2]$ systemctl enable mariadb --now
- Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
- Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
- Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
- [root@web1 nginx-1.23.2]$ systemctl status mariadb.service
- ● mariadb.service - MariaDB 10.3 database server
- Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
- Active: active (running) since Mon 2024-03-25 16:15:56 CST; 23s ago
-
7.3 安装 PHP
7.3.1 安装 PHP 及相关所需服务
- [root@web1 nginx-1.23.2]$ yum -y install php php-mysqlnd php-fpm
- [root@web1 nginx-1.23.2]$ rpm -qa install php php-mysqlnd php-fpm
- php-fpm-7.2.24-1.module+el8.4.0+413+c9202dda.x86_64
- php-mysqlnd-7.2.24-1.module+el8.4.0+413+c9202dda.x86_64
- php-7.2.24-1.module+el8.4.0+413+c9202dda.x86_64
-
7.3.2 修改 php-fpm 的配置文件,使 php-fom 监听本地9000端口,可以接收来自 Web 服务器的请求,并处理 php 脚本的执行
- [root@web1 nginx-1.23.2]$ vim /etc/php-fpm.d/www.conf
- 38 listen = 127.0.0.1:9000
-
7.3.3 设置 php-fpm 开机自启动,并启动服务
- [root@web1 nginx-1.23.2]$ systemctl enable php-fpm.service --now
- Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
- [root@web1 nginx-1.23.2]$ systemctl status php-fpm.service
- ● php-fpm.service - The PHP FastCGI Process Manager
- Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
- Active: active (running) since Mon 2024-03-25 16:18:42 CST; 20s ago
-
7.4 编写 Nginx 测试页面,检验 Nginnx 是否能处理 php 脚本
- [root@web1 nginx-1.23.2]$ vim /usr/local/nginx/html/test.php
- <html>
- <body>
- This is web1
- </body>
- </html>
-
8.1 设置防火墙永久关闭
- [root@web2 ~]$ systemctl disable firewalld --now
- Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
- Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
- [root@proxy ~]$ systemctl status firewalld
- ● firewalld.service - firewalld - dynamic firewall daemon
- Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
- Active: inactive (dead)
- Docs: man:firewalld(1)
-
8.2 设置 selinux 为宽容模式,并修改配置文件为禁用
- [root@web1 ~]$ setenforce 0
- [root@web1 ~]$ getenforce
- Permissive
- [root@web2 ~]$ sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
-
9.1 挂载光盘到 /dvd 目录
- [root@web2 ~]$ mkdir /dvd
- [root@web2 ~]$ mount /dev/cdrom /dvd/
- mount: /dvd: WARNING: device write-protected, mounted read-only.
-
9.2 将光盘设置为开机自动挂载
- [root@web2 ~]$ vim /etc/fstab
- /dev/cdrom /dvd iso9660 defaults 0 0
- [root@web2 ~]$ mount -a
- [root@web2 ~]$ df -h /dvd/
- 文件系统 容量 已用 可用 已用% 挂载点
- /dev/sr0 11G 11G 0 100% /dvd
-
9.3 将 proxy 主机的 yum 源文件拷贝到本地
- [root@web2 ~]$ rm -rf /etc/yum.repos.d/*
- [root@web2 ~]$ scp root@192.168.99.5:/etc/yum.repos.d/dvd.repo /etc/yum.repos.d/dvd.repo
-
9.4 验证 yum 是否可用
- [root@web2 ~]$ yum makecache
- AppStream 64 MB/s | 7.8 MB 00:00
- BaseOS 49 MB/s | 2.6 MB 00:00
- 上次元数据过期检查:0:00:02 前,执行于 2024年03月25日 星期一 16时57分43秒。
- 元数据缓存已建立。
-
10.1 安装 Nginx
10.1.1 安装后续操作所需要的依赖环境
- [root@web2 ~]$ yum -y install gcc make pcre-devel openssl-devel
- [root@web2 ~]$ rpm -qa gcc make pcre-devel openssl-devel
- openssl-devel-1.1.1k-6.el8_5.x86_64
- gcc-8.5.0-10.el8.x86_64
- make-4.2.1-11.el8.x86_64
- pcre-devel-8.42-6.el8.x86_64
-
10.1.2 将 proxy 主机上解压好的文件夹拷贝到本机,源码编译安装 Nginx
- [root@web2 ~]$ scp -r root@192.168.99.5:/root/lnmp_soft lnmp_soft
- [root@web2 ~]$ cd lnmp_soft/nginx-1.23.2/
- [root@web2 nginx-1.23.2]$ ./configure && make && make install
-
10.1.3 启动 Nginx 查看 Nginx 端口信息
- [root@web2 nginx-1.23.2]$ /usr/local/nginx/sbin/nginx
- [root@web2 nginx-1.23.2]$ netstat -anplut | grep nginx
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45057/nginx: master
-
10.1.4 将 proxy 主机的 Nginx 配置文件拷贝至本机,替换原本的 Nginx 配置文件
- [root@web2 nginx-1.23.2]$ scp root@192.168.99.5:/usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
-
10.1.5 重启 Nginx 重新加载配置文件
- [root@web2 nginx-1.23.2]$ /usr/local/nginx/sbin/nginx -s reload
-
10.2 安装 Mariadb
10.2.1 安装 Mariadb 及相关所需服务环境
- [root@web2 nginx-1.23.2]$ yum -y install mariadb mariadb-server mariadb-devel
- [root@web2 nginx-1.23.2]$ rpm -qa install mariadb mariadb-server mariadb-devel
- mariadb-server-10.3.32-2.module+el8.5.0+777+18007c86.x86_64
- mariadb-10.3.32-2.module+el8.5.0+777+18007c86.x86_64
- mariadb-devel-10.3.32-2.module+el8.5.0+777+18007c86.x86_64
-
10.2.2 设置 Mariadb 开机自启动,并启动服务
- [root@web2 nginx-1.23.2]$ systemctl enable mariadb --now
- Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
- Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
- Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
- [root@web2 nginx-1.23.2]$ systemctl status mariadb.service
- ● mariadb.service - MariaDB 10.3 database server
- Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
- Active: active (running) since Mon 2024-03-25 16:15:56 CST; 23s ago
-
10.3 安装 PHP
10.3.1 安装 PHP 及相关所需服务
- [root@web2 nginx-1.23.2]$ yum -y install php php-mysqlnd php-fpm
- [root@web2 nginx-1.23.2]$ rpm -qa install php php-mysqlnd php-fpm
- php-fpm-7.2.24-1.module+el8.4.0+413+c9202dda.x86_64
- php-mysqlnd-7.2.24-1.module+el8.4.0+413+c9202dda.x86_64
- php-7.2.24-1.module+el8.4.0+413+c9202dda.x86_64
-
10.3.2 修改 php-fpm 的配置文件,使 php-fom 监听本地9000端口,可以接收来自 Web 服务器的请求,并处理 php 脚本的执行
- [root@web2 nginx-1.23.2]$ vim /etc/php-fpm.d/www.conf
- 38 listen = 127.0.0.1:9000
-
10.3.3 设置 php-fpm 开机自启动,并启动服务
- [root@web2 nginx-1.23.2]$ systemctl enable php-fpm.service --now
- Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
- [root@web2 nginx-1.23.2]$ systemctl status php-fpm.service
- ● php-fpm.service - The PHP FastCGI Process Manager
- Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
- Active: active (running) since Mon 2024-03-25 16:18:42 CST; 20s ago
-
10.4 编写 Nginx 测试页面,检验 Nginnx 是否能处理 php 脚本
- [root@web2 nginx-1.23.2]$ vim /usr/local/nginx/html/test.php
- <html>
- <body>
- This is web2
- </body>
- </html>
-
11.1 修改 Nginx 的配置文件实现反向代理功能
- [root@proxy ~]$ vim /usr/local/nginx/conf/nginx.conf
- 22 upstream webservers{ #定义了一个名为"webservers"的upstream组,Nginx会将请求负载均衡地分发给这群组中的服务器
- 23 server 192.168.99.100:80; #定义了第一台后端服务器的IP地址和端口
- 24 server 192.168.99.200:80; #定义了第二台后端服务器的IP地址和端口
- 25 }
- 51 proxy_pass http://webservers; #指定了请求应该被代理到的upstream组,即"webservers"
-
11.2 重启 Nginx 重新加载配置文件
- [root@proxy ~]$ /usr/local/nginx/sbin/nginx -s reload
-
12.1 解压压缩包,将解压后文件夹中的内容拷贝到本机 Nginx 网页目录下
- [root@web1 ~]$ cd lnmp_soft/php_scripts/
- [root@web1 php_scripts]$ tar -zxf php-memcached-demo.tar.gz
- [root@web1 php_scripts]$ cp -r php-memcached-demo/* /usr/local/nginx/html/
-
12.2 使用客户端浏览器搜索 web1 进行检验
12.3 登录后可以看到网页中的 Session ID 与 /var/lib/php/session/ 下的相同
- [root@web1 php_scripts]$ cd /var/lib/php/session/
- [root@web1 session]$ ls
- sess_rfbab61nkj653jbiunfnv64b09
-
12.4 将 web1 的页面颜色修改为橙色,方便之后与 web2 区分
- [root@web1 session]$ vim /usr/local/nginx/html/index.php
- 10 <body bgcolor=orange>
- [root@web1 session]$ vim /usr/local/nginx/html/home.php
- 15 <body bgcolor=orange>
-
13.1 解压压缩包,将解压后文件夹中的内容拷贝到本机 Nginx 网页目录下
- [root@web2 ~]$ cd lnmp_soft/php_scripts/
- [root@web2 php_scripts]$ tar -xf php-memcached-demo.tar.gz
- [root@web2 php_scripts]$ cp -r php-memcached-demo/* /usr/local/nginx/html/
-
13.2 使用客户端浏览器搜索 web2 进行检验
13.3 登录后可以看到网页中的 Session ID 与 /var/lib/php/session/ 下的相同
- [root@web2 php_scripts]$ cd /var/lib/php/session/
- [root@web2 session]$ ls
- sess_r7jdpa1ja7na75vq5dq1e41559
-
13.4 将 web2 的页面颜色修改为粉色,方便之后与 web1 区分
- [root@web2 session]$ vim /usr/local/nginx/html/index.php
- 10 <body bgcolor=pink>
- [root@web2 session]$ vim /usr/local/nginx/html/home.php
- 15 <body bgcolor=pink>
-
在客户端浏览器上访问 proxy(192.168.99.5),先不输入账号密码,点击刷新页面可以看到 web1 、web2 的橙色、粉色页面交替出现;输入账号密码登录时发现需要输入两次账号密码(先在 web1 橙色页面输入一次账号密码,点击登录后需要再在 web2 粉色页面输入一次账号密码)才能跳转到 home.php 页面。
到目前为止上述情况属于正常,是因为没有配置 Session 共享。
15.1 解压 memcache 扩展文件包
- [root@web1 ~]$ cd lnmp_soft/php_scripts/
- [root@web1 php_scripts]$ tar -zxf memcache-4.0.5.2.tar.gz
-
15.2 使用 phpize 命令为 php 模块编译和安装过程准备环境
- [root@web1 php_scripts]$ cd memcache-4.0.5.2/
- [root@web1 memcache-4.0.5.2]$ /usr/bin/phpize
- Configuring for:
- PHP Api Version: 20170718
- Zend Module Api No: 20170718
- Zend Extension Api No: 320170718
- [root@web1 memcache-4.0.5.2]$ ls
- acinclude.m4 cloudbuild.yaml config.h.in config.status config.w32 example.php LICENSE Makefile.global missing README
- aclocal.m4 config9.m4 config.log config.sub CREDITS include ltmain.sh Makefile.objects mkinstalldirs run-tests.php
- autom4te.cache config.guess config.m4 configure docker install-sh Makefile memcache.la modules tests
- build config.h config.nice configure.ac Dockerfile libtool Makefile.fragments memcache.php php7
-
15.3 源码编译安装 memcache 扩展,记住安装完成后给出的扩展路径
- [root@web1 memcache-4.0.5.2]$ ./configure && make && make install
- ......
- Installing shared extensions: /usr/lib64/php/modules/
- [root@web1 memcache-4.0.5.2]$ ll /usr/lib64/php/modules/ | grep memcache.so
- -rwxr-xr-x 1 root root 790120 3月 27 15:47 memcache.so
-
15.4 修改 php 配置文件在 php 中加载 memcache 扩展模块
- [root@web1 memcache-4.0.5.2]$ vim /etc/php.ini
- 870 extension=/usr/lib64/php/modules/memcache.so
-
15.5 修改 php 的会话 Session 存储方式,并重启 php-fpm 重新加载配置
- [root@web1 memcache-4.0.5.2]$ vim /etc/php-fpm.d/www.conf
- 431 php_value[session.save_handler] = memcache #使用 memcache 来存储会话数据
- 432 php_value[session.save_path] = "tcp://192.168.99.5:11211" #指定了 memcache 服务器的地址和端口
- [root@web1 memcache-4.0.5.2]$ systemctl restart php-fpm.service
-
15.6 修改 Nginx 配置文件,使 Nginx 能从 Memcached 中获取相应内容,并重启 Nginx 加载配置
- [root@web1 memcache-4.0.5.2]$ vim /usr/local/nginx/conf/nginx.conf
- 43 location / { #表示针对所有请求路径为 / 的请求进行配置
- 44 root html; #指定了根目录为 html,即指定了静态文件资源的根目录
- 45 index index.php index.html index.htm; # 指定了当访问目录时默认展示的文件,按照指定的顺序查找并显示第一个找到的文件
- 46 set $memcached_key "$uri"; #将 URI 赋值给变量 $memcached_key,用于在后续的 memcached_pass 中作为键名
- 47 memcached_pass 192.168.99.5:11211; #将请求转发到 Memcached 服务器 192.168.99.5 的 11211 端口,并使用之前设置的 $memcached_key 作为键名
- 48 error_page 404 /home.php; #指定当出现 404 错误时,将请求重定向到 /home.php 这个页面
- 49 }
- [root@web1 php-memcached-demo]$ /usr/local/nginx/sbin/nginx -s reload
-
16.1 解压 memcache 扩展文件包
- [root@web2 ~]$ cd lnmp_soft/php_scripts/
- [root@web2 php_scripts]$ tar -zxf memcache-4.0.5.2.tar.gz
-
16.2 使用 phpize 命令为 php 模块编译和安装过程准备环境
- [root@web2 php_scripts]$ cd memcache-4.0.5.2/
- [root@web2 memcache-4.0.5.2]$ /usr/bin/phpize
- Configuring for:
- PHP Api Version: 20170718
- Zend Module Api No: 20170718
- Zend Extension Api No: 320170718
- [root@web2 memcache-4.0.5.2]$ ls
- acinclude.m4 cloudbuild.yaml config.h.in config.status config.w32 example.php LICENSE Makefile.global missing README
- aclocal.m4 config9.m4 config.log config.sub CREDITS include ltmain.sh Makefile.objects mkinstalldirs run-tests.php
- autom4te.cache config.guess config.m4 configure docker install-sh Makefile memcache.la modules tests
- build config.h config.nice configure.ac Dockerfile libtool Makefile.fragments memcache.php php7
-
16.3 源码编译安装 memcache 扩展,记住安装完成后给出的扩展路径
- [root@web2 memcache-4.0.5.2]$ ./configure && make && make install
- ......
- Installing shared extensions: /usr/lib64/php/modules/
- [root@web2 memcache-4.0.5.2]$ ll /usr/lib64/php/modules/ | grep memcache.so
- -rwxr-xr-x 1 root root 790120 3月 27 15:47 memcache.so
-
16.4 修改 php 配置文件在 php 中加载 memcache 扩展模块
- [root@web2 memcache-4.0.5.2]$ vim /etc/php.ini
- 870 extension=/usr/lib64/php/modules/memcache.so
-
16.5 修改 php 的会话 Session 存储方式,并重启 php-fpm 重新加载配置
- [root@web2 memcache-4.0.5.2]$ vim /etc/php-fpm.d/www.conf
- 431 php_value[session.save_handler] = memcache #使用 memcache 来存储会话数据
- 432 php_value[session.save_path] = "tcp://192.168.99.5:11211" #指定了 memcache 服务器的地址和端口
- [root@web2 memcache-4.0.5.2]$ systemctl restart php-fpm.service
-
16.6 修改 Nginx 配置文件,使 Nginx 能从 Memcached 中获取相应内容,并重启 Nginx 加载配置
- [root@web2 memcache-4.0.5.2]$ vim /usr/local/nginx/conf/nginx.conf
- 43 location / { #表示针对所有请求路径为 / 的请求进行配置
- 44 root html; #指定了根目录为 html,即指定了静态文件资源的根目录
- 45 index index.php index.html index.htm; # 指定了当访问目录时默认展示的文件,按照指定的顺序查找并显示第一个找到的文件
- 46 set $memcached_key "$uri"; #将 URI 赋值给变量 $memcached_key,用于在后续的 memcached_pass 中作为键名
- 47 memcached_pass 192.168.99.5:11211; #将请求转发到 Memcached 服务器 192.168.99.5 的 11211 端口,并使用之前设置的 $memcached_key 作为键名
- 48 error_page 404 /home.php; #指定当出现 404 错误时,将请求重定向到 /home.php 这个页面
- 49 }
- [root@web2 php-memcached-demo]$ /usr/local/nginx/sbin/nginx -s reload
-
使用客户端的浏览器访问 proxy(192.168.99.5)可以看到是 web1 的橙色页面
不输入账号密码点击刷新可以看到页面切换为 web2 的粉色页面
在任意颜色页面的用户名输入 “ root ” 密码输入 “ 123456 ” ,只需登录一次就能够查看到 Session 信息,点击刷新页面能够看到 web1 、web2 页面交替出现,并且 Session ID 不变