news 2026/1/15 12:50:06

LNMP架构实验部署

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
LNMP架构实验部署

文章目录

    • 基于动态页面的web架构
      • 1、为什么需要LNMP架构
      • 2、LNMP、LNMT
      • 3、简单安装LNMP架构
        • 3.1、部署nginx服务
        • 3.2、安装php
          • 1、使用第三方扩展 epel 源安装 php7.1
          • 2、安装扩展源(epel-release-7、webtatic-release)
          • 3、配置第三方源,使用 Remi 仓库
          • 4、安装php7.1版本
          • 5 、替换php-fpm运行的用户和组,启动服务
        • 3.3、部署mysql 5.7.36
          • 3.2.1、安装mysql
          • 1、使用仓库进行安装、启动服务
  • 卸载所有mysql-community相关包
  • 导入MySQL 5.7/8.0通用的最新GPG密钥(2022版)
  • (可选)若上述密钥仍不匹配,导入MySQL 5.7专属的旧密钥
  • 重新安装mysql-community-server(会自动安装依赖包)
          • 2、修改密码
        • 3.4、验证LNMP架构是否部署成功
          • 1、编写Nginx的配置文件,连接PHP
          • 2、创建站点目录文件、完成授权
          • 3、完成主机映射,可以通过域名访问
          • **验证一:通过Nginx是否可以正常访问PHP资源**
          • **验证二:PHP是否正常连接数据库**

基于动态页面的web架构

1、为什么需要LNMP架构

因为nginx模块少,轻量级只能解析静态的网页,为了解决nginx访问动态网页的问题,需要通过架构去解决

2、LNMP、LNMT

L:linux,系统的平台也可以是w

N:前端web服务 Nginx 也可以是A Apache

M:后端数据库 mysql

P:语言 用于连接前端和后端的角色 PHP Perl Python

L:linux,系统的平台也可以是w

N:负载均衡 (代理)

M:后端数据库 mysql

T:Tomcat Java中间件(web服务)

3、简单安装LNMP架构

3.1、部署nginx服务
yum安装 nginx1.14 php7.1 mysql5.71、使用官方yum源进行安装Nginx[root@web01 yum.repos.d]# cat nginx.repo[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1[root@web01 ~]# yum -y install nginx2、创建程序用户[root@web01 ~]# groupadd www -g 666[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M3、修改配置文件[root@web01 nginx]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf[root@web01 nginx]# cat /etc/nginx/nginx.conf |head -10user www;worker_processes auto;error_log /var/log/nginx/error.log notice;pid /var/run/nginx.pid;events{worker_connections1024;4、启动并且自启动[root@web01 nginx]# systemctl start nginx[root@web01 nginx]# systemctl enable nginx
3.2、安装php
1、使用第三方扩展 epel 源安装 php7.1
1、使用第三方扩展 epel 源安装 php7.1#1.移除旧版php[root@web01 nginx]# yum remove php-mysql-5.4 php php-fpm php-common -y
2、安装扩展源(epel-release-7、webtatic-release)
# 下载epel-release-7-14.noarch.rpm(归档版本,稳定可用)wgethttps://archives.fedoraproject.org/pub/archive/epel/7/x86_64/Packages/e/epel-release-7-14.noarch.rpm# 安装该rpm包rpm-Uvh epel-release-7-14.noarch.rpm 注意:webtatic 仓库已经停止维护,对 CentOS7的支持也已终止,建议尽量避免使用rpm-Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
3、配置第三方源,使用 Remi 仓库
安装Remi-release-7rpm-Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm 安装yum-utils(提供yum-config-manager工具) yuminstall-y yum-utils 启用Remi的PHP7.4仓库(根据需要替换为5.6/7.2/8.0等) yum-config-manager --enable remi-php74 清理缓存[root@web01 nginx]# yum clean all[root@web01 nginx]# yum makecache
4、安装php7.1版本
[root@web01 nginx]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
5 、替换php-fpm运行的用户和组,启动服务
[root@web01 php-fpm.d]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf[root@web01 php-fpm.d]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf[root@web01 php-fpm.d]# systemctl start php-fpm[root@web01 php-fpm.d]# systemctl enable php-fpmCreated symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
3.3、部署mysql 5.7.36

动态网站

根据用户输入的信息,在后端数据库中进行查询之后,组合的一个页面

静态网站

固定的页面,所见即所得,现在的网页大部分都是动静结合,一般网页的首页都是静态的

动态数据

用户的信息 网页的内容一般存放在数据库中

静态数据

固定的文件(图片、音频、视频、文本)一般存放在磁盘中

一般以5.6为分割,5.6已经稳定了,但是安全性还是不太好,5.7之后已经完善了

3.2.1、安装mysql
1、使用仓库进行安装、启动服务
1、安装Mysql5.7# 下载并安装MySQL社区版仓库包(适用于EL7/CentOS7)[root@web01 ~]# rpm -ivh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm默认情况下,上述仓库包会启用 MySQL8.0仓库,禁用5.7仓库,因此需要手动调整# 安装yum-utils(提供yum-config-manager工具,若已安装可跳过)[root@web01 ~]# yum install -y yum-utils# 禁用MySQL 8.0仓库[root@web01 ~]# yum-config-manager --disable mysql80-community# 启用MySQL 5.7仓库[root@web01 ~]# yum-config-manager --enable mysql57-community# 安装MySQL 5.7社区版服务器[root@web01 ~]# yum install -y mysql-community-server[root@web01 ~]# systemctl start mysqld[root@web01 ~]# systemctl enable mysqld

安装过程中如果报错key失败可以操作以下命令

卸载所有mysql-community相关包

yum remove -y mysql-community-*

导入MySQL 5.7/8.0通用的最新GPG密钥(2022版)

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

(可选)若上述密钥仍不匹配,导入MySQL 5.7专属的旧密钥

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql57

重新安装mysql-community-server(会自动安装依赖包)

yum install -y mysql-community-server

2、修改密码
修改密码[root@web01 ~]# grep "temporary password" /var/log/mysqld.log2025-12-17T02:02:36.996806Z1[Note]A temporary password is generatedforroot@localhost:#Elkj2gp0efb[root@web01 ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)mysql:[Warning]Using a password on thecommandline interface can be insecure. Welcome to the MySQL monitor. Commands end with;or\g. Your MySQL connectionidis3Server version:5.7.44 Copyright(c)2000,2023, Oracle and/or its affiliates. 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'forhelp. Type'\c'toclearthe current input statement. mysql>ALTERUSER'root'@'localhost'IDENTIFIED BY'Jy123.com';Query OK,0rows affected(0.01sec)
3.4、验证LNMP架构是否部署成功

1、Nginx 是否能正常解析 php 动态请求

2、php 程序能否正常连接数据库

1、编写Nginx的配置文件,连接PHP
[root@web01 ~]# cat /etc/nginx/conf.d/www.confserver{server_name www.jy.com;root /code/www;index index.php index.html;client_max_body_size 20M;location ~\.php${# 定义php页面的路径,如果跟默认的root一样,可以不写root /code/www;# 定义php-fpm的访问方式fastcgi_pass127.0.0.1:9000;# 定义php的索引文件名,跟站点默认index要匹配fastcgi_index index.php;# 定义fastcgi的解析路径fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;include fastcgi_params;}}#注释,如果我们将root定义在location里面,那么每一个location里面都需要写路径
2、创建站点目录文件、完成授权
[root@web01 ~]# mkdir -p /code/www[root@web01 ~]# echo "<?php phpinfo(); ?>" > /code/www/index.php[root@web01 ~]# echo "Hello World" > /code/www/index.html授权: 更改目录的所有者为nginx(或设置通用权限)[root@web01 ~]# chown -R www.www /code/www# 设置目录和文件的权限(目录755,文件644,安全且满足访问需求)[root@web01 ~]# chmod -R 755 /code/www[root@web01 ~]# chmod 644 /code/www/index.php /code/www/index.html# 重新设置SELinux上下文(递归应用到/code/www)chcon -R -t httpd_sys_content_t /code/www# (可选)永久生效(避免重启后失效)semanage fcontext -a -t httpd_sys_content_t"/code/www(/.*)?"restorecon -R /code/www
3、完成主机映射,可以通过域名访问
物理主机映射文件 C:\Windows\System32\drivers\etc
验证一:通过Nginx是否可以正常访问PHP资源

验证二:PHP是否正常连接数据库
编写配置文件[root@web01 ~]# cat /code/www/mysqli.php<?php$servername="localhost";$username="root";$password="Tf123.com";// 创建连接$conn=mysqli_connect($servername,$username,$password);// 检测连接if(!$conn){die("Connection failed: ".mysqli_connect_error());}echo"连接成功";?>

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/9 14:46:56

Kotaemon事实一致性检测:防止幻觉输出

Kotaemon事实一致性检测&#xff1a;防止幻觉输出 在医疗咨询、金融理财或法律建议等高风险场景中&#xff0c;一句看似合理却完全错误的回答&#xff0c;可能带来严重后果。然而&#xff0c;这正是当前大型语言模型&#xff08;LLM&#xff09;面临的现实困境——它们擅长“自…

作者头像 李华
网站建设 2026/1/14 4:29:04

14、网络异常检测:TCP、UDP与电子邮件的全方位洞察

网络异常检测:TCP、UDP与电子邮件的全方位洞察 1. 主机网络出现时间判断与端口分析 在网络管理中,我们可以通过查看DHCP、路由器或交换机日志来确定主机何时出现在网络中。最后一个包含该主机的端口报告的时间戳就是我们所需的关键信息。 对于端口报告中的特定行(如第4行…

作者头像 李华
网站建设 2026/1/15 0:48:45

41、网络安全技术与概念解析

网络安全技术与概念解析 1. 网络安全监测与防护设备 1.1 网络入侵检测系统(NIDS) 网络入侵检测系统(NIDS)持续监测网络流量,旨在检测恶意网络活动,例如端口扫描和拒绝服务(DoS)攻击等。它通过对网络流量的实时监控,分析其中是否存在异常行为模式,以此来判断是否有…

作者头像 李华
网站建设 2026/1/15 9:01:08

44、网络安全知识全解析

网络安全知识全解析 1. 恶意软件威胁与预防 1.1 常见恶意软件威胁 恶意软件威胁 定义 示例 病毒 在用户不知情的情况下在计算机上运行的代码,当代码被访问和执行时感染计算机 爱虫病毒(如 love - letter - for - you.txt.vbs) 蠕虫 与病毒类似,但能自我复制,而病…

作者头像 李华
网站建设 2026/1/14 12:30:53

博灵语音通知终端:全场景智能告警神器,让风险早发现、早响应!

博灵语音通知终端&#xff1a;全场景智能告警神器&#xff0c;让风险早发现、早响应&#xff01; 还在为设备故障漏报、异常告警不及时发愁&#xff1f;还在纠结多系统无法联动、告警定位难&#xff1f;博灵语音通知终端 —— 新一代智能语音声光告警设备&#xff0c;以网络为纽…

作者头像 李华