当前位置: 首页 > 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.lryc.cn/news/604671.html

相关文章:

  • MCU中的CAN总线是什么?
  • 44、鸿蒙HarmonyOS Next开发:视频播放 (Video)组件和进度条 (Progress)组件的使用
  • LLM—— 基于 MCP 协议(SSE 模式)的工具调用实践
  • 常见的cms框架的webshell方法
  • JAVAEE--4.多线程案例
  • 机器学习之线性回归的入门学习
  • SpringBoot学习 |springboot概念+微服务架构
  • 【HarmonyOS】鸿蒙ArkWeb加载优化方案详解
  • 相亲小程序匹配与推荐系统模块搭建
  • Redis知识点(2)
  • 问题1:uniapp在pages样式穿刺没有问题,在components组件中样式穿刺小程序不起效果
  • 短剧小程序系统开发:重塑影视内容消费格局
  • Apple基础(Xcode②-Flutter结构解析)
  • android9-activity启动流程
  • 2025年湖北中级注册安全工程师报考那些事
  • RHCA学习概述
  • Spark的累加器(Accumulator)
  • django-3模型操作
  • 【昇腾】基于Atlas 200I DK A2开发者套件修改usb0的默认IP重启后被恢复的问题处理_20250730
  • 【MySQL】MySQL索引—B树/B+树
  • 基于 Hadoop 生态圈的数据仓库实践 —— OLAP 与数据可视化(五)
  • wps批量让浮动在表格的图片跟随单元格移动和调整大小
  • 如何在生成式引擎优化(GEO)中取得成功
  • MySQL 9 Group Replication维护
  • 疯狂星期四文案网第24天运营日记
  • 力扣 hot100 Day60
  • Day 26 函数专题1:函数定义与参数
  • 少林寺用什么数据库?
  • pycharm的一些小细节
  • Linux:haproxy