首页主机资讯如何在Ubuntu上安装LNMP全栈环境

如何在Ubuntu上安装LNMP全栈环境

时间2025-12-04 21:09:03发布访客分类主机资讯浏览507
导读:在 Ubuntu 上安装 LNMP 全栈环境的实用步骤 一 准备与版本选择 更新系统并安装常用工具: sudo apt update && sudo apt upgrade -y sudo apt install -y...

在 Ubuntu 上安装 LNMP 全栈环境的实用步骤

一 准备与版本选择

  • 更新系统并安装常用工具:
    • sudo apt update & & sudo apt upgrade -y
    • sudo apt install -y wget curl vim unzip
  • 选择组件版本(示例以 Ubuntu 22.04/24.04 常见版本为例):
    • Nginx:使用系统仓库版本即可(apt 安装)
    • MySQL:安装 mysql-server(或选择 MariaDB)
    • PHP:优先 PHP 8.1(如需 7.4/8.0,见下方“版本切换”)
  • 如希望安装新版或指定版本的 Nginx,可添加官方仓库(可选):
    • curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg
    • echo “deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/ubuntu $(lsb_release -cs) nginx” | sudo tee /etc/apt/sources.list.d/nginx.list
    • sudo apt update

二 安装与启动 Nginx

  • 安装:sudo apt install -y nginx
  • 启动与开机自启:
    • sudo systemctl start nginx
    • sudo systemctl enable nginx
  • 验证:浏览器访问服务器 IP,出现 Nginx 欢迎页即正常
  • 常用目录与命令:
    • 配置:/etc/nginx/(主配置 nginx.conf,站点可用 /etc/nginx/sites-available/
    • 日志:/var/log/nginx/
    • 重载:sudo systemctl reload nginx;状态:sudo systemctl status nginx

三 安装与初始化 MySQL

  • 安装:sudo apt install -y mysql-server
  • 启动与开机自启:
    • sudo systemctl start mysql
    • sudo systemctl enable mysql
  • 安全加固(设置 root 密码、移除匿名用户、禁止远程 root 登录等):sudo mysql_secure_installation
  • 远程访问(按需开启):
    • 编辑:sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf,将 bind-address 改为 0.0.0.0
    • 重启:sudo systemctl restart mysql
    • 授权示例:GRANT ALL PRIVILEGES ON . TO ‘your_user’@‘%’ IDENTIFIED BY ‘your_password’; FLUSH PRIVILEGES;
  • 注意:开放远程访问需配合云安全组/防火墙策略并做好访问控制

四 安装与配置 PHP

  • 安装 PHP 与常用扩展(以 PHP 8.1 为例,按需替换为 7.4/8.0):
    • sudo apt install -y php8.1 php8.1-fpm php8.1-mysql php8.1-cli php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-opcache
  • 启动与开机自启:
    • sudo systemctl start php8.1-fpm
    • sudo systemctl enable php8.1-fpm
  • 让 Nginx 处理 PHP(编辑站点配置,示例为 /etc/nginx/sites-available/default 的 server 块):
    • location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; # 路径随 PHP 版本变化 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
  • 检查并重载 Nginx:sudo nginx -t & & sudo systemctl reload nginx
  • 测试:echo “” | sudo tee /var/www/html/info.php,访问 http://your_server_ip/info.php
  • 版本切换与扩展安装(示例思路):
    • Ubuntu 22.04 默认仓库可能无 PHP 8.1,可添加 ppa:ondrej/php 后安装 8.1/7.4/8.0
    • 安装扩展:sudo apt install -y php8.1-(如 php8.1-gd、php8.1-mbstring 等)

五 防火墙 验证与常见问题

  • 防火墙(如启用 UFW):
    • 允许 Web 流量:sudo ufw allow ‘Nginx Full’
    • 允许 SSH:sudo ufw allow ‘OpenSSH’
    • 启用:sudo ufw enable
  • 常见问题排查:
    • 502 Bad Gateway:多为 PHP-FPM 未运行或 fastcgi_pass 路径不匹配,执行 systemctl status php8.1-fpm 并核对 /run/php/php*.sock
    • 403 Forbidden:检查 root 目录权限与 Nginx 配置中的 index 指令
    • 页面空白/语法错误:查看 /var/log/nginx/error.log 与 PHP 错误日志
    • 配置文件语法:sudo nginx -t
  • 建议:生产环境禁用 info.php,删除或限制访问;为站点配置 server_name、开启 HTTPS/TLS

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 如何在Ubuntu上安装LNMP全栈环境
本文地址: https://pptw.com/jishu/763941.html
Debian Message提示更新可靠吗 如何在Ubuntu上配置LNMP数据库

游客 回复需填写必要信息