LAMP/LNMP 最佳实践

LAMP/LNMP 组件

LAMPLinux+Apache+Mysql/Mariadb+PHP/Python/Perl。

LNMPLinux+Nginx+Mysql/Mariadb+PHP/Python/Perl。

  • Linux:操作系统,提供程序运行基础。
  • Apache/Nginx:Web 服务器,提供网页访问。
  • Mysql/Mariadb:数据库,提供数据管理。
  • PHP/Python/Perl:脚本语言,提供动态执行生成。

LAMP/LNMP 工作原理

在这里插入图片描述

  1. 客户端通过HTTP协议请求web服务器资源

  2. web 服务器根据资源类型进行处理:

    • 静态资源:web直接把资源返回至客户端。

    • 动态资源:通过指定的通讯方式将脚本网页交给后端程序执行。如果运算期间需要连接mysql数据库,则通过mysql连接器连接mysql。后端程序将运算结果返回给web服务。

  3. web服务将结果返回给客户端。

PHP 与 WEB 协同工作模式

PHP 与 WEB 协同工作模式:

  1. CGI:Web 进程动态调用相应脚本解释器执行动态页面 ,执行完后再释放。特点:性能差。
  2. Modules:Web 进程动态加载相应模块执行动态页面 。特点:性能较好。
  3. FastCGI:后端进程独立运行管理,通过独立的网络套接字接口接收Web进程传过来的请求。特点:真正地实现前后端分离。适合于性能要求比较高的场景。Web 服务器需要开启反向代理功能,将请求转发到后端服务器。

ALL-IN-ONE

以部署 wordpress 应用为例。

实验环境
主机名IP 地址角色
blog.laoma.cloud10.1.8.10all

以部署 wordpress 应用为例。

部署数据库
# 安装服务端
[root@server ~ 21:55:21]# yum install -y mariadb-server# 启用并启动服务
[root@server ~ 22:00:25]# systemctl enable --now mariadb# 配置防火墙(一般防火墙是关闭的,不用做此步)
[root@blog ~]# firewall-cmd --permanent --add-service=mysql
[root@blog ~]# firewall-cmd --reload# 加固 MariaDB
[root@server ~ 22:00:42]# mysql_secure_installation
# 交互式提示您进行更改,包括: 
# - 为root帐户设置密码,例如123。 
# - 禁止root帐户从本地主机外部访问数据库。
# - 删除匿名用户帐户。
# - 删除用于演示的test数据库。 

准备数据库

[root@server ~ 22:03:48]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> CREATE DATABASE wordpress;
ERROR 1007 (HY000): Can't create database 'wordpress'; database exists
MariaDB [(none)]> CREATE DATABASE word;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> CREATE USER wp@'%' identified by '123';
Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO 'wp'@'%';
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> exit
Bye
部署 Nginx 服务
[root@server ~ 22:05:37]# yum install -y nginx
[root@server ~ 22:14:51]# echo 'Hello World !' > /usr/share/nginx/html/index.html
[root@server ~ 22:15:04]# systemctl enable nginx --now
部署 PHP 服务
部署 php 服务
[root@server ~ 22:15:09]# yum install -y php php-fpm php-mysqlnd
[root@server ~ 22:16:11]# systemctl enable php-fpm.service --now# 修改配置文件
[root@server ~ 22:16:21]# vim /etc/nginx/default.d/php.conf
location ~ \.php$ {try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;
}[root@server ~ 22:17:21]# systemctl restart nginx
准备 php 测试文件
[root@server ~ 22:22:33]# cd /usr/share/nginx/html/
# 准备 index.php
[root@server html 22:25:46]# cat > index.php <<EOF
<?phpecho "<h1>Hello World !</h1>\n";
?>
EOF# 准备 test-mysql.php
[root@server html 22:26:15]# cat > test-mysql.php <<'EOF'
<?php$link=mysqli_connect('10.1.8.10','wp','123');if($link)echo "<h1>Connect Mysql Success !</h1>\n";elseecho "<h1>Connect Mysql Failed !</h1>\n";$link->close();
?>
EOF# 准备 info.php
[root@server html 22:27:07]# cat > info.php <<EOF<?phpphpinfo()
?>
EOF[root@server html 22:27:20]# cp *.php /usr/share/nginx/html
cp: "index.php""/usr/share/nginx/html/index.php" 为同一文件
cp: "info.php""/usr/share/nginx/html/info.php" 为同一文件
cp: "php_test.php""/usr/share/nginx/html/php_test.php" 为同一文件
cp: "test-mysql.php""/usr/share/nginx/html/test-mysql.php" 为同一文件
php 程序测试
[root@server html 22:27:33]# php -f index.php 
<h1>Hello World !</h1>
[root@server html 22:27:46]# php -f test-mysql.php 
<h1>Connect Mysql Success !</h1>
部署 wordpress 应用

下载 wordpress,上传到家目录。

# 如果 Web 服务是 Nginx,则解压文件到/usr/share/nginx/html
[root@server ~ 22:35:00]# rz -E
rz waiting to receive.
[root@server ~ 22:35:13]# unzip -o wordpress-4.9.4-zh_CN.zip -d /usr/share/nginx/html
[root@server ~ 22:35:23]# chown -R nginx:nginx /usr/share/nginx/html/wordpress# php-fpm 进程默认以 apache 用户身份运行,修改运行用户为 nginx,并重启服务
[root@server ~ 22:35:52]# vim /etc/php-fpm.d/www.conf
user=nginx
group=nginx
[root@server ~ 22:36:19]# systemctl restart php-fpm

客户端配置 blog.xiexin.cloud 名称解析。访问http://blog.xiexin.cloud/wordpress/。

在这里插入图片描述

在这里插入图片描述

如果是nginx服务,则单击提交后,会出现如下提示:

在这里插入图片描述

根据提示创建文件,然后单击现在安装

在这里插入图片描述

Standalone

以部署 wordpress 应用为例。

实验环境
主机名IP 地址角色
www.xiexin.cloud10.1.8.21apache
php.xiexin.cloud10.1.8.22php
db.xiexin.cloud10.1.8.23mariadb
storage.xiexin.cloud10.1.8.24nfs

所有节点关闭防火墙和SELinux。

预配置

所有节点配置名称解析:

[root@all-node ~ ]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain610.1.8.21 www.xiexin.cloud www
10.1.8.22 php.xiexin.cloud php
10.1.8.23 db.xiexin.cloud db
10.1.8.24 storage.xiexin.cloud storage
部署存储服务器

由于静态文件和动态文件没有分开,所以 Web 服务器和 PHP 服务器都要存一份。实验环境通过NFS共享提供wordpress应用。

下载 wordpress,上传到家目录。

[root@storage ~ 22:57:11]# yum install -y nfs-utils
[root@storage ~ 22:57:18]# mkdir -m 777 /www
[root@storage ~ 22:57:30]# echo '/www 10.1.8.0/24(rw)' > /etc/exports
[root@storage ~ 22:57:38]# systemctl enable nfs-server.service --now# 准备 wordpress资源
[root@storage ~ 22:57:43]# rz -E
rz waiting to receive.
[root@storage ~ 22:58:20]# unzip -o wordpress-4.9.4-zh_CN.zip -d /www/# 准备网页测试文件
[root@storage ~ 22:58:28]# echo 'Hello World !' > /www/index.html
[root@storage ~ 22:58:54]# cat > /www/index.php <<EOF
> <?php
>   echo "<h1>Hello World !</h1>\n";
> ?>
> EOF[root@storage ~ 22:59:08]#  cat > /www/test-mysql.php <<'EOF'
> <?php
>   $link=mysqli_connect('db.xiexin.cloud','wp','123');
>   if($link)
>     echo "<h1>Connect Mysql Success !</h1>\n";
>   else
>     echo "<h1>Connect Mysql Failed !</h1>\n";
>   $link->close();
> ?>
> EOF[root@storage ~ 22:59:35]#  cat > /www/info.php <<EOF
> <?php
>   phpinfo();
> ?>
> EOF
部署数据库服务器
[root@db ~ 23:00:42]# yum install -y mariadb-server
[root@db ~ 23:00:47]# systemctl enable mariadb --now# 加固 MariaDB
[root@db ~ 23:01:02]# mysql_secure_installation
# 交互式提示您进行更改,包括: 
# - 为root帐户设置密码,例如123。 
# - 禁止root帐户从本地主机外部访问数据库。
# - 删除匿名用户帐户。
# - 删除用于演示的test数据库。 # 准备wordpress数据库和用户
[root@db ~ 23:01:40]# mysql -uroot -p123
mysql> CREATE DATABASE wordpress;
mysql> CREATE USER wp@'%' identified by 'Laoma@123';
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wp'@'%';
mysql> FLUSH PRIVILEGES;
mysql> exit
部署 Nginx 服务器
# 部署 Nginx 服务
[root@www ~ 23:09:11]# yum install -y nginx
[root@www ~ 23:09:17]# systemctl enable nginx --now# 安装 nfs 工具
[root@www ~ 23:09:31]# yum install -y nfs-utils# 挂载存储
[root@www ~ 23:09:36]# echo 'storage.xiexin.cloud:/www /usr/share/nginx/html nfs defaults 0 0' >> /etc/fstab 
[root@www ~ 23:09:39]# mount /usr/share/nginx/html/
[root@www ~ 23:09:49]# df -h/usr/share/nginx/html/
Filesystem            Size  Used Avail Use% Mounted on
storage.xiexin.cloud:/www   64G  3.2G   61G   5% /usr/share/nginx/html
[root@www ~ 23:09:55]# ls /usr/share/nginx/html/
index.html  index.php  info.php  test-mysql.php  wordpress
部署 PHP 服务器

部署 php 服务

[root@php ~ 23:12:09]# yum install -y php php-fpm php-mysqlnd
[root@php ~ 23:12:13]# vim /etc/php-fpm.d/www.conf
#使用;号注释掉原有listen行
;listen = 127.0.0.1:9000
# 新增listen 监听所有ip的9000端口
listen = 9000
# 支持监听特定ip的9000端口,例如listen = 10.1.8.22:9000# 使用;号注释掉原有 listen.allowed_clients 行
# 允许所有客户端访问
;listen.allowed_clients = 127.0.0.1[root@php ~ 23:13:00]# systemctl enable php-fpm.service --now[root@php ~ 23:13:06]# useradd -u 997 -s /sbin/nologin nginx

挂载存储

# 安装 nfs 工具
root@php ~ 23:13:14]# yum install -y nfs-utils# 挂载存储
[root@php ~ 23:15:27]# echo 'storage.xiexin.cloud:/www /www nfs defaults 0 0' >> /etc/fstab 
[root@php ~ 23:15:38]# mkdir /www
[root@php ~ 23:15:44]# mount /www
[root@php ~ 23:15:48]# df -h /www
文件系统                   容量  已用  可用 已用% 挂载点
storage.xiexin.cloud:/www   50G  1.7G   49G    4% /www
[root@php ~ 23:15:52]# ls /www
index.html  index.php  info.php  test-mysql.php  wordpress

php 程序测试

root@php ~ 23:16:21]# php /www/index.php
<h1>Hello World !</h1>
[root@php ~ 23:16:45]# php /www/test-mysql.php
<h1>Connect Mysql Success !</h1>
配置 Nginx 对接 PHP
[root@www ~ 23:09:41]# cat > /etc/nginx/conf.d/vhost-www.conf <<'EOF'
> server {
>     listen 80;
>     server_name www.xiexin.cloud;
> 
>     # 静态资源处理
>     location / {
>         root /usr/share/nginx/html;
>         index index.html index.htm index.php;
>     }
> 
>     # PHP 请求处理
>     location ~ \.php$ {
>         # 配置 PHP-FPM 监听的地址和端口
>         fastcgi_pass php.xiexin.cloud:9000;
>         fastcgi_index index.php;
>         # 配置 php 服务器上 wordpress 应用所在位置
>         fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
>         include fastcgi_params;
>     }
> }
> EOF# 重启服务
[root@www ~ 23:18:39]# systemctl restart nginx

配置存储权限

# 更改拥有者为nginx对应的uid
[root@storage ~ 22:59:47]# chown -R 997 /www# php-fpm 进程默认以 apache 用户身份运行,修改运行用户为 nginx,并重启服务
[root@php ~ 23:16:49]# useradd -u 997 -s /sbin/nologin nginx
[root@php ~ 23:19:49]# vim /etc/php-fpm.d/www.conf
user=nginx
group=nginx
[root@php ~ 23:20:07]# systemctl restart php-fpm
测试应用

客户端配置 www.xiexin.cloud 名称解析。访问http://www.xiexin.cloud/wordpress/。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/92610.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/92610.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/92610.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深入解析 resolv.conf 文件:DNS 配置的核心

/etc/resolv.conf 文件是 Linux 和类 Unix 系统中 DNS 配置的核心组件。它决定了系统如何将域名解析为 IP 地址&#xff0c;这是网络通信的关键环节。本文将深入探讨 resolv.conf 文件的核心内容&#xff0c;重点讲解 nameserver 指令以及 options 配置中的 attempts 和 rotate…

【LeetCode】102 - 二叉树的层序遍历

题目描述 给你二叉树的根节点 root&#xff0c;返回其节点值的层序遍历&#xff08;即逐层地&#xff0c;从左到右访问所有节点&#xff09;。 解题思路 使用 BFS&#xff08;广度优先搜索&#xff09;的思想&#xff0c;维护当前层的所有节点&#xff0c;逐层处理&#xff1a;…

计算机网络1-5:计算机网络的性能指标

目录 常用性能指标 速率 带宽 吞吐量 时延 时延带宽积 ​往返时间 ​利用率 ​丢包率 常用性能指标 性能指标可以从不同的方面来度量计算机网络的性能 常用的计算机网络的性能指标有8个:速率、带宽、吞吐量、时延、时延带宽积、往返时间、利用率、丢包率 速率 比特…

TDengine IDMP 文档介绍

TDengine IDMP (Industrial Data Management Platform) 是一款 AI 原生的物联网、工业数据管理平台。它通过经典的树状层次结构组织传感器、设备采集的数据&#xff0c;建立数据目录&#xff0c;对数据提供语境化、标准化的处理、并提供实时分析、可视化、事件管理与报警等功能…

使用 iFLOW-CLI GitHub Action 和 Qwen3-Coder 给 GitHub 仓库生成幻灯片风格的文档站点

阿里的心流 https://www.iflow.cn/ 团队最近开源了一款基于终端的 AI Agent 工具 iFLOW CLI, 目前可以免费使用到强大的 Qwen3-Coder、Kimi K2 等模型。又是一款类似 Anthropics Claude Code 的产品。 iFlow CLI 是一款直接在终端中运行的强大 AI 助手。它能够无缝分析代码仓库…

【2025最新】在 macOS 上构建 Flutter iOS 应用

推荐超级课程&#xff1a; 本地离线DeepSeek AI方案部署实战教程【完全版】Docker快速入门到精通Kubernetes入门到大师通关课AWS云服务快速入门实战 目录软件要求操作系统开发工具文本编辑器或集成开发环境安装 Flutter SDK下载并安装 Flutter将 Flutter 添加到您的PATH配置 i…

MySQL 临时表详细说明

目录 MySQL 临时表详细说明 1. 定义 2. 核心特性 3. 创建与使用 4. 典型应用场景 5. 生命周期管理 6. 注意事项 7. 性能优化建议 MySQL 临时表详细说明 1. 定义 临时表是存储在内存或磁盘上的临时性数据表&#xff0c;仅在当前数据库会话中存在。会话结束时自动销毁&a…

深入解析 Apache APISIX 在微服务网关中的性能优化实践指南

深入解析 Apache APISIX 在微服务网关中的性能优化实践指南 文章类型&#xff1a;性能优化实践指南 技术领域&#xff1a;微服务架构 —— API 网关 文章结构&#xff1a;原理深度解析型 目标读者&#xff1a;有一定微服务与运维基础的后端开发工程师一、技术背景与应用场景 随…

【Spring Boot刷新上下文核心流程详解】

Spring Boot 刷新上下文核心流程详解 一、前言 在使用 Spring Boot 启动应用时&#xff0c;控制台会打印出一大串日志&#xff0c;其中最核心的启动动作之一就是 刷新上下文&#xff08;refresh&#xff09;。 refresh 方法不仅负责 Bean 的创建与初始化&#xff0c;还涉及监…

关于过滤器(Filter)的学习

过滤器&#xff08;Filter&#xff09;概述 过滤器是 Java Servlet 规范的一部分&#xff0c;用于在请求到达 Servlet 之前或响应返回客户端之前拦截请求和响应。它可以用于执行各种任务&#xff0c;如请求预处理、响应后处理、身份验证、日志记录等。 过滤器的作用 预处理请…

Spring AI 打造智能面试人实战

Spring AI人工智能面试机器人相关实例 以下是与Spring AI人工智能面试机器人相关的实用案例,涵盖技术实现、功能设计及常见问题解决方案,按应用场景分类呈现: 技术集成案例 调用Hugging Face模型库处理专业领域问题 通过Spring Security添加面试会话身份验证 结合WebSoc…

QT 程序发布时候调用自定义动态库

1、需要在pro文件中增加下面的内容&#xff1a;QMAKE_LFLAGS "-Wl,-rpath,\\$$ORIGIN\" QMAKE_LFLAGS "-Wl,-rpath,\\$$ORIGIN/lib\" QMAKE_LFLAGS "-Wl,-rpath,\\$$ORIGIN/../lib\"其中lib为动态库的文件夹名称&#xff0c;可以根据自己喜好…

SpringBoot学习日记 Day6:解锁微服务与高效任务处理

一、开篇&#xff1a;从单体到微服务的思维转变刚开始接触微服务时&#xff0c;我总习惯把所有功能写在一个项目里。直到项目越来越臃肿&#xff0c;每次修改都要全量部署&#xff0c;才意识到微服务架构的价值。今天我们就来探索SpringBoot在微服务场景下的强大能力&#xff0…

机械学习--DBSCAN 算法(附实战案例)

DBSCAN 算法详解DBSCAN&#xff08;Density-Based Spatial Clustering of Applications with Noise&#xff0c;带噪声的基于密度的空间聚类应用&#xff09;是一种经典的密度聚类算法&#xff0c;由 Martin Ester 等人于 1996 年提出。与 K-means 等基于距离的聚类算法不同&am…

【昇腾】基于RK3588 arm架构Ubuntu22.04系统上适配Atlas 200I A2加速模块安装EP模式下的驱动固件包_20250808

一、背景 1.1 主要的硬件是&#xff1a;1.2 主要的软件是&#xff1a; RK3588跑操作系统Atlas 200I A2加速模块作为EP模式关键参数版本说明CPU架构aarch64OS版本Ubuntu 22.04.5 LTSkernel版本5.10.198 二、适配 准备固件run包文件&#xff1a;Ascend-hdk-310b-npu-firmware_7.…

如何在 VS Code 中进行 `cherry-pick`

cherry-pick 是 Git 的一个功能&#xff0c;允许你选择某个 commit 并将其应用到当前分支&#xff0c;而无需合并整个分支。在 VS Code 中&#xff0c;你可以通过 内置的 Git 功能 或 终端 来完成 cherry-pick。方法 1&#xff1a;使用 VS Code 的 Git 图形界面&#xff08;GUI…

STM32CubeMX(十三)FatFs文件系统(SPI驱动W25Qxx)

目录 一、知识点 1. 什么是Fatfs文件系统? 2. Fatfs操作系统控制流程 二、实战操作 1.CubeMX配置 2. 配置串口以及SPI 3. 修改功能映射接口 4. 添加测试代码 5. 实验现象 在完成本章之前需要完成一些基础配置,详情查看下面的文章。 STM32CubeMX(二)新建工…

【前端后端部署】将前后端项目部署到云服务器

更多笔记在这里☞ 全栈之路&#xff1a; https://gitee.com/oldbe/notes 【跳转到】 觉得有用请点个 star &#xff0c;非常感谢&#xff01; 现在AI太强大&#xff0c;开发个人产品的门槛和成本太低了&#xff0c;只要你有好的想法都可以很快速的开发一款产品 1.…

vue如何监听localstorage

在Vue中监听localStorage的变化可以通过几种方式实现&#xff0c;但需要注意的是&#xff0c;localStorage本身不提供原生的事件监听机制&#xff0c;如DOM元素的MutationObserver。不过&#xff0c;你可以通过一些间接的方法来监听localStorage的变化。方法1&#xff1a;使用w…

灰狼算法+四模型对比!GWO-CNN-LSTM-Attention系列四模型多变量时序预测

摘要&#xff1a;聚划算&#xff01;大对比&#xff01;灰狼算法四模型对比&#xff01;GWO-CNN-LSTM-Attention系列四模型多变量时序预测&#xff0c;该代码特别适合需要横向对比不同深度学习模型性能的时序预测场景&#xff0c;研究者可通过参数快速适配不同预测需求&#xf…