在 Ubuntu 上安装 LNMP(Linux + Nginx + MySQL/MariaDB + PHP),常见有两种方式:
以 Ubuntu 22.04 / 24.04 为例。
sudo apt update
sudo apt upgrade -y
sudo apt install nginx -y
启动并设置开机自启:
sudo systemctl enable nginx
sudo systemctl start nginx
浏览器访问服务器 IP,看到 Nginx 欢迎页即成功。
sudo apt install mysql-server -y
sudo mysql_secure_installation
sudo apt install mariadb-server -y
sudo mysql_secure_installation
sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
查看 PHP 版本:
php -v
编辑站点配置:
sudo nano /etc/nginx/sites-available/default
修改为类似如下:
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
}
测试并重启:
sudo nginx -t
sudo systemctl restart nginx
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
访问:
http://服务器IP/info.php
wget http://soft.lnmp.org/lnmp.tar.gz
tar zxf lnmp.tar.gz
cd lnmp
sudo ./install.sh lnmp
按提示选择:
root 路径systemctl status php-fpm
sudo ufw allow 80
sudo ufw allow 443
如果你告诉我:
我可以给你更精准的安装方案。