LEMP是一个由Linux,Nginx,MariaDB和PHP组成的软件堆栈。这些组件构成了一个能够处理高流量 Web 应用程序的堆栈。对于想要构建快速且可扩展的 Web 应用程序的 Web 开发人员来说,这是一个受欢迎的选择。
在 CentOS Stream 9 上安装 LEMP
第 1 步。首先,让我们首先确保您的系统是最新的。
<span class="pln">sudo dnf update sudo dnf install epel</span><span class="pun">-</span><span class="pln">release</span>
第 2 步。安装 Nginx。
默认情况下,Nginx 在 CentOS Stream 基础存储库中可用。现在我们运行以下命令将 Nginx 安装到您的系统中:
<span class="pln">sudo dnf install nginx</span>
安装完成后,现在启用 Nginx(在系统启动时自动启动),启动 Web 服务器,并使用以下命令验证状态:
<span class="pln">sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx</span>
要验证 Nginx 版本,请使用以下命令进行确认:
<span class="pln">nginx </span><span class="pun">-</span><span class="pln">v</span>
如果您的服务器受防火墙保护,则需要同时打开 HTTP (80) 和 HTTPS (443) 端口:
<span class="pln">sudo firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">permanent </span><span class="pun">--</span><span class="pln">zone</span><span class="pun">=</span><span class="kwd">public</span> <span class="pun">--</span><span class="kwd">add</span><span class="pun">-</span><span class="pln">service</span><span class="pun">=</span><span class="pln">http sudo firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">permanent </span><span class="pun">--</span><span class="pln">zone</span><span class="pun">=</span><span class="kwd">public</span> <span class="pun">--</span><span class="kwd">add</span><span class="pun">-</span><span class="pln">service</span><span class="pun">=</span><span class="pln">https sudo firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">reload</span>
成功安装后,在您选择的浏览器中打开,您将看到默认的Nginx欢迎页面,如下图所示:http://your-IP-address
有关安装 Nginx 的其他资源,请阅读下面的帖子:
- 如何在 CentOS Stream √ 上安装 Nginx
第 2 步。安装 MariaDB。
默认情况下,MariaDB 在 CentOS Stream 基础存储库中可用。现在我们运行以下命令将 MariaDB 安装到您的系统中:
<span class="pln">sudo dnf install mariadb</span><span class="pun">-</span><span class="pln">server</span>
安装完成后检查版本:
<span class="pln">mariadb </span><span class="pun">--</span><span class="pln">version</span>
安装完成后,现在启用MariaDB(在系统启动时自动启动),启动MariaDB,并使用以下命令验证状态:
<span class="pln">sudo systemctl start mariadb sudo systemctl enable mariadb sudo systemctl status mariadb</span>
默认情况下,MariaDB 未强化。您可以使用脚本保护 MariaDB。您应该仔细阅读并在每个步骤下方仔细阅读,这将设置root密码,删除匿名用户,禁止远程root登录,并删除测试数据库和对安全MariaDB的访问权限:mysql_secure_installation
<span class="pln">mysql_secure_installation</span>
像这样配置它:
<span class="pun">-</span> <span class="typ">Set</span><span class="pln"> root password</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Remove</span><span class="pln"> anonymous users</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Disallow</span><span class="pln"> root login remotely</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Remove</span><span class="pln"> test database </span><span class="kwd">and</span><span class="pln"> access to it</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Reload</span><span class="pln"> privilege tables now</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y</span>
要登录 MariaDB,请使用以下命令(请注意,这与用于登录 MariaDB 数据库的命令相同):
<span class="pln">mysql </span><span class="pun">-</span><span class="pln">u root </span><span class="pun">-</span><span class="pln">p</span>
现在我们创建一个数据库,创建一个 MariaDB 用户并启用与 MariaDB 数据库的远程连接:
例如,使用以下参数:
- DB_NAME = idroot_db
- USER_NAME = idroot_user
- REMOTE_IP = 10.0.46.36
- 密码 = 您的强密码
- 权限 = 全部
<span class="com">## CREATE DATABASE ##</span> <span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> CREATE DATABASE idroot_db</span><span class="pun">;</span> <span class="com">## CREATE USER ##</span> <span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> CREATE USER </span><span class="str">'idroot_user'</span><span class="pun">@</span><span class="str">'10.0.15.25'</span><span class="pln"> IDENTIFIED BY </span><span class="str">'your-strong-passwd'</span><span class="pun">;</span> <span class="com">## GRANT PERMISSIONS ##</span> <span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> GRANT ALL ON idroot_db</span><span class="pun">.*</span><span class="pln"> TO </span><span class="str">'idroot_user'</span><span class="pun">@</span><span class="str">'10.0.46.36'</span><span class="pun">;</span> <span class="com">## FLUSH PRIVILEGES, Tell the server to reload the grant tables ##</span> <span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> FLUSH PRIVILEGES</span><span class="pun">;</span>
有关安装 MariaDB 的其他资源,请阅读以下帖子:
- 如何在 CentOS Stream √ 上安装 MariaDB
第 3 步。安装 PHP。
默认情况下,PHP 在 CentOS 9 Stream 基础存储库中可用。要在 CentOS Stream 上安装 PHP,你可以使用以下命令:
<span class="pln">sudo dnf install php php</span><span class="pun">-</span><span class="pln">mysqlnd php</span><span class="pun">-</span><span class="pln">fpm php</span><span class="pun">-</span><span class="pln">opcache php</span><span class="pun">-</span><span class="pln">gd php</span><span class="pun">-</span><span class="pln">xml php</span><span class="pun">-</span><span class="pln">mbstring</span>
安装完成后,您可以通过运行以下命令来启动 PHP-FPM 服务:
<span class="pln">sudo systemctl start php</span><span class="pun">-</span><span class="pln">fpm sudo systemctl enable php</span><span class="pun">-</span><span class="pln">fpm</span>
由于我们使用的是 Nginx Web 服务器,因此我们需要更改以下行:
<span class="pln">nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php</span><span class="pun">-</span><span class="pln">fpm</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">www</span><span class="pun">.</span><span class="pln">conf user </span><span class="pun">=</span><span class="pln"> apache </span><span class="kwd">group</span> <span class="pun">=</span><span class="pln"> apache</span>
将它们更改为:
<span class="pln">user </span><span class="pun">=</span><span class="pln"> nginx </span><span class="kwd">group</span> <span class="pun">=</span><span class="pln"> nginx</span>
有关安装 PHP 的其他资源,请阅读下面的帖子:
- 如何在 CentOS Stream √ 上安装 PHP
第 4 步。测试安装。
现在我们创建一个PHP测试页面来确认它正在与Nginx服务器一起使用:
<span class="pln">nano </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">test</span><span class="pun">.</span><span class="pln">php</span>
添加以下文件:
<span class="pun"><?</span><span class="pln">php phpinfo </span><span class="pun">();</span> <span class="pun">?></span>
保存并关闭文件,然后重新启动Nginx Web服务器,知道它也将为PHP请求提供服务:
<span class="pln">sudo systemctl restart nginx sudo systemctl restart php</span><span class="pun">-</span><span class="pln">fpm</span>
完成后,现在通过输入您的服务器 IP 或域名,然后输入例如 或test.php
localhost/test.php
your-IP-address/test.php
感謝你使用這本教鬆在 CentOS Stream 9 上安裝 LEMP 栈。有关其他帮助或有用信息,我们建议您查看 LEMP 官方网站。