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

介绍 (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 written in PHP. This term is an acronym which represents the Linux operating system, with the Apache web server. The backend data is stored in a MariaDB database and the dynamic processing is handled by PHP.

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

The database layer in a LAMP stack is typically a MySQL database server, but prior to the release of CentOS 8, MySQL wasn’t available from the default CentOS repositories. Because of this, MariaDB, a community fork of MySQL, became a widely accepted alternative to MySQL as the default database system for LAMP stacks on CentOS machines. MariaDB works as a drop-in replacement for the original MySQL server, which in practice means you can switch to MariaDB without having to make any configuration or code changes in your application.

LAMP堆栈中的数据库层通常是MySQL数据库服务器,但是在CentOS 8发行之前,默认CentOS存储库中无法使用MySQL。 因此, MariaDB是MySQL的一个社区分支,成为CentOS计算机上LAMP堆栈的默认数据库系统,被MySQL广泛接受。 MariaDB可以替代原始MySQL服务器,实际上,这意味着您可以切换到MariaDB,而无需在应用程序中进行任何配置或代码更改。

In this guide, you’ll install a LAMP stack on a CentOS 8 server, using MariaDB as the database management system.

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

先决条件 (Prerequisites)

To follow this guide, you’ll need access to a CentOS 8 server as a non-root user with sudo privileges, and an active firewall installed on your server. To set this up, you can follow our Initial Server Setup Guide for CentOS 8.

要遵循本指南,您需要以具有sudo特权的非root用户身份访问CentOS 8服务器,并在服务器上安装了活动防火墙。 要进行设置,您可以遵循我们的《 CentOS 8初始服务器设置指南》

第1步-安装Apache Web服务器 (Step 1 — Installing the Apache Web Server)

In order to display web pages to our site visitors, we are going to employ Apache, a popular open source web server that can be configured to serve PHP pages. We’ll use the dnf package manager, which is the new default package manager on CentOS 8, to install this software.

为了向站点访问者显示网页,我们将使用Apache,这是一种流行的开源Web服务器,可以将其配置为提供PHP页面。 我们将使用dnf软件包管理器(它是CentOS 8上新的默认软件包管理器)来安装此软件。

Install the httpd package with:

使用以下命令安装httpd软件包:

  • sudo dnf install httpd

    须藤dnf安装httpd

When prompted, enter y to confirm that you want to install Apache.

出现提示时,输入y确认要安装Apache。

After the installation is finished, run the following command to enable and start the server:

安装完成后,运行以下命令来启用和启动服务器:

  • sudo systemctl start httpd

    sudo systemctl启动httpd

In case you have enabled the firewalld firewall as per our initial server setup guide, you will need to allow connections to Apache. The following command will permanently enable HTTP connections, which run on port 80 by default:

如果您按照我们的初始服务器设置指南启用了firewalld防火墙,则需要允许连接到Apache。 以下命令将永久启用HTTP连接,默认情况下该端口在端口80上运行:

  • sudo firewall-cmd --permanent --add-service=http

    sudo firewall-cmd-永久--add-service = http

To verify that the change was applied, you can run:

要验证是否已应用更改,可以运行:

  • sudo firewall-cmd --permanent --list-all

    sudo firewall-cmd-永久--list-all

You’ll see output like this:

您将看到如下输出:


   
   
Output
public target: default icmp-block-inversion: no interfaces: sources: services: cockpit dhcpv6-client http ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:

You’ll need to reload the firewall configuration so the changes take effect:

您需要重新加载防火墙配置,以使更改生效:

  • sudo firewall-cmd --reload

    sudo firewall-cmd-重新加载

With the new firewall rule added, you can test if the server is up and running by accessing your server’s public IP address or domain name from your web browser.

添加新的防火墙规则后,您可以通过从Web浏览器访问服务器的公用IP地址或域名来测试服务器是否已启动并正在运行。

Note: In case you are using DigitalOcean as DNS hosting provider, you can check our product docs for detailed instructions on how to set up a new domain name and point it to your server.

注意 :如果您使用DigitalOcean作为DNS托管服务提供商,则可以查看我们的产品文档,以获取有关如何设置新域名并将其指向服务器的详细说明。

If you do not have a domain name pointed at your server and you do not know your server’s public IP address, you can find it by running the following command:

如果没有指向服务器的域名,并且您不知道服务器的公共IP地址,则可以通过运行以下命令来找到它:

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

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

This will print out a few IP addresses. You can try each of them in turn in your web browser.

这将打印出一些IP地址。 您可以依次在网络浏览器中尝试使用它们。

As an alternative, you can check which IP address is accessible, as viewed from other locations on the internet:

作为替代方案,您可以检查从互联网上其他位置查看的哪个IP地址可访问:

  • curl -4 icanhazip.com

    卷曲-4 icanhazip.com

Type the address that you receive in your web browser and it will take you to Apache’s default landing page:

输入您在网络浏览器中收到的地址,它将带您到Apache的默认登录页面:

If you see this page, then your web server is now correctly installed.

如果看到此页面,则表明您的Web服务器现在已正确安装。

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

Now that you have a web server up and running, you need to install a database system to be able to store and manage data for your site. We’ll install MariaDB, a community-developed fork of the original MySQL server by Oracle.

现在,您已启动并运行了Web服务器,您需要安装数据库系统,以便能够存储和管理站点数据。 我们将安装MariaDB ,这是Oracle原始MySQL服务器的社区开发分支。

To install this software, run:

要安装此软件,请运行:

  • sudo dnf install mariadb-server

    须藤dnf安装mariadb服务器

When the installation is finished, you can enable and start the MariaDB server with:

安装完成后,可以使用以下命令启用和启动MariaDB服务器:

  • sudo systemctl start mariadb

    sudo systemctl启动mariadb

To improve the security of your database server, 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 user. 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设置进行一些更改。 第一个提示将要求您输入当前的数据库根密码。 请勿将其与系统root用户混淆。 数据库根用户是对数据库系统具有完全特权的管理用户。 因为您刚刚安装了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 the server immediately respects the changes you have made.

在此处,您可以按Y ,然后按ENTER以接受所有后续问题的默认设置。 这将删除匿名用户和测试数据库,禁用远程root登录,并加载这些新规则,以便服务器立即尊重您所做的更改。

When you’re finished, log in to the MariaDB console by typing:

完成后,输入以下命令登录到MariaDB控制台:

  • sudo mysql

    须藤MySQL

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推断出来的。 您应该看到如下输出:


   
   
Output
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.17-MariaDB MariaDB Server 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 [(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.

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

To create a new database, run the following command from your MariaDB console:

要创建一个新数据库,请从您的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控制台,以测试新用户是否具有适当的权限:

  • mysql -u example_user -p

    mysql -u example_user -p

Note the -p flag in this command, which will prompt you for the password you chose 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:

这将为您提供以下输出:


   
   
Output
+--------------------+ | Database | +--------------------+ | example_database | | information_schema | +--------------------+ 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)

You have Apache installed to serve your content and MariaDB installed to store and manage your data. PHP is the component of our setup that will process code to display dynamic content to the final user. In addition to the php package, you’ll need php-mysqlnd, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will automatically be installed as dependencies.

您已经安装了Apache来提供内容,已经安装了MariaDB来存储和管理数据。 PHP是我们安装程序的组件,它将处理代码以向最终用户显示动态内容。 除了php软件包之外,您还需要php-mysqlnd ,这是一个PHP模块,允许PHP与基于MySQL的数据库进行通信。 核心PHP软件包将自动安装为依赖项。

To install the php and php-mysqlnd packages using the dnf package manager, run:

要使用dnf软件包管理器安装phpphp-mysqlnd软件包,请运行:

sudo dnf install php php-mysqlnd

After the installation is finished, you’ll need to restart the Apache web server in order to enable the PHP module:

安装完成后,您需要重新启动Apache Web服务器才能启用PHP模块:

sudo systemctl restart httpd

Your web server is now fully set up. In the next step, we’ll create a PHP testing script to make sure everything works as expected.

您的Web服务器现已完全设置。 在下一步中,我们将创建一个PHP测试脚本以确保一切正常。

第4步-使用Apache测试PHP (Step 4 — Testing PHP with Apache)

The default Apache installation on CentOS 8 will create a document root located at /var/www/html. You don’t need to make any changes to Apache’s default settings in order for PHP to work correctly within your web server.

在CentOS 8上的默认Apache安装将创建位于/var/www/html的文档根目录。 您无需对Apache的默认设置进行任何更改即可使PHP在您的Web服务器中正常工作。

The only adjustment we’ll make is to change the default permission settings on your Apache document root folder. This way, you’ll be able to create and modify files in that directory with your regular system user, without the need to prefix each command with sudo.

我们要做的唯一调整是更改Apache文档根文件夹上的默认权限设置。 这样,您将可以与常规系统用户一起在该目录中创建和修改文件,而无需在每个命令前加上sudo前缀。

The following command will change the ownership of the default Apache document root to a user and group called sammy, so be sure to replace the highlighted username and group in this command to reflect your system’s username and group.

以下命令会将默认Apache文档根目录的所有权更改为名为sammy的用户和组,因此请确保在此命令中替换突出显示的用户名和组,以反映系统的用户名和组。

  • sudo chown -R sammy.sammy /var/www/html/

    sudo chown -R sammy 。 萨米 / var / www / html /

We’ll now create a test PHP page to make sure the web server works as expected.

现在,我们将创建一个测试PHP页面,以确保Web服务器能够按预期工作。

The default text editor that comes with CentOS 8 is vi. vi is an extremely powerful text editor, but it can be somewhat obtuse for users who lack experience with it. You might want to install a more user-friendly editor such as nano to facilitate editing files on your CentOS 8 server:

CentOS 8随附的默认文本编辑器是vivi是一个功能非常强大的文本编辑器,但对于缺乏使用经验的用户而言,它可能会有些迟钝。 您可能需要安装更加用户友好的编辑器(例如nano以方便在CentOS 8服务器上编辑文件:

  • sudo dnf install nano

    须藤dnf安装纳米

Type y when prompted to confirm the installation.

在提示您确认安装时,键入y

Now, create a new PHP file called info.php at the /var/www/html directory:

现在,在/var/www/html目录中创建一个名为info.php的新PHP文件:

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

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

The following PHP code will display information about the current PHP environment running on the server:

以下PHP代码将显示有关服务器上当前运行PHP环境的信息:

/var/www/html/info.php
/var/www/html/info.php
<?php

phpinfo();

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

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

Now we can test whether our web server can correctly display content generated by a PHP script. Go to your browser and access your server hostname or IP address, followed by /info.php:

现在,我们可以测试我们的Web服务器是否可以正确显示PHP脚本生成的内容。 转到浏览器并访问服务器主机名或IP地址,然后/info.php

http://server_host_or_IP/info.php

You’ll see a page similar to this:

您会看到类似以下页面:

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 CentOS server. You can use rm to remove that file:

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

  • rm /var/www/html/info.php

    rm / var / www / html / info.php

You can always regenerate this file if you need it later. Next, we’ll test the database connection from the PHP side.

如果以后需要,可以随时重新生成该文件。 接下来,我们将从PHP端测试数据库连接。

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

If you want to test if 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控制台:

  • mysql -u example_user -p

    mysql -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 (
    item_id INT AUTO_INCREMENT,
    content VARCHAR(255),
    PRIMARY KEY(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:

您将看到以下输出:


   
   
Output
+---------+--------------------------+ | item_id | content | +---------+--------------------------+ | 1 | My first important item | | 2 | My second important item | | 3 | My third important item | | 4 | and this one more thing | +---------+--------------------------+ 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/html/todo_list.php

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

Add the following content to your PHP script:

将以下内容添加到您PHP脚本中:

/var/www/html/todo_list.php
/var/www/html/todo_list.php
<?php
$user = "example_user";
$password = "password";
$database = "example_database";
$table = "todo_list";

try {
  $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
  echo "<h2>TODO</h2><ol>"; 
  foreach($db->query("SELECT content FROM $table") as $row) {
    echo "<li>" . $row['content'] . "</li>";
  }
  echo "</ol>";
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

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

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

You can now access this page in your web browser by visiting your server’s host name or public IP address, followed by /todo_list.php:

现在,您可以通过访问服务器的主机名或公共IP地址,然后/todo_list.php在Web浏览器中访问此页面:

http://server_host_or_IP/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, you’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as web server. You’ve set up Apache to handle PHP requests, and you’ve also set up a MariaDB database to store your website’s data.

在本指南中,您为使用Apache作为Web服务器为访问者提供PHP网站和应用程序奠定了灵活的基础。 您已经设置了Apache处理PHP请求,还设置了MariaDB数据库来存储您的网站数据。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值