下面给你一个在 CentOS 上配置 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)的通用、可操作教程,适用于 CentOS 7 / 8(CentOS 8 已 EOL,建议用 Rocky/Alma,但步骤基本一致)。
yum update -y
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
yum install -y epel-release
yum install -y nginx
systemctl start nginx
systemctl enable nginx
浏览器访问服务器 IP,看到 Nginx 欢迎页即成功。
yum install -y mariadb-server mariadb
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
yum install -y epel-release yum-utils
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php74
yum install -y php php-fpm php-mysqlnd php-gd php-mbstring php-xml php-json
systemctl start php-fpm
systemctl enable php-fpm
vi /etc/nginx/conf.d/default.conf
示例配置:
server {
listen 80;
server_name your_domain_or_ip;
root /usr/share/nginx/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginx -t
systemctl reload nginx
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
浏览器访问:
http://服务器IP/info.php
看到 PHP 信息页即成功 ✅
fastcgi_pass 端口(9000 或 unix socket)location ~ \.php$ 配置chown -R nginx:nginx /usr/share/nginx/html
如果不想手动配置:
wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz
tar zxf lnmp2.0.tar.gz
cd lnmp2.0
./install.sh lnmp
如果你告诉我:
我可以给你更精确的生产级配置。