在 Linux 上配置 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)站点,一般流程如下。下面以 Ubuntu / Debian 为例(CentOS 类似,只是包管理器和路径略有不同)。
sudo apt update
sudo apt install nginx -y
启动并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
访问服务器 IP,看到 Welcome to nginx 即成功。
sudo apt install mariadb-server -y
初始化数据库安全设置:
sudo mysql_secure_installation
登录数据库:
sudo mysql -u root -p
创建站点数据库(示例):
CREATE DATABASE testdb;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml -y
查看 PHP 版本:
php -v
Ubuntu 常见 PHP-FPM sock 路径:
/run/php/php-fpm.sock
或
/run/php/php8.1-fpm.sock
sudo mkdir -p /var/www/testsite
sudo chown -R www-data:www-data /var/www/testsite
sudo chmod -R 755 /var/www/testsite
创建测试 PHP 文件:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/testsite/index.php
sudo nano /etc/nginx/sites-available/testsite
server {
listen 80;
server_name example.com www.example.com; # 域名或 IP
root /var/www/testsite;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/testsite /etc/nginx/sites-enabled/
测试 Nginx 配置:
sudo nginx -t
重载 Nginx:
sudo systemctl reload nginx
server_name 已配置http://example.comsudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
证书会自动续期。
sudo systemctl status php*-fpm
sudo systemctl restart php-fpm
index 文件缺失root 路径错误fastcgi_pass sock 路径错误/etc/nginx/
├── sites-available/
└── sites-enabled/
/var/www/
└── testsite/
└── index.php
如果你愿意,我可以:
直接告诉我你的 系统版本 + 需求 即可。