使用 Apache、PHP 7 和 MariaDB 的 Debian 9 LAMP 服务器教程使用 Apache、PHP 7 和 MariaDB 的 Debian 9 LAMP 服务器教程使用 Apache、PHP 7 和 MariaDB 的 Debian 9 LAMP 服务器教程使用 Apache、PHP 7 和 MariaDB 的 Debian 9 LAMP 服务器教程
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

使用 Apache、PHP 7 和 MariaDB 的 Debian 9 LAMP 服务器教程

本教程适用于这些操作系统版本

  • Debian 9(扩展版)
  • Debian 6(压缩版)

在此页

  1. 1 条初步说明
  2. 2 安装 MariaDB 作为 MySQL 的替代品
  3. 3 安装 Apache 网络服务器
  4. 4 安装 PHP 7.1
  5. 5 测试 PHP/获取有关 PHP 安装的详细信息
  6. 6 在 PHP 中获得 MySQL 和 MariaDB 支持
  7. 7 PHP缓存提高PHP速度
  8. 8 phpMyAdmin
  9. 9 为 phpMyAdmin 启用 MySQL root 登录
  10. 10 个链接

LAMP 是 Linux、Apache、MySQL、PHP 的缩写。本教程展示了如何在支持 PHP 7 (mod_php) 和 MariaDB 的 Debian Stretch (9) 服务器上安装 Apache Web 服务器。 MariaDB 是著名的 MySQL 数据库服务器的一个分支,它提供了一个 MySQL 兼容的功能集,并且根据我在互联网上找到的基准测试速度要快一些。 MariaDB 将适用于所有需要 MySQL 的应用程序,如 WordPress、Joomla 等。

LAMP 设置是 CMS 系统(如 Joomla、Wordpress 或 Drupal)的完美基础。

1 初步说明

在本教程中,我使用 IP 地址为 192.168.1.100 的主机名 server1.example.com。这些设置可能因您而异,因此您必须在适当的地方替换它们。

2 安装 MariaDB 作为 MySQL 的替代品

首先,我们像这样安装 MariaDB:

apt-get -y install mariadb-server mariadb-client

接下来,我们将使用 mysql_secure_installation 命令保护 MariaDB。运行以下命令并按照向导进行操作。

mysql_secure_installation

推荐的输入以红色显示。

mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): <-- Hit return
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] <-- y
New password: <-- Enter the new password for the MariaDB root user
Re-enter new password: <-- Enter the password again
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <-- y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <-- y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <-- y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <-- y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

MariaDB 设置现在是安全的。

3 安装 Apache 网络服务器

Apache 作为 Debian 软件包提供,因此我们可以像这样安装它:

apt-get -y install apache2

现在将您的浏览器指向 http://192.168.1.100,您应该会看到 Apache2 占位符页面(有效!):

在 Debian 上 Apache 的默认文档根目录是 /var/www,配置文件是 /etc/apache2/apache2.conf。其他配置存储在 /etc/apache2 目录的子目录中,例如 /etc/apache2/mods-enabled(用于 Apache 模块)、/etc/apache2/sites-enabled(用于虚拟主机)和 /etc/apache2/conf -启用。

4 安装 PHP 7.1

我们可以安装 PHP 和 Apache PHP 模块,如下所示:

apt-get -y install php7.0 libapache2-mod-php7.0

之后我们必须重新启动 Apache:

service apache2 restart

5 测试 PHP/获取有关 PHP 安装的详细信息

默认网站的文档根目录是 /var/www/html。我们现在将在该目录中创建一个小的 PHP 文件 (info.php) 并在浏览器中调用它。该文件将显示有关我们的 PHP 安装的许多有用的详细信息,例如已安装的 PHP 版本。

nano /var/www/html/info.php
<?php
phpinfo();

现在我们在浏览器中调用该文件(例如 http://192.168.1.100/info.php):

如您所见,PHP 7.0 正在运行,并且它通过 Apache 2.0 处理程序运行,如服务器 API 行所示。如果进一步向下滚动,您将看到所有已在 PHP5 中启用的模块。那里没有列出 MySQL/MariaDB,这意味着我们在 PHP5 中还没有 MySQL 支持。

6 在 PHP 中获得 MySQL 和 MariaDB 支持

为了在 PHP 中获得 MySQL 支持,我们将安装 php7.0-mysql 包。最好安装一些其他 PHP 模块,因为您的应用程序可能需要它们。您可以像这样搜索可用的 PHP 7 模块:

apt-cache search php7.0

选择你需要的并像这样安装它们:

apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl

现在重新启动 Apache:

service apache2 restart

7 PHP缓存提高PHP速度

为了加速 PHP,应该安装 Opcache。检查 PHP Opcache 模块是否已正确安装和启用。运行此命令:

php --version

输出应包含我用红色标记的行。

PHP 7.0.27-0+deb9u1 (cli) (built: Jan 5 2018 13:51:52) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.27-0+deb9u1, Copyright (c) 1999-2017, by Zend Technologies

如果您没有在结果中看到 Opcache 模块,请使用以下命令安装它:

apt-get -y install php7.0-opcache

还有一个可能有用的缓存,它的名字是 APCu。 APCu 是一个免费的 PHP 操作码缓存器,用于缓存和优化 PHP 中间代码。

APCu 可以按如下方式安装:

apt-get -y install php-apcu

现在重新启动 Apache:

service apache2 restart

现在在浏览器中重新加载 http://192.168.1.100/info.php 并再次向下滚动到模块部分。你现在应该在那里找到很多新模块,包括用作 MariaDB 驱动程序的 MySQL 模块:

8 php我的管理员

phpMyAdmin 是一个 Web 界面,您可以通过它管理 MySQL 和 MariaDB 数据库。安装它是个好主意:

apt-get -y install phpmyadmin

您将看到以下问题:

Web server to reconfigure automatically: <-- apache2
Configure database for phpmyadmin with dbconfig-common?<-- Yes
MySQL application password for phpmyadmin: <-- Press enter, apt will create a random password automatically.

之后就可以在http://192.168.1.100/phpmyadmin/下访问phpMyAdmin了:

9 为 phpMyAdmin 启用 MySQL root 登录

虽然您可以在 shell 上以 root 用户身份登录 MariaDB,但 root 登录在 phpMyAdmin 中不起作用。要允许 root 用户也使用 phpMyAdmin,请在 shell 上运行以下命令:

echo "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';FLUSH PRIVILEGES;" | mysql -u root -p

10 个链接

  • 阿帕奇:http://httpd.apache.org/
  • PHP:http://www.php.net/
  • MySQL:http://www.mysql.com/
  • Debian:http://www.debian.org/
  • phpMyAdmin:http://www.phpmyadmin.net/

©2015-2025 艾丽卡 support@alaica.com