下面给你一份从零部署 Linux LNMP(Linux + Nginx + MySQL/MariaDB + PHP)的实用指南,适用于 CentOS 7/8、Ubuntu 20.04/22.04 等常见发行版。你可以按需选择手动部署或一键脚本。
# CentOS
yum update -y
# Ubuntu / Debian
apt update && apt upgrade -y
systemctl stop firewalld
systemctl disable firewalld
# Ubuntu
ufw disable
# CentOS
yum install nginx -y
systemctl enable nginx
systemctl start nginx
# Ubuntu
apt install nginx -y
systemctl enable nginx
systemctl start nginx
验证:
curl http://localhost
yum install mysql-server -y
systemctl enable mysqld
systemctl start mysqld
apt install mariadb-server -y
systemctl enable mariadb
systemctl start mariadb
初始化数据库:
mysql_secure_installation
创建测试用户:
CREATE DATABASE testdb;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
# CentOS
yum install php php-fpm php-mysqlnd -y
# Ubuntu
apt install php-fpm php-mysql -y
启动 PHP-FPM:
systemctl enable php-fpm
systemctl start 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;
location / {
try_files $uri $uri/ =404;
}
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;
}
}
重启 Nginx:
nginx -t
systemctl restart nginx
测试 PHP:
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php
wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz
tar zxf lnmp2.0.tar.gz
cd lnmp2.0
./install.sh lnmp
支持:
fastcgi_pass 路径错误| 组件 | 路径 |
|---|---|
| Nginx | /etc/nginx |
| PHP | /etc/php |
| MySQL | /var/lib/mysql |
| Web根目录 | /usr/share/nginx/html |
如果你告诉我:
我可以给你更精确的部署方案。