---
title: "Debian / Ubuntu 使用源安装 LAMP 教程"
tags: ['Debian', 'Ubuntu', 'Apache', 'PHP', 'MySQL', 'MariaDB']
published: 2022-01-23 19:49:04
updated: 2026-07-05 00:00:00
excerpt: "本文将介绍使用官方源和第三方源在 Debian 和 Ubuntu 安装最新版 Apache 2 + PHP + MySQL 的教程,并且可以自行选择 PHP 版本。"
---
> 本文件是烧饼博客文章的 Markdown 原文,正式版本见 https://u.sb/debian-install-apache-php-mysql/
> 全站文章与页面索引见 https://u.sb/llms.txt,建议先读索引再继续抓取。
本文将介绍使用官方源和第三方源在 Debian 和 Ubuntu 安装最新版 Apache 2 + 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 dist-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、增加 Ondřej Surý 大神打包的 PHP 源并安装 PHP 8.x
和 [LEMP](/debian-install-nginx-php-mysql/) 安装方法一样,我们还是使用 [Ondřej Surý](https://deb.sury.org/) 大佬打包的 PHP 源。
至于为啥先装 PHP 再装 Apache 2,因为装了 PHP 以后 Apache 2 会识别你 PHP 版本然后生成对应的配置文件,所以顺序不要搞反。
### 2.1 Debian 和 Ubuntu 安装 LAMP 区别
唯一区别就是 Apache 2 的安装添加源方法不一样(PPA 过时以后 PHP 源两边已经一模一样了),其他的步骤都一毛一样。
### 2.2 加入大神做好的源
以前 Ubuntu 用的 [PPA for PHP](https://launchpad.net/~ondrej/+archive/ubuntu/php) 已经不再更新,现在 Debian 和 Ubuntu 都直接添加 deb.sury.org 的源:
```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
```
Debian 下也可以直接使用 [extrepo](/debian-extrepo/):
```bash
sudo apt update
sudo apt install extrepo -y
sudo extrepo enable sury
```
### 2.3 更新系统源
```bash
sudo apt update
sudo apt upgrade -y
```
### 2.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` 增加上传大小限制
```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、增加 Ondřej Surý 大神打包的 Apache 2 源并安装
这里我们推荐 [Ondřej Surý](https://deb.sury.org/) 打包的 Apache 2 源。
### 3.1 Debian 和 Ubuntu 安装 LAMP 区别
唯一区别就是 Apache 2 的安装添加源方法不一样,其他的步骤都一毛一样。
### 3.2 首先增加 Apache 源
```bash
sudo wget -O /usr/share/keyrings/apache2.gpg https://packages.sury.org/apache2/apt.gpg
echo "deb [signed-by=/usr/share/keyrings/apache2.gpg] https://packages.sury.org/apache2/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/apache2.list
```
```bash
sudo add-apt-repository ppa:ondrej/apache2
```
Debian 下也可以直接使用 [extrepo](/debian-extrepo/):
```bash
sudo extrepo enable sury_apache2
```
### 3.3 接着更新并安装 Apache 2.4
```bash
sudo apt update
sudo apt upgrade -y
sudo apt install apache2 -y
```
安装完毕后,我们可以使用 `apachectl -v` 命令看到 Apache 2 已经是最新的 2.4 版本了
```bash
root@debian ~ # apachectl -v
Server version: Apache/2.4.67 (Debian)
Server built: 2026-06-05T10:55:53
```
### 3.4 Apache 2 开启 PHP-FPM 支持
首先,需要开启 Apache 2 的 PHP-FPM 支持,我们以 PHP 8.5 为例,按照自己的需求,可以开启如下模块
```bash
sudo a2enconf php8.5-fpm
sudo a2enmod proxy_fcgi
sudo a2enmod headers
sudo a2enmod http2
sudo a2enmod remoteip
sudo a2enmod ssl
sudo a2enmod rewrite
sudo a2enmod expires
sudo a2enmod deflate
sudo a2enmod mime
sudo a2enmod setenvif
```
然后我们重启 PHP-FPM 服务
```bash
sudo systemctl restart php8.5-fpm
```
对应 PHP 7.4.x 命令如下
```bash
sudo systemctl restart php7.4-fpm
```
Apache 2 参考配置文件如下,因为默认 Debian 的 Apache 2 默认配置已经使用了 `example.com` 这个域名,所以我们以 `example.org` 为例,新建立个 `/etc/apache2/sites-available/example.org.conf`
```bash
sudo bash -c 'cat > /etc/apache2/sites-available/example.org.conf << EOF
ServerName example.org
DocumentRoot /var/www/example.org
DirectoryIndex index.php index.html index.htm
ErrorLog \${APACHE_LOG_DIR}/example.org.error.log
CustomLog \${APACHE_LOG_DIR}/example.org.access.log combined
Options FollowSymLinks
AllowOverride All
Require all granted
EOF'
```
然后使用 `a2ensite` 命令使其生效
```bash
sudo a2ensite example.org.conf
```
*如果不需要这个 vhost 的时候可以使用 `a2dissite example.org.conf` 命令移除*
检测是否已经软链接到 `/etc/apache2/sites-enabled` 目录
```bash
root@debian ~ # ls /etc/apache2/sites-enabled
000-default.conf example.org.conf
```
到这里基本没有问题,我们可以执行 `apache2ctl configtest` 命令检查配置文件是否出错
```bash
root@debian ~ # apache2ctl configtest
Syntax OK
```
显示 `Syntax OK` 则说明所有配置文件均无问题,可以重启 Apache 2 使我们的配置生效
```bash
sudo systemctl restart apache2
```
我们的目录在 `/var/www/example.org`,我们先创建这个目录
```bash
sudo mkdir -p /var/www/example.org
```
然后创建一个 `phpinfo.php` 并输入 `phpinfo()` 函数
```bash
sudo bash -c 'cat > /var/www/example.org/phpinfo.php << EOF
EOF'
```
好了,此时在浏览器输入 `http://example.org/phpinfo.php`,如果看到经典的 `phpinfo` 页面则说明安装成功,如果不成功,请仔细对比步骤查找哪里出错或在烧饼博客下方留言。
最终效果如下:

## 4、安装 MariaDB
这里我们使用 MariaDB 作为 MySQL 的代替
### 4.1 首先,添加并导入 Mariadb 的官方源
按照官方的[教程](https://mariadb.com/kb/en/gpg/)导入 GPG
下载 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://ip.sb/password/)生成一个强大的随机密码,比如 32 位,然后随意挑选一个使用*
```bash
sudo apt install pwgen
pwgen 32
```
使用 MySQL 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.org/mysql-test.php` 文件并测试
```bash
sudo tee /var/www/example.org/mysql-test.php > /dev/null << 'EOF'
EOF
```
创建完毕后访问 `http://example.org/mysql-test.php` 如果出现 `MySQL is working fine. There are no tables.` 则说明 MySQL 工作正常。
## 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 安装最新版 LAMP 的教程,如有问题可以随时发评论留言讨论。