LNMP 环境部署笔记

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 cmake

2. 关闭防火墙和 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.rpm

2. 安装并启动 Nginx

bash

运行

复制代码
# 安装 Nginx
yum install -y nginx

# 启动 Nginx
systemctl start nginx

# 设置开机自启
systemctl enable nginx

# 验证是否启动成功(返回 active (running) 则正常)
systemctl status nginx

# 验证端口(默认 80 端口监听)
netstat -tnlp | grep nginx

3. 测试 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 mariadb

2. 初始化 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-php74

2. 安装 PHP 及扩展

bash

运行

复制代码
# 安装核心包 + 常用扩展(适配 MySQL/Nginx)
yum install -y php php-fpm php-mysqlnd php-gd php-xml php-mbstring php-json php-opcache

3. 配置 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 = nginx

4. 启动 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.php

2. 配置 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 nginx

4. 测试 PHP 解析

浏览器访问 http://服务器IP/test/info.php,能看到 PHP 信息页面,说明 LNMP 环境配置成功

相关推荐
HAPPY酷1 小时前
Linux `shutdown` 命令速查:安全关机与重启
linux·chrome·安全
zhping10112 小时前
Linux 系统上使用 GitHub 加速工具
linux·运维·github
dreamread2 小时前
Linux下MySQL的简单使用
linux·mysql·adb
YXXY3132 小时前
Linux进程概念(一)
linux
角砾岩队长2 小时前
CASS常用快捷指令
经验分享·笔记
云边散步2 小时前
godot2D游戏教程系列二(14)
笔记·学习·游戏·游戏开发
煜3642 小时前
Linux初识与基本指令
linux·运维·服务器
执笔论英雄2 小时前
【大模型推理】cudastream 学习
linux·运维·学习
shada3 小时前
在Linux x86_64系统中编译mission
linux