在这篇文章中:

    手动搭建 LAMP 环境

    操作场景

    LAMP 环境是指 Linux 系统下,由 Apache + MariaDB + PHP 及其它相关辅助组件组成的网站服务器架构。本文本文档介绍如何在交大云主机上手动搭建 LAMP 环境。

    进行手动搭建 LAMP 环境,您需要熟悉 Linux 命令,并对所安装软件使用的版本特性比较了解。

    示例软件版本

    本文档搭建 LAMP 环境组成及使用版本说明如下:

    • Linux:Linux 操作系统,本文以 CentOS 7.6 为例。
    • Apache:Web 服务器软件,本文以 Apache 2.4.6 为例。
    • MariaDB:数据库管理系统,本文以 MariaDB 10.4.8 为例。
    • PHP:脚本语言,本文以 PHP 7.0.33 为例。

    前提条件

    已创建了 Linux 云主机。如您还未创建云主机,请参考 快速创建 Linux 云主机

    操作步骤

    步骤1:登录 Linux 实例

    您可以根据使用习惯,选择通过:远程登录方式登录 Linux 云主机(推荐)或控制台方式登录云主机。

    步骤2:安装 Apache

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

      1
      yum install httpd -y
    2. 依次执行以下命令,启动 Apache 并设置为开机自启动。

      1
      systemctl start httpd
      1
      systemctl enable httpd
    3. 在本地浏览器中访问以下地址,查看 Apache 服务是否正常运行。

      1
      http://云服务器实例的公网 IP

      显示如下,则说明 Apache 安装成功。

    步骤3:安装配置 MariaDB

    1. 执行以下命令,查看系统中是否已安装 MariaDB。

      1
      rpm -qa | grep -i mariadb
      • 返回结果类似如下内容,则表示已存在 MariaDB。

        为避免安装版本不同造成冲突,请执行下面命令移除已安装的 MariaDB。

        1
        yum -y remove 包名
      • 若返回结果为空,则说明未预先安装,则执行下一步。

    2. 执行以下命令,在/etc/yum.repos.d/下创建MariaDB.repo文件。

      1
      vi /etc/yum.repos.d/MariaDB.repo
    3. 按 “i” 切换至编辑模式,并写入以下内容。

      1
      2
      3
      4
      5
      6
      7
      # MariaDB 10.4 CentOS repository list - created 2019-11-05 11:56 UTC
      # http://downloads.mariadb.org/mariadb/repositories/
      [mariadb]
      name = MariaDB
      baseurl = http://yum.mariadb.org/10.4/centos7-amd64
      gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
      gpgcheck=1

      说明:

      您可前往 MariaDB 官网 获取其他版本操作系统的安装信息。

    4. 按 “Esc”,输入 “:wq”,保存文件并返回。

    5. 执行以下命令,安装 MariaDB。

      1
      yum -y install MariaDB-client MariaDB-server
    6. 依次执行以下命令,启动 MariaDB 服务,并设置为开机自启动。

      1
      systemctl start mariadb
      1
      systemctl enable mariadb
    7. 执行以下命令,验证 MariaDB 是否安装成功。

      1
      mysql

      显示结果如下,则成功安装。

    8. 执行以下命令,退出 MariaDB。

      1
      q

    步骤4:安装配置 PHP

    1. 执行以下命令,安装epel-release

      1
      yum -y install epel-release
    2. 执行以下命令,更新 yum 中 PHP 的软件源。

      1
      rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    3. 执行以下命令,安装 PHP 7.0.33 所需要的包。

      1
      yum -y install php70w php70w-opcache php70w-mbstring php70w-gd php70w-xml php70w-pear php70w-fpm php70w-mysql php70w-pdo
    4. 执行以下命令,修改 Apache 配置文件。

      1
      vi /etc/httpd/conf/httpd.conf
    5. 按 “i” 切换至编辑模式,并依次修改为如下图所示的内容。

      1). 在ServerName www.example.com:80下另起一行,输入以下内容:

      1
      ServerName localhost:80

    2). 将 <Directory> 中的 Require all denied 修改为 Require all granted

    3). 将 <IfModule dir_module> 中内容替换为 DirectoryIndex index.php index.html

    4). 在AddType application/x-gzip .gz .tgz下另起一行,输入以下内容:

    1
    2
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    1. 按 “Esc”,输入 “:wq”,保存文件并返回。

    2. 执行以下命令,重启 Apache 服务。

      1
      systemctl restart httpd

    验证环境配置

    1. 执行以下命令,创建测试文件。

      1
      echo "<?php phpinfo(); ?>" >> /var/www/html/index.php
    2. 在本地浏览中访问以下地址,查看环境配置是否成功。

      1
      http://云服务器实例的公网 IP/index.php

      显示结果如下,则说明 LAMP 环境配置成功。

    后续操作

    在完成了 LAMP 环境搭建后,您可在此基础上搭建网站。