下面给你一个**在 Linux 上搭建 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)**的完整、通用教程,适用于 CentOS 7/8、Rocky、AlmaLinux、Ubuntu,我会分别说明不同系统的差异。
# CentOS / Rocky / Alma
yum update -y
# Ubuntu
apt update && apt upgrade -y
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
yum install -y epel-release
yum install -y nginx
apt install -y nginx
systemctl start nginx
systemctl enable nginx
访问服务器 IP,看到 Welcome to nginx 表示成功。
yum install -y mariadb mariadb-server
apt install -y mariadb-server
启动并初始化:
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
# CentOS
yum install -y mysql-server
# Ubuntu
apt install -y mysql-server
yum install -y epel-release
yum install -y yum-utils
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php81
yum install -y php php-fpm php-mysql php-gd php-mbstring php-xml php-json
apt install -y php-fpm php-mysql php-gd php-mbstring php-xml php-curl
systemctl start php-fpm
systemctl enable php-fpm
vim /etc/nginx/conf.d/default.conf
示例配置:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock; # CentOS
# fastcgi_pass unix:/var/run/php/php-fpm.sock; # Ubuntu
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
systemctl restart nginx
systemctl restart php-fpm
vim /usr/share/nginx/html/info.php
内容:
<?php
phpinfo();
浏览器访问:
http://服务器IP/info.php
能显示 PHP 信息页面 ✅
fastcgi_pass 路径错误location ~ \.php$ 未配置chown -R nginx:nginx /usr/share/nginx/html
chmod -R 755 /usr/share/nginx/html
如果你不想手动配置,可以使用:
wget http://soft.vpser.net/lnmp/lnmp1.9.tar.gz
tar -zxvf lnmp1.9.tar.gz
cd lnmp1.9
./install.sh lnmp
✅ 已完成:
如果你告诉我:
我可以给你 定制版 LNMP 安装方案 ✅