1 摘要
本文描述基于京东云云主机,搭建WordPress网站所需的MySQL数据库和Web应用,并在完成Plugin配置后发布一篇博文的全过程。本文将用到如下京东云资源:
- 云主机(m.n2.large,内存优化标准型,机器名为jdcoe-srv010):作为MySQL数据库
- 云主机(c.n2.large,计算优化标准型,机器名为jdcoe-srv011):作为Web服务
- 2 * 40G SSD云硬盘:作为jdcoe-srv010和jdcoe-srv011云主机系统盘
- 1 * 40G SSD云硬盘:作为jdcoe-srv010数据盘,用于存储MySQL数据
- 1 * 弹性公网IP: 提供公网访问,并绑定到云主机jdcoe-srv011上。
2 京东云资源准备
安装WordPress网站所需的云资源可以选择京东云华北-北京、华东-宿迁、华东-上海、华南-广州等区域创建,选择的依据取决与网络延时、每个区域的服务可用情况、云资源架构等因素。
获得网络延时的简单方法是使用ping命令。下面是在位于北京的电脑ping位于京东云华东-上海区域到云主机之间的网络延时情况。
serv001:~ user001$ ping 114.67.93.156 -c 10
PING 114.67.93.156 (114.67.93.156): 56 data bytes
64 bytes from 114.67.93.156: icmp_seq=0 ttl=45 time=74.244 ms
64 bytes from 114.67.93.156: icmp_seq=1 ttl=45 time=112.256 ms
64 bytes from 114.67.93.156: icmp_seq=2 ttl=45 time=58.010 ms
64 bytes from 114.67.93.156: icmp_seq=3 ttl=45 time=183.164 ms
64 bytes from 114.67.93.156: icmp_seq=4 ttl=45 time=152.838 ms
64 bytes from 114.67.93.156: icmp_seq=5 ttl=45 time=87.067 ms
64 bytes from 114.67.93.156: icmp_seq=6 ttl=45 time=136.476 ms
64 bytes from 114.67.93.156: icmp_seq=7 ttl=45 time=181.784 ms
64 bytes from 114.67.93.156: icmp_seq=8 ttl=45 time=138.437 ms
64 bytes from 114.67.93.156: icmp_seq=9 ttl=45 time=93.021 ms
--- 114.67.93.156 ping statistics ---
10 packets transmitted, 10 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 58.010/121.730/183.164/41.564 ms
在上述ping命令输出中,可获得从本机到114.67.93.156的网络延时最小58.010毫秒,最大183.164毫秒,平均值是121.730毫秒。
本文所用到京东云资源均创建在京东云华东-上海区域。
3 安装WordPress网站
本节将介绍安装WordPress网站所需的数据库、Web应用网站,并完成WordPress网站的初始化。
3.1 搭建MySQL数据库
京东云提供了MySQL托管服务,提供自动备份、监控和告警等功能,可以简化MySQL数据库的管理成本。客户也可以基于京东云云主机自己安装MySQL数据库,实现更加灵活的数据管理策略。本文采用在云主机上自己搭建MySQL数据库模式,为了提高数据库性能,选择的云主机规格为内存优化型,挂载SSD云盘。由于该数据库云主机需要获得MySQL安装包,因此需要绑定公网IP。整个MySQL安装过程如下:
- 下载MySQL安装包Yum源配置。
wget http://repo.mysql.com/mysql57-community-release-el7.rpm
- 配置MySQL yum源。
[root@srv013 ~]# rpm -ivh mysql57-community-release-el7.rpm
- 安装MySQL服务器。
[root@srv013 ~]# yum install -y mysql-server
- 挂载云硬盘作为数据盘,修改MySQL缺省数据文件目录。其中/mnt/vdb1是云硬盘的mount点。
MySQL缺省的数据目录位于/var/lib/mysql,为了有更大的数据存储空间,创建MySQL数据目录,并分配权限。
mkdir -p /mnt/vdb1/mysql
chown -R mysql:mysql /mnt/vdb1/mysql
将MySQL数据存储在数盘的特定目录下(假设为/mnt/vdb1/mysql)。修改/etc/my.cnf
datadir=/mnt/vdb1/mysql
- 配置并启动MySQL服务。
[root@srv013 ~]# chkconfig mysqld on
Note: Forwarding request to 'systemctl enable mysqld.service'.
[root@srv013 ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
- 重置MySQL缺省口令。首先查看MySQL安装时生成的缺省口令,然后登录MySQL设置口令。
[root@srv013 ~]# grep "password" /var/log/mysqld.log
2018-11-06T03:01:40.331842Z 1 [Note] A temporary password is generated for root@localhost: ftmgLQl;r0qh
mysql> set password=password('Passw0rd@123');
在完成MySQL数据库实例安装后,需要初始化WordPress数据库。具体过程如下:
[root@jdcoe-srv010 ~]# mysql -uroot -pPassw0rd@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#创建数据库
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
#创建用户
mysql> create user 'wpuser'@'%' identified by 'Passw0rd@123';
Query OK, 0 rows affected (0.00 sec)
#给用户授权
mysql> grant all on *.* to 'wpuser'@'%' identified by 'Passw0rd@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
#验证新用户访问数据库
[root@jdcoe-srv010 ~]# mysql -uwpuser -p -hlocalhost
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use wordpress;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql>
3.2 搭建Nginx服务器
WordPress Web服务器我们选择Nginx,并运行在计算密集型云主机上。同时由于WordPress基于PHP技术栈,因此安装PHP-FPM。由于Nginx服务器需要提供互联网访问,因此该云主机需要绑定公网IP。
3.2.1 安装Nginx
安装Nginx,将自动创建名为nginx的账号和账号组,并创建Nginx服务,该服务以nginx账号身份运行。
[root@srv011 ~]# yum install nginx -y
[root@srv011 ~]# id nginx
uid=996(nginx) gid=994(nginx) groups=994(nginx)
[root@srv011 ~]# chkconfig nginx on
[root@srv011 ~]# service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@srv011 ~]# service nginx status
Redirecting to /bin/systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2018-10-30 15:10:22 CST; 1s ago
Process: 1412 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1409 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1407 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 1414 (nginx)
CGroup: /system.slice/nginx.service
├─1414 nginx: master process /usr/sbin/nginx
├─1415 nginx: worker process
└─1416 nginx: worker process
Oct 30 15:10:22 srv011 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 30 15:10:22 srv011 nginx[1409]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 30 15:10:22 srv011 nginx[1409]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 30 15:10:22 srv011 systemd[1]: Started The nginx HTTP and reverse proxy server.
通过执行curl命令验证Nginx运行是否正常,并访问缺省的页面。
[root@srv011 ~]# curl http://localhost -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 30 Oct 2018 07:15:19 GMT
Content-Type: text/html
Content-Length: 3700
Last-Modified: Tue, 06 Mar 2018 09:26:21 GMT
Connection: keep-alive
ETag: "5a9e5ebd-e74"
Accept-Ranges: bytes
[root@srv011 ~]# curl http://localhost
获得如下HTML页面信息,可发现缺省的配置文件是/etc/nginx/nginx.conf,文档根目录是/usr/share/nginx/html
<div class="content">
<p>This is the default <tt>index.html</tt> page that
is distributed with <strong>nginx</strong> on
Fedora. It is located in
<tt>/usr/share/nginx/html</tt>.</p>
<p>You should now put your content in a location of
your choice and edit the <tt>root</tt> configuration
directive in the <strong>nginx</strong>
configuration file
<tt>/etc/nginx/nginx.conf</tt>.</p>
</div>
3.2.2 配置PHP模块
WordPress是基于PHP的架构,因此需要配置php-fp作为FastCGI后台程序运行。下面是需要安装的模块。
- php-fpm: PHP FastCGI Process Manager
- php: A module for PHP applications that use MySQL databases
- php-cli: Command-line interface for PHP
[root@srv011 html]# yum -y install php-fpm php-mysql php-cli
Loaded plugins: fastestmirror, langpacks
Repository base is listed more than once in the configuration
[root@srv011 html]# chkconfig php-fpm on
Note: Forwarding request to 'systemctl enable php-fpm.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@jdcoe-srv011 ~]# service php-fpm start
Redirecting to /bin/systemctl start php-fpm.service
[root@jdcoe-srv011 ~]# service php-fpm status
Redirecting to /bin/systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2018-11-27 10:45:39 CST; 2s ago
Main PID: 3125 (php-fpm)
Status: "Ready to handle connections"
CGroup: /system.slice/php-fpm.service
├─3125 php-fpm: master process (/etc/php-fpm.conf)
├─3127 php-fpm: pool www
├─3128 php-fpm: pool www
├─3129 php-fpm: pool www
├─3130 php-fpm: pool www
└─3131 php-fpm: pool www
Nov 27 10:45:39 jdcoe-srv011 systemd[1]: Starting The PHP FastCGI Process Manager...
Nov 27 10:45:39 jdcoe-srv011 systemd[1]: Started The PHP FastCGI Process Manager.
编辑PHP-FPM配置文件/etc/php-fpm.d/www.conf, 让PHP FastCGI Process Manager以nginx账户身份去访问文件。原文件内容为:
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache
修改后到文件内容为:
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
在/usr/share/nginx/html/目录下创建phpinfo.php,内容如下:
<?php phpinfo();?>
此时,执行如下命令,发现nginx并没有把php文件发送给后台的php-fpm模块,原因是我们还没有完成nginx.conf的配置,把PHP文件请求发送给PHP FPM。
[root@srv011 html]# curl http://localhost/phpinfo.php
<?php phpinfo();?>
修改/etc/nginx/nginx.conf
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
在重新启动nginx服务后,将看到如下信息(特别是X-Powered-By: PHP/5.4.16),表示nginx和php的集成配置成功。
[root@srv011 nginx]# curl http://localhost/phpinfo.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 30 Oct 2018 07:47:36 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16
3.3 安装WordPress
3.3.1 下载Wordpress安装包
访问https://cn.wordpress.org/download/网站,可获得最新的中文版本。下面下载最新的WordPress中文版,并解压缩到/opt目录下。
[root@srv011 ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
--2018-10-31 10:25:34-- https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252
Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9082696 (8.7M) [application/octet-stream]
Saving to: ‘wordpress-4.9.4-zh_CN.tar.gz’
100%[======================================>] 9,082,696 3.48MB/s in 2.5s
2018-10-31 10:25:38 (3.48 MB/s) - ‘wordpress-4.9.4-zh_CN.tar.gz’ saved [9082696/9082696]
[root@srv011 opt]# tar zxvf /root/wordpress-4.9.8.tar.gz
wordpress/
wordpress/xmlrpc.php
wordpress/wp-blog-header.php
然后修改文件拥有者为nginx。
[root@srv011 opt]# chown -R nginx:nginx wordpress
3.3.2 修改Nginx配置文件
为了能通过Nginx访问WordPress,需要修改/etc/nginx/nginx.conf文件配置,具体内容如下。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /opt/wordpress;
index index.php index.html index.htm;
}
# redirect server error pages to the static page /404.html
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /opt/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
3.3.2 修改WordPress配置文件
修改/opt/wordpress/wp-config.php配置文件。
[root@srv011 wordpress]# cp wp-config-sample.php wp-config.php
修改配置文件wp-config.php,设置数据访问信息。
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'Passw0rd@123');
/** MySQL主机 */
define('DB_HOST', '10.0.3.3');
在完成上述配置后,重新启动nginx和php-fpm服务,在浏览器中访问地址http://{公网IP}/wp-admin/install.php, 将显示如下信息:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0RIj7pxT-1571413846257)(http://114.67.93.156/wp-content/uploads/2018/11/wordpress-install-1024x967.png)]
在上图中输入必要的信息(包括站点标题、用户名、密码等),点击[安装WordPress],将完成安装。此时,访问MySQL数据库,将看到已经生成如下表。
[root@jdcoe-srv010 ~]# mysql -uwpuser -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 176
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use wordpress
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_wordpress |
+-----------------------+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_termmeta |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
12 rows in set (0.01 sec)
3.3.3 配置Web网站图标
对于网站,通常需要配置网站图标,否则会在Nginx的访问错误日志报如下错误:
2018/11/27 11:24:38 [error] 4703#0: *7 open() "/opt/wordpress/favicon.ico" failed (2: No such file or directory), client: 223.104.3.193, server: _, request: "GET /favicon.ico HTTP/1.1", host: "114.67.93.156"
首先安装php-gd.x86_64程序包,如果不安装改程序包,在WordPress中无法对图片文件进行剪辑等操作。
[root@jdcoe-srv011 ~]# yum install php-gd.x86_64
[root@jdcoe-srv011 ~]# yum info php-gd.x86_64
Installed Packages
Name : php-gd
Arch : x86_64
Version : 5.4.16
Release : 45.el7
Size : 343 k
Repo : installed
From repo : base
Summary : A module for PHP applications for using the gd graphics library
URL : http://www.php.net/
License : PHP and BSD
Description : The php-gd package contains a dynamic shared object that will add
: support for using the gd graphics library to PHP.
在完整完成gd图形包后,然后通过【外观】->【自定义】功能配置图标。操作界面如下图。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E3Cx2Cra-1571413846259)(http://114.67.93.156/wp-content/uploads/2018/11/wordpress-favicon-352x1024.jpg)]
3.3.4 配置WordPress主题和Markdown编辑
通过浏览器访问http://{公网IP}/wp-login.php 地址,并用WordPress初始安装过程设置的用户名和口令登录到WordPress,并完成如下配置:
- 在【外观】-> 【主题】页面,启动“Twenty Sixteen“主题,并更新到最新版本。
- 在【插件】界面,选择添加插件,安装“Table of Contents Plus”和“WP Editor.md”插件。如在线安装失败,可先下载插件,然后通过upload插件进行安装。最终安装的plugin如下图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d4p5kvCO-1571413846261)(http://114.67.93.156/wp-content/uploads/2018/11/wordpress-plugins-1-1024x446.png)] - 配置“Table of Contents Plus”插件,使得在文章的顶部显示文章目录。配置界面如下图。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1sEVsbfN-1571413846261)(http://114.67.93.156/wp-content/uploads/2018/11/wordpress-content-785x1024.png)] - 配置“WP Editor.md”插件,设置文章中的代码格式。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-odfyOLyM-1571413846263)(http://114.67.93.156/wp-content/uploads/2018/11/wordpress-wpeditor-1024x961.png)]
4 新建第一篇文章
在工具栏中选择【文章】->【写文章】,可按Markdown格式编辑文章。其中图片通过WordPress媒体库集中管理。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CZcjfu8e-1571413846263)(http://114.67.93.156/wp-content/uploads/2018/11/wordpress-post-1024x623.png)]