文档首页> 弹性云服务器 ECS> 最佳实践> 手工搭建LNMP环境(Ubuntu 20.04)
更新时间:2024-04-17 GMT+08:00
分享

手工搭建LNMP环境(Ubuntu 20.04)

简介

本文主要介绍了在华为云上如何使用弹性云服务器的Linux实例手工搭建LNMP平台的Web环境。本文档以Ubuntu 20.04 64位操作系统为例。

Linux实例手工部署LNMP环境具体操作步骤如下:
  1. 安装Nginx
  2. 安装MySQL
  3. 安装PHP
  4. 浏览器访问测试

前提条件

  1. 弹性云服务器已绑定弹性公网IP。
  2. 弹性云服务器所在安全组添加了如下表所示的安全组规则,具体步骤参见为安全组添加安全组规则
    表1 安全组规则

    方向

    协议/应用

    端口/范围

    源地址

    入方向

    HTTP(80)

    80

    0.0.0.0/0

  3. 为了更好的获取和更新系统和软件,建议您更新镜像源为华为云镜像源,详细操作,请参见如何使用自动化工具配置华为云镜像源(x86_64和ARM)?

资源规划

本次实践所用的资源配置及软件版本如表2中所示。当您使用不同的硬件规格或软件版本时,本指导中的命令及参数可能会发生改变,需要您根据实际情况进行调整。

表2 资源和成本规划

资源

资源说明

成本说明

弹性云服务器

  • 计费模式:按需计费
  • 可用区:可用区1
  • 规格:s6.large.2
  • 镜像:Ubuntu 20.04 64bit
  • 系统盘:40G
  • 弹性公网IP:现在购买
  • 线路:全动态BGP
  • 公网带宽:按流量计费
  • 带宽大小:5 Mbit/s

ECS涉及以下几项费用:

  • 云服务器
  • 云硬盘
  • 弹性公网IP

具体的计费方式及标准请参考计费模式概述

Nginx

是一个高性能的HTTP和反向代理web服务器。

免费

MySQL

是一款开源的关系数据库软件。

免费

PHP

是一款开源软件,用于Web开发。

免费

操作步骤

  1. 安装Nginx。

    1. 登录弹性云服务器。
    2. 执行以下命令安装Nginx。

      sudo apt-get update

      sudo apt-get install nginx

    3. 调整防火墙(可选)。

      UFW(Uncomplicated Firewall)是一个iptables的接口,可以简化配置防火墙的过程。Ubuntu默认安装了UFW,执行以下命令查看防火墙的状态。

      sudo ufw status

      如果你没有也不想开启防火墙,则可以直接跳过此步骤,如果你想要开启防火墙可以通过以下命令实现。

      sudo ufw enable

      之后再次检查防火墙状态验证是否成功开启防火墙。

      在测试Nginx之前,需要重新配置防火墙软件以允许访问Nginx。执行以下命令,将Nginx自动注册在UFW。

      sudo ufw app list

      回显信息:

      Available applications:
        Nginx Full
        Nginx HTTP
        Nginx HTTPS
        ...
      • Nginx Full:此配置文件打开端口 80(正常,未加密的Web流量)和端口443(TLS / SSL加密流量)
      • Nginx HTTP:此配置文件仅打开端口 80(正常,未加密的Web流量)
      • Nginx HTTPS:此配置文件仅打开端口 443(TLS / SSL加密流量)

      执行以下命令确保防火墙允许HTTP和HTTPS连接。

      sudo ufw allow 'Nginx Full'

    4. 验证Nginx是否正常工作。

      在浏览器中通过域名或者IP地址进行访问Nginx,如果Nginx正常启动则会打开Welcome to nginx的欢迎页面。

      使用浏览器访问 “http://云服务器IP地址”,显示如下页面,说明Nginx安装成功。

      图1 测试访问nginx

  2. 安装MySQL。

    1. 执行以下命令安装MySQL。

      sudo apt -y install mysql-server

    2. 查看MySQL运行状态。

      sudo systemctl status mysql

      ● mysql.service - MySQL Community Server
           Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
           Active: active (running) since Wed 2023-07-26 15:57:29 CST; 22min ago
         Main PID: 10770 (mysqld)
           Status: "Server is operational"
             Tasks: 37 (limit: 4217)
           Memory: 364.9M
           CGroup: /system.slice/mysqld.service
                 └─10770 /usr/sbin/mysqld
      
      Jul 26 15:57:29 ecs-ubuntu systemd[1]: Starting MySQL Community Server...
      Jul 26 15:57:29 ecs-ubuntu systemd[1]: Started MySQL Community Server.
    3. 执行以下命令,进入MySQL。

      sudo mysql

    4. 执行以下命令,设置root用户密码。

      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'xxxxx';

      其中'xxxxx'为待设置的密码。

    5. 执行以下命令,退出MySQL数据库。

      exit;

    6. 执行以下命令,并按照回显提示信息进行操作,加固MySQL。

      mysql_secure_installation

      Securing the MySQL server deployment.
      
      Enter password for user root:    #输入步骤4中设置的root用户密码
      The 'validate_password' component is installed on the server.
      The subsequent steps will run with the existing configuration of the component.
      Using existing password for root.
      
      Estimated strength of the password: 100
      Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y   #是否更改root用户密码,输入Y
      
      New password:  #设置新的root用户密码
      
      Re-enter new password:   #再次输入密码
      
      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
      By default, a MySQL installation has an anonymous user,
      allowing anyone to log into MySQL without having to have
      a user account created for them. This is intended only for
      testing, and to make the installation go a bit smoother.
      You should remove them before moving into a production
      environment.
      
      Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y   #是否删除匿名用户,输入Y
      Success.
      
      Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
      
      Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y   #禁止root远程登录,输入Y
      Success.
      
      By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
      
      Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y   #是否删除test库和对它的访问权限,输入Y
       - Dropping test database...
      Success.
      
       - Removing privileges on test database...
      Success.
      
      Reloading the privilege tables will ensure that all changes
      made so far will take effect immediately.
      
      Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y   #是否重新加载授权表,输入Y
      Success.
      
      All done!

  3. 安装PHP。

    1. 执行以下命令,安装PHP。

      sudo apt update

      sudo apt install php-fpm

    2. 执行以下命令,验证PHP的安装版本。

      php -v

      回显如下类似信息:

      PHP 7.4.3-4ubuntu2.19 (cli) (built: Jun 27 2023 15:49:59) ( NTS )
      Copyright (c) The PHP Group
      Zend Engine v3.4.0, Copyright (c) Zend Technologies
          with Zend OPcache v7.4.3-4ubuntu2.19, Copyright (c), by Zend Technologies
    3. 执行以下命令,查看PHP运行状态。

      systemctl status php7.4-fpm

      回显如下信息:

      ● 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 2023-07-31 17:33:35 CST; 3min 50s ago
             Docs: man:php-fpm7.4(8)

      回显信息中若出现“lines 1-16/16 (end)”,可按q键退出。

    4. 修改Nginx配置文件以支持PHP。
      1. 执行以下命令,打开Nginx默认的配置文件。

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

      2. i键进入编辑模式。
      3. 修改打开的Nginx配置文件。

        在server{}内,找到index开头的配置行,在该行中添加index.php。

        在server{}内找到location ~ \.php$ {},去除以下配置行的注释符号。

      4. Esc键退出编辑模式,并输入:wq保存后退出。
    5. 执行以下命令,重新载入nginx的配置文件。

      sudo systemctl restart nginx

  4. 浏览器访问测试。

    1. 在Nginx网站根目录中,新建phpinfo.php文件。

      sudo vim /var/www/html/phpinfo.php

    2. i键进入编辑模式。
    3. 修改打开的“phpinfo.php”文件,将如下内容写入文件。
      <?php echo phpinfo(); ?>
    4. Esc键退出编辑模式,并输入:wq保存后退出。
    5. 使用浏览器访问“http://服务器IP地址/phpinfo.php”,显示如下页面,说明环境搭建成功。

分享:

    相关文档

    相关产品