当前位置:   article > 正文

如何在Debian 10上安装Linux,Apache,MariaDB,PHP(LAMP)堆栈

debian能ashmem_linux

介绍 (Introduction)

A “LAMP” stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is stored in a MariaDB database, and dynamic content is processed by PHP.

“ LAMP”堆栈是一组开源软件,通常会一起安装以使服务器能够托管动态网站和Web应用程序。 该术语实际上是首字母缩写词,代表A pache Web服务器的L inux操作系统。 站点数据被存储在A M ariaDB数据库,和动态内容被P HP处理。

Although this software stack typically includes MySQL as the database management system, some Linux distributions — including Debian — use MariaDB as a drop-in replacement for MySQL.

尽管此软件堆栈通常包括MySQL作为数据库管理系统,但某些Linux发行版(包括Debian)使用MariaDB替代MySQL。

In this guide, we will install a LAMP stack on a Debian 10 server, using MariaDB as the database management system.

在本指南中,我们将使用MariaDB作为数据库管理系统,在Debian 10服务器上安装LAMP堆栈。

先决条件 (Prerequisites)

In order to complete this tutorial, you will need to have a Debian 10 server with a non-root sudo-enabled user account and a basic firewall. This can be configured using our initial server setup guide for Debian 10.

为了完成本教程,您将需要具有Debian 10服务器,该服务器具有未启用root用户的sudo用户帐户和基本防火墙。 可以使用我们的Debian 10初始服务器设置指南进行配置。

第1步-安装Apache并更新防火墙 (Step 1 — Installing Apache and Updating the Firewall)

The Apache web server is among the most popular web servers in the world. It’s well-documented and has been in wide use for much of the history of the web, which makes it a great default choice for hosting a website.

Apache Web服务器是世界上最受欢迎的Web服务器之一。 它有据可查,并且已在网络的许多历史中得到广泛使用,这使其成为托管网站的理想默认选择。

Install Apache using Debian’s package manager, APT:

使用Debian的软件包管理器APT安装Apache:

  • sudo apt update

    sudo apt更新
  • sudo apt install apache2

    须藤apt install apache2

Since this is a sudo command, these operations are executed with root privileges. It will ask you for your regular user’s password to verify your intentions.

由于这是sudo命令,因此将以root特权执行这些操作。 它将要求您提供普通用户的密码以验证您的意图。

Once you’ve entered your password, apt will tell you which packages it plans to install and how much extra disk space they’ll take up. Press Y and hit ENTER to continue, and the installation will proceed.

输入密码后, apt会告诉您它计划安装哪些软件包以及它们将占用多少磁盘空间。 按Y然后按ENTER继续,安装将继续。

Next, assuming that you have followed the initial server setup instructions by installing and enabling the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic.

接下来,假设您已按照初始服务器设置说明进行操作, 并安装并启用了UFW防火墙 ,请确保防火墙允许HTTP和HTTPS通信。

When installed on Debian 10, UFW comes loaded with app profiles which you can use to tweak your firewall settings. View the full list of application profiles by running:

在Debian 10上安装时,UFW随附了应用程序配置文件,您可以使用它们来调整防火墙设置。 通过运行以下命令查看应用程序配置文件的完整列表:

  • sudo ufw app list

    sudo ufw应用程序列表

The WWW profiles are used to manage ports used by web servers:

WWW配置文件用于管理Web服务器使用的端口:

  1. Output
  2. Available applications:
  3. . . .
  4. WWW
  5. WWW Cache
  6. WWW Full
  7. WWW Secure
  8. . . .

If you inspect the WWW Full profile, it shows that it enables traffic to ports 80 and 443:

如果检查“ WWW Full配置文件,则表明它启用了到端口80443

  • sudo ufw app info "WWW Full"

    sudo ufw应用程序信息“ WWW Full”
  1. Output
  2. Profile: WWW Full
  3. Title: Web Server (HTTP,HTTPS)
  4. Description: Web Server (HTTP,HTTPS)
  5. Ports:
  6. 80,443/tcp

Allow incoming HTTP and HTTPS traffic for this profile:

允许此配置文件的传入HTTP和HTTPS通信:

  • sudo ufw allow in "WWW Full"

    sudo ufw在“ WWW Full”中允许

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser:

您可以通过在Web浏览器中访问服务器的公用IP地址来立即进行抽查,以验证一切是否按计划进行:

http://your_server_ip

You will see the default Debian 10 Apache web page, which is there for informational and testing purposes. It should look something like this:

您将看到默认的Debian 10 Apache网页,该网页用于提供信息和测试目的。 它看起来应该像这样:

If you see this page, then your web server is now correctly installed and accessible through your firewall.

如果您看到此页面,则您的Web服务器现在已正确安装并可以通过防火墙访问。

If you do not know what your server’s public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH.

如果您不知道服务器的公用IP地址是什么,可以通过多种方法找到它。 通常,这是您用于通过SSH连接到服务器的地址。

There are a few different ways to do this from the command line. First, you could use the iproute2 tools to get your IP address by typing this:

从命令行有几种不同的方法可以做到这一点。 首先,您可以使用iproute2工具通过键入以下命令来获取IP地址:

  • ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

    ip addr显示eth0 | grep inet | awk'{print $ 2; }'| sed's /\/.*$//'

This will give you two or three lines back. They are all correct addresses, but your computer may only be able to use one of them, so feel free to try each one.

这将给您两三行。 它们都是正确的地址,但是您的计算机可能只能使用其中之一,因此可以随意尝试每个。

An alternative method is to use the curl utility to contact an outside party to tell you how it sees your server. This is done by asking a specific server what your IP address is:

一种替代方法是使用curl实用程序与外部联系人联系,以告诉您如何看待您的服务器。 通过询问特定的服务器您的IP地址是什么来完成此操作:

  • sudo apt install curl

    sudo apt安装curl
  • curl http://icanhazip.com

    卷曲http://icanhazip.com

Regardless of the method you use to get your IP address, type it into your web browser’s address bar to view the default Apache page.

无论使用哪种方法获取IP地址,都请在Web浏览器的地址栏中键入它以查看默认的Apache页面。

第2步-安装MariaDB (Step 2 — Installing MariaDB)

Now that you have a web server up and running, you need to install the database system to be able to store and manage data for your site.

现在,您已经启动并运行了Web服务器,您需要安装数据库系统,以便能够存储和管理站点数据。

In Debian 10, the metapackage mysql-server, which was traditionally used to install the MySQL server, was replaced by default-mysql-server. This metapackage references MariaDB, a community fork of the original MySQL server by Oracle, and it’s currently the default MySQL-compatible database server available on debian-based package manager repositories.

在Debian 10中,传统上用于安装MySQL服务器的元软件包mysql-serverdefault-mysql-server取代。 此元包引用了MariaDB ,这是Oracle原始MySQL服务器的社区分支,并且它是当前基于debian的软件包管理器存储库中可用的默认MySQL兼容数据库服务器。

For longer term compatibility, however, it’s recommended that instead of using the metapackage you install MariaDB using the program’s actual package, mariadb-server.

但是,为了获得长期兼容性,建议您不要使用元软件包,而应使用程序的实际软件包mariadb-server安装MariaDB。

To install this software, run:

要安装此软件,请运行:

  • sudo apt install mariadb-server

    sudo apt安装mariadb-server

When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MariaDB. This script will remove some insecure default settings and lock down access to your database system. Start the interactive script by running:

安装完成后,建议您运行随MariaDB预先安装的安全脚本。 该脚本将删除一些不安全的默认设置,并锁定对数据库系统的访问。 通过运行以下命令来启动交互式脚本:

  • sudo mysql_secure_installation

    须藤mysql_secure_installation

This script will take you through a series of prompts where you can make some changes to your MariaDB setup. The first prompt will ask you to enter the current database root password. This is not to be confused with the system root. The database root user is an administrative user with full privileges over the database system. Because you just installed MariaDB and haven’t made any configuration changes yet, this password will be blank, so just press ENTER at the prompt.

该脚本将引导您完成一系列提示,您可以在其中对MariaDB设置进行一些更改。 第一个提示将要求您输入当前的数据库根密码。 这不要与系统根目录混淆。 数据库根用户是对数据库系统具有完全特权的管理用户。 因为您刚刚安装了MariaDB且尚未进行任何配置更改,所以该密码将为空,因此只需在提示符下按ENTER

The next prompt asks you whether you’d like to set up a database root password. Because MariaDB uses a special authentication method for the root user that is typically safer than using a password, you don’t need to set this now. Type N and then press ENTER.

下一个提示询问您是否要设置数据库根密码。 由于MariaDB为用户使用一种特殊的身份验证方法,该方法通常比使用密码更安全,因此您现在无需设置此方法。 键入N ,然后按ENTER

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove anonymous users and the test database, disable remote root login, and load these new rules so that MariaDB immediately respects the changes you have made. When you’re finished, log in to the MariaDB console by typing:

在此处,您可以按Y ,然后按ENTER以接受所有后续问题的默认设置。 这将删除匿名用户和测试数据库,禁用远程root登录,并加载这些新规则,以便MariaDB立即尊重您所做的更改。 完成后,输入以下命令登录到MariaDB控制台:

  • sudo mariadb

    苏多·玛丽亚德

This will connect to the MariaDB server as the administrative database user root, which is inferred by the use of sudo when running this command. You should see output like this:

这将以管理数据库用户root身份连接到MariaDB服务器,这是通过在运行此命令时使用sudo推断出来的。 您应该看到如下输出:

  1. Output
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 74
  4. Server version: 10.3.15-MariaDB-1 Debian 10
  5. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  7. MariaDB [(none)]>

Notice that you didn’t need to provide a password to connect as the root user. That works because the default authentication method for the administrative MariaDB user is unix_socket instead of password. Even though this might look like a security concern at first, it makes the database server more secure because the only users allowed to log in as the root MariaDB user are the system users with sudo privileges connecting from the console or through an application running with the same privileges. In practical terms, that means you won’t be able to use the administrative database root user to connect from your PHP application.

请注意,您无需提供密码即可以root用户身份进行连接。 之所以unix_socket ,是因为管理MariaDB用户的默认身份验证方法是unix_socket而不是password 。 即使最初看起来像是一个安全问题,它仍使数据库服务器更加安全,因为唯一允许以root MariaDB用户身份登录的用户是具有sudo特权的系统用户,这些用户从控制台或通过运行该命令的应用程序进行连接相同的特权。 实际上,这意味着您将无法使用管理数据库用户从PHP应用程序进行连接。

For increased security, it’s best to have dedicated user accounts with less expansive privileges set up for every database, especially if you plan on having multiple databases hosted on your server. To demonstrate such a setup, we’ll create a database named example_database and a user named example_user, but you can replace these names with different values. To create a new database, run the following command from your MariaDB console:

为了提高安全性,最好为每个数据库设置专用的用户帐户,并为其设置较少的扩展特权,尤其是如果您计划在服务器上托管多个数据库时。 为了演示这种设置,我们将创建一个名为example_database的数据库和一个名为example_user的用户,但是您可以将这些名称替换为不同的值。 要创建一个新数据库,请从您的MariaDB控制台运行以下命令:

  • CREATE DATABASE example_database;

    创建数据库example_database ;

Now you can create a new user and grant them full privileges on the custom database you’ve just created. The following command defines this user’s password as password, but you should replace this value with a secure password of your own choosing.

现在,您可以创建一个新用户,并向他们授予您刚创建的自定义数据库的全部特权。 以下命令将该用户的密码定义为password ,但是您应使用自己选择的安全密码替换此值。

  • GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

    将所有内容都授予example_database 。*到' example_user '@'localhost'由' password '加上GRANT OPTION标识;

This will give the example_user user full privileges over the example_database database, while preventing this user from creating or modifying other databases on your server.

这将为example_user用户提供对example_database数据库的完全特权,同时阻止该用户在服务器上创建或修改其他数据库。

Flush the privileges to ensure that they are saved and available in the current session:

刷新特权以确保它们已保存并在当前会话中可用:

  • FLUSH PRIVILEGES;

    冲洗特权;

Following this, exit the MariaDB shell:

之后,退出MariaDB shell:

  • exit

    出口

You can test if the new user has the proper permissions by logging in to the MariaDB console again, this time using the custom user credentials:

您可以使用自定义用户凭据再次登录到MariaDB控制台,以测试新用户是否具有适当的权限:

  • mariadb -u example_user -p

    mariadb -u example_user -p

Note the -p flag in this command, which will prompt you for the password used when creating the example_user user. After logging in to the MariaDB console, confirm that you have access to the example_database database:

注意此命令中的-p标志,它将提示您输入创建example_user用户时使用的密码。 登录到MariaDB控制台后,请确认您有权访问example_database数据库:

  • SHOW DATABASES;

    显示数据库;

This will give you the following output:

这将为您提供以下输出:

  1. Output
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | example_database |
  6. | information_schema |
  7. +--------------------+
  8. 2 rows in set (0.000 sec)

To exit the MariaDB shell, type:

要退出MariaDB shell,请输入:

  • exit

    出口

At this point, your database system is set up and you can move on to installing PHP, the final component of the LAMP stack.

至此,您的数据库系统已建立,您可以继续安装LAMP堆栈的最终组件PHP。

第3步-安装PHP (Step 3 — Installing PHP)

PHP is the component of your setup that will process code to display dynamic content. It can run scripts, connect to your MariaDB databases to get information, and hand the processed content over to your web server to display.

PHP是安装程序的组件,它将处理代码以显示动态内容。 它可以运行脚本,连接到MariaDB数据库以获取信息,并将处理后的内容移交给Web服务器进行显示。

Once again, leverage the apt system to install PHP. In addition, include some helper packages which will ensure that PHP code can run under the Apache server and talk to your MariaDB database:

再次利用apt系统安装PHP。 此外,包括一些帮助程序包,这些程序包将确保PHP代码可以在Apache服务器下运行并与您的MariaDB数据库对话:

  • sudo apt install php libapache2-mod-php php-mysql

    须藤apt安装php libapache2-mod-php php-mysql

This should install PHP without any problems. We’ll test this in a moment.

这应该安装PHP没有任何问题。 我们稍后将对此进行测试。

In most cases, you will want to modify the way that Apache serves files. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell the web server to prefer PHP files over others, so make Apache look for an index.php file first.

在大多数情况下,您将需要修改Apache提供文件的方式。 当前,如果用户从服务器请求目录,则Apache首先将查找名为index.html的文件。 我们要告诉Web服务器比其他服务器更喜欢PHP文件,因此让Apache首先寻找index.php文件。

To do this, type the following command to open the dir.conf file in a text editor with root privileges:

为此,请键入以下命令以使用root用户权限在文本编辑器中打开dir.conf文件:

  • sudo nano /etc/apache2/mods-enabled/dir.conf

    须藤纳米/etc/apache2/mods-enabled/dir.conf

It will look like this:

它看起来像这样:

/etc/apache2/mods-enabled/dir.conf
/etc/apache2/mods-enabled/dir.conf
  1. <IfModule mod_dir.c>
  2. DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
  3. </IfModule>

Move the PHP index file (highlighted above) to the first position after the DirectoryIndex specification, like this:

将PHP索引文件(上面突出显示)移动到DirectoryIndex规范之后的第一个位置,如下所示:

/etc/apache2/mods-enabled/dir.conf
/etc/apache2/mods-enabled/dir.conf
  1. <IfModule mod_dir.c>
  2. DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
  3. </IfModule>

When you are finished, save and close the file. If you’re using nano, you can do that by pressing CTRL+X, then Y and ENTER to confirm.

完成后,保存并关闭文件。 如果您使用的是nano ,则可以按CTRL+X ,然后按YENTER进行确认。

Now reload Apache’s configuration with:

现在,使用以下命令重新加载Apache的配置:

  • sudo systemctl reload apache2

    sudo systemctl重新加载apache2

You can check on the status of the apache2 service with systemctl status:

您可以使用systemctl status来检查apache2服务的systemctl status

  • sudo systemctl status apache2

    sudo systemctl状态apache2
  1. Sample Output
  2. ● apache2.service - The Apache HTTP Server
  3. Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Mon 2019-07-08 12:58:31 UTC; 8s ago
  5. Docs: https://httpd.apache.org/docs/2.4/
  6. Process: 11948 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
  7. Main PID: 11954 (apache2)
  8. Tasks: 6 (limit: 4719)
  9. Memory: 11.5M
  10. CGroup: /system.slice/apache2.service
  11. ├─11954 /usr/sbin/apache2 -k start
  12. ├─11955 /usr/sbin/apache2 -k start
  13. ├─11956 /usr/sbin/apache2 -k start
  14. ├─11957 /usr/sbin/apache2 -k start
  15. ├─11958 /usr/sbin/apache2 -k start
  16. └─11959 /usr/sbin/apache2 -k start

At this point, your LAMP stack is fully operational, but before you can test your setup with a PHP script it’s best to set up a proper Apache Virtual Host to hold your website’s files and folders. We’ll do that in the next step.

此时,您的LAMP堆栈可以完全使用,但是在使用PHP脚本测试设置之前,最好先设置适当的Apache虚拟主机来保存网站的文件和文件夹。 我们将在下一步中进行操作。

步骤4 —为您的网站创建虚拟主机 (Step 4 — Creating a Virtual Host for your Website)

By default, Apache serves its content from a directory located at /var/www/html, using the configuration contained in /etc/apache2/sites-available/000-default.conf. Instead of modifying the default website configuration file, we are going to create a new virtual host for testing your PHP environment. Virtual hosts enable us to keep multiple websites hosted on a single Apache server.

默认情况下,Apache使用/etc/apache2/sites-available/000-default.conf包含的配置从/var/www/html的目录中提供其内容。 我们将创建一个新的虚拟主机来测试您PHP环境,而不是修改默认的网站配置文件。 虚拟主机使我们能够将多个网站保留在单个Apache服务器上。

Following that, you’ll create a directory structure within /var/www for an example website named your_domain.

之后,您将在/var/www为名为your_domain的示例网站创建目录结构。

Create the root web directory for your_domain as follows:

your_domain创建根Web目录,如下所示:

  • sudo mkdir /var/www/your_domain

    须藤mkdir / var / www / your_domain

Next, assign ownership of the directory with the $USER environment variable, which should reference your current system user:

接下来,使用$ USER环境变量分配目录的所有权,该变量应引用您当前的系统用户:

  • sudo chown -R $USER:$USER /var/www/your_domain

    须藤chown -R $ USER:$ USER / var / www / your_domain

Then, open a new configuration file in Apache’s sites-available directory using your preferred command-line editor. Here, we’ll use nano:

然后,使用首选的命令行编辑器在Apache的sites-available目录中打开一个新的配置文件。 在这里,我们将使用nano

  • sudo nano /etc/apache2/sites-available/your_domain.conf

    须藤纳米/ etc / apache2 / sites-available / your_domain .conf

This will create a new blank file. Paste in the following bare-bones configuration:

这将创建一个新的空白文件。 粘贴以下基本配置:

/etc/apache2/sites-available/your_domain
/ etc / apache2 / sites-available / your_domain
  1. <VirtualHost *:80>
  2. ServerName your_domain
  3. ServerAlias www.your_domain
  4. ServerAdmin webmaster@localhost
  5. DocumentRoot /var/www/your_domain
  6. ErrorLog ${APACHE_LOG_DIR}/error.log
  7. CustomLog ${APACHE_LOG_DIR}/access.log combined
  8. </VirtualHost>

With this VirtualHost configuration, we’re telling Apache to serve your_domain using /var/www/your_domain as the web root directory. If you’d like to test Apache without a domain name, you can remove or comment out the options ServerName and ServerAlias by adding a # character in the beginning of each option’s lines.

通过此VirtualHost配置,我们告诉Apache使用/var/www/ your_domain作为Web根目录来服务your_domain 。 如果要测试不带域名的Apache,则可以通过在每个选项行的开头添加#字符来删除或注释掉ServerNameServerAlias选项。

You can now use a2ensite to enable this virtual host:

现在,您可以使用a2ensite启用此虚拟主机:

  • sudo a2ensite your_domain

    须藤a2ensite your_domain

You might want to disable the default website that comes installed with Apache. This is required if you’re not using a custom domain name, because in this case Apache’s default configuration would overwrite your Virtual Host. To disable Apache’s default website, type:

您可能要禁用Apache随附的默认网站。 如果您不使用自定义域名,则需要这样做,因为在这种情况下,Apache的默认配置会覆盖您的虚拟主机。 要禁用Apache的默认网站,请输入:

  • sudo a2dissite 000-default

    须藤a2dissite 000-默认

To make sure your configuration file doesn’t contain syntax errors, you can run:

要确保您的配置文件不包含语法错误,可以运行:

  • sudo apache2ctl configtest

    须藤apache2ctl configtest

Finally, reload Apache so these changes take effect:

最后,重新加载Apache,以使这些更改生效:

  • sudo systemctl reload apache2

    sudo systemctl重新加载apache2

Your new website is now active, but the web root /var/www/your_domain is still empty. In the next step, we’ll create a PHP script to test the new setup and confirm that PHP is correctly installed and configured on your server.

您的新网站现在处于活动状态,但是Web根目录/var/www/ your_domain仍然为空。 在下一步中,我们将创建一个PHP脚本来测试新设置,并确认PHP已在您的服务器上正确安装和配置。

第5步-在Web服务器上测试PHP处理 (Step 5 — Testing PHP Processing on your Web Server)

Now that you have a custom location to host your website’s files and folders, we’ll create a simple PHP test script to confirm that Apache is able to handle and process requests for PHP files.

现在您已经有了一个自定义位置来托管您网站的文件和文件夹,我们将创建一个简单PHP测试脚本来确认Apache能够处理和处理对PHP文件的请求。

Create a new file named info.php inside your custom web root folder:

在自定义Web根文件夹中创建一个名为info.php的新文件:

  • nano /var/www/your_domain/info.php

    纳米/ var / www / your_domain /info.php

This will open a blank file. Add the following text, which is valid PHP code, inside the file:

这将打开一个空白文件。 在文件内添加以下文本,这是有效PHP代码:

/var/www/your_domain/info.php
/var/www/your_domain/info.php
  1. <?php
  2. phpinfo();

When you are finished, save and close the file.

完成后,保存并关闭文件。

Now you can test whether your web server is able to correctly display content generated by this PHP script. To try this out, visit this page in your web browser. You’ll need your server’s public IP address again.

现在,您可以测试您的Web服务器是否能够正确显示此PHP脚本生成的内容。 要尝试此操作,请在网络浏览器中访问此页面。 您将再次需要服务器的公共IP地址。

The address you will want to visit is:

您要访问的地址是:

http://your_domain/info.php

You should see a page similar to this:

您应该看到类似于以下页面:

This page provides some basic information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.

该页面从PHP的角度提供了有关服务器的一些基本信息。 这对于调试和确保正确应用设置很有用。

If you can see this page in your browser, then your PHP installation is working as expected.

如果您可以在浏览器中看到此页面,则说明您PHP安装已按预期进行。

After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your Debian server. You can use rm to do so:

在通过该页面检查有关PHP服务器的相关信息之后,最好删除您创建的文件,因为该文件包含有关PHP环境和Debian服务器的敏感信息。 您可以使用rm来这样做:

  • sudo rm /var/www/your_domain/info.php

    须藤rm / var / www / your_domain /info.php

You can always recreate this page if you need to access the information again later.

如果以后需要再次访问该信息,则始终可以重新创建此页面。

第6步-从PHP测试数据库连接(可选) (Step 6 — Testing Database Connection from PHP (Optional))

If you want to test whether PHP is able to connect to MariaDB and execute database queries, you can create a test table with dummy data and query for its contents from a PHP script.

如果要测试PHP是否能够连接到MariaDB并执行数据库查询,则可以创建带有伪数据的测试表,并从PHP脚本中查询其内容。

First, connect to the MariaDB console with the database user you created in Step 2 of this guide:

首先,使用您在本指南的步骤2中创建的数据库用户连接到MariaDB控制台:

  • mariadb -u example_user -p

    mariadb -u example_user -p

Create a table named todo_list. From the MariaDB console, run the following statement:

创建一个名为todo_list的表。 在MariaDB控制台中,运行以下语句:

  • CREATE TABLE example_database.todo_list (

    创建表example_database 。 todo_list (

  • item_id INT AUTO_INCREMENT,

    item_id INT AUTO_INCREMENT,
  • content VARCHAR(255),

    内容VARCHAR(255),
  • PRIMARY KEY(item_id)

    主键(item_id)
  • );

    );

Now, insert a few rows of content in the test table. You might want to repeat the next command a few times, using different values:

现在,在测试表中插入几行内容。 您可能需要使用不同的值重复执行下一条命令几次:

  • INSERT INTO example_database.todo_list (content) VALUES ("My first important item");

    插入INTO example_database 。 todo_list (内容)VALUES(“ 我的第一个重要项目 ”);

To confirm that the data was successfully saved to your table, run:

要确认数据已成功保存到表中,请运行:

  • SELECT * FROM example_database.todo_list;

    SELECT * FROM example_database 。 todo_list ;

You will see the following output:

您将看到以下输出:

  1. Output
  2. +---------+--------------------------+
  3. | item_id | content |
  4. +---------+--------------------------+
  5. | 1 | My first important item |
  6. | 2 | My second important item |
  7. | 3 | My third important item |
  8. | 4 | and this one more thing |
  9. +---------+--------------------------+
  10. 4 rows in set (0.000 sec)

After confirming that you have valid data in your test table, you can exit the MariaDB console:

在确认测试表中有有效数据之后,可以退出MariaDB控制台:

  • exit

    出口

Now you can create the PHP script that will connect to MariaDB and query for your content. Create a new PHP file in your custom web root directory using your preferred editor. We’ll use nano for that:

现在,您可以创建将连接到MariaDB并查询您内容PHP脚本。 使用首选编辑器在自定义Web根目录中创建一个新PHP文件。 我们将为此使用nano

  • nano /var/www/your_domain/todo_list.php

    纳米/ var / www / your_domain / todo_list.php

The following PHP script connects to the MariaDB database and queries for the content of the todo_list table, exhibiting the results in a list. If there’s a problem with the database connection, it will throw an exception. Copy this content into your todo_list.php script:

以下PHP脚本连接到MariaDB数据库,并查询todo_list表的内容,并将结果显示在列表中。 如果数据库连接有问题,它将引发异常。 将此内容复制到您的todo_list.php脚本中:

/var/www/your_domain/todo_list.php
/var/www/your_domain/todo_list.php
  1. <?php
  2. $user = "example_user";
  3. $password = "password";
  4. $database = "example_database";
  5. $table = "todo_list";
  6. try {
  7. $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
  8. echo "<h2>TODO</h2><ol>";
  9. foreach($db->query("SELECT content FROM $table") as $row) {
  10. echo "<li>" . $row['content'] . "</li>";
  11. }
  12. echo "</ol>";
  13. } catch (PDOException $e) {
  14. print "Error!: " . $e->getMessage() . "<br/>";
  15. die();
  16. }

Save and close the file when you’re done editing.

完成编辑后,保存并关闭文件。

You can now access this page in your web browser by visiting the domain name or public IP address for your website, followed by /todo_list.php:

现在,您可以在Web浏览器中访问此页面,方法是访问网站的域名或公共IP地址,然后访问/todo_list.php

http://your_domain/todo_list.php

You should see a page like this, showing the content you’ve inserted in your test table:

您应该看到这样的页面,显示您已插入测试表中的内容:

That means your PHP environment is ready to connect and interact with your MariaDB server.

这意味着您PHP环境已准备就绪,可以连接MariaDB服务器并与之交互。

结论 (Conclusion)

In this guide, we’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as web server and MariaDB as database system.

在本指南中,我们使用Apache作为Web服务器,使用MariaDB作为数据库系统,为为访问者提供PHP网站和应用程序奠定了灵活的基础。

To further improve your current setup, you can install Composer for dependency and package management in PHP, and you can also install an OpenSSL certificate for your website using Let’s Encrypt.

为了进一步改善当前设置,您可以在PHP中安装Composer进行依赖关系和程序包管理,还可以使用Let's Encrypt为您的网站安装OpenSSL证书。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mariadb-php-lamp-stack-on-debian-10

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号