文章 文章 posts

Rocky Linux 安装 LNMP 环境教程

LNMP 是 Linux 下主流的 Web 开发环境,包含:

  • Linux:操作系统(本文基于 Rocky Linux 9)
  • Nginx:高性能 Web 服务器
  • MySQL/MariaDB:数据库服务
  • PHP-FPM:动态脚本解析引擎

一、系统准备

1. 更新系统

bash
sudo dnf update -y

2. 安装常用工具

bash
sudo dnf install -y epel-release wget curl vim unzip git firewalld

启用并启动防火墙:

bash
sudo systemctl enable firewalld --now

二、安装 Nginx

1. 安装 Nginx

Rocky Linux 9 默认仓库已包含 Nginx:

bash
sudo dnf install -y nginx

2. 启动与开机自启

bash
sudo systemctl enable nginx --now

3. 检查运行状态

bash
sudo systemctl status nginx

访问浏览器 http://服务器IP,应能看到 “Welcome to Nginx” 页面。

三、安装 MySQL / MariaDB

Rocky Linux 推荐使用 MariaDB 作为 MySQL 的兼容替代方案。

1. 安装 MariaDB

bash
sudo dnf install -y mariadb-server

2. 启动并设置开机自启

bash
sudo systemctl enable mariadb --now

3. 执行安全配置脚本

bash
sudo mysql_secure_installation

按提示设置 root 密码、删除匿名用户、禁用远程 root 登录、删除测试数据库等。

4. 验证数据库连接

bash
mysql -u root -p

四、安装 PHP

1. 启用 Remi 仓库(提供最新 PHP 版本)

bash
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.2 -y

可将 8.2 替换为其他版本(如 8.1、8.3)。

2. 安装 PHP-FPM 及常用扩展

bash
sudo dnf install -y php php-fpm php-mysqlnd php-cli php-gd php-mbstring php-xml php-zip php-bcmath php-opcache php-curl

3. 启动并设置开机自启

bash
sudo systemctl enable php-fpm --now

4. 查看 PHP 版本

bash
php -v

五、配置 Nginx 支持 PHP

1. 编辑默认虚拟主机配置

bash
sudo vim /etc/nginx/conf.d/default.conf

修改为如下内容:

nginx
server {
    listen 80;
    server_name _;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

2. 测试配置语法

bash
sudo nginx -t

若无报错则重启 Nginx:

bash
sudo systemctl restart nginx

六、测试 PHP 运行环境

创建测试页面:

bash
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php

在浏览器访问:

txt
http://服务器IP/info.php

出现 PHP 信息页面则说明 LNMP 安装成功。

七、PHP 配置优化(可选)

编辑 PHP 配置文件:

bash
sudo vim /etc/php.ini

推荐修改以下参数:

ini
memory_limit = 256M
upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 300
date.timezone = Asia/Shanghai

保存后重启 PHP-FPM:

bash
sudo systemctl restart php-fpm

八、启用 PHP OPcache(推荐)

OPcache 能显著提升 PHP 性能。

确认模块启用:

bash
php -m | grep opcache

若未启用,编辑 /etc/php.d/10-opcache.ini:

ini
[opcache]
zend_extension=opcache
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

然后重启服务:

bash
sudo systemctl restart php-fpm
sudo systemctl reload nginx

九、防火墙放行端口

允许 HTTP / HTTPS:

bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

十、服务管理命令速查表

服务 功能 启动 重启 查看状态
Nginx Web 服务 systemctl start nginx systemctl restart nginx systemctl status nginx
MariaDB 数据库服务 systemctl start mariadb systemctl restart mariadb systemctl status mariadb
PHP-FPM PHP 解析 systemctl start php-fpm systemctl restart php-fpm systemctl status php-fpm

十一、验证 LNMP 运行状态

访问:

txt
http://your_server_ip/info.php

页面若显示 PHP 版本与 OPcache 信息,即表示 LNMP 环境已部署成功。