--- title: "Debian / Ubuntu 使用源安装 LEMP 教程" tags: ['Debian', 'Ubuntu', 'Nginx', 'PHP', 'MySQL', 'MariaDB'] published: 2022-01-23 19:49:04 updated: 2026-07-05 00:00:00 excerpt: "本文将介绍使用官方源和第三方源在 Debian 和 Ubuntu 安装最新版 Nginx + PHP + MySQL 的教程,并且可以自行选择 PHP 版本。" --- > 本文件是烧饼博客文章的 Markdown 原文,正式版本见 https://u.sb/debian-install-nginx-php-mysql/ > 全站文章与页面索引见 https://u.sb/llms.txt,建议先读索引再继续抓取。 本文将介绍使用官方源和第三方源在 Debian 和 Ubuntu 安装最新版 Nginx + PHP + MySQL 的教程,并且可以自行选择 PHP 版本。 **2026 年 7 月 9 日更新** - Ubuntu 的 PPA for PHP 已经过时,现在 Ubuntu 也和 Debian 一样直接添加 deb.sury.org 源。 **2026 年 7 月 5 日更新** - 升级 MariaDB 到 12.3 LTS。 - 更新 MySQL 安装文件包。 **PS:本文适用于 Debian Stable 以及 Ubuntu LTS** 以下操作需要 root 权限,本文命令均已带上 `sudo` 前缀,请确保当前用户拥有 `sudo` 权限;或者直接使用 root 用户操作并省略命令中的 `sudo`。 ## 1、更新系统并安装部分必要软件 ```bash sudo apt update sudo apt upgrade -y sudo apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates ``` **如果您通过 iso 方式安装 Debian 并且设置了 root 密码,则默认不带 `sudo` 包,使用 `apt install sudo` 安装即可** ## 2、增加烧饼博客打包的 Nginx 源并安装 这里我们推荐[烧饼博客](https://n.wtf/)团队打包的 Nginx 源,这货是在[官方 Nginx 打包组](https://salsa.debian.org/nginx-team)的基础上,保持更新最新版本的 Nginx 以及 OpenSSL。 ### 2.1 首先增加 GPG Key ```bash curl -sSL https://n.wtf/public.key | sudo gpg --dearmor -o /usr/share/keyrings/n.wtf.gpg ``` ### 2.2 然后增加 Nginx 源 ```bash echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/n.wtf.gpg] https://mirror-cdn.xtom.com/sb/nginx/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/n.wtf.list ``` 国内机器可以用[清华 TUNA](https://mirrors.tuna.tsinghua.edu.cn/) 的国内源: ```bash echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/n.wtf.gpg] https://mirrors.tuna.tsinghua.edu.cn/n.wtf/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/n.wtf.list ``` Debian 下也可以直接使用 [extrepo](/debian-extrepo/): ```bash sudo apt update sudo apt install extrepo -y sudo extrepo enable n.wtf ``` ### 2.3 接着更新并安装 Nginx ```bash sudo apt update sudo apt install nginx-extras -y ``` 安装完毕后,我们可以使用 `nginx -V` 命令看到 Nginx 已经是最新的 1.31.2 主线版了: ```bash root@debian ~ # nginx -V nginx version: nginx-n.wtf/1.31.2 built by gcc 14.2.0 (Debian 14.2.0-19) built with OpenSSL 4.0.1 9 Jun 2026 TLS SNI support enabled ``` ## 3、增加 Ondřej Surý 大神打包的 PHP 源并安装 PHP 8.x [Ondřej Surý](https://deb.sury.org/) 大佬打包的 PHP 源更好用。以前 Ubuntu 用的是这位大佬维护的 [PPA for PHP](https://launchpad.net/~ondrej/+archive/ubuntu/php),不过 PPA 已经不再更新,现在 Ubuntu 也和 Debian 一样直接添加 deb.sury.org 的源即可,下面一步一步来。 ### 3.1 Debian 和 Ubuntu 安装 LEMP 区别 现在没有区别了:自从 PPA 过时以后,Ubuntu 和 Debian 都直接添加 deb.sury.org 的源,所有步骤都一毛一样。 ### 3.2 加入大神做好的源 ```bash sudo wget -O /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list ``` 这个大神的 GPG 密钥每两年会更新一个新的,如果 GPG 密钥失效,重新下载 GPG 密钥即可。 Debian 下也可以直接使用 [extrepo](/debian-extrepo/): ```bash sudo extrepo enable sury ``` 国内机器可以用[南京大学](https://mirror.nju.edu.cn/) 的国内源: ```bash sudo wget -O /usr/share/keyrings/deb.sury.org-php.gpg https://mirror.nju.edu.cn/sury/php/apt.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://mirror.nju.edu.cn/sury/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list ``` ### 3.3 更新系统 ```bash sudo apt update sudo apt upgrade -y ``` ### 3.4 安装自己需要的 PHP 版本 这个源目前默认的 PHP 是 8.5.x,如果您需要其他版本,那么请修改对应的 PHP 版本号 (注意配置文件哦)。 这里举例 WordPress 需要的部分 PHP 包: 安装 PHP 8.5.x (从 PHP 8.5 开始 OPcache 已经强制集成,不需要单独安装): ```bash sudo apt install php8.5-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,soap,gmp,bcmath} -y ``` 安装 PHP 8.4.x: ```bash sudo apt install php8.4-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,opcache,soap,gmp,bcmath} -y ``` 安装 PHP 8.3.x: ```bash sudo apt install php8.3-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,opcache,soap,gmp,bcmath} -y ``` 安装 PHP 8.2.x: ```bash sudo apt install php8.2-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,opcache,soap,gmp,bcmath} -y ``` **以下版本 PHP 已经 EOL,PHP 官方不再提供支持,请尽快更新您的程序兼容最新的 PHP,如果您的程序还未兼容,建议鞭策开发者** 安装 PHP 8.1.x: ```bash sudo apt install php8.1-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,opcache,soap,gmp,bcmath} -y ``` 安装 PHP 8.0.x: ```bash sudo apt install php8.0-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,opcache,soap,gmp,bcmath} -y ``` 安装 PHP 7.4.x: ```bash sudo apt install php7.4-{fpm,cli,mysql,curl,gd,mbstring,xml,xmlrpc,zip,json,imap,opcache,soap,gmp,bcmath} -y ``` 安装 PHP 7.3.x: ```bash sudo apt install php7.3-{fpm,mysql,curl,gd,mbstring,xml,xmlrpc,zip,opcache} ``` 安装 PHP 7.2.x (PHP 7.2 开始已经不支持 mcrypt 组件): ```bash sudo apt install php7.2-{fpm,mysql,curl,gd,mbstring,xml,xmlrpc,zip,opcache} ``` 安装 PHP 7.1.x: ```bash sudo apt install php7.1-{fpm,mysql,curl,gd,mbstring,mcrypt,xml,xmlrpc,zip,opcache} ``` 安装 PHP 7.0.x: ```bash sudo apt install php7.0-{fpm,mysql,curl,gd,mbstring,mcrypt,xml,xmlrpc,zip,opcache} ``` 安装 PHP 5.6.x: ```bash sudo apt install php5.6-{fpm,mysql,curl,gd,mbstring,mcrypt,xml,xmlrpc,zip,opcache} ``` 如果希望安装其他组件,可以通过搜索看看有没有对应的包: ```bash apt-cache search php8.5* | grep php8.5 ``` 修改 `php.ini` 防止跨目录攻击,如果安装的 PHP 8.5.x 请修改 `/etc/php/8.5/fpm/php.ini` PHP 7.4.x 请对应修改 `/etc/php/7.4/fpm/php.ini`: ```bash sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/8.5/fpm/php.ini ``` 修改 `php.ini` 增加上传大小限制,比如我们设置 10MB: ```bash sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 10M/' /etc/php/8.5/fpm/php.ini sudo sed -i 's/post_max_size = 8M/post_max_size = 10M/' /etc/php/8.5/fpm/php.ini ``` 您也可以同时安装多个 PHP 版本,然后使用以下命令选择系统默认的 PHP 版本: ```bash sudo update-alternatives --config php ``` ### 3.5 重启 PHP 和 Nginx ```bash sudo systemctl restart php8.5-fpm ``` 对应 PHP 7.4.x 命令如下: ```bash sudo systemctl restart php7.4-fpm ``` Nginx 参考配置文件如下,新建立个 `/etc/nginx/sites-available/example.com.conf`: ```bash sudo bash -c 'cat > /etc/nginx/sites-available/example.com.conf << EOF server { listen 80; listen [::]:80; # 指定网站目录,可根据自己情况更换,建议放在 /var/www 目录下 root /var/www/example.com; index index.php index.html index.htm; # 默认第一个域名,替换 example.com 为您的域名 server_name example.com; location / { try_files \$uri \$uri/ =404; } # 开启 PHP8.5-fpm 模式,如需要安装 PHP 7.4.x 请修改为 fastcgi_pass unix:/run/php/php7.4-fpm.sock; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.5-fpm.sock; } } EOF' ``` 然后把这个配置文件软链接到 `/etc/nginx/sites-enabled` 目录使其生效: ```bash sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf ``` 到这里基本没有问题,可以直接重新加载 Nginx: ```bash sudo nginx -t sudo nginx -s reload ``` 或者暴力点直接重启 Nginx: ```bash sudo systemctl restart nginx ``` 我们的目录在 `/var/www/example.com`,我们先创建这个目录: ```bash sudo mkdir -p /var/www/example.com ``` 然后创建一个 `phpinfo.php` 并输入 `phpinfo()` 函数: ```bash sudo bash -c 'cat > /var/www/example.com/phpinfo.php << EOF EOF' ``` 好了,此时在浏览器输入 `http://example.com/phpinfo.php`,如果看到经典的 `phpinfo` 页面则说明安装成功,如果不成功,请仔细对比步骤查找哪里出错。 效果如下: ![Debian Install LEMP PHPINFO](https://s.bh.sb/images/debian-lemp-php-info.png) ## 4、安装 MariaDB 自从 Debian 9.x Stretch 开始,Debian [已经默认使用](https://mariadb.com/resources/blog/mariadb-server-default-debian-9) Mariadb,所以我们不做对于 MySQL 和 MariaDB 的争论,直接跟着开源社区走即可。 ### 4.1 首先,添加并导入 Mariadb 的官方源 下载 GPG Key: ```bash curl -sSL https://supplychain.mariadb.com/MariaDB-Server-GPG-KEY | sudo gpg --dearmor -o /usr/share/keyrings/mariadb.gpg ``` 然后添加 MariaDB 的源:
```bash echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://dlm.mariadb.com/repo/mariadb-server/12.3/repo/debian $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/mariadb.list ```
```bash echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://dlm.mariadb.com/repo/mariadb-server/12.3/repo/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/mariadb.list ```
Debian 下也可以直接使用 [extrepo](/debian-extrepo/): ```bash sudo extrepo enable mariadb-12.3 ``` 国内可以用清华 TUNA 的源:
```bash echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/12.3/debian $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/mariadb.list ```
```bash echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/12.3/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/mariadb.list ```
您也可以[在这儿](https://mariadb.com/kb/en/mirror-sites-for-mariadb/)找到更多的 MariaDB 源。 ### 4.2 接着更新一下系统 ```bash sudo apt update ``` ### 4.3 然后直接安装最新稳定版 MariaDB ```bash sudo apt install mariadb-server mariadb-client ``` 安装完毕后强烈推荐使用 `sudo mariadb-secure-installation` 命令做一次安全设置。 ### 4.4 创建数据库并测试 *开启数据库之前,您可以使用 `pwgen` 这个小工具或者[随机密码生成器](https://free.tools/tools/random-password-generator/)生成一个强大的随机密码,比如 32 位,然后随意挑选一个使用* ```bash sudo apt install pwgen pwgen 32 ``` 使用 Mariadb root 用户登陆,因为默认使用 Unix domain socket 模式,所以本机不需要 MySQL root 密码即可登录: ```sql sudo mariadb -u root ``` 创建数据库 `example_database`: ```sql CREATE DATABASE example_database DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ``` 创建用户名 `example_user` 并赋予权限: ```sql GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY '这里改成您要设置的强大的没人能猜出来的随机的密码'; ``` 刷新 MySQL 权限: ```sql FLUSH PRIVILEGES; ``` 没问题以后就可以退出了: ```sql EXIT; ``` 新建立一个 `/var/www/example.com/mysql-test.php` 文件并测试: ```bash sudo tee /var/www/example.com/mysql-test.php > /dev/null << 'EOF' EOF ``` 创建完毕后访问 `http://example.com/mysql-test.php` 如果出现 `MySQL is working fine. There are no tables.` 则说明 MariaDB 工作正常。 ## 5、安装 MySQL (可选) 如果您必须使用某些 MySQL 才有的功能,那么可以按照 MySQL [官网的教程](https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#apt-repo-fresh-install)安装 MySQL。 *注意:除非您知道您在做什么,否则不要同时安装 MySQL 和 MariaDB。* ### 5.1 添加 apt 源 ```bash wget https://repo.mysql.com/mysql-apt-config_0.8.36-1_all.deb sudo dpkg -i mysql-apt-config_0.8.36-1_all.deb ``` 国内的机器可以在添加完成后修改为清华 TUNA 源,您可以修改 `/etc/apt/sources.list.d/mysql-community.list` 文件,替换成如下内容: ```bash deb https://mirrors.tuna.tsinghua.edu.cn/mysql/apt/debian trixie mysql-8.0 mysql-8.4-lts mysql-apt-config mysql-tools ``` Debian 下也可以直接使用 [extrepo](/debian-extrepo/): ```bash sudo extrepo enable mysql-lts ``` ### 5.2 安装 MySQL ```bash sudo apt update sudo apt install mysql-server -y ``` 默认 MySQL 会安装最新的 8.4 版本,如果您需要更低的版本,比如 8.0,可以使用如下命令: ```bash sudo dpkg-reconfigure mysql-apt-config ``` 您可能需要设置一个强大的 root 密码,接下来的步骤和 MariaDB 基本相同,把 `mariadb` 命令换成 `mysql` 命令即可,本文不再赘述。 好了,以上就是基本的 Debian 和 Ubuntu 安装最新版 LEMP 的教程,如有问题可以随时发评论留言讨论。