首页主机资讯Ubuntu LNMP服务器搭建流程是怎样的

Ubuntu LNMP服务器搭建流程是怎样的

时间2025-12-18 18:10:03发布访客分类主机资讯浏览1489
导读:Ubuntu LNMP 服务器搭建流程 一 准备与环境说明 以 Ubuntu 22.04 LTS 为例,组件版本:Nginx 1.18、MySQL 8.0、PHP 8.1(实际以仓库可用版本为准)。 准备事项: 具有 sudo 权限的账...

Ubuntu LNMP 服务器搭建流程

一 准备与环境说明

  • Ubuntu 22.04 LTS 为例,组件版本:Nginx 1.18MySQL 8.0PHP 8.1(实际以仓库可用版本为准)。
  • 准备事项:
    • 具有 sudo 权限的账号与可登录的 SSH
    • 开放云服务器安全组/本机防火墙的 TCP 80/443 端口。
    • 域名(可选,后续可配 HTTPS)。

二 安装与配置 Nginx

  • 更新索引并安装:
    • sudo apt update
    • sudo apt -y install nginx
    • sudo systemctl start nginx & & sudo systemctl enable nginx
  • 验证:访问服务器 http://公网IP 能看到 Nginx 欢迎页。
  • 基本站点与 PHP 支持(编辑默认站点):
    • sudo vim /etc/nginx/sites-enabled/default
    • 在 server 块中确保包含:
      • root /var/www/html; index index.php index.html;
      • location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; }
    • 检查并重载:
      • sudo nginx -t & & sudo systemctl reload nginx
  • 说明:Nginx 默认根目录为 /var/www/html,可按需改为项目目录。

三 安装与配置 MySQL 8.0

  • 安装与启动:
    • sudo apt -y install mysql-server
    • sudo systemctl start mysql & & sudo systemctl enable mysql
  • 安全初始化(推荐):
    • sudo mysql_secure_installation(设置 root 密码、移除匿名用户、禁止 root 远程登录、删除 test 库等)
  • 登录验证与基础设置:
    • mysql -u root -p
    • 如需修改 root 认证方式或密码(示例):
      • ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密码’;
      • FLUSH PRIVILEGES;
  • 远程访问(如确需):编辑 /etc/mysql/mysql.conf.d/mysqld.cnf,将 bind-address 改为 0.0.0.0,并开放云安全组/系统防火墙 3306 端口后再重启 MySQL。生产环境不建议对 root 开放远程。

四 安装与配置 PHP 与 PHP-FPM

  • 安装 PHP 与常用扩展(按需增减):
    • sudo apt -y install php-fpm php-mysql php-gd php-curl php-mbstring php-xml php-zip
    • sudo systemctl start php8.1-fpm & & sudo systemctl enable php8.1-fpm
  • 确认版本与进程:
    • php -v
    • 查看套接字:ls /var/run/php/(应为 php8.1-fpm.sock)
  • 测试页验证:
    • echo “” | sudo tee /var/www/html/info.php
    • 访问 http://公网IP/info.php,看到 PHP 信息页即成功。

五 验证与后续优化

  • 快速验证清单:
    • Nginx:systemctl status nginx;访问 http://IP 正常
    • PHP:访问 http://IP/info.php 显示配置信息
    • MySQL:mysql -u root -p 可登录并执行 SHOW DATABASES;
  • 防火墙放行(UFW 示例):
    • sudo ufw allow ‘Nginx Full’(同时放行 80/443)
  • 启用 HTTPS(Let’s Encrypt,域名已解析到服务器时):
    • sudo apt install certbot python3-certbot-nginx
    • sudo certbot --nginx -d your.domain -d www.your.domain
  • 性能与常见扩展建议:
    • 启用 OPcache(生产推荐):sudo apt install php-opcache;在 php.ini 中确保 opcache.enable=1
    • 常用扩展按需安装:php-gd、php-curl、php-mbstring、php-xml、php-zip 等
  • 安全提示:
    • 不要长期保留 info.php 在生产环境
    • 数据库与应用分离账号权限,避免使用 root 直连应用
    • 定期 apt update & & apt upgrade,关注安全公告与备份策略。

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


若转载请注明出处: Ubuntu LNMP服务器搭建流程是怎样的
本文地址: https://pptw.com/jishu/775318.html
怎样检查centos上k8s部署状态 Ubuntu下如何切换Compton和Mutter

游客 回复需填写必要信息