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

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.xdnf.cn/news/16582.html

相关文章:

  • 《安富莱嵌入式周报》第356期:H7-TOOL的250M示波器模组批量生产中,自主开发QDD执行器,开源14bit任意波形发生器(2025-07-28)
  • 【Linux】重生之从零开始学习运维之Mysql事务
  • Kubernetes自动扩容方案
  • 【C语言进阶】题目练习
  • 《 java 随想录》| LeetCode链表高频考题
  • Linux文件归档和备份
  • 云原生MySQL Operator开发实战(五):扩展与生态系统集成
  • 基于Matlab图像处理的静态雨滴去除与质量评估系统
  • windows下Docker安装路径、存储路径修改
  • Docker初学者需要了解的几个知识点(三):Docker引擎与Docker Desktop
  • 实时行情接口使用教程
  • deepseek+飞书多维表格 打造小红书矩阵
  • python优秀案例:基于机器学习算法的景区旅游评论数据分析与可视化系统,技术使用django+lstm算法+朴素贝叶斯算法+echarts可视化
  • 移动端 WebView 调试实战,多平台行为差异排查与统一调试流程
  • zoho crm为什么xx是deal的关联对象但是调用函数时报错说不是关联对象
  • p5.js 三角形triangle的用法
  • 【RAG搭建Agent应用实战】基于检索增强生成(RAG)搭建特定场景Agent应用
  • Git分支
  • c++ nlohmann/json读写json文件
  • 溶解能计算
  • 【24】C# 窗体应用WinForm ——日历MonthCalendar属性、方法、实例应用
  • 电磁兼容(EMC):整改案例(十三)屏蔽外壳开孔解决433MHz无线通信问题
  • hive专题面试总结
  • c++-list
  • Elasticsearch索引设计与性能优化实战指南
  • 查询mac 安装所有python 版本
  • vscode开发微信小程序
  • 2411.按位或最大的最小子数组长度
  • 信息技术发展与区块链的崛起:深度解析与未来展望
  • 基于web的在线购物系统的设计与实现/在线商城的设计与实现