LEMP 堆栈(Linux、Nginx、MySQL/MariaDB 和 PHP)是一个免费的开源 Web 应用程序堆栈,用于开发和部署 Web 应用程序。 LEMP 堆栈类似于 LAMP 堆栈,但它用 Nginx 代替 Apache 网络服务器。 本指南介绍了怎样在 Debian 11 上安装 LEMP 堆栈并使用 Certbot 通过 Let’s Encrypt TLS/SSL 证书对其进行保护。
先决条件
- 部署一个 Debian 11 云服务器。
- 创建一个非root用户 sudo 使用 SSH 访问和登录。
- 更新 Debian 服务器。
本指南假定您要同时使用 apex 域 example.com
和 www.example.com
服务器的主机名。 要遵循本指南,您应该同时分配顶点域(有时称为 @
) 和 www
您的 DNS 设置中的服务器 IP 地址的主机名。
1.安装Nginx
安装 Nginx 网络服务器。
$ sudo apt-get install nginx -y
启动 Nginx 服务。
$ sudo systemctl start nginx
启用 Nginx 服务以在系统重新启动时启动。
$ sudo systemctl enable nginx
检查 Nginx 版本以验证安装。
$ sudo nginx -v
你应该看到这样的输出:
$ nginx version: nginx/1.18.0
2. 配置防火墙
列出可用的应用程序配置文件。
$ sudo ufw app list
在其他条目中,您应该看到以下配置文件:
Nginx Full Nginx HTTP Nginx HTTPS
- 这 Nginx 完整版 profile 同时打开 HTTPS (443) 和 HTTP (80) 端口。
- 这 Nginx HTTP profile 仅打开 HTTP (80) 端口。
- 这 Nginx HTTPS profile 仅打开 HTTPS (443) 端口。
允许 Nginx 完整版 防火墙中的配置文件。 Certbot 需要端口 80 和 443 来安装 Let’s Encrypt TLS/SSL 证书。
$ sudo ufw allow 'Nginx Full'
检查防火墙状态。
$ sudo ufw status
你应该看到这样的输出:
To Action From -- ------ ---- 22 ALLOW Anywhere Nginx Full ALLOW Anywhere 22 (v6) ALLOW Anywhere (v6) Nginx Full (v6) ALLOW Anywhere (v6)
3. 创建 Nginx 虚拟主机
删除默认的 Nginx 配置。
$ sudo rm -rf /etc/nginx/sites-enabled/default && $ sudo rm -rf /etc/nginx/sites-available/default
创建 Nginx 虚拟主机配置文件。 代替
your-domain-name.com
与您的域名。$ sudo nano /etc/nginx/sites-available/your-domain-name.com
将此粘贴到文件中。 代替
example.com
与您的域名。server { listen 80; server_name example.com www.example.com; root /var/www/html; index index.php index.html index.nginx-debian.html; access_log /var/log/nginx/example_access.log; error_log /var/log/nginx/example_error.log; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ .php$ { try_files $uri =404; fastcgi_index index.php; include fastcgi_params; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
启用新的 Nginx 配置。 代替
example.com
与您的域名。$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
重新加载 Nginx 服务。
$ sudo systemctl reload nginx
4. 安装 MariaDB
安装 MariaDB 数据库服务器。
$ sudo apt-get install mariadb-server -y
启动 MariaDB 服务。
$ sudo systemctl start mariadb
启用 MariaDB 服务以在系统重新启动时启动。
$ sudo systemctl enable mariadb
5. 保护 MariaDB 数据库服务器
MariaDB 提供了一个安全脚本来保护数据库。 运行它并回答所有安全问题,如图所示。
$ sudo mysql_secure_installation
最初,root 没有密码。 按 ENTER。
Enter current password for root (enter for none): OK, successfully used password, moving on...
按 Y 切换到 unix_socket 身份验证。
Switch to unix_socket authentication [Y/n] Y Enabled successfully! Reloading privilege tables.. ... Success!
按 Y 更改 root 密码。
Change the root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!
按 Y 删除匿名用户。
Remove anonymous users? [Y/n] Y ... Success!
按 Y 删除远程 root 登录。
Disallow root login remotely? [Y/n] Y ... Success!
按 Y 删除测试数据库并访问它。
Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!
按 Y 重新加载权限表。
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!
连接到 MariaDB shell 并输入您的 MariaDB 根密码。
$ sudo mysql -u root -p
检查 MariaDB 版本以验证安装。
MariaDB [(none)]> SELECT @@version;
它应该返回如下内容:
+---------------------------+ | @@version | +---------------------------+ | 10.5.12-MariaDB-0+deb11u1 | +---------------------------+ 1 row in set (0.000 sec)
Exit MariaDB 外壳。
MariaDB [(none)]> exit
6.安装PHP
安装 PHP-FPM 7.4 和其他所需的软件包。
$ sudo apt-get install php php-fpm php-curl php-cli php-zip php-mysql php-xml -y
检查 PHP 版本以验证安装。
$ php -v
它应该返回如下内容:
PHP 7.4.28 (cli) (built: Feb 17 2022 16:17:19) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies
在您的编辑器中创建一个 PHP 测试文件。
$ sudo nano /var/www/html/phpinfo.php
将此粘贴到您的
phpinfo.php
文件。<?php phpinfo(); ?>
Save 并退出文件。
- 在浏览器中,导航到
https://www.example.com/phpinfo.php
查看 PHP 测试文件,其中显示了 PHP 信息。
7. 安装 Let’s Encrypt 证书
Certbot 需要 Snap。 安装
snapd
并启用经典 Snap 支持。$ sudo apt install snapd
注销并重新登录,或重新启动系统,以更新 Snap 的路径。
安装核心 Snap 以获取最新版本
snapd
.$ sudo snap install core
更新核心快照。
$ sudo snap refresh core
验证没有安装 Certbot 软件包
apt
.$ sudo apt remove certbot
使用 Snap 安装 Certbot。
$ sudo snap install --classic certbot
将 Certbot 链接到
/usr/bin
.$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
为您的服务器申请证书。 更换 example 电子邮件和域名您的价值观。 显示的命令请求多域 (SAN) 证书
example.com
和www.example.com
.$ sudo certbot --nginx --agree-tos --redirect --email [email protected] -d example.com -d www.example.com
在 SSL 检查网站上测试您的 SSL 配置,例如 SSL 实验室.
- 导航到您的网站并验证 SSL 证书是否按预期工作。
这样就完成了 LEMP 服务器的初始设置。 该服务器已准备好让您安装与 LEMP 一起使用的应用程序,或开发您自己的应用程序。
注:本教程在Vultr VPS上测试通过,如需部署请前往Vultr.com