如何在 Debian 12 上安装 LEMP

Linux命令 Edge插件网 1年前 (2023-09-21) 244次浏览 已收录 0个评论

在不断发展的 Web 开发环境中,强大而安全的服务器环境是成功 Web 存在的基石。LEMP堆栈,包括Linux,Nginx,MySQL和PHP,是托管动态网站和Web应用程序的受人尊敬的选择。

如何在 Debian 12 上安装 LEMP

在 Debian 12 书虫上安装 LEMP

第 1 步。在我们安装任何软件之前,通过在终端中运行以下命令来确保您的系统是最新的非常重要:apt

<span class="pln">sudo apt update</span>

这将刷新程序包列表并将现有程序包升级到其最新版本,从而确保您拥有最新的安全更新。

第 2 步。安装Nginx。

Nginx是Web应用程序的看门人。它是一个高性能的Web服务器,以其速度和可扩展性而闻名:

<span class="pln">sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx</span>

第一个命令安装 Nginx,而随后的两个命令启动它并确保它在系统启动时自动启动。Nginx将很快成为您有效提供Web内容的最佳朋友。

要验证 Nginx 是否正常工作,请打开 Web 浏览器并输入服务器的 IP 地址或域名。您应该会看到默认的 Nginx 欢迎页面。这确认了Nginx已安装并正在运行。

第 3 步。安装 MySQL。

MySQL 是为您的动态 Web 应用程序提供支持的数据库引擎。让我们安装并保护它:

<span class="pln">sudo apt install mysql</span><span class="pun">-</span><span class="pln">server
sudo mysql_secure_installation</span>

第二个命令启动MySQL安全安装过程。按照屏幕上的提示设置 root 密码并删除测试数据库。这是增强服务器安全性的关键步骤。

第 4 步。安装 PHP。

PHP 是为您的 Web 应用程序带来生命的脚本语言。首先,将 PHP 存储库添加到您的系统中:

<span class="pln">sudo apt install software</span><span class="pun">-</span><span class="pln">properties</span><span class="pun">-</span><span class="pln">common
sudo </span><span class="kwd">add</span><span class="pun">-</span><span class="pln">apt</span><span class="pun">-</span><span class="pln">repository ppa</span><span class="pun">:</span><span class="pln">ondrej</span><span class="pun">/</span><span class="pln">php</span>

现在安装 PHP 8.2 以及一些常见的扩展:

<span class="pln">sudo apt update
sudo apt install php8</span><span class="pun">.</span><span class="lit">2</span><span class="pln"> php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">fpm php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">mysql php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">common php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">gd php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">json php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">cli php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">curl php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">zip php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">xml php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">mbstring php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">bcmath php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">json</span>

要确保正确安装 PHP,请运行以下命令:

<span class="pln">php </span><span class="pun">-</span><span class="pln">v</span>

第5步。配置 Nginx。

Nginx已安装,但我们需要对其进行配置以提供您的Web内容。这需要编辑默认服务器块配置:

<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">sites</span><span class="pun">-</span><span class="pln">available</span><span class="pun">/</span><span class="kwd">default</span>

在服务器块中,添加以下行,将 替换为您的实际域名和 Web 根目录:your_domain.comyour_root_directory

<span class="pln">server_name your_domain</span><span class="pun">.</span><span class="pln">com www</span><span class="pun">.</span><span class="pln">your_domain</span><span class="pun">.</span><span class="pln">com</span><span class="pun">;</span><span class="pln">
root </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">;</span><span class="pln">
index index</span><span class="pun">.</span><span class="pln">php index</span><span class="pun">.</span><span class="pln">html index</span><span class="pun">.</span><span class="pln">htm</span><span class="pun">;</span>

接下来,将 Nginx 配置为使用 PHP。在目录中创建一个新的 PHP 池配置文件:/etc/php/8.2/fpm/pool.d/

<span class="pln">sudo 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="lit">8.2</span><span class="pun">/</span><span class="pln">fpm</span><span class="pun">/</span><span class="pln">pool</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</span>

在文件中,找到并更新以下行:

<span class="pln">listen </span><span class="pun">=</span> <span class="str">/run/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">fpm</span><span class="pun">.</span><span class="pln">sock</span>

在块内,添加以下行以启用.php文件的 PHP 处理:server

<span class="pln">location </span><span class="pun">~</span><span class="pln"> \.php$ </span><span class="pun">{</span><span class="pln">
include snippets</span><span class="pun">/</span><span class="pln">fastcgi</span><span class="pun">-</span><span class="pln">php</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">;</span><span class="pln">
fastcgi_pass unix</span><span class="pun">:</span><span class="str">/run/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">fpm</span><span class="pun">.</span><span class="pln">sock</span><span class="pun">;</span>
<span class="pun">}</span>

保存文件并继续测试 Nginx 配置:

<span class="pln">sudo nginx </span><span class="pun">-</span><span class="pln">t</span>

此命令验证您的配置是否存在语法错误。如果一切正常,请重新启动Nginx:

<span class="pln">sudo systemctl restart nginx</span>

第 6 步。创建 MySQL 数据库。

您的MySQL服务器已安装,但我们需要创建一个数据库和一个用户来与之交互。让我们深入了解 MySQL shell:

<span class="pln">sudo mysql </span><span class="pun">-</span><span class="pln">u root </span><span class="pun">-</span><span class="pln">p</span>

出现提示时输入您的 MySQL 根密码。现在,让我们创建一个新的数据库、用户并授予权限:

<span class="pln">CREATE DATABASE your_database</span><span class="pun">;</span><span class="pln">
CREATE USER </span><span class="str">'your_user'</span><span class="pun">@</span><span class="str">'localhost'</span><span class="pln"> IDENTIFIED BY </span><span class="str">'your_strong_password'</span><span class="pun">;</span><span class="pln">
GRANT ALL PRIVILEGES ON your_database</span><span class="pun">.*</span><span class="pln"> TO </span><span class="str">'your_user'</span><span class="pun">@</span><span class="str">'localhost'</span><span class="pun">;</span><span class="pln">
FLUSH PRIVILEGES</span><span class="pun">;</span><span class="pln">
EXIT</span><span class="pun">;</span>

将 、 和 替换为首选值。此设置使 Web 应用程序能够与数据库无缝交互。your_databaseyour_useryour_password

步骤 7.安装Let’s Encrypt Certbot。

在当今的数字环境中,使用HTTPS保护您的网站是不可协商的。我们将使用 Let’s Encrypt 和 Certbot 来获取 SSL 证书:

<span class="pln">sudo apt install certbot python3</span><span class="pun">-</span><span class="pln">certbot</span><span class="pun">-</span><span class="pln">nginx</span>

安装Certbot后,获得SSL证书轻而易举。执行以下命令,替换为您的实际域:your_domain.com

<span class="pln">sudo certbot </span><span class="pun">--</span><span class="pln">nginx </span><span class="pun">-</span><span class="pln">d your_domain</span><span class="pun">.</span><span class="pln">com </span><span class="pun">-</span><span class="pln">d www</span><span class="pun">.</span><span class="pln">your_domain</span><span class="pun">.</span><span class="pln">com</span>

Certbot将指导您完成整个过程,要求您提供电子邮件并要求您同意条款。完成后,Certbot将自动为您的Nginx服务器块配置SSL。

让我们加密证书的有效期为 90 天,但您可以自动执行续订过程。Certbot 将在您的证书即将到期时自动续订证书。要测试续订过程,您可以运行:

<span class="pln">sudo certbot renew </span><span class="pun">--</span><span class="pln">dry</span><span class="pun">-</span><span class="pln">run</span>

第8步。防火墙配置。

防火墙是服务器的守护者,控制传入和传出流量。让我们配置简单防火墙 (UFW) 以确保服务器的安全:

<span class="pln">sudo ufw allow </span><span class="str">'Nginx Full'</span><span class="pln">
sudo ufw allow </span><span class="str">'OpenSSH'</span><span class="pln">
sudo ufw enable</span>

第一个命令允许完全访问Nginx,而第二个命令允许SSH流量进行远程服务器管理。最后,我们使UFW能够实施这些规则。

第9步。测试您的 LEMP 堆栈。

现在您的 LEMP 堆栈已安装并受到保护,必须验证其功能:

<span class="pln">sudo nano </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">info</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>

保存并退出文件,然后打开 Web 浏览器并导航到 (替换为您的实际域)。您应该会看到一个显示 PHP 信息的页面。这证实了 PHP 按预期运行。http://your_domain.com/info.phpyour_domain.com

感谢您使用本教程在 Debian 12 书虫上安装最新版本的 LEMP 堆栈。有关其他帮助或有用信息,我们建议您查看 LEMP 官方网站。


Edge插件网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在 Debian 12 上安装 LEMP
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址