8、利用LNMP实现wordpress站点搭建

8、利用LNMP实现wordpress站点搭建

#部署规划:

10.0.0.7Nginx php-fpm 运行web服务

10.0.0.17:运行MySQL数据库,Redis服务

 

 

6.3.5.1 部署数据库

10.0.0.17主机部署MySQL服务

6.3.5.1.1 二进制部署MySQL数据库

 

[root@centos7 ~]# ls

anaconda-ks.cfg       install_mysql5.7or8.0_for_centos.sh         reset_centos.sh

initial-setup-ks.cfg  mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz

 

 

[root@centos7 ~]# vim install_mysql5.7or8.0_for_centos.sh

#!/bin/bash

. /etc/init.d/functions

SRC_DIR=`pwd`

MYSQL='mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz'                                         

COLOR='echo -e \E[01;31m'

END='\E[0m'

MYSQL_ROOT_PASSWORD=magedu

 

check (){

if [ $UID -ne 0 ]; then

  action "当前用户不是root,安装失败" false

  exit 1

fi

cd  $SRC_DIR

if [ !  -e $MYSQL ];then

        $COLOR"缺少${MYSQL}文件"$END

        $COLOR"请将相关软件放在${SRC_DIR}目录下"$END

        exit

elif [ -e /usr/local/mysql ];then

        action "数据库已存在,安装失败" false

        exit

else

    return

fi

}

 

install_mysql(){

    $COLOR"开始安装MySQL数据库..."$END

    yum  -y -q install libaio numactl-libs   libaio &> /dev/null

    cd $SRC_DIR

    tar xf $MYSQL -C /usr/local/

    MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`

    ln -s  /usr/local/$MYSQL_DIR /usr/local/mysql

    chown -R  root.root /usr/local/mysql/

    id mysql &> /dev/null || { useradd -s /sbin/nologin -r  mysql ; action "创建mysql用户"; }

 

    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh

    .  /etc/profile.d/mysql.sh

    ln -s /usr/local/mysql/bin/* /usr/bin/

    cat > /etc/my.cnf <<-EOF

[mysqld]

server-id=1

log-bin

datadir=/data/mysql

socket=/data/mysql/mysql.sock                                                                                                   

log-error=/data/mysql/mysql.log

pid-file=/data/mysql/mysql.pid

[client]

socket=/data/mysql/mysql.sock

EOF

mysqld --initialize --user=mysql --datadir=/data/mysql

   cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld

    chkconfig --add mysqld

    chkconfig mysqld on

    service mysqld start

    [ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }

    MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`

    mysqladmin  -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/null

    action "数据库安装完成"

}

 

check

install_mysql  

 

[root@centos7 ~]# bash install_mysql5.7or8.0_for_centos.sh

开始安装MySQL数据库...

创建mysql用户                                              [  OK  ]

Starting MySQL.. SUCCESS!

数据库安装完成                                             [  OK  ]

 

 

 

6.3.5.1.2 创建wordpress数据库和用户并授权

[root@centos7 ~]# mysql -uroot -pmagedu

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 9

Server version: 8.0.19 MySQL Community Server - GPL

 

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

 

mysql> create database wordpress;

Query OK, 1 row affected (0.04 sec)

 

mysql> create user wordpress@'10.0.0.%' identified by '123456';

Query OK, 0 rows affected (0.01 sec)

 

mysql> grant all on wordpress.* to wordpress@'10.0.0.%';

Query OK, 0 rows affected (0.00 sec)

 

6.3.5.1.3 验证MySQL账户权限

WordPress服务器使用授权的MySQL账户远程登录测试权限

先装个mysql客户端

 

[root@centos7 ~]# hostname -I

10.0.0.7  

[root@centos7 ~]# mysql -uwordpress -p123456 -h10.0.0.17

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 10

Server version: 8.0.19 MySQL Community Server - GPL

 

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| wordpress          |

+--------------------+

2 rows in set (0.02 sec)

 

 

 

6.3.5.2 部署PHP

10.0.0.7主机部署php-fpm服务

6.3.5.2.1 编译安装 php

[root@centos7 ~]# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel

[root@centos7 ~]# cd /usr/local/src/

[root@centos7 src]# ll -h php-7.4.30.tar.xz

-rw-r--r-- 1 root root 10M Aug 14  2022 php-7.4.30.tar.xz

 

[root@centos7 src]# tar xf php-7.4.30.tar.xz

[root@centos7 src]# cd php-7.4.30/

[root@centos7 php-7.4.30]# mkdir /apps

 

[root@centos7 php-7.4.30]# ./configure --prefix=/apps/php74 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo

 

......

 

+--------------------------------------------------------------------+

| 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@centos7 php-7.4.30]# make && make install

 

 

 

 

6.3.5.2.2 准备 php 配置文件

#生成配置文件

[root@centos7 php-7.4.11]#cp /usr/local/src/php-7.4.11/php.ini-production

/etc/php.ini

[root@centos7 php-7.4.11]#cd /apps/php74/etc

[root@centos7 etc]#cp php-fpm.conf.default php-fpm.conf

[root@centos7 php-7.4.11]#cd php-fpm.d/

[root@centos7 php-fpm.d]#cp www.conf.default www.conf

[root@centos7 php-fpm.d]#vim www.conf

[root@centos7 php-fpm.d]#grep '^[^;]' www.conf

[www]

user = www

group = www

listen = 127.0.0.1:9000

pm = dynamic

pm.max_children = 5

pm.start_servers = 2

pm.min_spare_servers = 1

pm.max_spare_servers = 3

pm.status_path = /pm_status

ping.path = /ping

access.log = log/$pool.access.log

slowlog = log/$pool.log.slow

#创建用户

[root@centos7 php-fpm.d]#useradd -r -s /sbin/nologin www

#创建访问日志文件路径

[root@centos7 php-fpm.d]#mkdir /apps/php74/log

 

 

 

6.3.5.2.3 启动并验证 php-fpm 服务

[root@centos7 php-fpm.d]# /apps/php74/sbin/php-fpm -t

[14-Aug-2022 21:22:26] NOTICE: configuration file /apps/php74/etc/php-fpm.conf test is successful

[root@centos7 php-fpm.d]# cp /usr/local/src/php-7.4.30/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

[root@centos7 php-fpm.d]# systemctl daemon-reload

[root@centos7 php-fpm.d]# systemctl enable --now php-fpm

Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

 

[root@centos7 php-fpm.d]# ss -ntl

State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              

LISTEN      0      128               *:22                            *:*                  

LISTEN      0      128       127.0.0.1:631                           *:*                  

LISTEN      0      100       127.0.0.1:25                            *:*                  

LISTEN      0      128       127.0.0.1:6010                          *:*                  

LISTEN      0      128       127.0.0.1:6011                          *:*                  

LISTEN      0      128       127.0.0.1:6012                          *:*                  

LISTEN      0      128       127.0.0.1:9000                          *:*                  

LISTEN      0      128               *:111                           *:*                  

LISTEN      0      128            [::]:22                         [::]:*                  

LISTEN      0      128           [::1]:631                        [::]:*                  

LISTEN      0      100           [::1]:25                         [::]:*                  

LISTEN      0      128           [::1]:6010                       [::]:*                  

LISTEN      0      128           [::1]:6011                       [::]:*                  

LISTEN      0      128           [::1]:6012                       [::]:*                  

LISTEN      0      80             [::]:3306                       [::]:*                  

LISTEN      0      128            [::]:111                        [::]:*          

 

[root@centos7 php-fpm.d]# pstree -p | grep php

           |-php-fpm(20416)-+-php-fpm(20419)

           |                `-php-fpm(20420)

 

[root@centos7 php-fpm.d]# ps -ef | grep php

root     20416     1  0 21:25 ?        00:00:00 php-fpm: master process (/apps/php74/etc/php-fpm.conf)

nobody   20419 20416  0 21:25 ?        00:00:00 php-fpm: pool www

nobody   20420 20416  0 21:25 ?        00:00:00 php-fpm: pool www

root     20441 20227  0 21:26 pts/2    00:00:00 grep --color=auto php

 

 

 

6.3.5.3 部署 Nginx

10.0.0.7主机部署nginx服务

 

6.3.5.3.1 编译安装 nginx

[root@centos7 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel

[root@centos7 ~]#cd /usr/local/src/

[root@centos7 src]# ls

nginx-1.18.0.tar.gz  php-7.4.30  php-7.4.30.tar.xz

[root@centos7 src]#tar xf nginx-1.18.0.tar.gz

[root@centos7 src]#cd nginx-1.18.0/

[root@centos7 nginx-1.18.0]#./configure --prefix=/apps/nginx \

--user=www \

--group=www \

--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

[root@centos7 nginx-1.18.0]#make && make install

 

 

 

6.3.5.3.2 准备服务文件并启动 nginx

[root@centos7 src]# vim /usr/lib/systemd/system/nginx.service

[Unit]

Description=nginx - high performance web server

Documentation=http://nginx.org/en/docs/

After=network-online.target remote-fs.target nss-lookup.targ

Wants=network-online.target

 

[Service]

Type=forking

PIDFile=/apps/nginx/run/nginx.pid

ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s TERM $MAINPID

 

[Install]

WantedBy=multi-user.target

 

[root@centos7 src]# mkdir /apps/nginx/run/

[root@centos7 src]# vim /apps/nginx/conf/nginx.conf

[root@centos7 src]# systemctl daemom-reload

Unknown operation 'daemom-reload'.

[root@centos7 src]# systemctl daemon-reload

[root@centos7 src]# systemctl enable --now nginx

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

[root@centos7 src]# ss -ntl

State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              

LISTEN      0      128               *:80                            *:*                  

LISTEN      0      128               *:22                            *:*                  

LISTEN      0      128       127.0.0.1:631                           *:*                  

LISTEN      0      100       127.0.0.1:25                            *:*                  

LISTEN      0      128       127.0.0.1:6010                          *:*                  

LISTEN      0      128       127.0.0.1:6011                          *:*                  

LISTEN      0      128       127.0.0.1:6012                          *:*                  

LISTEN      0      128       127.0.0.1:6013                          *:*                  

LISTEN      0      128       127.0.0.1:9000                          *:*                  

LISTEN      0      128               *:111                           *:*                  

LISTEN      0      128            [::]:22                         [::]:*  

 

 

 

6.3.5.3.3 配置 Nginx 支持 fastcgi

 

[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf

 

[root@centos7 src]# grep -Ev '#|^$' /apps/nginx/conf/nginx.conf

worker_processes  1;

pid     /apps/nginx/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name www.magedu.org;

        location / {

            root /data/nginx/wordpress;

            index index.php index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        location ~ \.php$ {

            root    /data/nginx/wordpress;

            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 PATH_TRANSLATED $document_root$fastcgi_script_name;

        }

    }

}

 

[root@centos7 src]#  /apps/nginx/sbin/nginx -t

nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /apps/nginx/conf/nginx.conf test is successful

 

[root@centos7 src]# systemctl reload nginx.service

 

 

 

 

6.3.5.3.4 准备 php 测试页

[root@centos7 src]# mkdir /data/nginx/wordpress -pv

mkdir: created directory ‘/data/nginx’

mkdir: created directory ‘/data/nginx/wordpress’

[root@centos7 src]# vim /data/nginx/wordpress/test.php

 

[root@centos7 src]# cat /data/nginx/wordpress/test.php

<?php

phpinfo();

?>

 

 

 

 

 

6.3.5.4 部署 WordPress

10.0.0.7主机部署 wordpress

6.3.5.4.1 准备 WordPress 文件

[root@centos7 ~]#tar xf wordpress-5.4.2.tar.gz

 

[root@centos7 ~]#cp -r wordpress/* /data/nginx/wordpress

 

[root@centos7 ~]#chown -R www.www /data/nginx/wordpress/

 

 

 

 

 

 

 

 

解决方法:(实验中)

[root@centos7 wordpress]# chmod 777 *.php

 

 

 

 

 

 

 

 

6.3.5.4.3 登录后台管理界面并发表文章

 

 

 

 

 

 

 

 

 

 

 

解决方法:

[root@centos7 wordpress]# chmod 777 wp-content/

 

效果如下:

 

 

 

#可以看到上传的图片

[root@centos7 wordpress]# tree /data/nginx/wordpress/wp-content/uploads/

/data/nginx/wordpress/wp-content/uploads/

└── 2022

    └── 08

        └── 200773118462697089.gif

 

2 directories, 1 file

 

 

 

6.3.5.4.5 配置允许上传大文件

#注意:默认只支持1M以下文件上传,要利用php程序上传大图片,还需要修改下面三项配置,最大上传由三项值

的最小值决定

#直接上传大于1M文件,会出现下面413错误

[root@centos7 ~]#tail -f /apps/nginx/logs/access.log

10.0.0.1 - - [27/Nov/2020:12:21:16 +0800] "POST /wp-admin/async-upload.php

HTTP/1.1" 413 585 "http://10.0.0.7/wp-admin/upload.php" "Mozilla/5.0 (Windows NT

10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67

Safari/537.36 Edg/87.0.664.47"

 

#nginx上传文件大小限制

[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf

server {

      client_max_body_size 10m; #默认值为1M

.....

#php上传文件大小限制

[root@centos7 ~]#vim /etc/php.ini

post_max_size = 30M   #默认值为8M

upload_max_filesize = 20M  #默认值为2M

 

 

[root@centos7 ~]#systemctl restart nginx php-fpm

 

 

6.3.5.4.6 安全加固

[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf

[root@centos7 ~]#grep -Ev '#|^$' /apps/nginx/conf/nginx.conf

worker_processes  1;

pid       /apps/nginx/run/nginx.pid;

events {

  worker_connections  1024;

}

http {

  include       mime.types;

  default_type application/octet-stream;

  sendfile       on;   

  keepalive_timeout  65;

  server {

      listen       80;

      server_name www.magedu.org;

      server_tokens off; #添加此行

      location / {

          root   /data/nginx/wordpress;

          index index.php index.html index.htm;

      }

      error_page   500 502 503 504 /50x.html;

      location = /50x.html {

          root   html;

      }

      location ~ \.php$ {

          root           /data/nginx/wordpress;

          fastcgi_pass   127.0.0.1:9000;

          fastcgi_index index.php;

          fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;

          include       fastcgi_params;

          fastcgi_hide_header X-Powered-By;  #添加此行

      }

      location ~ ^/(ping|pm_status)$ {

          include fastcgi_params;

          fastcgi_pass 127.0.0.1:9000;

          fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;

      }

  }

}

[root@centos7 ~]#systemctl reload nginx

 

 

 

 

6.3.5.4.7 配置 php 开启 opcache 加速

10.0.0.7主机进行以下修改配置

#编辑php.ini配置文件

[root@centos7 ~]#vim /etc/php.ini

[opcache]

; Determines if Zend OPCache is enabled

zend_extension=opcache.so                                                       

                            opcache.enable=1

.....

[root@centos7 ~]#systemctl restart php-fpm

#访问测试页确认开启opcache加速

 

 

 

6.3.5.5 PHP 扩展session模块支持redis

PECLPHP 扩展的存储库,提供用于下载和开发 PHP 扩展的所有已知扩展和托管功能的目录

官方链接: http://pecl.php.net/package-stats.php

github: https://github.com/phpredis/phpredis

github安装文档: https://github.com/phpredis/phpredis/blob/develop/INSTALL.markdown

开始在 PHP 中使用 Redis 前, 需要确保已经安装了 redis 服务及 PHP redis 驱动,

PHP redis 驱动下载地址为:https://github.com/phpredis/phpredis/releases

 

 

 

6.3.5.5.1 编译安装phpredis模块

10.0.0.7主机进行以下编译安装phpredis模块

[root@centos7 ~]# cd /usr/local/src

[root@centos7 src]# ls

nginx-1.18.0  nginx-1.18.0.tar.gz  php-7.4.30  php-7.4.30.tar.xz

[root@centos7 src]# rz -E

rz waiting to receive.

[root@centos7 src]# ls

nginx-1.18.0  nginx-1.18.0.tar.gz  php-7.4.30  php-7.4.30.tar.xz  phpredis-5.3.3.tar.gz

[root@centos7 src]# tar xv php

php-7.4.30/            php-7.4.30.tar.xz      phpredis-5.3.3.tar.gz

[root@centos7 src]# tar xf phpredis-5.3.3.tar.gz

[root@centos7 src]# ls

nginx-1.18.0         php-7.4.30         phpredis-5.3.3

nginx-1.18.0.tar.gz  php-7.4.30.tar.xz  phpredis-5.3.3.tar.gz

[root@centos7 src]# mv phpredis-5.3.3 redis-5.3.3

[root@centos7 src]# ls

nginx-1.18.0         php-7.4.30         phpredis-5.3.3.tar.gz

nginx-1.18.0.tar.gz  php-7.4.30.tar.xz  redis-5.3.3

[root@centos7 src]# cd redis-5.3.3/

[root@centos7 redis-5.3.3]#

[root@centos7 redis-5.3.3]# ls

arrays.markdown    debian             README.markdown     redis_sentinel.h

Changelog.md       debian.control     redis_array.c       redis_session.c

cluster_library.c  INSTALL.markdown   redis_array.h       redis_session.h

cluster_library.h  ISSUE_TEMPLATE.md  redis_array_impl.c  rpm

cluster.markdown   liblzf             redis_array_impl.h  sentinel_library.c

common.h           library.c          redis.c             sentinel_library.h

config.m4          library.h          redis_cluster.c     sentinel.markdown

config.w32         mkdeb-apache2.sh   redis_cluster.h     serialize.list

COPYING            mkdeb.sh           redis_commands.c    tests

crc16.h            package.xml        redis_commands.h

CREDITS            php_redis.h        redis_sentinel.c

 

#如果是yum安装php,需要执行yum -y install php-cli php-devel

#以下为编译安装php的对应方式

[root@centos7 redis-5.3.3]# /apps/php74/bin/phpize

Configuring for:

PHP Api Version:         20190902

Zend Module Api No:      20190902

Zend Extension Api No:   320190902

Cannot find autoconf. Please check your autoconf installation and the #报错提示

$PHP_AUTOCONF environment variable. Then, rerun this script.

[root@centos7 redis-5.3.1]#yum -y install autoconf

#重新执行成功

[root@centos7 redis-5.3.3]# /apps/php74/bin/phpize

Configuring for:

PHP Api Version:         20190902

Zend Module Api No:      20190902

Zend Extension Api No:   320190902

#查看生成configure脚本

 

[root@centos7 redis-5.3.3]# ls

arrays.markdown    config.w32         mkdeb.sh            redis_commands.h

autom4te.cache     COPYING            package.xml         redis_sentinel.c

build              crc16.h            php_redis.h         redis_sentinel.h

Changelog.md       CREDITS            README.markdown     redis_session.c

cluster_library.c  debian             redis_array.c       redis_session.h

cluster_library.h  debian.control     redis_array.h       rpm

cluster.markdown   INSTALL.markdown   redis_array_impl.c  run-tests.php

common.h           ISSUE_TEMPLATE.md  redis_array_impl.h  sentinel_library.c

config.h.in        liblzf             redis.c             sentinel_library.h

config.m4          library.c          redis_cluster.c     sentinel.markdown

configure          library.h          redis_cluster.h     serialize.list

configure.ac       mkdeb-apache2.sh   redis_commands.c    tests

 

 

 

 

#如果是基于yum安装php,无需指定--with-php-config

[root@centos7 redis-5.3.3]#

 ./configure--with-php-config=/apps/php74/bin/php-config

 

[root@centos7 redis-5.3.3]# make -j 2 && make install

......

----------------------------------------------------------------------

 

Build complete.

Don't forget to run 'make test'.

 

Installing shared extensions:     /apps/php74/lib/php/extensions/no-debug-zts-20190902/

 

#验证redis模块

#yum安装php,模块文件默认存放在 /usr/lib64/php/modules/redis.so

[root@centos7 redis-5.3.3]# ll /apps/php74/lib/php/extensions/no-debug-zts-20190902/

total 9600

-rwxr-xr-x 1 root root 4652492 Aug 14 21:14 opcache.a

-rwxr-xr-x 1 root root 2518544 Aug 14 21:14 opcache.so

-rwxr-xr-x 1 root root 2658176 Aug 15 00:40 redis.so

 

 

 

6.3.5.5.2 编辑php配置文件支持redis

10.0.0.7主机进行以下修改配置

#编辑php.ini配置文件,扩展redis.so模块

[root@centos7 ~]#vim /etc/php.in

#编辑php.ini配置文件,扩展redis.so模块

[root@centos7 ~]#vim /etc/php.ini

.....

#extension=/apps/php74/lib/php/extensions/no-debug-zts-20190902/redis.so

extension=redis.so    #文件最后一行添加此行,路径可省略

[root@centos7 ~]#systemctl restart php-fpm

 

6.3.5.5.3 验证加载 redis 模块

访问测试页确认redis模块开启

 

 

6.3.5.5.4 安装和配置 redis 服务

#10.0.0.17主机安装redis服务

[root@centos7 ~]#yum -y install redis

[root@centos7 ~]#vim /etc/redis.conf

bind 0.0.0.0

requirepass 123456

[root@centos7 ~]# systemctl enable --now redis

[root@centos7 ~]# ss -ntl

State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              

LISTEN      0      128               *:111                           *:*                  

LISTEN      0      128               *:22                            *:*                  

LISTEN      0      128       127.0.0.1:631                           *:*                  

LISTEN      0      100       127.0.0.1:25                            *:*                  

LISTEN      0      128       127.0.0.1:6010                          *:*                  

LISTEN      0      128       127.0.0.1:6011                          *:*                  

LISTEN      0      128       127.0.0.1:6379                          *:*                  

LISTEN      0      128            [::]:111                        [::]:*                  

LISTEN      0      128            [::]:22                         [::]:*                  

LISTEN      0      128           [::1]:631                        [::]:*                  

LISTEN      0      100           [::1]:25                         [::]:*                  

LISTEN      0      128           [::1]:6010                       [::]:*                  

LISTEN      0      128           [::1]:6011                       [::]:*                  

LISTEN      0      70             [::]:33060                      [::]:*                  

LISTEN      0      128            [::]:3306                       [::]:*    

 

 

6.3.5.5.5 配置 php 支持 redis 保存 session

10.0.0.7 主机进行以下配置

 #10.0.0.7主机配置phpsession保存在redis服务

[root@centos7 ~]#vim /etc/php.ini

[Session]

; Handler used to store/retrieve data.

; http://php.net/session.save-handler

session.save_handler = redis

session.save_path = "tcp://10.0.0.17:6379?auth=123456"  

 

[root@centos7 ~]#systemctl restart php-fpm

 

 

http://www.magedu.org/test.php

 

 

 

6.3.5.5.6 准备 php实现 session 的测试页面

10.0.0.7主机进行准备相关文件

[root@centos7 ~]#cat /data/nginx/wordpress/session.php

<?php

session_start();

//redissession_id作为key 并且是以string的形式存储

$redisKey = 'PHPREDIS_SESSION:' . session_id();

// SESSION 赋值测试

$_SESSION['message'] = "Hello, I'm in redis";

$_SESSION['arr'] = [1, 2, 3, 4, 5, 6];

echo $_SESSION["message"] , "<br/>";

echo "Redis key =   " . $redisKey . "<br/>";

echo "以下是从Redis获取的数据", "<br/>";

// 取数据'

$redis = new Redis();

$redis->connect('10.0.0.17', 6379);

$redis->auth('123456');

 

echo $redis->get($redisKey);

?>

 

 

6.3.5.5.7 访问 web 页面测试实现session保存在redis服务

 

 

6.3.5.5.8 redis 主机验证 session 数据

10.0.0.17主机进行验证

 

[root@centos7 ~]# redis-cli -h 10.0.0.17 -a 123456

Could not connect to Redis at 10.0.0.17:6379: Connection refused

Could not connect to Redis at 10.0.0.17:6379: Connection refused

not connected>

 

改改

[root@centos7 ~]# vim /etc/redis.conf

# bind 192.168.1.100 10.0.0.1

#bind 127.0.0.1 ::1

bind 0.0.0.0                                                                               

requirepass 123456

 

#bind 127.0.0.1

 

[root@centos7 ~]# systemctl restart redis

 

 

[root@centos7 ~]# redis-cli -h 10.0.0.17 -a 123456

10.0.0.17:6379>keys *

1) "PHPREDIS_SESSION:ad3nujfqfvcltkg2ml4snsf72h"

10.0.0.17:6379> get PHPREDIS_SESSION:ad3nujfqfvcltkg2ml4snsf72h

"message|s:19:\"Hello, I'm in redis\";arr|a:6:

{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;}"

 

posted @   惊起千层浪  阅读(222)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示