如何在 Debian 12 上安装和配置 LEMP

在本教程中,您将了解在 Debian 12 上安装和配置 LEMP 所需遵循的步骤。Nginx、MariaDB 和 PHP 是高性能 Web 托管的必要软件包,无论是演示网站、博客还是在线商店。

LEMP 是缩写 Linux、Nginx、MySQL/MariaDB、PHP,是世界各地许多 Web 项目和 Web 应用程序的基础。 不然后面你就看不到这篇文章了 stealthsettings.com 不会拥有所有这些软件包来确保我们网站的在线运行。

Debian 是一个基于 Debian 的开源操作系统 Linux,以其稳定性、安全性和灵活性而闻名。 Debian 拥有悠久的历史和活跃的开发者社区,提供了广泛的软件包和库来满足用户的多样化需求。 该操作系统经常用于为公司创建可靠且高效的服务器,但它也可以适合个人使用。

如何在 Debian 12 上安装和配置 LEMP

在 Debian 12 上安装和配置 LEMP 之前的第一步是通过 SSH 连接到服务器(如果您有本地访问权限,则直接从控制台连接)。 确保服务器上尚未安装来自 LEMP 或管理系统的软件包,例如:cPanel、 VestaCP 或其他。

本教程中的命令集以“sudo”,但是如果您使用用户“连接到服务器root“, 没有必要 ”sudo” 在命令行前面。 命令 ”sudo” 用于在命令行前面临时向普通用户授予超级用户权限,允许他们执行需要特殊权限或访问受保护资源的命令,例如安装或配置软件、管理系统或执行其他敏感操作。

Update 软件。

在开始 LAMP 安装之前,建议更新操作系统和已安装的软件包。

sudo apt update
sudo apt upgrade

如果有可用的软件包 update, 按 ”Y” 当被问到时:

Do you want to continue? [Y/n] Y

在 Debian 12 上安装和配置 Nginx Web 服务器

为了使网络服务器向访问者提供网页, Nginx Web Server 必须正确安装和配置它。

sudo apt install nginx

类型 ”Y”以确认 Nginx Web 服务器的安装。

Do you want to continue? [Y/n] Y
Install Debian 12 上的 Nginx Web 服务器
Install Debian 12 上的 Nginx Web 服务器

安装过程结束时的行应如下所示:

Setting up nginx-common (1.22.1-9) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
Setting up nginx (1.22.1-9) ...
Upgrading binary: nginx.
Processing triggers for man-db (2.11.2-2) ...

最后,为了确保一切正常工作,请访问地址 IP 在网络浏览器中。 http://your_server_ip.

欢迎来到 NGINX 页面
欢迎来到 NGINX 页面

Nginx 服务已在您的服务器上启动并运行,并准备好提供网页服务。

相关新闻:

在 Debian 12 上安装和配置 MariaDB 服务器

与 MySQL 相比,MariaDB 提供了改进的性能、更快的复制速度、增强的安全措施以及额外的存储引擎。

运行下面的命令行并输入“Y” 确认安装 MariaDB Server.

sudo apt install mariadb-server

安装后,您需要通过运行脚本来保护 MariaDB Server: mysql_secure_installation。 它将限制对服务器的访问并删除未使用的帐户。

运行命令行:

sudo mysql_secure_installation

按 ”Enter“当前用户密码”root“。

如何在 Debian 12 上安装和配置 LEMP
安全 SQL #1
Switch to unix_socket authentication [Y/n] Y

设置密码 MariaDB:

Change the root password? [Y/n] Y     
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

接下来您将删除放置的权限和数据库 default 安装期间 MariaDB Server.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

上面为帐户设置的密码“root” 登录远程服务器时需要。 偏僻的。 这是密码 MariaDB.

测试您的 SQL Server 安装。

root@mars:~# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.11.3-MariaDB-1 Debian 12

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)]> select version();
+-------------------+
| version()         |
+-------------------+
| 10.11.3-MariaDB-1 |
+-------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> quit
Bye
root@mars:~# 

在 Debian 12 上安装和配置 PHP / PHP-FPM

如果到此步骤一切正常,为了在 Debian 12 上安装和配置 LEMP,您还需要 PHP 软件。 要在 Nginx 中支持 PHP,您还需要安装 PHP-FPM。

sudo apt install php-fpm php-mysql php-gd php-cli php-curl php-mbstring php-zip php-opcache php-xml

等待安装过程完成 PHP-FPM,以及最重要的 PHP 模块。

最后,在我的教程中我能够安装 PHP 8.2 在 Debian 12 操作系统上。

root@mars:~# php -v
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies
root@mars:~# 

至此,LEMP的所有元素都已安装,但仍有一些配置需要完成 Nginx 执行 PHP 文件。

配置 NGINX 执行 PHP 文件

为页面配置Nginx“default“, 编辑 ”/etc/nginx/sites-enabled/default”。 我更喜欢 ”nano”进行编辑。

替换为“location /”行:

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
     }

与行:

location / {
    try_files $uri $uri/ /index.php?$args;
    }

接下来,在块“中添加以下行server” 以允许 Nginx 处理 PHP。

location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/var/run/php/php-fpm.sock;
     }
}
如何在 Debian 12 上安装和配置 LEMP
如何在 Debian 12 上安装和配置 LEMP

保存文件并检查配置:

root@mars:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@mars:~# 

如果配置测试成功,重启服务 nginx.

sudo systemctl restart nginx

测试您是否已在 Debian 12 上成功安装和配置 LEMP

在 Debian 12 (LEMP) 上安装并配置 Nginx、MariaDB 和 PHP 后,是时候测试一切是否正常。 要进行测试,最简单的方法是创建一个 info.php 文件。

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

在浏览器中访问“https://your_server_ip/info.php“。

PHP 版本信息
PHP 版本信息

PHP 页面意味着您已成功安装 Debian 12 的 LEMP。

恭喜!

作为科技爱好者,我从2006年开始在StealthSettings.com上愉快地撰写文章。我在操作系统方面有丰富的经验,包括macOS、Windows和Linux,还熟悉编程语言和博客平台(WordPress),以及在线商店平台(WooCommerce、Magento、PrestaShop)。

如何以 » Web Hosting » 如何在 Debian 12 上安装和配置 LEMP
发表评论