如何在Ubuntu上部署LNMP
导读:在 Ubuntu 上部署 LNMP 的实操步骤 一 环境准备与版本选择 更新系统并安装常用工具: sudo apt update && sudo apt upgrade -y sudo apt install -y cu...
在 Ubuntu 上部署 LNMP 的实操步骤
一 环境准备与版本选择
- 更新系统并安装常用工具:
- sudo apt update & & sudo apt upgrade -y
- sudo apt install -y curl wget gnupg2 ca-certificates lsb-release
- PHP 版本建议:优先使用 PHP 8.1(或 7.4)。若默认仓库版本偏旧,可添加 ondrej/php PPA 获取新版本:
- sudo apt install -y software-properties-common
- sudo add-apt-repository -y ppa:ondrej/php
- sudo apt update
- 防火墙建议:启用 UFW 并放行 Nginx
- sudo ufw allow ‘Nginx Full’
- sudo ufw enable(如启用,请务必保留 OpenSSH 规则)
二 安装与启动 Nginx
- 安装 Nginx:
- sudo apt install -y nginx
- 启动并设置开机自启:
- sudo systemctl start nginx
- sudo systemctl enable nginx
- 验证:浏览器访问 http://服务器IP,出现 Nginx 欢迎页即成功。
三 安装与初始化数据库 MySQL 或 MariaDB
- 选择其一安装:
- MySQL:sudo apt install -y mysql-server
- MariaDB:sudo apt install -y mariadb-server mariadb-client
- 启动并设置开机自启:
- sudo systemctl start mysql 或 sudo systemctl start mariadb
- sudo systemctl enable mysql 或 sudo systemctl enable mariadb
- 安全加固(设置 root 密码、移除匿名用户、禁止远程 root 登录、移除测试库等):
- sudo mysql_secure_installation
- 如需仅本地访问数据库,可保持默认绑定 127.0.0.1;如需远程访问,再按需调整绑定地址与授权(见文末“常见问题”)。
四 安装与配置 PHP 与 PHP-FPM
- 安装 PHP 与常用扩展(以 PHP 8.1 为例,按需替换为 7.4):
- sudo apt install -y php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-zip php8.1-opcache
- 启动并设置开机自启:
- sudo systemctl start php8.1-fpm
- sudo systemctl enable php8.1-fpm
- 确认 FPM 监听套接字路径(两种常见路径,任一存在即可):
- /var/run/php/php8.1-fpm.sock
- /run/php/php8.1-fpm.sock
- 验证 PHP:
- php -v 查看版本
- 创建测试文件:echo “” | sudo tee /var/www/html/info.php
- 浏览器访问 http://服务器IP/info.php,能看到 PHP 信息页即成功。
五 配置 Nginx 处理 PHP 并测试
- 编辑站点配置(示例为默认站点):
- sudo nano /etc/nginx/sites-available/default
- 在 server 块中确保 index 包含 index.php,并添加 PHP 处理 location(注意将版本号与 sock 路径与系统一致,如 php8.1-fpm.sock 或 php7.4-fpm.sock):
- index index.php index.html index.htm index.nginx-debian.html;
- location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # 或 /run/php/php8.1-fpm.sock fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
- 检查配置并生效:
- sudo nginx -t
- sudo systemctl reload nginx
- 再次访问 http://服务器IP/info.php,应正常显示 PHP 信息。
六 常见问题与优化
- 502 Bad Gateway:多为 PHP-FPM 未运行或 fastcgi_pass 路径不匹配。执行:
- sudo systemctl status php8.1-fpm
- 确认 /var/run/php/ 或 /run/php/ 下存在对应 .sock 文件,并与 Nginx 配置一致。
- 权限与目录:确保 /var/www/html 可被 www-data(Nginx 运行用户)读取,必要时:
- sudo chown -R www-data:www-data /var/www/html
- sudo chmod -R 755 /var/www/html
- 防火墙:如启用 UFW,放行 Nginx Full(HTTP/HTTPS);如启用 SSH,务必保留 OpenSSH 规则,避免被锁。
- 远程访问数据库(如确需):
- 编辑 /etc/mysql/mysql.conf.d/mysqld.cnf,将 bind-address 改为 0.0.0.0(或注释掉该行)
- 重启 MySQL:sudo systemctl restart mysql
- 在 MySQL 中授权:
- GRANT ALL PRIVILEGES ON . TO ‘your_user’@‘%’ IDENTIFIED BY ‘your_password’;
- FLUSH PRIVILEGES;
- 同时确保云服务器安全组/防火墙放行 3306 端口。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 如何在Ubuntu上部署LNMP
本文地址: https://pptw.com/jishu/757247.html
