文章 文章 posts

Debian 安装 LNMP 环境教程

LNMP 是 Web 开发中最常用的后端环境之一,包含:

  • Linux:系统平台(这里为 Debian)
  • Nginx:高性能 Web 服务器
  • MySQL/MariaDB:数据库服务
  • PHP-FPM:动态脚本解析

一、系统准备

1. 更新系统软件包

bash
sudo apt update && sudo apt upgrade -y
代码

### 2. 安装基础依赖

```
bash
sudo apt install -y curl wget git vim unzip ufw software-properties-common lsb-release ca-certificates

```

## 二、安装 Nginx

### 1. 安装 Nginx

```
bash
sudo apt install -y nginx

```

### 2. 启动并设置开机自启

```
bash
sudo systemctl enable nginx
sudo systemctl start nginx

```

### 3. 测试安装是否成功

打开浏览器访问服务器 IP 或执行:

```
bash
curl -I http://localhost

```

若返回 `200 OK` 则表示安装成功。

## 三、安装 MySQL(或 MariaDB)

### 1. 安装 MySQL

```
bash
sudo apt install -y mysql-server

```

或安装 MariaDB(推荐):

```
bash
sudo apt install -y mariadb-server

```

### 2. 启动与安全配置

```
bash
sudo systemctl enable mysql
sudo systemctl start mysql

sudo mysql_secure_installation

```

按提示设置 root 密码、删除匿名用户、禁用远程 root 登录、删除测试数据库等。

### 3. 测试登录

```
bash
mysql -u root -p

```

## 四、安装 PHP

### 1. 添加 PHP 官方仓库(可选,安装新版本)

```
bash
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

```

### 2. 安装 PHP 与扩展

```
bash
sudo apt install -y php php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-bcmath php-opcache

```

### 3. 检查 PHP 版本

```
bash
php -v

```

## 五、配置 Nginx 支持 PHP

### 1. 编辑默认站点配置

```
bash
sudo vim /etc/nginx/sites-available/default

```

修改部分内容如下:

```
nginx
server {
    listen 80;
    server_name _;
    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

```

> 注意:`php8.2-fpm.sock` 根据实际 PHP 版本调整。

### 2. 测试配置语法并重启 Nginx

```
bash
sudo nginx -t
sudo systemctl reload nginx

```

## 六、测试 PHP 环境

创建测试文件:

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

```

在浏览器访问:

```

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

```

如果能看到 PHP 信息页面,说明 LNMP 环境安装成功。

## 七、PHP 性能优化(可选)

编辑 PHP 配置文件(通常在 `/etc/php/8.x/fpm/php.ini`):

```
bash
sudo vim /etc/php/8.x/fpm/php.ini

```

优化参数示例:

```
ini
memory_limit = 256M
upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 300
date.timezone = Asia/Shanghai

```

## 八、启用 PHP OPcache(推荐)

OPcache 可显著提高 PHP 性能。

确认模块已启用:

```
bash
php -m | grep opcache

```

若未启用,在 `/etc/php/8.x/fpm/php.ini` 中取消注释并调整:

```
ini
[opcache]
zend_extension=opcache
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

```

重启服务:

```
bash
sudo systemctl restart php8.x-fpm
sudo systemctl reload nginx

```

## 九、防火墙设置

仅开放 Web 常用端口:

```
bash
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

```

## 十、总结

| 组件          | 功能     | 管理命令                            |
| ------------- | -------- | ----------------------------------- |
| Nginx         | Web 服务 | `sudo systemctl restart nginx`      |
| MySQL/MariaDB | 数据库   | `sudo systemctl restart mysql`      |
| PHP-FPM       | 动态脚本 | `sudo systemctl restart php8.x-fpm` |

至此,你已在 Debian 上成功部署 LNMP 环境,可用于 WordPress、Laravel 等 PHP 项目运行。
```
`