温馨提示×

ubuntu lnmp 怎么安装

小樊
42
2026-10-24 14:49:23
栏目: 智能运维

在 Ubuntu 上安装 LNMP(Linux + Nginx + MySQL/MariaDB + PHP),常见有两种方式:


一、使用官方源手动安装(推荐,可控性强)

以 Ubuntu 22.04 / 24.04 为例。

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装 Nginx

sudo apt install nginx -y

启动并设置开机自启:

sudo systemctl enable nginx
sudo systemctl start nginx

浏览器访问服务器 IP,看到 Nginx 欢迎页即成功。


3. 安装 MySQL(或 MariaDB)

MySQL

sudo apt install mysql-server -y
sudo mysql_secure_installation

或 MariaDB

sudo apt install mariadb-server -y
sudo mysql_secure_installation

4. 安装 PHP

sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y

查看 PHP 版本:

php -v

5. 配置 Nginx 支持 PHP

编辑站点配置:

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

6. 测试 PHP

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

访问:

http://服务器IP/info.php

二、使用一键脚本安装(省事)

1. LNMP 官方脚本

wget http://soft.lnmp.org/lnmp.tar.gz
tar zxf lnmp.tar.gz
cd lnmp
sudo ./install.sh lnmp

按提示选择:

  • MySQL / MariaDB 版本
  • PHP 版本

2. 或使用 apt 快速组合(不推荐生产)


三、常见问题

1. 403 / 空白页

  • 检查 root 路径
  • 检查 PHP-FPM 是否运行
systemctl status php-fpm

2. 防火墙

sudo ufw allow 80
sudo ufw allow 443

如果你告诉我:

  • Ubuntu 版本
  • 是否用 MySQL 还是 MariaDB
  • 是否用于生产环境

我可以给你更精准的安装方案。

0 踩