如何在CentOS 8上安装Linux,Nginx,MySQL,PHP(LEMP)堆栈[快速入门]

介绍 (Introduction)

In this tutorial, you’ll install a LEMP 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 LEMP stack with MariaDB as the database management system.

在本教程中,您将在CentOS 8服务器上安装LEMP堆栈。 尽管CentOS 8的默认存储库中提供了MySQL,但本指南将逐步介绍使用MariaDB作为数据库管理系统设置LEMP堆栈的过程。

For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Install Linux, Nginx, MySQL, PHP (LEMP) Stack on CentOS 8.

有关本教程的更详细版本,以及每个步骤的更多说明,请参阅如何在CentOS 8上安装Linux,Nginx,MySQL,PHP(LEMP)堆栈

先决条件 (Prerequisites)

To follow this guide, you’ll need access to a CentOS 8 server as a sudo user.

要遵循本指南,您需要以sudo用户身份访问CentOS 8服务器。

第1步-安装Nginx (Step 1 — Install Nginx)

Install the nginx package with:

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

  • sudo dnf install nginx

    须藤dnf安装nginx

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

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

  • sudo systemctl start nginx

    sudo systemctl启动nginx

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-FPM (Step 3 — Install PHP-FPM)

To install the php-fpm and php-mysql packages, run:

要安装php-fpmphp-mysql软件包,请运行:

  • sudo dnf install php-fpm php-mysqlnd

    须藤dnf安装php-fpm php-mysqlnd

When the installation is finished, you’ll need to edit the /etc/php-fpm.d/www.conf file in order to adjust a couple settings. We’ll install nano to facilitate editing these files:

安装完成后,您需要编辑/etc/php-fpm.d/www.conf文件,以调整一些设置。 我们将安装nano以便于编辑这些文件:

  • sudo dnf install nano

    须藤dnf安装纳米

Now open the /etc/php-fpm.d/www.conf configuration file using nano or your editor of choice:

现在,使用nano或您选择的编辑器打开/etc/php-fpm.d/www.conf配置文件:

  • sudo nano /etc/php-fpm.d/www.conf

    须藤纳米/etc/php-fpm.d/www.conf

Look for the user and group directives. If you are using nano, you can hit CTRL+W to search for these terms inside the open file. Make sure to change both values from apache to nginx:

查找usergroup指令。 如果您使用的是nano ,则可以按CTRL + W在打开的文件中搜索这些术语。 确保将两个值都从apache更改为nginx

/etc/php-fpm.d/www.conf
/etc/php-fpm.d/www.conf
…
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
…

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

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

To enable and start the php-fpm service, run:

要启用并启动php-fpm服务,请运行:

  • sudo systemctl start php-fpm

    sudo systemctl启动php-fpm

Finally, restart the Nginx web server so that it loads the configuration files created by the php-fpm installation:

最后,重新启动Nginx Web服务器,以便它加载由php-fpm安装创建的配置文件:

  • sudo systemctl restart nginx

    sudo systemctl重启nginx

第4步-使用Nginx测试PHP (Step 4 — Test PHP with Nginx)

On CentOS 8, the default php-fpm installation automatically creates configuration files that will allow your Nginx web server to handle .php files in the default document root located at /usr/share/nginx/html. You won’t need to make any changes to Nginx’s configuration in order for PHP to work correctly within your web server.

在CentOS 8上,默认的php-fpm安装会自动创建配置文件,该文件将使您的Nginx Web服务器能够处理位于/usr/share/nginx/html的默认文档根目录中的.php文件。 您无需对Nginx的配置进行任何更改即可使PHP在您的Web服务器中正常工作。

We’ll only need to modify the default owner and group on Nginx’s document root, so that you can create and modify files in that location using your regular non-root system user:

我们只需要修改Nginx文档根目录上的默认所有者和组,以便您可以使用常规的非根目录系统用户在该位置创建和修改文件:

  • sudo chown -R sammy.sammy /usr/share/nginx/html/

    sudo chown -R sammy 。 萨米 / usr / share / nginx / html /

Create a new PHP file called info.php at the /usr/share/nginx/html directory:

/usr/share/nginx/html目录中创建一个名为info.php的新PHP文件:

  • nano /usr/share/nginx/html/info.php

    纳米/ usr / share / nginx / html / info.php

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

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

/usr/share/nginx/html/info.php
/usr/share/nginx/html/info.php
<?php

phpinfo();

Copy this content to your info.php file, and don’t forget to save it when you’re done.

将此内容复制到您的info.php文件中,完成后不要忘记保存。

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:

您会看到类似以下页面:

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-8-quickstart

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值