Introduction

LEMP软件堆栈是一组软件,可用于提供用PHP编写的动态网页和web应用程序。这是描述Linux操作系统的首字母缩写,带有Nginx(发音类似“Engine-X”)web服务器。后台数据存储在MySQL数据库中,动态处理由PHP处理。

本指南演示如何在Ubuntu服务器上安装LEMP堆栈。Ubuntu操作系统负责堆栈中的Linux部分。我们将描述如何启动和运行其余组件。

Reference

How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu

CentOS 7 安装 LNMP 环境

Prerequisites

本教程对应系统版本为 Ubuntu 22.04,以非 root sudo 用户的身份登录,忽略 Ubuntu 防火墙配置(若需要防火墙请自行搜索文档进行配置),不考虑数据库的外部可访问性

关于配置 ssh 服务

Reference: ssh-default-port-not-changing-ubuntu-22-10-and-later

In Ubuntu 24.04, everything works with the default setup, just the behavior is now slightly different than before.

To change the ssh port, just uncomment the line starting with Port in /etc/ssh/sshd_config (remove the hashtag # in front of the line), then change the value from 22 to whatever is suitable for your needs.

The new thing is: To activate this new config, it is now required to inform systemd about the change:

sudo systemctl daemon-reload

 

Then the ssh service and socket can be restarted as before, to activate the change.

配置防火墙

在 Ubuntu 中配置防火墙通常使用 UFW(Uncomplicated Firewall),它是基于 iptables 的简化工具。

安装和启用 UFW:

sudo apt update && sudo apt install ufw
sudo ufw enable

 

允许/拒绝特定端口或服务:

# 允许 SSH
sudo ufw allow ssh  # 或指定端口:sudo ufw allow 22/tcp

# 允许 HTTP/HTTPS
sudo ufw allow http   # 80/tcp
sudo ufw allow https  # 443/tcp

# 允许特定端口
sudo ufw allow 8080/tcp

# 拒绝端口
sudo ufw deny 3306/tcp  # 阻止 MySQL 默认端口

 

配置默认策略:

# 默认拒绝所有传入流量,允许所有传出流量
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 允许特定 IP 或子网
sudo ufw allow from 192.168.1.100  # 允许单个 IP
sudo ufw allow from 192.168.1.100 to any port 22  # 允许 IP 访问特定端口
sudo ufw allow from 192.168.1.0/24  # 允许子网

 

管理规则:

# 查看规则
sudo ufw status numbered  # 显示带编号的规则列表

# 删除规则
sudo ufw delete [规则编号]  # 根据编号删除
sudo ufw delete allow 80/tcp  # 根据规则内容删除

# 重置防火墙
sudo ufw reset

 

其他操作:

# 禁用 UFW
sudo ufw disable

# 查看日志
sudo ufw logging on  # 启用日志(默认路径:/var/log/ufw.log)

# 检查防火墙状态
sudo ufw status verbose

 

安装 Nginx Web 服务器

使用 APT 包管理器安装 Nginx:

# 更新服务器的包索引
sudo apt update

# 安装 Nginx
sudo apt install nginx

# 设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx

 

在浏览器中输入服务器的地址,应该能看到 Nginx 的默认页面,则表示已成功安装 Nginx

安装 MySQL

使用 APT 包管理器安装 MySQL:

sudo apt install mysql-server

 

安装完成后,运行 MySQL 预装的安全脚本,该脚本将删除一些不安全的默认设置并锁定对数据库系统的访问:

sudo mysql_secure_installation

 

系统将提示您一个问题,询问您是否要配置 VALIDATE PASSWORD PLUGIN

Output
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: 

 

回答 Y 表示启用,或者回答其他任何问题以继续而不启用:

注意:启用此功能需要进行判断。如果启用,不符合指定条件的密码将被 MySQL 拒绝并显示错误。禁用验证是安全的,但您应该始终对数据库凭据使用强且唯一的密码。

如果启用的话,系统会要求您选择密码验证级别。请记住,如果您输入 2 作为最强级别,则在尝试设置任何不包含数字、大小写字母和特殊字符的密码时,您将收到错误消息:

Output
There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

 

无论您是否选择设置 VALIDATE PASSWORD PLUGIN ,您的服务器都会要求您选择并确认 MySQL root 用户的密码。不要将其与系统 root 用户混淆,数据库 root 用户是对数据库系统拥有完全权限的管理用户。尽管 MySQL root 用户的默认身份验证方法不需要使用密码,但即使设置了密码,您也应该在此处定义一个强密码作为额外的安全措施。

如果您启用了密码验证,您将看到您输入的 root 密码的密码强度,并且您的服务器将询问您是否要继续使用该密码。如果您对当前密码感到满意,请在出现提示时按 Y 选择“是”:

Output
Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

 

对于其余问题,请按 Y 并在每次提示时按 ENTER 键。这将删除一些匿名用户和测试数据库,禁用远程 root 登录,并加载这些新规则,以便 MySQL 立即应用这里所做的更改。

完成后,测试是否能够登录 MySQL 控制台:

sudo mysql

 

这将以管理数据库用户 root 的身份连接到 MySQL 服务器:

Output
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28-0ubuntu4 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

 

退出 MySQL 控制台:

mysql> exit

 

请注意,即使您在运行 mysql_secure_installation 脚本时定义了密码,也无需提供密码即可以 root 用户身份进行连接。这是因为,当安装在 Ubuntu 上时,管理 MySQL 用户的默认身份验证方法是 auth_socket ,而不是使用密码的方法。乍一看这似乎是一个安全问题,但它使数据库服务器更加安全,因为唯一允许以 root MySQL 用户身份登录的用户是具有从控制台或通过连接的 sudo 权限的系统用户以相同权限运行的应用程序。实际上,这意味着您将无法使用管理数据库根用户从 PHP 应用程序进行连接。

如果要将 root 从 auth_socket 修改为密码登录,可参考以下方法:

sudo mysql
mysql> use mysql;
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

# enable password login
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';
mysql> FLUSH PRIVILEGES;
mysql> exit;

# should be able to login with password now
mysql -u root -p
Enter password:

mysql>

 

为了提高安全性,最好为每个数据库设置专用的用户帐户,并设置较少的权限,特别是如果您计划在服务器上托管多个数据库。

注意:某些旧版本的本机 MySQL PHP 库 mysqlnd 不支持 caching_sha2_authentication ,这是创建用户 MySQL 8 的默认身份验证方法。因此,在为 PHP 创建数据库用户时MySQL 8 上的应用程序,您可能需要确保它们配置为使用 mysql_native_password

安装 PHP

Nginx 需要外部程序来处理 PHP 处理,并充当 PHP 解释器本身和 Web 服务器之间的桥梁。这可以在大多数基于 PHP 的网站中提供更好的整体性能,但需要额外的配置。

安装 php8.1-fpm 和相关软件包:

sudo apt install php8.3-fpm php-common php-opcache php-mysql php-mysqlnd php-zip php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json

配置 Nginx 使用 PHP

在 nginx 站点中参考如下配置:

server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/your_domain;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

}

 

Ubuntu 基础 LEMP 环境整备
https://ailitonia.com/archives/ubuntu-%e5%9f%ba%e7%a1%80-lemp-%e7%8e%af%e5%a2%83%e6%95%b4%e5%a4%87/
本文被阅读了:1,462次
作者
Ailitonia
发布于
2025年5月22日
许可协议
版权声明

作者头像
关于 @Ailitonia
正因站在了巨人的肩膀上,才越发觉得自己渺小。不求成为巨人,但求与其同行。 把自己所见所闻,记录下来。
→查看所有由Ailitonia发布的文章