下面给你一份在 Ubuntu 上配置 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 的实用指南,适用于 Ubuntu 20.04 / 22.04,PHP 以 PHP 8.1 为例(可按需替换版本)。
sudo apt update
sudo apt upgrade -y
sudo apt install nginx -y
启动并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
测试:
curl http://localhost
sudo apt install mysql-server -y
sudo mysql_secure_installation
sudo apt install mariadb-server -y
sudo mysql_secure_installation
启动:
sudo systemctl start mysql
sudo systemctl enable mysql
sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
查看 PHP 版本:
php -v
PHP-FPM 服务名(示例为 8.1):
sudo systemctl status php8.1-fpm
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
echo "<?php phpinfo(); ?>" | sudo tee /var/www/example.com/index.php
sudo nano /etc/nginx/sites-available/example.com
示例配置:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
sudo ufw allow 'Nginx Full'
浏览器访问:
http://服务器IP
或
http://example.com
看到 phpinfo() 页面即配置成功。
sudo systemctl status php8.1-fpm
fastcgi_pass 的 sock 路径正确sudo nano /etc/php/8.1/fpm/php.ini
修改后:
sudo systemctl restart php8.1-fpm
phpinfoopcache如果你需要:
告诉我你的使用场景,我可以直接给你对应配置。