在本页面上

  • 先决条件
  • 安装 Apache Web 服务器
  • 安装 MariaDB 数据库服务器
  • 安装 PHP 处理语言
  • 为 Apache 创建虚拟主机
  • 验证 PHP 在 Apache 上的安装
  • 结论

LAMP Stack 是一组常用的开源工具,用于在 Web 上托管网站和应用程序。LAMP Stack 是一个 Web 开发平台,它使用 Linux 作为操作系统,Apache 作为 Web 服务器,MariaDB/MySQL 作为数据库服务器,PHP 作为脚本语言。它被广泛使用,并提供了一套经过验证的软件,用于交付高性能的 Web 应用程序。

在本教程中,我们将向您展示如何在 Debian 11 上安装 LAMP stack。

先决条件

  • 一台运行 Debian 11 的服务器。
  • 一个指向您服务器 IP 的有效域名。
  • 服务器上已配置 root 密码。

安装 Apache Web 服务器

Apache webserver 软件包包含在 Debian 11 的默认仓库中。您只需运行以下命令即可安装它:

apt-get install apache2 apache2-utils -y

安装 Apache Web 服务器后,使用以下命令验证已安装的 Apache 版本:

apache2 -v

您应该看到以下输出:

Server version: Apache/2.4.48 (Debian)
Server built:   2021-08-12T11:51:47

要启动 Apache 服务,请运行以下命令:

systemctl start apache2

要使 Apache 服务在系统启动时自动启动,请运行以下命令:

systemctl enable apache2

您还可以使用以下命令检查 Apache 的完整状态:

apt-get install elinks
apache2ctl fullstatus

您应该看到以下输出:

 Apache Server Status for localhost (via ::1)

   Server Version: Apache/2.4.48 (Debian)

   Server MPM: prefork

   Server Built: 2021-08-12T11:51:47

   --------------------------------------------------------------------------

   Current Time: Tuesday, 17-Aug-2021 09:29:41 UTC

   Restart Time: Tuesday, 17-Aug-2021 09:29:33 UTC

   Parent Server Config. Generation: 1

   Parent Server MPM Generation: 0

   Server uptime: 7 seconds

   Server load: 0.05 0.01 0.00

   Total accesses: 0 - Total Traffic: 0 kB - Total Duration: 0

   CPU Usage: u.03 s.02 cu0 cs0 - .714% CPU load

   0 requests/sec - 0 B/second

   1 requests currently being processed, 4 idle workers

默认情况下,Apache 运行在 80 端口上,您可以使用以下命令进行检查:

ss -antpl | grep apache2

您应该得到以下输出:

LISTEN 0      511                *:80              *:*    users:(("apache2",pid=44022,fd=4),("apache2",pid=44021,fd=4),("apache2",pid=44020,fd=4),("apache2",pid=44019,fd=4),("apache2",pid=44018,fd=4),("apache2",pid=44017,fd=4))

您还可以通过浏览 URL http://your-server-ip 来检查 Apache 的安装。您应该会在以下屏幕上看到 Apache 测试页面:

在 Debian 11 上安装 LAMP 栈的完整详细步骤教程

安装 MariaDB 数据库服务器

MariaDB 是一个数据库管理系统,用于存储和管理您网站的数据。您可以使用以下命令安装它:

apt-get install mariadb-server -y

安装 MariaDB 后,使用以下命令启动 MariaDB 服务并使其在系统启动时自动启动:

systemctl start mariadb
systemctl enable mariadb

您还可以使用以下命令检查 MariaDB 服务的状态:

systemctl status mariadb

您应该看到以下输出:

? mariadb.service - MariaDB 10.3.31 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-08-21 04:13:25 UTC; 1min 36s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 1838 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2353)
     Memory: 66.1M
     CGroup: /system.slice/mariadb.service
             ??1838 /usr/sbin/mysqld

还建议您保护 MariaDB 安装并设置 MariaDB root 密码。您可以运行 mysql_secure_installation 脚本来保护 MariaDB 安装:

mysql_secure_installation

系统将要求您设置 MariaDB root 密码、删除匿名用户、禁止 root 远程登录并删除测试数据库,如下所示:

Enter current password for root (enter for none): 
Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

默认情况下,MariaDB 运行在 3306 端口上。您可以使用以下命令进行检查:

ss -antpl | grep mariadb

您应该看到以下命令:

LISTEN 0      80         127.0.0.1:3306      0.0.0.0:*    users:(("mariadbd",pid=12181,fd=15))

要连接 MariaDB shell,请运行以下命令:

mysql -u root -p

提供您的 MariaDB root 密码并按 Enter 键以连接到 MariaDB shell。连接后,您应该看到以下输出:

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 64
Server version: 10.5.11-MariaDB-1 Debian 11

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

现在,运行以下命令以检查 MariaDB 版本:

MariaDB [(none)]> SELECT VERSION();

您应该看到以下输出:

+-------------------+
| VERSION()         |
+-------------------+
| 10.5.11-MariaDB-1 |
+-------------------+
1 row in set (0.001 sec)

安装 PHP 处理语言

PHP 是用于在 Web 服务器上提供 PHP 应用程序的处理语言。默认情况下,Debian 11 中可用的 PHP 版本是 PHP 7.4。您可以使用以下命令安装 PHP 以及其他常用扩展:

apt-get install php libapache2-mod-php php-cli php-mysql php-zip php-curl php-xml -y

安装 PHP 后,使用以下命令验证 PHP 版本:

php -v

您应该看到以下输出:

PHP 7.4.21 (cli) (built: Jul  2 2021 03:59:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies

为 Apache 创建虚拟主机

此时,LAMP stack 已安装在您的服务器上。现在,您需要创建一个 Apache 虚拟主机配置文件来托管任何应用程序。

在这里,我们将创建一个 example.com 目录来托管 PHP 页面。您可以使用以下命令创建它:

mkdir /var/www/html/example.com

接下来,将 example.com 目录的所有权设置为 www-data:

chown -R www-data:www-data /var/www/html/example.com

接下来,使用以下命令创建一个 Apache 虚拟主机配置文件:

nano /etc/apache2/sites-available/example.com.conf

添加以下行:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com
    DocumentRoot /var/www/html/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

保存并关闭文件,然后使用以下命令激活 example.com 虚拟主机并禁用默认虚拟主机:

a2ensite example.com.conf
a2dissite 000-default

接下来,使用以下命令检查虚拟主机文件是否存在任何语法错误:

apache2ctl configtest

如果一切正常,您应该看到以下输出:

Syntax OK

最后,重启 Apache 服务以应用配置更改:

systemctl reload apache2

您还可以使用以下命令检查 Apache 服务的状态:

systemctl status apache2

您应该看到以下输出:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-08-21 04:13:30 UTC; 2min 56s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 2257 (apache2)
      Tasks: 55 (limit: 2353)
     Memory: 5.3M
     CGroup: /system.slice/apache2.service
             ??2257 /usr/sbin/apache2 -k start
             ??2259 /usr/sbin/apache2 -k start
             ??2260 /usr/sbin/apache2 -k start

Aug 21 04:13:30 debian11 systemd[1]: Starting The Apache HTTP Server...
Aug 21 04:13:30 debian11 apachectl[2246]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 216.9>
Aug 21 04:13:30 debian11 systemd[1]: Started The Apache HTTP Server.

验证 PHP 在 Apache 上的安装

接下来,您需要创建一个示例 PHP 文件,以便通过 Apache Web 服务器提供访问。您可以在 example.com 目录中创建一个 phpinfo.php 文件:

nano /var/www/html/example.com/phpinfo.php

添加以下行:

<?php phpinfo(); ?>

保存并关闭文件,然后打开 Web 浏览器并使用 URL http://example.com/phpinfo.php 访问您的 PHP 页面。您应该会在以下屏幕上看到您的 PHP 页面:

在 Debian 11 上安装 LAMP 栈的完整详细步骤教程

结论

在上述指南中,我们解释了如何在 Debian 11 上安装 LAMP stack。现在您可以开始使用 LAMP stack 托管您的第一个网站。如果您有任何问题,请随时向我提问。