LNMP安装配置详解
LNMP(Linux、Nginx、MySQL、PHP)是经典Web服务器环境组合,适用于中小型网站、博客、电商平台等场景,本文从系统准备到各组件部署,再到集成测试与优化,全面介绍LNMP的安装配置流程,帮助读者快速搭建稳定、高效的Web环境。

系统准备与基础环境搭建
选择CentOS 7/8作为操作系统(推荐CentOS 8,其稳定性与LNMP兼容性更优)。
- 更新系统
sudo yum update -y
- 设置主机名
修改/etc/hostname文件,设置主机名(如lnmp-server),并重启网络服务:sudo hostnamectl set-hostname lnmp-server sudo systemctl restart network
- 关闭防火墙(可选)
若使用防火墙,需放行80(HTTP)和443(HTTPS)端口,或暂时关闭:sudo systemctl stop firewalld sudo systemctl disable firewalld
Nginx安装与基础配置
Nginx作为反向代理服务器,负责静态资源服务与PHP文件解析。
安装Nginx
sudo yum install nginx -y
启动与自启
sudo systemctl start nginx sudo systemctl enable nginx
基础配置修改
编辑主配置文件/etc/nginx/nginx.conf,调整关键参数:- 工作进程数:
worker_processes auto; # 自动适配CPU核心数
- 最大连接数:
worker_connections 4096; # 每个工作进程支持4096个连接
- 日志配置:
error_log /var/log/nginx/error.log warn; # 错误日志 access_log /var/log/nginx/access.log; # 访问日志
- 工作进程数:
虚拟主机配置
创建默认虚拟主机文件/etc/nginx/conf.d/default.conf,配置静态文件服务与PHP处理:server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$query_string; # 处理静态文件与PHP } # PHP处理配置 location ~ .php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; # 指向PHP-FPM fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # 静态文件缓存 location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; add_header Cache-Control "public, no-transform"; } # 错误页配置 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }语法检查与重启
sudo nginx -t # 检查配置语法 sudo systemctl restart nginx
MySQL安装与安全配置
MySQL作为数据库,需确保安全性与性能。
安装MySQL
sudo yum install mysql-server -y
启动与自启

sudo systemctl start mysqld sudo systemctl enable mysqld
设置root密码
运行安全脚本,根据提示设置密码(需记住):sudo mysql_secure_installation
删除匿名用户(y)、禁用root远程登录(y)、删除测试数据库(y)、刷新权限(y)。
创建数据库与用户
登录MySQL,创建测试数据库与用户:CREATE DATABASE test_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'test_password'; GRANT ALL PRIVILEGES ON test_db.* TO 'test_user'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;
性能优化(可选)
编辑/etc/my.cnf,调整参数:- 最大连接数:
[mysqld] max_connections = 200
- 慢查询日志:
slow_query_log = 1 slow_query_log_file = /var/log/mysql/slow-query.log long_query_time = 2
重启MySQL服务:
sudo systemctl restart mysqld
- 最大连接数:
PHP安装与扩展配置
PHP作为脚本语言,需安装常用扩展并优化配置。
- 安装PHP及扩展
sudo yum install php php-fpm php-mysql php-gd php-xml php-mbstring php-xmlrpc php-soap php-intl php-pdo php-pecl-apc php-pecl-redis php-pecl-xdebug -y
- 启动与自启
sudo systemctl start php-fpm sudo systemctl enable php-fpm
- 配置PHP参数
修改/etc/php.ini:- 内存限制:
memory_limit = 256M
- 启用OPcache:
opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=4000
重启PHP-FPM:
sudo systemctl restart php-fpm
- 内存限制:
Nginx与PHP-FPM集成配置
确保Nginx能正确调用PHP-FPM处理PHP文件。
- 修改Nginx配置
在/etc/nginx/nginx.conf中添加fastcgi_params:include /etc/nginx/fastcgi_params;
- 调整虚拟主机配置
确保fastcgi_pass指向PHP-FPM(推荐使用socket方式):fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
- 语法检查与重启
sudo nginx -t sudo systemctl restart nginx sudo systemctl restart php-fpm
综合测试与性能优化
创建测试页面
在/usr/share/nginx/html下创建index.php:<?php phpinfo(); ?>
访问页面(如
http://lnmp-server),若显示PHP信息页,说明集成成功。
测试MySQL连接
创建test.php:<?php $servername = "localhost"; $username = "test_user"; $password = "test_password"; $dbname = "test_db"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; ?>访问
test.php,若显示“连接成功”,说明MySQL连接正常。性能优化建议
- Nginx:调整
worker_processes为CPU核心数,增加fastcgi_buffer_size(如16k)。 - MySQL:调整
max_connections(根据并发量),启用慢查询日志分析SQL。 - PHP:启用OPcache,调整
memory_limit为合适值(如256M)。
- Nginx:调整
常见问题解答(FAQs)
问题:Nginx无法启动,提示“no listening sockets available, shutting down”
解答:
- 防火墙问题:检查防火墙状态,关闭或放行80端口。
- 端口占用:使用
netstat -tuln | grep 80查看端口占用,终止占用服务或修改端口。 - 配置错误:用
nginx -t检查配置语法,修正后重启服务。
解决步骤示例:
sudo systemctl stop firewalld netstat -tuln | grep 80 nginx -t sudo systemctl restart nginx
问题:PHP页面加载慢
解答:
- 内存不足:调整
memory_limit(如256M)。 - 缓存未启用:确保
opcache.enable=1,并优化opcache参数。 - MySQL查询慢:使用索引、减少查询次数或配置连接池。
优化示例:
- PHP配置:
memory_limit = 256M。 - Nginx配置:增加静态文件缓存(
expires 30d;)。 - MySQL:创建索引(
ALTER TABLE table_name ADD INDEX idx_name (column_name);)。
通过以上步骤,可完成LNMP的安装配置,实现稳定、高效的Web服务部署。
图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/202048.html


