# 手工搭建Drupal网站（Linux）
#### 简介
本文介绍了如何在华为云上使用弹性云服务器的Linux实例手工搭建Drupal电子商务网站。
Drupal是使用PHP语言编写的开源内容管理框架（CMF），它由内容管理系统（CMS）和PHP开发框架（Framework）共同构成。Drupal用于构造提供多种功能和服务的动态网站，能支持从个人博客到大型社区等各种不同应用的网站项目。
更多关于Drupal的信息请参考<https://new.drupal.org/home>。
#### 前提条件
- 已购买一台弹性云服务器，且已为其绑定弹性公网IP。
- 弹性云服务器所在安全组添加了如下表所示的安全组规则，具体步骤参见[为安全组添加安全组规则](https://support.huaweicloud.com/usermanual-vpc/zh-cn_topic_0030969470.html)。
  表1安全组规则 
  | 方向 | 优先级 | 策略 | 类型  | 协议端口    | 源地址        |
  |:---|:---|:---|:---|:---|:---|
  | 入方向 | 1       | 允许   | IPv4 | TCP: 80 | 0.0.0.0/0 |
     
  
- 为了更好地获取和更新系统和软件，建议您更新镜像源为华为云镜像源，详细操作，请参见[如何使用华为云开源镜像配置yum源(x86_64和鲲鹏)？](https://support.huaweicloud.com/ecs_faq/zh-cn_topic_0118948562.html)。
 
#### 资源规划
本次实践所用的资源配置及软件版本如[表2]中所示。当您使用不同的硬件规格或软件版本时，本指导中的命令及参数可能会发生改变，需要您根据实际情况进行调整。
 表2资源和成本规划 
| 资源    | 资源说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 成本说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|
| 弹性云服务器   | - 计费模式：按需计费  - 可用区：可用区1  - 规格：c7.large.2  - 镜像：CentOS 7.8 64bit  - 系统盘：40G  - 弹性公网IP：现在购买  - 线路：全动态BGP  - 公网带宽：按流量计费  - 带宽大小：5 Mbit/s   | ECS涉及以下几项费用： - 云服务器  - 云硬盘  - 弹性公网IP   具体的计费方式及标准请参考[计费模式概述](https://support.huaweicloud.com/price-ecs/ecs_billing_2001.html)。 |
| Apache | 是一个开放源码的Web服务器。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| MySQL      | 是一款开源的关系数据库软件。 获取方式： <https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| PHP     | 是一款开源软件，用于Web开发。 获取方式： <https://mirror.webtatic.com/yum/el7/epel-release.rpm> <https://mirror.webtatic.com/yum/el7/webtatic-release.rpm>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| Drupal   | 使用PHP语言编写的开源内容管理框架。 获取方式： <https://ftp.drupal.org/files/projects/drupal-8.1.1.zip>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
   
#### 操作步骤
1. 登录弹性云服务器。 
   具体操作请参见[Linux ECS登录方式概述](https://support.huaweicloud.com/usermanual-ecs/zh-cn_topic_0013771089.html)。
   
   
2. 安装Apache。 
   1. 使用root用户执行以下命令更新软件包，并安装Apache。
      ```
      yum -y update
      ```
      ```
      yum -y install httpd httpd-manual mod_ssl mod_perl
      ```
      
   
   2. 执行以下命令，验证Apache的安装版本。
      ```
      httpd -v
      ```
      回显如下类似信息：
      ```
      Server version：Apache/2.4.6 (CentOS)
      Server built：  May 30 2023 14:01:11
      ```
      
   
   3. 依次执行以下命令，启动Apache服务并设置开机自启动。
      ```
      systemctl start httpd
      systemctl enable httpd
      ```
      
   
   4. 使用浏览器访问 "http://*服务器IP地址* "，显示如下页面，说明Apache安装成功。
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001743532601.png "点击放大")
      
   
   
   
   
3. 安装MySQL。 
   1. 依次执行以下命令，安装MySQL。
      ![](https://support.huaweicloud.com/bestpractice-ecs/public_sys-resources/note_3.0-zh-cn.png)
      以下命令适用于安装MySQL 8.4。如需安装其他版本，建议您访问[MySQL官方网站](https://dev.mysql.com/downloads/repo/yum/)获取相应的版本信息，并替换以下命令中的RPM软件包链接和名称。
      ```
      wget -i -c https://dev.mysql.com/get/mysql84-community-release-el7-1.noarch.rpm
      yum -y install mysql84-community-release-el7-1.noarch.rpm
      yum -y install mysql-community-server --nogpgcheck
      ```
      
   
   2. 执行以下命令，验证MySQL的安装版本。
      ```
      mysql -V
      ```
      回显如下类似信息：
      ```
      mysql  Ver 8.4.5 for Linux on x86_64 (MySQL Community Server - GPL)
      ```
      
   
   3. 依次执行以下命令，启动MySQL服务并设置开机自启动。
      ```
      systemctl start mysqld
      systemctl enable mysqld
      ```
      
   
   4. 查看MySQL运行状态。
      ```
      systemctl status mysqld.service
      ```
      回显如下类似信息。
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002261774514.png "点击放大")
      
   
   5. 执行以下命令，获取安装MySQL时自动设置的root用户密码。
      ```
      grep 'temporary password' /var/log/mysqld.log
      ```
      回显如下类似信息。
      ```
      2023-10-31T11:53:08.691748Z 1 [Note] A temporary password is generated for root@localhost: 2YY?3uHUA?Ys
      ```
      
   
   6. 执行以下命令，并按照回显提示信息进行操作，加固MySQL。
      ```
      mysql_secure_installation
      ```
      ```
      Securing the MySQL server deployment.
      Enter password for user root:    #输入上一步骤中获取的安装MySQL时自动设置的root用户密码
      The existing password for the user account root has expired. Please set a new password.
      New password:  #设置新的root用户密码
      Re-enter new password:   #再次输入密码
      The 'validate_password' plugin is installed on the server.
      The subsequent steps will run with the existing configuration of the plugin.
      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) : N   #是否更改root用户密码，输入N
       ... skipping.
      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!
      ```
      
   
   
   
   
4. 安装PHP。 
   1. 依次执行以下命令，安装EPEL源、Remi仓库，并启用PHP 7.0仓库。
      ```
      yum -y install epel-release
      yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
      yum -y install yum-utils
      yum-config-manager --enable remi-php70
      ```
      ![](https://support.huaweicloud.com/bestpractice-ecs/public_sys-resources/note_3.0-zh-cn.png)
      本实践默认使用PHP 7.0。如需安装其他版本，请将模块名称修改为对应版本。（例如：安装PHP 8.0，则需要将模块名称修改为remi-php80）。
      
   
   2. 执行以下命令，安装PHP 7.0和一些所需的PHP扩展。
      ```
      yum -y install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-opcache
      ```
      
   
   3. 执行以下命令，验证PHP的安装版本。
      ```
      php -v
      ```
      回显如下类似信息：
      ```
      PHP 7.0.33 (cli) (built: Dec 6 2018 22:30:44) ( NTS )
      Copyright (c) 1997-2017 The PHP Group
      Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
      ```
      
   
   4. 执行以下命令，启动PHP服务并设置开机自启动。
      ```
      systemctl start php-fpm
      systemctl enable php-fpm
      ```
      
   
   
   
   
5. 下载并配置Drupal。 
   1. 依次执行以下命令，进入网站根目录并下载Drupal安装包。
      ```
      cd /var/www/html
      wget https://ftp.drupal.org/files/projects/drupal-8.1.1.zip
      ```
      
   
   2. 依次执行以下命令，解压安装包并重命名。
      ```
      unzip drupal-8.1.1.zip
      mv drupal-8.1.1/ drupal/
      ```
      
   
   3. 执行以下命令，打开 Apache 配置文件。
      ```
      vim /etc/httpd/conf/httpd.conf
      ```
      
   
   4. 按**i**键进入编辑模式。
   
   5. 将下图所示位置的配置参数"AllowOverride None "改为" AllowOverride All "： ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001697438044.jpg "点击放大")
      
   
   6. 按**Esc** 键退出编辑模式，并输入**:wq**保存后退出。
   
   7. 执行以下命令，修改网站根目录用户权限。
      ```
      chown -R apache:apache /var/www/html
      ```
      
   
   8. 执行以下命令，重启Apache服务。
      ```
      systemctl restart httpd
      ```
      
   
   
   
   
6. 配置数据库信息。 
   1. 执行以下命令，并按照提示信息输入MySQL的root用户，登录到MySQL命令行。
      ```
      mysql -u root -p
      ```
      
   
   2. 执行以下命令，创建一个新的数据库。
      ```
      CREATE DATABASE drupal;
      ```
      其中，"*drupal*"为数据库名，可以自行设置。
      
   
   3. 执行以下命令，为数据库创建用户并为用户分配数据库的完全访问权限。
      ```
      GRANT ALL ON drupal.* TO user@localhost IDENTIFIED BY 'xxxxx';
      ```
      其中，"*user* "为数据库用户名，"*xxxxx*"为对应的账户密码，可以自行设置。
      
   
   4. 执行以下命令，退出MySQL命令行。
      ```
      exit
      ```
      
   
   5. （可选）依次执行以下命令，验证数据库和用户是否已成功创建，并退出MySQL命令行。
      ```
      mysql -u user -p
      SHOW DATABASES;
      exit
      ```
      其中，"*user*"为刚刚创建的数据库用户名。
      
   
   
   
   
7. 安装Drupal。 
   1. 浏览器访问"http://服务器IP地址/drupal"进入安装向导。
   
   2. 选择语言为简体中文后单击"Save and continue"。 ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001745155761.png "点击放大")
      
   
   3. 选择标准安装方式，单击"保存并继续"。 ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001745196021.png "点击放大")
      
   
   4. 填写已配置完成的数据库信息，单击"保存并继续"。 ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001697276520.png "点击放大")
      
   
   5. 自动安装完成后进入网站设置界面，填写站点信息，单击"保存并继续"。 ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001745196321.png "点击放大")
      
   
   6. 安装完成后，网站自动进入首页并登录维护账号。 ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001697278484.png "点击放大")
      
   
   
   
   
您已成功搭建 Drupal 个人站点，可根据实际需求对网站进行个性化设置。
