一、LNMP/LAMP架构概述1.1 组件定义LAMP由Linux操作系统、ApacheWeb服务器、Mysql/Mariadb数据库、PHP/Python/Perl脚本语言组成的经典Web服务架构提供动态网页运行环境。LNMP将LAMP中的Apache替换为Nginx的轻量级Web服务架构具备更高的并发处理能力是目前主流的电商平台部署架构。1.2 核心组件作用组件核心作用Linux提供程序运行的底层操作系统环境Apache/Nginx接收客户端HTTP请求提供网页访问服务Mysql/Mariadb存储和管理电商平台的业务数据商品、订单、用户等PHP/Python/Perl执行动态脚本生成动态网页内容二、LNMP工作原理LNMP架构的请求处理流程分为3个核心步骤客户端通过HTTP协议向Web服务器发起资源请求如访问商城首页、提交订单Web服务器根据请求资源类型分流处理静态资源如图片、HTML、CSS、JSWeb服务器直接读取文件并返回给客户端动态资源如PHP脚本Web服务器通过FastCGI协议将脚本请求转发给后端PHP程序执行若PHP脚本需要访问数据库会通过MySQL连接器连接MariaDB完成数据查询/写入后PHP将运算结果返回给Web服务器Web服务器将最终处理结果动态生成的网页内容返回给客户端。三、实验环境规划3.1 服务器节点配置主机名IP地址角色核心作用shop.liu.cloud10.1.8.21Nginx服务器接收客户端请求转发动态请求至PHP节点php.liu.cloud10.1.8.22PHP服务器执行PHP脚本处理动态业务逻辑db.liu.cloud10.1.8.23MariaDB服务器存储电商平台所有业务数据nfs.liu.cloud10.1.8.24NFS服务器共享ECShop应用程序文件保证Web/PHP节点文件一致性前置要求所有节点需关闭防火墙和SELinux避免端口/权限拦截。四、全节点预配置4.1 配置主机名解析为所有节点配置本地hosts解析避免DNS解析问题保证节点间通信# 登录shop节点编辑hosts文件[rootshop ~ 09:29:22]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.1.8.21 shop.liu.cloud shop# Nginx节点10.1.8.22 php.liu.cloud php# PHP节点10.1.8.23 db.liu.cloud db# MariaDB节点10.1.8.24 nfs.liu.cloud nfs# NFS节点4.2 配置免密登录批量操作为 shop 节点配置对所有节点的免密登录简化后续批量操作# 生成RSA密钥对无密码[rootshop ~ 09:34:53]# ssh-keygen -t rsa -N -f ~/.ssh/id_rsa# 批量推送公钥至所有节点密码123[rootshop ~ 09:44:24]# for host in 10.1.8.{21,22,23,24}; do sshpass -p123 ssh-copy-id root$host; done# 批量同步hosts文件至所有节点[rootshop ~ 09:45:06]# for host in 10.1.8.{21,22,23,24};do scp /etc/hosts root$host:/etc/hosts;done# 验证hosts配置结果[rootshop ~ 09:45:40]# for host in shop php db nfs;do echo $host ; ssh root$host cat /etc/hosts;done五、部署 NFS 文件服务器nfs 节点5.1 部署目的ECShop 的应用程序文件需要同时被 Nginx读取静态文件和 PHP执行动态脚本访问通过 NFS 共享可保证所有节点的文件版本一致避免文件同步问题。5.2 安装并配置 NFS 服务# 安装NFS服务端工具[rootnfs ~ 09:31:07]# yum install -y nfs-utils# 创建NFS共享目录[rootnfs ~ 09:31:39]# mkdir /www# 配置NFS共享规则允许10.1.8.0/24网段读写访问/www目录[rootnfs ~ 09:31:54]# echo /www 10.1.8.0/24(rw) /etc/exports# 启动并开机自启NFS服务[rootnfs ~ 09:32:12]# systemctl enable nfs-server.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.5.3 准备 ECShop 应用及测试文件# 安装wget工具用于下载ECShop安装包[rootnfs ~ 09:32:55]# yum install -y wget# 下载ECShop V4.1.20安装包UTF8编码版[rootnfs ~ 09:33:27]# wget https://www.liu.cloud/course-materials/softwares/stage01/ECShop_V4.1.20_UTF8.zip# 解压安装包[rootnfs ~ 09:41:40]# unzip ECShop_V4.1.20_UTF8.zip# 将ECShop核心程序复制到NFS共享目录[rootnfs ~ 09:49:04]# cp -a ECShop_V4.1.20_UTF8_release20250416/source/ecshop /www# 创建静态测试文件验证NFS共享和Nginx静态资源访问[rootnfs ~ 09:50:02]# echo Hello World ! /www/test.html# 创建PHP基础测试文件验证PHP环境[rootnfs ~ 09:51:42]# cat /www/test.php EOF?phpechoh1Hello World !/h1\n;// 输出HTML格式的Hello World ?EOF# 创建PHP信息测试文件查看PHP环境配置[rootnfs ~ 09:52:18]# cat /www/info.php EOF?php phpinfo();// 输出PHP的详细配置信息 ?EOF# 创建PHP连接MySQL测试文件验证PHP与MariaDB通信[rootnfs ~ 09:53:45]# cat /www/test-mysql.php EOF?php$linkmysqli_connect(db.liu.cloud,ecshop,123);// 连接MariaDB节点 if($link)echoh1Connect Mysql Success !/h1\n;// 连接成功提示elseechoh1Connect Mysql Failed !/h1\n;// 连接失败提示$link-close();// 关闭数据库连接 ?EOF5.4 调整文件权限Nginx 进程以 nginx 用户运行需将 NFS 共享目录权限调整为 nginx 用户 / 组避免权限拒绝# 先从shop节点查询nginx用户的UID/GID保证权限一致[rootshop ~ 09:51:01]# yum install -y nginx[rootshop ~ 09:52:57]# grep nginx /etc/passwdnginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin# UID997GID995# 回到nfs节点调整/www目录权限[rootnfs ~10:05:36]# chown -R 997:995 /www六、部署 MariaDB 数据库服务器db 节点6.1 安装并启动 MariaDB# 安装MariaDB服务端[rootdb ~ 09:41:40]# yum install -y mariadb-server# 启动并开机自启MariaDB[rootdb ~ 09:54:27]# systemctl enable mariadb --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.6.2 加固 MariaDB[rootdb ~ 09:54:51]# mysql_secure_installation交互式操作说明设置 root 用户密码建议设为 123便于测试禁止 root 用户从远程登录仅本地登录提升安全性删除匿名用户避免未授权访问删除 test 测试数据库避免测试数据干扰刷新权限表使配置生效。6.3 创建 ECShop 专用数据库和用户# 登录MariaDB密码为上述设置的123[rootdb ~ 09:55:26]# mysql -uroot -p123Welcome to the MariaDB monitor. Commands end with;or\g. Your MariaDB connectionidis10Server version:5.5.68-MariaDB MariaDB Server Copyright(c)2000,2018, Oracle, MariaDB Corporation Ab and others. Typehelp;or\hforhelp. Type\ctoclearthe current input statement.# 创建ECShop数据库MariaDB[(none)]CREATE DATABASE ecshop;Query OK,1row affected(0.00sec)# 创建ecshop用户允许所有网段访问密码123MariaDB[(none)]CREATEUSERecshop%identified by123;Query OK,0rows affected(0.00sec)# 授予ecshop用户对ecshop数据库的全部权限MariaDB[(none)]GRANT ALL PRIVILEGES ON ecshop.* TOecshop%;Query OK,0rows affected(0.00sec)# 刷新权限使配置生效MariaDB[(none)]FLUSH PRIVILEGES;Query OK,0rows affected(0.00sec)# 退出MariaDBMariaDB[(none)]exitBye七、部署 PHP 服务器php 节点7.1 配置 nginx 用户# 添加nginx用户UID997GID995与shop节点一致[rootphp ~ 09:41:40]# echo nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin /etc/passwd# 创建nginx用户组GID995[rootphp ~ 09:58:51]# groupadd -g 995 nginx7.2 安装 PHP 及扩展组件# 安装PHP核心包含PHP-FPMFastCGI进程管理器[rootphp ~ 09:59:08]# yum install -y php php-fpm php-mysqlnd# 安装ECShop所需的PHP扩展包[rootphp ~ 09:59:38]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt7.3 配置 PHP-FPM配置项修改说明监听地址注释原有本地监听改为监听所有 IP 的 9000 端口允许 Nginx 节点访问;listen 127.0.0.1:9000 # 注释原有行listen 9000 # 新增行也可指定具体 IPlisten 10.1.8.22:9000允许客户端注释原有仅本地允许改为允许所有客户端;listen.allowed_clients 127.0.0.1 # 注释原有行运行用户改为 nginx 用户 / 组匹配 NFS 目录权限user nginxgroup nginx7.4 启动 PHP-FPM 并调整权限# 启动并开机自启PHP-FPM[rootphp ~10:01:46]# systemctl enable php-fpm.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.# 调整PHP默认目录权限避免执行脚本时权限拒绝[rootphp ~10:02:08]# chown -R nginx:nginx /var/lib/php/*7.5 挂载 NFS 共享目录# 安装NFS客户端工具[rootphp ~10:03:59]# yum install -y nfs-utils# 配置开机自动挂载NFS共享目录[rootphp ~10:13:41]# echo nfs.liu.cloud:/www /www nfs defaults 0 0 /etc/fstab# 创建本地挂载点[rootphp ~10:13:48]# mkdir /www# 执行挂载无需重启立即生效[rootphp ~10:13:55]# mount /www# 验证挂载结果[rootphp ~10:14:19]# df -h /www文件系统 容量 已用 可用 已用% 挂载点 nfs.liu.cloud:/www 50G1.8G 49G4% /www# 验证共享文件是否同步[rootphp ~10:14:23]# ls /wwwecshop info.php test.html test-mysql.php test.php7.6 测试 PHP 环境# 测试基础PHP执行[rootphp ~10:14:33]# php /www/test.phph1Hello World!/h1# 测试PHP连接MariaDB[rootphp ~10:14:51]# php /www/test-mysql.phph1Connect Mysql Success!/h1# 可选测试PHP信息输出内容较多可按需执行# [rootphp ~]# php /www/info.php八、部署 Nginx 服务器shop 节点8.1 安装并启动 Nginx# 安装Nginx[rootshop ~ 09:53:24]# yum install -y nginx# 启动并开机自启Nginx[rootshop ~10:16:16]# systemctl enable nginx --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.8.2 挂载 NFS 共享目录同步 ECShop 文件# 安装NFS客户端工具[rootshop ~10:16:42]# yum install -y nfs-utils# 配置开机自动挂载挂载至Nginx默认网页根目录[rootshop ~10:17:02]# echo nfs.liu.cloud:/www /usr/share/nginx/html nfs defaults 0 0 /etc/fstab# 执行挂载[rootshop ~10:18:13]# mount /usr/share/nginx/html/# 验证挂载结果[rootshop ~10:18:25]# df -h /usr/share/nginx/html/文件系统 容量 已用 可用 已用% 挂载点 nfs.liu.cloud:/www 50G1.8G 49G4% /usr/share/nginx/html# 验证文件同步[rootshop ~10:18:43]# ls /usr/share/nginx/html/ecshop info.php test.html test-mysql.php test.php8.3 配置 Nginx 对接 PHP# 创建ECShop虚拟主机配置文件[rootshop ~10:19:05]# cat /etc/nginx/conf.d/vhost-shop.conf EOFserver{listen80;# 监听80端口HTTP默认端口server_name shop.liu.cloud;# 绑定商城域名# 处理静态资源和默认请求location /{root /usr/share/nginx/html/ecshop;# ECShop程序根目录index index.html index.htm index.php;# 默认首页优先级}# 处理PHP动态请求location ~\.php${fastcgi_pass php.liu.cloud:9000;# 转发至PHP节点的9000端口fastcgi_index index.php;# PHP默认首页fastcgi_param SCRIPT_FILENAME /www/ecshop/$fastcgi_script_name;# PHP脚本实际路径需与PHP节点的NFS挂载路径一致include fastcgi_params;# 加载FastCGI默认参数}}EOF# 重启Nginx使配置生效[rootshop ~10:33:48]# systemctl restart nginx九、ECShop 平台安装配置9.1 客户端准备在客户端机器如本地电脑的 hosts 文件中添加解析10.1.8.21 shop.liu.cloud确保能访问商城域名。9.2 访问安装页面打开浏览器访问http://shop.liu.cloud进入 ECShop 安装向导。9.3 安装步骤1.初始页面点击【下一步配置系统】2.系统配置页面忽略 “使用系统时区不安全” 的 Warning 提示3.数据库配置输入数据库信息主机db.liu.cloud、用户名ecshop、密码123点击【搜】选中搜索到的 ecshop 数据库​ 在弹出的确认框中点击【确定】4.完成剩余配置时区选择【中华人民共和国】点击【立即安装】安装完成提示关闭提示窗口完成安装十、ECShop 平台访问验证10.1 商城前台访问地址http://shop.liu.cloud10.2 商城后台访问地址http://shop.liu.cloud/admin10.3 后台登录验证使用 ecshop 账户登录安装时配置的账户登录后后台首页管理中心界面