兰雅sRGB 个人笔记 https://262235.xyz

什么是 LEMP 堆栈?如何在 Ubuntu 20.04 上进行设置 # What is LEMP Stack? How to set up on Ubuntu 20.04


What is LEMP Stack? How to set up on Ubuntu 20.04

什么是 LEMP 堆栈?如何在 Ubuntu 20.04 上进行设置

Are you looking for simple steps to install LEMP stack on your Ubuntu 20.04
server? Read this tutorial, as I am going to share steps to install LEMP stack
on Ubuntu.

您是否正在寻找在 Ubuntu 20.04 服务器上安装 LEMP 堆栈的简单步骤?阅读本教程,因为我将分享在 Ubuntu 上安装 LEMP 堆栈的步骤。

LEMP stack is a bundle of software tools that include Linux, Nginx, MariaDB,
and PHP. All these tools are open source and free to use for anyone around the

  1. The combination of these tools make a powerful server stack which can
  2. used to host high-performance websites. It is also a great way to install

WordPress on your VPS server.

LEMP 堆栈是一组软件工具,包括 Linux、Nginx、MariaDB 和 PHP。所有这些工具都是开源的,可供世界各地的任何人免费使用。这些工具的组合构成了一个强大的服务器堆栈,可用于托管高性能网站。这也是在 VPS 服务器上安装 WordPress 的好方法。

Let me tell you what these tools stand for in short.

  • Linux : A very popular & commonly used Operating System 一个非常流行和常用的操作系统
  • Nginx : A web server 一个网络服务器
  • MariaDB/MySQL : A database server 数据库服务器
  • PHP : Coding language used to build any kind of website. WordPress which is an open source platform is built using PHP. 用于构建任何类型网站的编码语言。WordPress 是一个使用 PHP 构建的开源平台。



To set up the LEMP stack on Ubuntu 20.04, you need Ubuntu 20.04 installed on
your server. Minimum server configuration should be

要在 Ubuntu 20.04 上设置 LEMP 堆栈,您需要在服务器上安装 Ubuntu 20.04。最低服务器配置应该是

  • 1GB Ram
  • 25GB SSD Storage
  • Root Access to the Server

You can easily create an Ubuntu 20.04 server on Skysilk for as low as
您可以以低至 5 美元/月的价格在 Skysilk 上轻松创建 Ubuntu 20.04 服务器。(实际5美元1年也有)

Let us proceed with this tutorial so you can set up & get it running quickly.

Connecting your Server


If you are running MacOS or Ubuntu operating system on your machine, you can
use Terminal app to remotely connect with your server. Simply use the below
command in your terminal app to login.

如果您的机器上运行 MacOS 或 Ubuntu 操作系统,您可以使用终端应用程序远程连接您的服务器。只需在您的终端应用程序中使用以下命令即可登录。

ssh root@

Step 1: Package Updates 软件包更新

It is a good practice to update the software packages and refresh the apt
repositories by running a few commands on Ubuntu 20.04 OS. Here are the
通过在 Ubuntu 20.04 操作系统上运行一些命令来更新软件包和刷新 apt 存储库是一个很好的做法。以下是命令:

sudo apt update
sudo apt upgrade

Step 2: Nginx Web Server Installation 服务器安装

Follow the below steps with commands to install Nginx server. Nginx is a
widely used server nowadays. To ensure, you install it properly on your
computer, enter the following commands.
按照以下步骤使用命令安装 Nginx 服务器。Nginx 是当今广泛使用的服务器。为确保在计算机上正确安装它,请输入以下命令。

sudo apt install nginx

With this command, Nginx will be installed. But now we need to enable it to
auto-start at boot time.
使用此命令,将安装 Nginx。但是现在我们需要让它在启动时自动启动。

So, run the following command to auto-start Nginx.
因此,运行以下命令以自动启动 Nginx。

sudo systemctl enable nginx

Now start Nginx with the below command
现在使用以下命令启动 Nginx

sudo systemctl start nginx

To check the status of Nginx, use the following command
要检查 Nginx 的状态,请使用以下命令

sudo systemctl status nginx

This is the output you get when you check the status of Nginx
这是您检查 Nginx 状态时得到的输出

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-07-05 13:09:03 UTC; 1min 22s ago
       Docs: man:nginx(8)
   Main PID: 14447 (nginx)
      Tasks: 2 (limit: 464250)
     Memory: 2.9M
     CGroup: /system.slice/nginx.service
             ├─14447 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─14448 nginx: worker process

If the status shows Enabled, it means Nginx will auto-start at boot time and
is running properly. You can also check the memory consumption by Nginx and
quit the command manually by pressing ‘q’ if it doesn't quit automatically.
如果状态显示为 Enabled,则表示 Nginx 将在启动时自动启动并正常运行。您还可以通过 Nginx 检查内存消耗,如果没有自动退出,请按“q”手动退出命令。

To check the Nginx version, use the below command
要检查 Nginx 版本,请使用以下命令

nginx -v

And it will show the output as follows: 它将显示如下输出:

nginx version: nginx/1.18.0 (Ubuntu)

Next, type the public IP address of your Ubuntu 20.04 server which you can get
from Skysilk VPS dashboard.
接下来,键入您可以从 Skysilk VPS 仪表板获取的 Ubuntu 20.04 服务器的公共 IP 地址。

If the Nginx server is running properly, you will see “ Welcome to Nginx! " page.
如果 Nginx 服务器运行正常,您将看到“欢迎使用 Nginx!” 页。

Just in case it does not work, the chances are that the UFW firewall is
preventing Port 80.
以防万一它不起作用,可能是 UFW 防火墙阻止了端口 80。

To open TCP port 80 when using UFW firewall, use this command
要在使用 UFW 防火墙时打开 TCP 端口 80,请使用此命令

sudo ufw allow http

To make Nginx user as the owner of the web directory, use:
要使 Nginx 用户成为 web 目录的所有者,请使用:

sudo chown www-data:www-data /usr/share/nginx/html -R

Step 3: MariaDB Database Server Installation 数据库服务器安装

You already know that MariaDB is an alternative for MySQL developed by the
former members of MySQL. To install MariaDB on Ubuntu 20.04, enter the below
你已经知道 MariaDB 是 MySQL 的前成员开发的 MySQL 的替代品。要在 Ubuntu 20.04 上安装 MariaDB,请输入以下命令。

sudo apt install mariadb-server mariadb-client

Now that MariaDB is installed, you can check its status.
现在 MariaDB 已安装,您可以检查其状态。

systemctl status mariadb

The output should be like:

● mariadb.service - MariaDB 10.3.29 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-07-05 13:13:28 UTC; 18s ago
       Docs: man:mysqld(8)
   Main PID: 15247 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 464250)
     Memory: 66.3M
     CGroup: /system.slice/mariadb.service
             └─15247 /usr/sbin/mysqld

If the status shows enabled, it means MariaDB has started. But if not, you can
use this command
如果状态显示已启用,则表示 MariaDB 已启动。但如果没有,你可以使用这个命令

sudo systemctl start mariadb

Just like Nginx, you can set MariaDB to auto-start at the boot time. Use this
command to auto-start MariaDB at boot time.
就像 Nginx 一样,您可以将 MariaDB 设置为在启动时自动启动。使用此命令在启动时自动启动 MariaDB。

sudo systemctl enable mariadb

Next, you need to run the post installation security script

sudo mysql_secure_installation

When asked about MariaDB root password, simply press Enter as there is no root
当询问 MariaDB root 密码时,只需按 Enter,因为没有 root 密码。

Now press Y to set the new root password and re-enter the password.
现在按 Y 设置新的 root 密码并重新输入密码。

Now press enter for all the other questions. This includes removing anonymous
users,disabling root login remotely, removing test databases & its access, and
reloading privilege tables.
现在按回车键回答所有其他问题。这包括删除匿名用户、远程禁用 root 登录、删除测试数据库及其访问权限以及重新加载权限表。

After completion of these steps, MariaDB installation is secured.
完成这些步骤后,MariaDB 安装就安全了。

If you want to login MariaDB without root password, you can run the command

sudo mariadb -u root

To exit, simple enter


To check MariaDB version, enter:
要检查 MariaDB 版本,请输入:

mariadb --version

So you could see that MariaDB 10.3.22 has been installed successfully.
可以看到 MariaDB 10.3.22 已经安装成功。

mariadb  Ver 15.1 Distrib 10.3.29-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Step 4: PHP 7.4 Installation 安装

To install PHP 7.4 with some minor improvement over PHP 7.3, enter the
following command:
要安装对 PHP 7.3 稍有改进的 PHP 7.4,请输入以下命令:

sudo apt install php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl

To ensure WordPress runs error free, install PHP extensions. Now use the
following command to to start PHP 7.4-fpm
为确保 WordPress 无错误运行,请安装 PHP 扩展。现在使用以下命令启动 PHP 7.4-fpm

sudo systemctl start php7.4-fpm

Just like you had run commands to auto-start Nginx and MariaDB, you can enable
auto-start PHP.
就像您运行命令来自动启动 Nginx 和 MariaDB 一样,您可以启用自动启动 PHP。

sudo systemctl enable php7.4-fpm

Now check the status of PHP

systemctl status php7.4-fpm

The output should be like:

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-07-05 13:21:45 UTC; 45s ago
       Docs: man:php-fpm7.4(8)
   Main PID: 26650 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 464250)
     Memory: 9.4M
     CGroup: /system.slice/php7.4-fpm.service
             ├─26650 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─26651 php-fpm: pool www
             └─26652 php-fpm: pool www

Press Q if the above command doesn’t exit immediately.
如果上述命令没有立即退出,请按 Q。

Step 5: Setting up Nginx Server Block 设置 Nginx 服务器块

The default server block cannot run PHP code so we create a Nginx server block
that acts like a virtual host in Apache. Run the following command to remove
the default symlink in sites-enabled directory.
默认服务器块无法运行 PHP 代码,因此我们创建了一个 Nginx 服务器块,它的作用类似于 Apache 中的虚拟主机。运行以下命令以删除启用站点的目录中的默认符号链接。

sudo rm /etc/nginx/sites-enabled/default

To create a new server block, use the following command:

sudo nano /etc/nginx/conf.d/default.conf

Now paste the following text into the file to make Nginx listen on IPv4 port
80 and IPv6 port 80.
现在将以下文本粘贴到文件中,使 Nginx 侦听 IPv4 端口 80 和 IPv6 端口 80。

server {
  listen 80;
  listen [::]:80;
  server_name _;
  root /usr/share/nginx/html/;
  index index.php index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ /index.php;

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;

 # A long browser cache lifetime can speed up repeat visits to your page
  location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;

  # disable access to hidden files
  location ~ /\.ht {
      access_log off;
      log_not_found off;
      deny all;

Save the file by Ctrl+x, type Y and enter to confirm. To close the file press
按Ctrl+x保存文件,输入Y并回车确认。要关闭文件,请按 Ctrl+X。

Now test the Nginx configuration.
现在测试 Nginx 配置。

sudo nginx -t

If the test is successful and your configuration files have proper syntax, it
will show you a prompt like this.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reload Nginx if the test is successful
如果测试成功,请重新加载 Nginx

sudo systemctl reload nginx

Step 5: Test PHP 测试 PHP

Create info.php file in the webroot directory to start testing PHP-FPM with
Nginx web server.
在 webroot 目录中创建 info.php 文件以开始使用 Nginx Web 服务器测试 PHP-FPM。

sudo nano /usr/share/nginx/html/info.php

Now paste the below php code into the file

<?php phpinfo(); ?>

Close the file and open a new browser window. Then enter **server-ip-
address/info.php** and replace server ip address with actual IP.
关闭文件并打开一个新的浏览器窗口。然后输入server-ip-address/info.php并将服务器 ip 地址替换为实际 IP。

You can see the PHP information on your system.
您可以在系统上查看 PHP 信息。

So if you follow the steps mentioned in this tutorial, you can successfully
install Nginx, MariaDB, and PHP 7.4 on ubuntu 20.04.
因此,如果您按照本教程中提到的步骤操作,您可以在 ubuntu 20.04 上成功安装 Nginx、MariaDB 和 PHP 7.4。

Do not forget to delete the info.php file for server security. Deleting this
file prevents your file from getting hacked.
为了服务器安全,不要忘记删除 info.php 文件。删除此文件可防止您的文件被黑客入侵。

sudo rm /usr/share/nginx/html/info.php

If at any time you find an error, you can check the Nginx error
log(/var/log/nginx/error.log). This would help you troubleshoot the issues
quite effectively.
如果任何时候发现错误,可以查看 Nginx 错误日志(/var/log/nginx/error.log)。这将帮助您非常有效地解决问题。

Nginx Auto-restart 自动重启

If the Nginx process breaks, you can run the following command:
如果 Nginx 进程中断,您可以运行以下命令:

sudo systemctl restart nginx

You can alternatively restart Nginx automatically instead of manually typing
this command. Create a separate directory to override the default system
您也可以自动重新启动 Nginx,而不是手动输入此命令。创建一个单独的目录来覆盖默认的系统服务。

sudo mkdir -p /etc/systemd/system/nginx.service.d/

Now create a file under the directory you just created.

sudo nano /etc/systemd/system/nginx.service.d/restart.conf

By adding the following command lines, Nginx will restart automatically If a
failure is detected. The default value is 100ms which is too small, so change
it to 5 seconds.
通过添加以下命令行,Nginx 会在检测到故障时自动重启。默认值是 100ms 太小了,所以把它改成 5 秒。


For changes to take effect, reload the system after closing the file.

sudo systemctl daemon-reload

Kill Nginx and check it with the following command:
杀死 Nginx 并使用以下命令检查它:

sudo pkill nginx

Now check the Nginx status
现在检查 Nginx 状态

systemctl status nginx

Conclusion 结论

After following the steps in this tutorial, you can install LEMP stack on
Ubuntu 20.04 successfully. You can use LEMP
stack to host multiple websites on a VPS server by simply creating multiple
server blocks.
按照本教程中的步骤操作后,您可以在Ubuntu 20.04 上成功安装 LEMP 堆栈。您可以通过简单地创建多个服务器块,使用 LEMP 堆栈在 VPS 服务器上托管多个网站。

About the author : Vashishtha Kapoor
is a Web Hosting Expert and has over 5 years of experience in SEO, PPC,
Website Management and WordPress development. Vashishtha writes about
WordPress customizations, web design, server administration and ad-tech topics
关于作者: Vashishtha Kapoor 是一名虚拟主机专家,在 SEO、PPC、网站管理和 WordPress 开发方面拥有超过 5 年的经验。Vashishtha 撰写有关 WordPress 自定义、网页设计、服务器管理和广告技术主题的文章

本原创文章自由转载,转载请注明本博来源及网址 | 当前页面:兰雅sRGB 个人笔记 » 什么是 LEMP 堆栈?如何在 Ubuntu 20.04 上进行设置 # What is LEMP Stack? How to set up on Ubuntu 20.04