下面给你一份Linux 下 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)常见配置方式,以 CentOS / Ubuntu 通用思路为主,适合新手快速搭建和配置。
# 军哥 LNMP 一键包
wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz
tar zxf lnmp2.0.tar.gz
cd lnmp2.0
./install.sh
# Ubuntu
apt install nginx -y
# CentOS
yum install nginx -y
路径(Ubuntu):
/etc/nginx/sites-available/example.conf
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
启用配置:
ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
# Ubuntu
apt install php-fpm php-mysql -y
# CentOS
yum install php-fpm php-mysqlnd -y
vim /etc/php/8.1/fpm/pool.d/www.conf
确保:
listen = /run/php/php8.1-fpm.sock
启动:
systemctl start php8.1-fpm
systemctl enable php8.1-fpm
# Ubuntu
apt install mysql-server -y
# CentOS
yum install mariadb-server -y
mysql_secure_installation
CREATE DATABASE testdb;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
echo "<?php phpinfo(); ?>" > /var/www/example/index.php
浏览器访问:
http://服务器IP
listen 443 ssl;
ssl_certificate /path/fullchain.pem;
ssl_certificate_key /path/privkey.pem;
location / {
try_files $uri $uri/ /index.php?$args;
}
client_max_body_size 50m;
upload_max_filesize = 50M
post_max_size = 50M
| 问题 | 解决 |
|---|---|
| 502 Bad Gateway | PHP-FPM 未启动或 sock 路径错误 |
| 403 Forbidden | 目录权限或 index 文件问题 |
| 页面直接下载 PHP | Nginx 未正确解析 PHP |
| 数据库连接失败 | 用户权限或 host 限制 |
如果你告诉我:
我可以给你一份完全针对你环境的 LNMP 配置。