当前位置: 首页 > news >正文

Ubuntu LNMP

Ubuntu LNMP

前言:

LNMP是由linux nginx mysql php 组成的一个架构 我们前面说过在Centos7中安装 但在Ubuntu中怎么安装 其实很简单 在Centos7或者Ubuntu中 yum或者apt的功能之强大 是可以做到无脑安装的

在 Ubuntu 上安装 LNMP(Linux, Nginx, MySQL/MariaDB, PHP)栈是搭建 Web 服务器的常见方式

安装 Nginx

bash

sudo apt install nginx -y# 启动Nginx并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx# 检查Nginx状态
sudo systemctl status nginx

PS:nginx的默认端口号是80 要注意容易和HTTPD的80端口冲突

装 MySQL/MariaDB

Ubuntu 默认仓库中是 MariaDB(MySQL 的分支):

bash

sudo apt install mariadb-server -y#或者
sudo apt -y install mysql-server# 启动服务并设置开机自启
sudo systemctl start mariadb
sudo systemctl enable mariadb# 运行安全脚本(设置root密码、移除匿名用户等)
sudo mysql_secure_installation

按照提示进行配置,建议回答如下:

  • 设置 root 密码:Y 并设置强密码
  • 移除匿名用户:Y
  • 禁止 root 远程登录:Y
  • 移除测试数据库:Y
  • 重新加载权限表:Y

安装 PHP

安装 PHP 及必要扩展:

bash

# 安装PHP和常用扩展
sudo apt install php-fpm php-mysql php-cli php-mbstring php-gd php-xml php-curl php-zip -y# 启动PHP-FPM并设置开机自启
sudo systemctl start php8.1-fpm  # 注意版本号可能不同,可使用php-fpm代替
sudo systemctl enable php8.1-fpm# 检查PHP-FPM状态
sudo systemctl status php8.1-fpm
  • php-fpm:PHP FastCGI 进程管理器,用于处理 PHP 请求
  • php-mysql:PHP 连接 MySQL/MariaDB 的扩展
  • php-cli:PHP 命令行接口
  • php-mbstring:处理多字节字符串的扩展
  • php-gd:图像处理扩展
  • php-xml:XML 解析扩展
  • php-curl:处理 URL 请求的扩展
  • php-zip:处理 ZIP 压缩文件的扩展

配置 Nginx 处理 PHP

修改配置文件

sudo vim /etc/nginx/sites-enabled/default

配置文件

server {listen 80 default_server;listen [::]:80 default_server;# SSL configuration## listen 443 ssl default_server;# listen [::]:443 ssl default_server;## Note: You should disable gzip for SSL traffic.# See: https://bugs.debian.org/773332## Read up on ssl_ciphers to ensure a secure configuration.# See: https://bugs.debian.org/765782## Self signed certs generated by the ssl-cert package# Don't use them in a production server!## include snippets/snakeoil.conf;root /var/www/html;# Add index.php to the list if you are using PHPindex index.php index.html index.htm index.nginx-debian.html; #添加index.php server_name _;location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files <span class="katex--inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>r</mi><mi>i</mi></mrow><annotation encoding="application/x-tex">uri </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6595em;"><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">i</span></span></span></span></span>uri/ =404;}# pass PHP scripts to FastCGI server#location ~ \.php$ {	#去“#”include snippets/fastcgi-php.conf;# With php-fpm (or other unix sockets):fastcgi_pass unix:/run/php/php8.1-fpm.sock; #更改为8.1 详情看下 测试PHP版本# With php-cgi (or other tcp sockets):#fastcgi_pass 127.0.0.1:9000; #加“#”}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#       deny all;#}</span>
# 同时存在这两行是错误的
fastcgi_pass unix:/run/php/php8.1-fpm.sock;  # Unix套接字方式
fastcgi_pass 127.0.0.1:9000;                # TCP端口方式
  • 在处理 PHP 的location ~ .php$块中,同时启用了两种fastcgi_pass配置,这会导致 Nginx 无法正常工作。

  • Nginx 不允许在同一个 location 块中对同一个指令设置两个值,这会导致配置错误。

  • 保留其中一种方式即可,通常推荐使用 Unix 套接字方式(性能更好)。

  • 查看PHP版本

    hp -v 
    PHP 8.1.2-1ubuntu2.22 (cli) (built: Jul 15 2025 12:11:22) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.1.2, Copyright (c) Zend Technologieswith Zend OPcache v8.1.2-1ubuntu2.22, Copyright (c), by Zend Technologies
    

  • /run/php/php8.1-fpm.sock

    ls /run/php
    php8.1-fpm.pid  php8.1-fpm.sock  php-fpm.sock
    

  • fastcgi_pass unix:/run/php/php8.1-fpm.sock; 是 Nginx 配置中用于将 PHP 请求转发给 PHP-FPM 进程处理的核心指令。

  • 如果版本不同 nginx会启动失败

测试 PHP 处理

创建一个 PHP 测试文件

sudo vim /var/www/html/index.php

添加以下内容:

<?php
phpinfo();
?>

访问测试

firefox 127.0.0.1/index.php

总结

好了 LNMP或者LAMP是企业中常用的一种网络架构 做到提升访问的速度或者其他等等 其实大致过程很简单 因为我们尝试的是apt直接安装 并不是编译源码安装 所以会简单一点 但如果必要的编译安装 其实就是安装步骤的不同和版本的适配 细节部分也大差不差

好了 今天是2025.7.30 希望自己开心 也希望大家平安喜乐

http://www.dtcms.com/a/306565.html

相关文章:

  • 容器化与Docker核心原理
  • 流程制造的数字孪生:从黑箱生产到全息掌控
  • 滚珠导轨在电子制造中的流畅性优势
  • 虚实共生的智能革命:元宇宙、物联网与 AI 融合生态全景图谱
  • 高可靠液晶屏系统解决方案深度解析
  • 基于 Python 开发的信阳市天气数据可视化系统源代码+数据库+课程报告
  • 老旧泵房物联网改造方案与成本效益深度解析
  • C++线程详解
  • 关于“LoggerFactory is not a Logback LoggerContext but Logback is on ......“的解决方案
  • [源力觉醒 创作者计划]_巅峰对话:文心 vs. DeepSeek/Qwen 3.0 深度解析
  • Wan2.2 - 阿里最新开源视频生成模型 支持文生视频/图生视频 支持50系显卡 一键整合包
  • 293F细胞是什么?
  • C语言《智能自平衡小车,实现平衡功能的基础上,加入了超声波避障、超声波跟随、蓝牙遥控等功能》+源代码+文档说明
  • DEC 指令
  • spark的broadcast variables
  • 重庆邮电大学2026年计算机/软件/人工智能/网安考研备考指南
  • css初学者第二天
  • RabbitMQ 发送方确认的两大工具 (With Spring Boot)
  • 15、点云<—>深度图转换原理
  • Centos 7.9安装部署cobbler-自动化部署服务器完整教程
  • 【Flask 基础 ①】 | 路由、参数与模板渲染
  • 【AI】开源项目整理
  • 数据库账号密码、查找文件、文件权限
  • Python 程序设计讲义(45):组合数据类型——集合类型:集合的常用操作
  • TCP面试
  • Mint聊天室 · 猫猫狐狐的QA夜会· Vol.01
  • 智慧界桩:湿地与地质公园的生态链守护者
  • 【数据结构初阶】--二叉树(五)
  • 模板初阶
  • C++ 中 NULL 与 nullptr 有什么区别?