# 基于WordPress搭建个人网站（Linux）
#### 应用场景
WordPress简称WP，最初是一款博客系统，后逐步演化成一款免费的CMS（内容管理系统/建站系统）。本文主要介绍了如何使用CentOS 7.2 64位操作系统的Linux实例手工搭建LNMP平台，并部署WordPress环境。
#### 操作视频
<video controls="controls" preload="none" id="object15371312114216" class="idp-external-video" src="https://res-video.hc-cdn.com/cloudbu-site/china/zh-cn/support/ecs-video/1751030682480343091.mp4" title="基于WordPress搭建个人网站（Linux）" poster="https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002405714077.jpg" height="340.0000" width="600.0000"></video>
#### 方案架构
图1基于WordPress搭建个人网站示意图（Linux）   
![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001342108753.png "点击放大")
#### 方案优势
- 快速构建站点，组网架构简单。
- 网站安全易用。
 
#### 资源和成本规划
表1资源和成本规划 
| 资源        | 资源说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | 成本说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|
| 虚拟私有云VPC  | VPC网段：192.168.0.0/16                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| 虚拟私有云子网  | - 可用区：可用区1  - 子网网段：192.168.0.0/24                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| 安全组       | 入方向规则： - 优先级：1  - 策略：允许  - 类型：IPv4  - 协议端口：TCP: 80  - 源地址：0.0.0.0/0                                                                                                                                                                                                                                                                                                                                                                                                        | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| 弹性云服务器    | - 计费模式：包年/包月  - 可用区：可用区1  - 规格：s6.large.4  - 镜像：CentOS 7.2 64bit  - 系统盘：40G  - 弹性公网IP：现在购买  - 线路：全动态BGP  - 公网带宽：按流量计费  - 带宽大小：5 Mbit/s   | ECS涉及以下几项费用： - 云服务器  - 云硬盘  - 弹性公网IP   具体的计费方式及标准请参考[计费模式概述](https://support.huaweicloud.com/price-ecs/ecs_billing_2001.html)。 |
| Nginx   | 是一个高性能的HTTP和反向代理web服务器。 获取方式： <https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| MySQL    | 是一款开源的关系数据库软件。 获取方式： <https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| PHP      | 是一款开源软件，用于Web开发。 获取方式： <https://mirrors.huaweicloud.com/remi/enterprise/remi-release-7.rpm>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| WordPress | 是一款开源的博客软件。 获取地址： <https://wordpress.org/download/releases/>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | 免费                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| 域名      | 用于访问搭建的个人网站。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | 域名的价格以域名注册商所示为准，请参考域名注册商帮助文档。 如果您通过华为云的域名注册服务购买域名，具体的计费方式及标准请参考[计费说明](https://support.huaweicloud.com/productdesc-domain/domain_pd_130000.html)。                                                                                                                                                                                                                                                                                         |
   
#### 基于WordPress搭建个人网站操作流程
Linux实例基于WordPress手动搭建个人网站的操作步骤如下：
1. [安装Nginx。]
2. [安装MySQL。]
3. [安装PHP。]
4. [创建数据库。]
5. [安装WordPress。]
6. [购买域名。]
7. [进行备案。]
8. [配置域名解析。]
 
#### 实施步骤（自动）
[基于WordPress搭建个人网站](https://www.huaweicloud.com/solution/implementations/build-a-personal-website-based-on-wordpress.html)
#### 实施步骤（手动）
**安装前准备**
- 已购买虚拟私有云和弹性公网IP。
- 如果规划为网站配置域名，需已经购买好相应的域名。
- 弹性云服务器所在安全组添加了如[表2]所示的安全组规则，具体步骤参见[为安全组添加安全组规则](https://support.huaweicloud.com/usermanual-ecs/zh-cn_topic_0030878383.html)。
   表2安全组规则 
  | 方向 | 优先级 | 策略 | 类型  | 协议端口    | 源地址        |
  |:---|:---|:---|:---|:---|:---|
  | 入方向 | 1       | 允许   | IPv4 | TCP: 80 | 0.0.0.0/0 |
     
  
- 为了更好地获取和更新系统和软件，建议您更新镜像源为华为云镜像源，详细操作，请参见[如何使用华为云开源镜像配置yum源(x86_64和鲲鹏)？](https://support.huaweicloud.com/ecs_faq/zh-cn_topic_0118948562.html)。
**操作步骤**
![](https://support.huaweicloud.com/bestpractice-ecs/public_sys-resources/note_3.0-zh-cn.png)
- 不同的操作系统搭建WordPress的命令存在差异，请根据您的ECS实例操作系统选择对应的操作，否则会安装失败。本文档以CentOS 7.2为例部署WordPress 6.6.1。
- WordPress对PHP、MySQL版本存在依赖关系，如果版本不匹配则会导致WordPress安装失败，具体依赖信息，请参见[WordPress Compatibility](https://make.wordpress.org/hosting/handbook/compatibility/#wordpress-php-mysql-mariadb-versions)。
 
1. 登录弹性云服务器。 
   具体操作请参见[Linux ECS登录方式概述](https://support.huaweicloud.com/usermanual-ecs/zh-cn_topic_0013771089.html)。
   
   
2. 安装Nginx。
   
   1. 执行以下命令，下载对应当前系统版本的Nginx包。
      ```
      wget https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
      ```
      
   
   2. 执行以下命令，建立Nginx的yum仓库。
      ```
      rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
      ```
      
   
   3. 执行以下命令，安装Nginx。
      ```
      yum -y install nginx
      ```
      
   
   4. 执行以下命令，查看Nginx版本。
      ```
      nginx -v
      ```
      回显如下类似信息。
      ```
      nginx version: nginx/1.26.1
      ```
      
   
   5. 执行以下命令，启动Nginx并设置开机启动。
      ```
      systemctl start nginx
      systemctl enable nginx
      ```
      
   
   6. 查看启动状态。
      ```
      systemctl status nginx.service
      ```
      回显如下类似信息。
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002295835177.png "点击放大")
      
   
   7. 使用浏览器访问 "http://*服务器IP地址* "，显示如下页面，说明Nginx安装成功。
      图2测试访问Nginx   
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001987965090.png "点击放大") 
   
   
   
   
3. 安装MySQL。
   
   1. 依次执行以下命令，安装MySQL。
      ```
      rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
      yum -y install mysql-community-server
      ```
      
   
   2. 执行以下命令，查看MySQL版本。
      ```
      mysql -V
      ```
      回显如下类似信息。
      ```
      mysql  Ver 8.0.42 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_0000002295771009.png "点击放大")
      
   
   5. 执行以下命令，获取安装MySQL时自动设置的root用户密码。
      ```
      grep 'temporary password' /var/log/mysqld.log
      ```
      回显如下类似信息。
      ```
      2018-08-29T07:27:37.541944Z 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仓库。
      ```
      yum install -y epel-release
      rpm -Uvh https://mirrors.huaweicloud.com/remi/enterprise/remi-release-7.rpm
      ```
      
   
   2. 执行以下命令，安装Yum源管理工具。
      ```
      yum -y install yum-utils
      ```
      
   
   3. 执行以下命令，启用PHP 8.0仓库。
      ```
      yum-config-manager --enable remi-php80
      ```
      
   
   4. 执行以下命令，安装PHP。
      ```
      yum install -y 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
      ```
      
   
   5. 执行以下命令，验证PHP的安装版本。
      ```
      php -v
      ```
      回显如下类似信息：
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002024484829.png "点击放大")
      
   
   6. 执行以下命令，启动PHP服务并设置开机自启动。
      ```
      systemctl start php-fpm
      systemctl enable php-fpm
      ```
      
   
   7. 修改Nginx配置文件以支持PHP。
      1. 执行以下命令打开配置文件/etc/nginx/nginx.conf。
         ```
         vim /etc/nginx/nginx.conf
         ```
         图3nginx.conf   
         ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002024604377.png "点击放大")
         从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段落，修改或添加下列配置信息。
         - 在location的index配置行中添加index.php。
           ```
           location / {
                   root   /usr/share/nginx/html;
                   index  index.html index.htm index.php;  
               }
           ```
           
         
         - 添加以下配置信息。
           ```
           location ~ \.php$ {
                   root           /usr/share/nginx/html;
                   fastcgi_pass   127.0.0.1:9000;
                   fastcgi_index  index.php;
                   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                   include        fastcgi_params;
               }
           ```
           
         
         
         如[图4]所示：
         图4修改后截图   
         ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001987965094.png "点击放大") 
      
      6. 按**Esc** 键退出编辑模式，并输入**:wq**保存后退出。
       
   
   8. 执行以下命令，重新载入Nginx的配置文件。
      ```
      service nginx reload
      ```
      
   
   
   
   
5. 创建数据库。
   
   1. 执行以下命令，并按照提示信息输入MySQL的root用户密码，登录到MySQL命令行。
      ```
      mysql -u root -p
      ```
      
   
   2. 执行以下命令，创建一个新的数据库。
      ```
      CREATE DATABASE wordpress;
      ```
      其中，"*wordpress*"为数据库名，可以自行设置。
      
   
   3. 执行以下命令，创建一个新用户。
      ```
      CREATE USER 'user'@'localhost' IDENTIFIED BY 'xxxxx';
      ```
      其中，"*user"* 为用户名，**'** *xxxxx* **'**为对应的账户密码，可以自行设置。
      
   
   4. 执行以下命令，赋予用户对 "wordpress" 数据库的全部权限。
      ```
      GRANT ALL PRIVILEGES ON wordpress.* TO 'user'@'localhost';
      ```
      
   
   5. 执行以下命令，使所有配置生效。
      ```
      FLUSH PRIVILEGES;
      ```
      
   
   6. 执行以下命令，退出MySQL命令行。
      ```
      exit
      ```
      
   
   7. （可选）依次执行以下命令，验证数据库和用户是否已成功创建，并退出MySQL命令行。
      ```
      mysql -u user -p
      SHOW DATABASES;
      exit
      ```
      其中，"*user*"为刚刚创建的数据库用户名。
      
   
   
   
   
6. 安装WordPress。
   
   1. 执行以下命令，进入Nginx网站根目录，下载WordPress压缩包。
      ```
      cd /usr/share/nginx/html
      wget https://cn.wordpress.org/wordpress-6.6.1-zh_CN.tar.gz
      ```
      
   
   2. 执行以下命令，解压缩WordPress软件包。
      ```
      tar zxvf wordpress-6.6.1-zh_CN.tar.gz
      ```
      解压后生成一个"wordpress"的文件夹。
      
   
   3. 执行以下命令，进入 WordPress 安装目录，将wp-config-sample.php文件复制到wp-config.php文件中，并将原先的示例配置文件保留作为备份。
      ```
      cd /usr/share/nginx/html/wordpress
      cp wp-config-sample.php wp-config.php
      ```
      
   
   4. 执行以下命令，打开并编辑新创建的配置文件。
      ```
      vim wp-config.php
      ```
      
   
   5. 按**i** 键进入编辑模式。找到文件中MySQL的部分，并将相关配置信息修改为[步骤3]中的内容。
      图5修改MySQL配置   
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001991208544.png "点击放大") 
   
   6. 按**Esc** 键退出编辑模式，并输入**:wq**保存后退出。
   
   7. 浏览器访问"http://服务器IP地址/wordpress"进入安装向导。
      图6安装向导   
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002027888189.png "点击放大") 
   
   8. 设置站点标题、管理员账号、密码和邮箱，并单击"安装 WordPress"。
      表3配置参数 
      | 参数     | 说明                                                                                   |
      |:---|:---|
      | 站点标题    | WordPress网站名称。                                                                       |
      | 用户名      | WordPress管理员名称。                                                                     |
      | 密码     | 可以使用默认的密码或者自定义密码。 请勿重复使用现有密码，并确保将密码保存在安全的位置。 |
      | 您的电子邮箱地址 | 接收通知的电子邮件地址。                                                                           |
         
      
   
   9. 安装完成后，弹出安装成功界面。
      图7安装成功   
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001991368288.png "点击放大") 
   
   10. 单击"登录"，或者浏览器访问"http://服务器IP地址/wordpress/wp-login.php"，输入用户名/电子邮箱地址和密码后单击"登录"，进入WordPress。
       图8登录WordPress   
       ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002027807729.png "点击放大")
       图9进入WordPress界面   
       ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000001991208580.png "点击放大") 
   
   
   
   
7. 购买域名。
   
   为了便于网站的访问和使用，可以给网站设置一个单独的域名，使用域名访问网站。首先，需要在域名注册商处获得授权的域名。
   
   
8. 进行备案。
   
   如果网站未进行备案，且需要使用华为云进行托管，则需要通过华为云备案系统进行备案。具体操作请参见[备案流程](https://support.huaweicloud.com/usermanual-icp/zh-cn_topic_0000002127712329.html)。
   
   
9. 配置域名解析。
   
   配置域名解析后才能使用注册的域名访问网站。具体操作请参见[配置域名解析](https://support.huaweicloud.com/qs-dns/dns_qs_0002.html)。
   例如，配置的域名为"www.example.com"，配置成功后，可在浏览器地址栏中输入"http://www.example.com"访问部署的网站。
   
   
 
#### 相关文档
- [远程登录Linux ECS](https://support.huaweicloud.com/usermanual-ecs/zh-cn_topic_0013771089.html)
- LNMP环境搭建验证请参考[手工搭建LNMP环境](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_topic_0130142475.html)中"浏览器访问测试"章节。
 
