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

教程:在 Azure 中的 Linux 虚拟机上安装 LAMP Web 服务器Tutorial: Install a LAMP web server on a Linux virtual machine in Azure

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

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

此设置用于快速测试或概念证明。This setup is for quick tests or proof of concept. 有关 LAMP 堆栈的详细信息,包括针对生产环境的建议,请参阅 Ubuntu 文档For more on the LAMP stack, including recommendations for a production environment, see the Ubuntu documentation.

打开 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Cloud Shell 中预安装并配置了常用 Azure 工具供你与帐户一起使用。Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. 只需选择“复制”按钮即可复制代码,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. 可通过多种方式打开 Cloud Shell:There are a few ways to open Cloud Shell:

选择代码块右上角的“试用”。Select 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/bash
选择 Azure 门户右上角菜单上的“Cloud Shell”按钮。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. 门户中的 Cloud Shell

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.30 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.30 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

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

运行以下命令更新 Ubuntu 包源并安装 Apache、MySQL 和 PHP。Run the following command to update Ubuntu package sources and install Apache, MySQL, and PHP. 请注意命令末尾的脱字号 (^),它是 lamp-server^ 包名称的一部分。Note the caret (^) at the end of the command, which is part of the lamp-server^ package name.

sudo apt update && sudo apt install lamp-server^

系统会提示安装包和其他依赖项。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

ApacheApache

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

apache2 -v

安装 Apache 并向 VM 打开端口 80 之后,即可通过 Internet 访问 Web 服务器。With Apache installed, and port 80 open to your VM, the web server can now be accessed from the internet. 若要查看 Apache2 Ubuntu 默认页,请打开 Web 浏览器并输入 VM 的公共 IP 地址。To view the Apache2 Ubuntu Default 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:

Apache 默认页

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

如果想要进一步测试,请创建一个可在浏览器中查看的快速 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 中部署了一台 LAMP 服务器。In this tutorial, you deployed a LAMP server in Azure. 你已了解如何:You learned how to:

  • 创建 Ubuntu VMCreate an Ubuntu VM
  • 为 Web 流量打开端口 80Open port 80 for web traffic
  • 安装 Apache、MySQL 和 PHPInstall Apache, MySQL, and PHP
  • 验证安装和配置Verify installation and configuration
  • 在 LAMP 服务器上安装 WordPressInstall WordPress on the LAMP server

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