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

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

LEMP 代表 Linux、Nginx、MySQL/MariaDB、PHP,是世界各地许多 Web 项目和 Web 应用程序的基础。此外,如果在stealthsettings.com 后面没有所有这些包来确保我们网站的在线运行,您将无法看到本文。

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

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

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

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

更新软件。

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

sudo apt update
sudo apt upgrade

如果有可供更新的软件包,请按 “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
在 Debian 12 上安装 Nginx Web 服务器
在 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!

接下来您将删除安装过程中默认设置的权限和数据库 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 “默认”, 编辑 “/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年开始,我对技术充满热情,在StealthSetts.com上写作。我在操作系统方面拥有丰富的经验:MacOS,Windows和Linux,以及编程语言和博客平台(WordPress)和在线商店(WooCommerce,Magento,Magento,Presashop)。

您的教程,有用的提示和新闻。 如何在Debian 12上安装和配置LEMP
发表评论