下面以 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 为例,系统地讲一下环境配置步骤。适用于 CentOS / Ubuntu / Debian,我会分别说明关键差异。
# CentOS
yum update -y
# Ubuntu / Debian
apt update && apt upgrade -y
yum install nginx -y
systemctl enable nginx
systemctl start nginx
apt install nginx -y
systemctl enable nginx
systemctl start nginx
验证:
nginx -v
浏览器访问服务器 IP,看到 Nginx 欢迎页即成功。
CentOS(MariaDB)
yum install mariadb-server -y
systemctl enable mariadb
systemctl start mariadb
Ubuntu
apt install mysql-server -y
systemctl enable mysql
systemctl start mysql
mysql_secure_installation
按提示设置 root 密码、删除匿名用户、禁止远程 root 登录。
CREATE DATABASE lnmp_db DEFAULT CHARSET utf8mb4;
CREATE USER 'lnmp_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON lnmp_db.* TO 'lnmp_user'@'localhost';
FLUSH PRIVILEGES;
yum install epel-release -y
yum install yum-utils -y
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php81
yum install php php-fpm php-mysqlnd php-json php-gd php-mbstring -y
apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml -y
systemctl enable php-fpm
systemctl start php-fpm
mkdir -p /www/example.com
echo "<?php phpinfo(); ?>" > /www/example.com/index.php
编辑:
vim /etc/nginx/conf.d/example.conf
示例配置:
server {
listen 80;
server_name example.com;
root /www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
⚠️ Ubuntu 的 php-fpm sock 路径通常是:
/run/php/php8.1-fpm.sock
nginx -t
systemctl restart nginx
浏览器访问:
http://服务器IP
看到 PHP 信息页即配置成功。
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
ufw allow 80
ufw allow 443
如果不想手动配置,可用:
wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz
tar zxf lnmp2.0.tar.gz
cd lnmp2.0
./install.sh
如果你告诉我:
我可以给你更精确的配置示例。