温馨提示×

怎样给Debian装LNMP

小樊
44
2026-09-01 20:33:38
栏目: 智能运维

下面以 Debian 11/12 为例,给你一套最常用、稳定的 LNMP 安装方式(Nginx + MySQL/MariaDB + PHP)。你可以按需调整版本。


一、准备工作

sudo apt update
sudo apt upgrade -y
sudo apt install -y curl wget gnupg2 ca-certificates lsb-release

二、安装 Nginx

sudo apt install -y nginx

启动并设置开机自启:

sudo systemctl enable --now nginx

测试:

curl http://localhost

三、安装 MySQL / MariaDB

方案 A:MariaDB(推荐,Debian 默认)

sudo apt install -y mariadb-server
sudo systemctl enable --now mariadb

安全初始化:

sudo mysql_secure_installation

方案 B:MySQL(如果你指定要 MySQL)

sudo apt install -y default-mysql-server

四、安装 PHP

以 PHP 8.2 为例(Debian 12 默认可直接装):

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

查看 PHP 版本:

php -v

五、配置 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/php8.2-fpm.sock;
    }
}

测试并重载:

sudo nginx -t
sudo systemctl reload nginx

六、测试 PHP

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

浏览器访问:

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

七、(可选)防火墙

sudo apt install -y ufw
sudo ufw allow 22,80,443/tcp
sudo ufw enable

八、常见补充

  • 启用 HTTPS(Let’s Encrypt):
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx
  • 重启服务命令:
sudo systemctl restart nginx php8.2-fpm mariadb

如果你想要:

  • 一键脚本版(如 lnmp.org)
  • Docker 版 LNMP
  • 指定 PHP 7.4 / 8.1
  • 生产环境优化配置

告诉我你的使用场景,我可以给你更定制的方案。

0 踩