您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure VM 上安装 LEMP Web 服务器Install a LEMP web server on an Azure VM

本文逐步讲解如何在 Azure 中的 Ubuntu VM 上部署 NGINX Web 服务器、MySQL 和 PHP(LEMP 堆栈)。This article walks you through how to deploy an NGINX web server, MySQL, and PHP (the LEMP stack) on an Ubuntu VM in Azure. LEMP 堆栈可以替代常用的 LAMP 堆栈,可安装在 Azure 中。The LEMP stack is an alternative to the popular LAMP stack, which you can also install in Azure. 若要了解 LEMP 服务器的运作情况,可以选择性地安装并配置 WordPress 站点。To see the LEMP server in action, you can optionally install and configure a WordPress site. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 创建 Ubuntu VM(LEMP 堆栈中的“L”)Create an Ubuntu VM (the 'L' in the LEMP stack)
  • 为 Web 流量打开端口 80Open port 80 for web traffic
  • 安装 NGINX、MySQL 和 PHPInstall NGINX, MySQL, and PHP
  • 验证安装和配置Verify installation and configuration
  • 在 LEMP 服务器上安装 WordPressInstall WordPress on the LEMP server

此设置用于快速测试或概念证明。This setup is for quick tests or proof of concept.

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”按钮对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy button to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/bashhttps://shell.azure.com/bash
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud Shell

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.4 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.4 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLI 2.0If you need to install or upgrade, see Install Azure CLI 2.0.

创建资源组Create a resource group

使用 az group create 命令创建资源组。Create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

以下示例在“eastus”位置创建名为“myResourceGroup”的资源组。The following example creates a resource group named myResourceGroup in the eastus location.

az group create --name myResourceGroup --location eastus

创建虚拟机Create a virtual machine

使用 az vm create 命令创建 VM。Create a VM with the az vm create command.

下面的示例创建一个名为 myVM 的 VM,并且在默认密钥位置中不存在 SSH 密钥时创建这些密钥。The following example creates a VM named myVM and creates SSH keys if they do not already exist in a default key location. 若要使用特定的一组密钥,请使用 --ssh-key-value 选项。To use a specific set of keys, use the --ssh-key-value option. 该命令还会将 azureuser 设置为管理员用户名。The command also sets azureuser as an administrator user name. 稍后要使用此名称连接到 VM。You use this name later to connect to the VM.

az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image UbuntuLTS \
    --admin-username azureuser \
    --generate-ssh-keys

创建 VM 后,Azure CLI 会显示类似于以下示例的信息。When the VM has been created, the Azure CLI shows information similar to the following example. 记下 publicIpAddressTake note of the publicIpAddress. 在后续步骤中,将使用此地址访问 VM。This address is used to access the VM in later steps.

{
  "fqdns": "",
  "id": "/subscriptions/<subscription ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
  "location": "eastus",
  "macAddress": "00-0D-3A-23-9A-49",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "40.68.254.142",
  "resourceGroup": "myResourceGroup"
}

为 Web 流量打开端口 80Open port 80 for web traffic

默认情况下,仅允许通过 SSH 连接登录到 Azure 中部署的 Linux VM。By default, only SSH connections are allowed into Linux VMs deployed in Azure. 由于此 VM 将用作 Web 服务器,因此需要从 Internet 打开端口 80。Because this VM is going to be a web server, you need to open port 80 from the internet. 使用 az vm open-port 命令打开所需端口。Use the az vm open-port command to open the desired port.

az vm open-port --port 80 --resource-group myResourceGroup --name myVM

通过 SSH 连接到 VMSSH into your VM

如果还不知道 VM 的公共 IP 地址,请运行 az network public-ip list 命令。If you don't already know the public IP address of your VM, run the az network public-ip list command. 在多个后续步骤中都需要用到此 IP 地址。You need this IP address for several later steps.

az network public-ip list --resource-group myResourceGroup --query [].ipAddress

使用以下命令创建与虚拟机的 SSH 会话。Use the following command to create an SSH session with the virtual machine. 使用虚拟机的正确的公共 IP 地址进行替换。Substitute the correct public IP address of your virtual machine. 在此示例中,IP 地址为 40.68.254.142In this example, the IP address is 40.68.254.142. azureuser 是创建 VM 时设置的管理员用户名。azureuser is the administrator user name set when you created the VM.

ssh azureuser@40.68.254.142

安装 NGINX、MySQL 和 PHPInstall NGINX, MySQL, and PHP

运行以下命令更新 Ubuntu 包源并安装 NGINX、MySQL 和 PHP。Run the following command to update Ubuntu package sources and install NGINX, MySQL, and PHP.

sudo apt update && sudo apt install nginx mysql-server php-mysql php php-fpm

系统会提示安装包和其他依赖项。You are prompted to install the packages and other dependencies. 出现提示时,请为 MySQL 设置 root 密码,并按 [Enter] 继续。When prompted, set a root password for MySQL, and then [Enter] to continue. 遵照剩余的提示操作。Follow the remaining prompts. 此股从会安装最低要求的 PHP 扩展,这些扩展是通过 MySQL 使用 PHP 所必需的。This process installs the minimum required PHP extensions needed to use PHP with MySQL.

MySQL root 密码页

验证安装和配置Verify installation and configuration

NGINXNGINX

使用以下命令检查 NGINX 版本:Check the version of NGINX with the following command:

nginx -v

安装 NGINX 并向 VM 打开端口 80 以后,即可通过 Internet 访问 Web 服务器。With NGINX installed, and port 80 open to your VM, the web server can now be accessed from the internet. 若要查看 NGINX 欢迎页,请打开 Web 浏览器并输入 VM 的公共 IP 地址。To view the NGINX welcome page, open a web browser, and enter the public IP address of the VM. 使用通过 SSH 连接到 VM 时所用的公共 IP 地址:Use the public IP address you used to SSH to the VM:

NGINX 默认页

MySQLMySQL

使用以下命令检查 MySQL 版本(请注意大写的 V 参数):Check the version of MySQL with the following command (note the capital V parameter):

mysql -V

若要帮助保护 MySQL 的安装,请运行 mysql_secure_installation 脚本。To help secure the installation of MySQL, run the mysql_secure_installation script. 如果只是设置临时服务器,则可以跳过此步骤。If you are only setting up a temporary server, you can skip this step.

mysql_secure_installation

输入 MySQL 的 root 密码,并配置环境的安全设置。Enter a root password for MySQL, and configure the security settings for your environment.

如果想要试用 MySQL 功能(创建 MySQL 数据库、添加用户或更改配置设置),请登录到 MySQL。If you want to try MySQL features (create a MySQL database, add users, or change configuration settings), login to MySQL. 此步骤非本教程必需步骤。This step is not required to complete this tutorial.

mysql -u root -p

完成后,键入 \q 退出 mysql 提示符。When done, exit the mysql prompt by typing \q.

PHPPHP

使用以下命令检查 PHP 版本:Check the version of PHP with the following command:

php -v

配置 NGINX 以使用 PHP FastCGI Process Manager (PHP-FPM)。Configure NGINX to use the PHP FastCGI Process Manager (PHP-FPM). 运行以下命令备份原始 NGINX 服务器块配置文件,并在所选的编辑器中编辑原始文件:Run the following commands to back up the original NGINX server block config file and then edit the original file in an editor of your choice:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default_backup

sudo sensible-editor /etc/nginx/sites-available/default

在编辑器中,将 /etc/nginx/sites-available/default 的内容替换为以下内容。In the editor, replace the contents of /etc/nginx/sites-available/default with the following. 请参阅注释了解设置说明。See the comments for explanation of the settings. 用自己 VM 的公共 IP 地址替换 yourPublicIPAddress,并保留其余设置。Substitute the public IP address of your VM for yourPublicIPAddress, and leave the remaining settings. 然后保存文件。Then save the file.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    # Homepage of website is index.php
    index index.php;

    server_name yourPublicIPAddress;

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

    # Include FastCGI configuration for NGINX
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

检查 NGINX 配置中的语法错误:Check the NGINX configuration for syntax errors:

sudo nginx -t

如果语法正确,请使用以下命令重启 NGINX:If the syntax is correct, restart NGINX with the following command:

sudo service nginx restart

如果想要进一步测试,请创建一个可在浏览器中查看的快速 PHP 信息页。If you want to test further, create a quick PHP info page to view in a browser. 以下命令创建 PHP 信息页:The following command creates the PHP info page:

sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/info.php'

现在,可以检查创建的 PHP 信息页。Now you can check the PHP info page you created. 打开浏览器并转到 http://yourPublicIPAddress/info.phpOpen a browser and go to http://yourPublicIPAddress/info.php. 替换 VM 的公共 IP 地址。Substitute the public IP address of your VM. 应如下图所示。It should look similar to this image.

PHP 信息页

安装 WordPressInstall WordPress

若要尝试堆栈,请安装示例应用。If you want to try your stack, install a sample app. 例如,以下步骤安装用于创建网站和博客的开源 WordPress 平台。As an example, the following steps install the open source WordPress platform to create websites and blogs. 其他可尝试的工作负荷包括 DrupalMoodleOther workloads to try include Drupal and Moodle.

此 WordPress 设置适用于概念证明。This WordPress setup is only for proof of concept. 若要在生产环境中使用推荐的安全设置安装最新的 WordPress,请参阅 WordPress 文档To install the latest WordPress in production with recommended security settings, see the WordPress documentation.

安装 WordPress 包Install the WordPress package

运行以下命令:Run the following command:

sudo apt install wordpress

配置 WordPressConfigure WordPress

将 WordPress 配置为使用 MySQL 和 PHP。Configure WordPress to use MySQL and PHP.

在工作目录中创建文本文件 wordpress.sql,以配置 WordPress 的 MySQL 数据库:In a working directory, create a text file wordpress.sql to configure the MySQL database for WordPress:

sudo sensible-editor wordpress.sql

添加以下命令,将 yourPassword 替换为所选的数据库密码(其他值不变)。Add the following commands, substituting a database password of your choice for yourPassword (leave other values unchanged). 如果以前将 MySQL 安全策略设置为验证密码强度,请确保该密码符合强度要求。If you previously set up a MySQL security policy to validate password strength, make sure the password meets the strength requirements. 保存文件。Save the file.

CREATE DATABASE wordpress;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER
ON wordpress.*
TO wordpress@localhost
IDENTIFIED BY 'yourPassword';
FLUSH PRIVILEGES;

若要创建数据库,请运行以下命令:Run the following command to create the database:

cat wordpress.sql | sudo mysql --defaults-extra-file=/etc/mysql/debian.cnf

由于文件 wordpress.sql 包含数据库凭据,因此在使用后请将其删除:Because the file wordpress.sql contains database credentials, delete it after use:

sudo rm wordpress.sql

若要配置 PHP,请运行以下命令,打开所选文本编辑器并创建文件 /etc/wordpress/config-localhost.phpTo configure PHP, run the following command to open a text editor of your choice and create the file /etc/wordpress/config-localhost.php:

sudo sensible-editor /etc/wordpress/config-localhost.php

将以下行复制到文件中,并将 yourPassword 替换为 WordPress 数据库密码(其他值不变)。Copy the following lines to the file, substituting your WordPress database password for yourPassword (leave other values unchanged). 然后保存文件。Then save the file.

<?php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', 'yourPassword');
define('DB_HOST', 'localhost');
define('WP_CONTENT_DIR', '/usr/share/wordpress/wp-content');
?>

将 WordPress 安装移至 Web 服务器文档根目录:Move the WordPress installation to the web server document root:

sudo ln -s /usr/share/wordpress /var/www/html/wordpress

sudo mv /etc/wordpress/config-localhost.php /etc/wordpress/config-default.php

现在可以完成 WordPress 设置并在平台上进行发布了。Now you can complete the WordPress setup and publish on the platform. 打开浏览器并转到 http://yourPublicIPAddress/wordpressOpen a browser and go to http://yourPublicIPAddress/wordpress. 替换 VM 的公共 IP 地址。Substitute the public IP address of your VM. 应如下图所示。It should look similar to this image.

WordPress 安装页

后续步骤Next steps

本教程在 Azure 中部署了一台 LEMP 服务器。In this tutorial, you deployed a LEMP server in Azure. 你已了解如何:You learned how to:

  • 创建 Ubuntu VMCreate an Ubuntu VM
  • 为 Web 流量打开端口 80Open port 80 for web traffic
  • 安装 NGINX、MySQL 和 PHPInstall NGINX, MySQL, and PHP
  • 验证安装和配置Verify installation and configuration
  • 在 LEMP 堆栈上安装 WordPressInstall WordPress on the LEMP stack

转到下一教程,了解如何使用 SSL 证书保护 Web 服务器。Advance to the next tutorial to learn how to secure web servers with SSL certificates.