介绍 (Introduction)
In this tutorial, you’ll install a LAMP stack on a CentOS 8 server. Although MySQL is available from the default repositories in CentOS 8, this guide will walk through the process of setting up a LAMP stack with MariaDB as the database management system.
在本教程中,您将在CentOS 8服务器上安装LAMP堆栈。 尽管CentOS 8的默认存储库中提供了MySQL,但本指南将逐步介绍使用MariaDB作为数据库管理系统设置LAMP堆栈的过程。
For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on CentOS 8.
有关本教程的更详细版本,以及每个步骤的更多说明,请参阅如何在CentOS 8上安装Linux,Apache,MySQL,PHP(LAMP)堆栈 。
先决条件 (Prerequisites)
To follow this guide, you’ll need access to a CentOS 8 server as a sudo
user.
要遵循本指南,您需要以sudo
用户身份访问CentOS 8服务器。
第1步-安装Apache (Step 1 — Install Apache)
Install the httpd
package with:
使用以下命令安装httpd
软件包:
- sudo dnf install httpd 须藤dnf安装httpd
After the installation is finished, run the following command to enable and start the server:
安装完成后,运行以下命令来启用和启动服务器:
- sudo systemctl start httpd sudo systemctl启动httpd
If firewalld
is active, you’ll need to run the following command to allow external access on port 80
(HTTP):
如果firewalld
处于活动状态,则需要运行以下命令以允许在端口80
(HTTP)上进行外部访问:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd-永久--add-service = http
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. You’ll see a page like this:
添加新的防火墙规则后,您可以通过从Web浏览器访问服务器的公用IP地址或域名来测试服务器是否已启动并正在运行。 您会看到这样的页面:
第2步-安装MariaDB (Step 2 — Install MariaDB)
We’ll now install MariaDB, a community-developed fork of the original MySQL server by Oracle. To install this software, run:
现在,我们将安装MariaDB ,这是Oracle原始MySQL服务器的社区开发分支。 要安装此软件,请运行:
- sudo dnf install mariadb-server 须藤dnf安装mariadb服务器
When the installation is finished, 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. Start the interactive script with:
为了提高数据库服务器的安全性,建议您运行预先安装了MariaDB的安全脚本。 使用以下命令启动交互式脚本:
- sudo mysql_secure_installation 须藤mysql_secure_installation
The first prompt will ask you to enter the current database root password. 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且尚未进行任何配置更改,所以该密码将为空,因此只需在提示符下按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.
在此处,您可以按Y
,然后按ENTER
以接受所有后续问题的默认设置。
第3步-安装PHP (Step 3 — Install PHP)
To install the php
and php-mysqlnd
packages using the dnf
package manager, run:
要使用dnf
软件包管理器安装php
和php-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
第4步-使用Apache测试PHP (Step 4 — Test 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. The following command will change the ownership of the default Apache document root to a user and group called sammy:
我们要做的唯一调整是更改Apache文档根文件夹上的默认权限设置。 以下命令会将默认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. First, you might want to install nano
, a more user-friendly text editor, since that doesn’t come installed with CentOS 8 by default:
现在,我们将创建一个测试PHP页面,以确保Web服务器能够按预期工作。 首先,您可能需要安装更用户友好的文本编辑器nano
,因为默认情况下CentOS 8并未安装该编辑器:
- sudo dnf install nano 须藤dnf安装纳米
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环境的信息:
<?php
phpinfo();
When you are finished, save and close the file.
完成后,保存并关闭文件。
To 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:
您会看到类似以下页面: