LNMP 环境部署笔记
LNMP 是 Linux + Nginx + MySQL/MariaDB + PHP 的缩写,是目前主流的 Web 服务架构之一。
一、环境准备
1. 系统基础配置
bash
运行
# 1. 切换到 root 用户(避免权限问题) su root # 2. 更新系统软件包(可选,但建议执行) yum update -y # 3. 安装基础依赖工具 yum install -y wget vim net-tools gcc gcc-c++ make cmake2. 关闭防火墙和 SELinux(测试环境)
生产环境建议配置防火墙规则,而非直接关闭:
bash
运行
# 关闭防火墙 systemctl stop firewalld systemctl disable firewalld # 临时关闭 SELinux setenforce 0 # 永久关闭 SELinux(需重启生效) sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config二、安装 Nginx
1. 添加 Nginx 官方源
bash
运行
# CentOS 7 wget -O /etc/yum.repos.d/nginx.repo https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm # CentOS 8 wget -O /etc/yum.repos.d/nginx.repo https://nginx.org/packages/centos/8/noarch/RPMS/nginx-release-centos-8-0.el8.ngx.noarch.rpm2. 安装并启动 Nginx
bash
运行
# 安装 Nginx yum install -y nginx # 启动 Nginx systemctl start nginx # 设置开机自启 systemctl enable nginx # 验证是否启动成功(返回 active (running) 则正常) systemctl status nginx # 验证端口(默认 80 端口监听) netstat -tnlp | grep nginx3. 测试 Nginx
浏览器访问服务器 IP(如http://192.168.1.100),能看到 Nginx 默认欢迎页即安装成功。
三、安装 MySQL/MariaDB
MariaDB 是 MySQL 的开源分支,兼容性更好,优先推荐安装:
1. 安装 MariaDB
bash
运行
# 安装 MariaDB 服务器和客户端 yum install -y mariadb-server mariadb # 启动 MariaDB systemctl start mariadb # 设置开机自启 systemctl enable mariadb # 验证状态 systemctl status mariadb2. 初始化 MariaDB(安全配置)
bash
运行
# 执行安全配置脚本(必做!) mysql_secure_installation执行后按提示操作:
- 按回车(初始 root 无密码);
- 设置 root 密码(建议强密码);
- 移除匿名用户(选 Y);
- 禁止 root 远程登录(测试环境可选 N,生产环境选 Y);
- 删除 test 数据库(选 Y);
- 刷新权限(选 Y)。
3. 验证 MariaDB
bash
运行
# 登录数据库(输入刚才设置的密码) mysql -u root -p # 退出数据库 exit;四、安装 PHP
1. 添加 PHP 官方源(Remi 源,支持高版本 PHP)
bash
运行
# 安装 EPEL 源 yum install -y epel-release # 安装 Remi 源 yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm # CentOS 7 # yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm # CentOS 8 # 启用 PHP 7.4 源(推荐稳定版本,也可选 8.0/8.1) yum install -y yum-utils yum-config-manager --enable remi-php742. 安装 PHP 及扩展
bash
运行
# 安装核心包 + 常用扩展(适配 MySQL/Nginx) yum install -y php php-fpm php-mysqlnd php-gd php-xml php-mbstring php-json php-opcache3. 配置 PHP-FPM
PHP-FPM 是 PHP 的进程管理器,需与 Nginx 配合:
bash
运行
# 编辑 PHP-FPM 配置文件 vim /etc/php-fpm.d/www.conf修改以下关键配置(默认是 apache,需改为 nginx):
ini
user = nginx group = nginx listen = 127.0.0.1:9000 # PHP-FPM 监听端口 listen.owner = nginx listen.group = nginx4. 启动 PHP-FPM
bash
运行
# 启动 PHP-FPM systemctl start php-fpm # 设置开机自启 systemctl enable php-fpm # 验证状态 systemctl status php-fpm五、配置 Nginx 解析 PHP
1. 创建测试站点目录
bash
运行
# 创建站点根目录 mkdir -p /usr/share/nginx/html/test chown -R nginx:nginx /usr/share/nginx/html/test # 创建 PHP 测试文件 echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/test/info.php chmod 644 /usr/share/nginx/html/test/info.php2. 配置 Nginx 虚拟主机
bash
运行
# 编辑 Nginx 配置文件 vim /etc/nginx/conf.d/test.conf添加以下配置:
nginx
server { listen 80; server_name localhost; # 可改为你的域名或服务器 IP root /usr/share/nginx/html/test; # 站点根目录 index index.php index.html index.htm; # 解析 PHP 文件的核心配置 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; # 连接 PHP-FPM 监听端口 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # 禁止访问 .htaccess 等隐藏文件 location ~ /\.ht { deny all; } }3. 验证并重启 Nginx
bash
运行
# 检查 Nginx 配置语法(无报错则正常) nginx -t # 重启 Nginx 加载配置 systemctl restart nginx4. 测试 PHP 解析
浏览器访问http://服务器IP/test/info.php,能看到 PHP 信息页面,说明 LNMP 环境配置成功