文档首页/ 弹性云服务器 ECS/ 最佳实践/ 搭建环境/ 搭建LNMP环境/ 手工搭建LNMP环境(CentOS 8.0)
更新时间:2024-09-12 GMT+08:00

手工搭建LNMP环境(CentOS 8.0)

简介

LNMP分别代表Linux、Nginx、MySQL和PHP,是目前主流的网站服务器架构之一,适合运行大型和高并发的网站应用,例如电子商务网站、社交网络、内容管理系统等。本文主要介绍了在华为云上如何使用CentOS 8.0 64位操作系统的Linux实例搭建LNMP环境。

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.4
  • 镜像:CentOS 8.0 64bit
  • 系统盘:40G
  • 弹性公网IP:现在购买
  • 线路:全动态BGP
  • 公网带宽:按流量计费
  • 带宽大小:5 Mbit/s

ECS涉及以下几项费用:

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

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

Nginx

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

示例版本:Nginx 1.20.1

免费

MySQL

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

示例版本:MySQL 8.0.26

免费

PHP

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

示例版本:PHP 7.4.19

免费

操作步骤

  1. 安装Nginx。

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

      sudo dnf -y install https://nginx.org/packages/centos/8/x86_64/RPMS/nginx-1.20.1-1.el8.ngx.x86_64.rpm

    3. 执行以下命令,查看Nginx版本。

      nginx -v

      回显如下类似信息。

      nginx version: nginx/1.20.1
    4. 执行以下命令,启动Nginx并设置开机启动。

      systemctl start nginx

      systemctl enable nginx

    5. 查看启动状态。

      systemctl status nginx.service

      回显如下类似信息。

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

  2. 安装MySQL。

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

      sudo dnf -y install @mysql

    2. 执行以下命令,查看MySQL版本。

      mysql -V

      回显如下类似信息。

      mysql  Ver 8.0.26 for Linux on x86_64 (Source distribution)
    3. 依次执行以下命令,启动MySQL服务并设置开机自启动。

      systemctl start mysqld

      systemctl enable mysqld

    4. 查看MySQL运行状态。

      systemctl status mysqld.service

      回显如下类似信息。

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

      mysql_secure_installation

      Securing the MySQL server deployment.
      
      Connecting to MySQL using a blank password.
      
      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, ant other key for No: Y    #设置密码验证策略,输入Y
      
      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: 2       #选择密码验证策略,输入2
      Please set the password for root here.
      
      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. 执行以下命令,添加并更新epel源。

      sudo dnf -y install epel-release

      sudo dnf -y update epel-release

    2. 执行以下命令,删除缓存的无用软件包并更新软件源。

      sudo dnf clean all

      sudo dnf makecache

    3. 执行以下命令,启动PHP 7.4模块。

      dnf module enable php:7.4

    4. 执行以下命令,安装PHP对应模块。

      sudo dnf -y install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium

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

      php -v

      回显如下类似信息:

    6. 执行以下命令,启动PHP服务并设置开机自启动。

      systemctl start php-fpm

      systemctl enable php-fpm

    7. 修改Nginx配置文件以支持PHP。
      1. 执行以下命令打开配置文件/etc/nginx/nginx.conf。

        vim /etc/nginx/nginx.conf

        图2 nginx.conf

        从nginx.conf可以看出,配置文件定向至/etc/nginx/conf.d/*.conf。

      2. 输入:quit退出nginx.conf。
      3. 执行以下命令打开配置文件/etc/nginx/conf.d/default.conf。

        vim /etc/nginx/conf.d/default.conf

      4. i键进入编辑模式。
      5. 修改打开的“default.conf”文件。
        找到server段落,修改或添加下列配置信息。
            server {
                listen       80;
                server_name  localhost;
        
                #access_log /var/log/nginx/host.access.log  main;
        
            location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm index.php;    
            }
        
            location ~ \.php$ {
                root           /usr/share/nginx/html;
                fastcgi_pass   unix:/run/php-fpm/www.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
          }

        图3所示:

        图3 修改后截图
      6. Esc键退出编辑模式,并输入:wq保存后退出。
    8. 执行以下命令,重新载入Nginx的配置文件。

      service nginx reload

  4. 浏览器访问测试。

    1. 在/usr/share/nginx/html/目录下创建“info.php”的测试页面。
      1. 执行以下命令创建并打开“info.php”的测试文件。

        vim /usr/share/nginx/html/info.php

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