前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >配置WordPress去使用远程数据库

配置WordPress去使用远程数据库

作者头像
大大刺猬
发布2018-08-30 16:38:12
5.4K0
发布2018-08-30 16:38:12

你的准备工作

  • 本指南使用同一数据中心内的两个Linode通过专用IP地址进行通信。您需要在其中一个上配置LEMP(LNMP)LAMP堆栈。 tips:(lemp=linux+nginx+mariadb/mysql+php)
  • 确保所有包都是最新的。
  • 按照入门保护您的服务器指南来创建非root sudo用户。
  • 虽然配置现有数据库的步骤可能类似,但本指南是为新数据库和WordPress安装编写的。请访问我们的指南,了解如何备份现有数据库

本指南中使用的变量

  • 数据库服务器:安装数据库的Linode。
  • Web服务器下载WordPress的 Linode 。
  • example.com:您的完全限定域名(FQDN)或IP地址。
  • wordpress: 数据库名称。
  • wpuser:WordPress客户端数据库用户。
  • password:SQL数据库密码。
  • 192.0.2.100:数据库服务器的私有IP。
  • 192.0.2.255:Web服务器的私有IP。
  • example_user:本地非root sudo用户。
  • 203.0.113.15:Web服务器的FQDN或IP。

在自己的Linode 上安装MariaDB

在数据库服务器上运行这些步骤。

  1. 安装MariaDB: sudo apt install mariadb-server #红帽centos用 sudo yum install mariadb-server
  2. 运行mysql_secure_installation脚本以设置root密码并删除不必要的服务。设置root密码并响应y所有提示: #root密码一定得记住,忘了挺麻烦的,本文密码是:password sudo mysql_secure_installation

接受远程连接

  1. 更改bind-address为数据库服务器的专用IP以将MariaDB配置为接受远程连接: 编辑配置文件/etc/mysql/mariadb.conf.d/50-server.cnf,内容如下: bind-address = 192.0.2.100
  2. 重新启动MariaDB并允许连接3306通过防火墙进行端口连接。此示例使用UFW通过IPv4和IPv6自动打开端口: sudo systemctl restart mysql sudo ufw allow mysql
  3. 以root身份登录MariaDB,创建数据库和远程用户,并授予远程用户对数据库的访问权限。替换192.0.2.255为您的Web服务器的私有IP: sudo mysql -u root -p # 这是输入密码的地方,不显示的, #下面的操作是创建数据库,授权,退出 CREATE DATABASE wordpress; CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost'; CREATE USER 'wpuser'@'192.0.2.255' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'192.0.2.255'; FLUSH PRIVILEGES; exit
  4. 测试新用户的本地登录: mysql -u wpuser -p # 这是输入密码的地方,不显示的status; #这显示一堆信息就表示OK了,不是error的信息 exit

从Web服务器连接到远程数据库

在Web服务器上运行这些步骤。

  1. Web服务器应该已经安装了MariaDB。如果没有,请安装它。WordPress需要PHP-MySQL: sudo apt update && sudo apt install mariadb-client php-mysql
  2. 使用新的远程用户测试远程登录。替换192.0.2.100为数据库Linode的私有IP: mysql -u wpuser -h 192.0.2.100 -p # 这是输入密码的地方,不显示的 status; #这显示一堆信息就表示OK了,不是error的信息 exit Web服务器现在可以连接到远端数据库了。

配置WordPress以使用远程数据库

首次通过Web界面和本地数据库安装和配置时,WordPress会创建一个名为的文件wp-config.php。配置初始远程数据库设置。 提示:如果你还没有WordPress,可以到官网去下载,然后解压到/var/www/html目录下

  1. 切换到解压WordPress的目录,复制示例配置并将其设置为使用远程数据库: cd /var/www/html/example.com/public_html sudo cp wp-config-sample.php wp-config.php
  2. 更改登录变量以匹配数据库和用户。把192.0.2.100替换为数据库服务器的私有IP: 编辑这个文件:/var/www/html/example.com/public_html/wp-config.php /** wordpress 数据库名字 */ define('DB_NAME', 'wordpress'); /** 要使用数据库的用户名 */ define('DB_USER', 'wpuser'); /**上面用户的数据库密码 */ define('DB_PASSWORD', 'password'); /** mysql数据库服务器的IP地址 */ define('DB_HOST', '192.0.2.100');

添加安全密钥以保护wp-admin登录

使用WordPress安全密钥生成器创建随机复杂的哈希值,WordPress将使用它来加密登录数据。复制结果并替换匹配的部分wp-config.php

编辑配置文件/var/www/html/example.com/public_html/wp-config.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

/**#@+ * Authentication Unique Keys and Salts. 唯一可信密钥(和盐) :??? * * Change these to different unique phrases! * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service} 你可以使用括号里的生成这些内容 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.这上面一堆的注释都是废话, * * @since 2.6.0 */ define('AUTH_KEY', 'put your unique phrase here'); define('SECURE_AUTH_KEY', 'put your unique phrase here'); define('LOGGED_IN_KEY', 'put your unique phrase here'); define('NONCE_KEY', 'put your unique phrase here'); define('AUTH_SALT', 'put your unique phrase here'); define('SECURE_AUTH_SALT', 'put your unique phrase here'); define('LOGGED_IN_SALT', 'put your unique phrase here'); define('NONCE_SALT', 'put your unique phrase here'); /**#@-*/

使用SSL 保护WordPress数据库流量

  1. 在Web服务器上你需要做: 创建一个目录以接收在此部分中创建的证书: mkdir ~/certs
  2. 在数据库服务器上: 创建并切换到用于生成密钥和证书的目录: mkdir ~/certs && cd ~/certs
  3. 生成CA密钥并创建证书和私钥。根据需要回应提示。这个例子中的关键在100年后到期。更改-days 36500此步骤和以下步骤中的值,根据需要设置证书期限: sudo openssl genrsa 4096 > ca-key.pem sudo openssl req -new -x509 -nodes -days 36500 -key ca-key.pem -out cacert.pem You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:PA Locality Name (eg, city) []:Phila Organization Name (eg, company) [Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) []: Common Name (e.g. server FQDN or YOUR name) []:MariaDB Email Address []:
  4. 创建服务器证书并编写RSA密钥。Common Name应该是你的Web服务器的FQDN或IP地址: sudo openssl req -newkey rsa:4096 -days 36500 -nodes -keyout server-key.pem -out server-req.pem Generating a 4096 bit RSA private key ......................+++ .............................+++ writing new private key to 'server-key.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:US #这些跟刚才输的一样 State or Province Name (full name) [Some-State]:PA Locality Name (eg, city) []:Phila Organization Name (eg, company) [Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) []: Common Name (e.g. server FQDN or YOUR name) []:203.0.113.15 #你服务器的IP Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: sudo openssl rsa -in server-key.pem -out server-key.pem
  5. 签署证书: sudo openssl x509 -req -in server-req.pem -days 36500 -CA cacert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
  6. 将密钥和证书移动到相应位置: sudo mkdir /etc/mysql/ssl sudo mv *.* /etc/mysql/ssl && cd /etc/mysql/ssl
  7. 生成客户端密钥。根据需要响应提示并将其设置Common Name为Web服务器的FQDN或IP地址: sudo openssl req -newkey rsa:2048 -days 36500 -nodes -keyout client-key.pem -out client-req.pem Generating a 4096 bit RSA private key ....................+++ ............................................................................................+++ writing new private key to 'client-key.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:PA Locality Name (eg, city) []:Phila Organization Name (eg, company) [Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) []: Common Name (e.g. server FQDN or YOUR name) []:203.0.113.15 Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
  8. 写入RSA密钥: sudo openssl rsa -in client-key.pem -out client-key.pem
  9. 签署(同意)客户证书: sudo openssl x509 -req -in client-req.pem -days 36500 -CA cacert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
  10. 验证证书: openssl verify -CAfile cacert.pem server-cert.pem client-cert.pem
  11. 配置MariaDB服务器以使用证书。找到以下行并删除#以取消注释证书位置。修改匹配的路径: 编辑文件/etc/mysql/mariadb.conf.d/50-server.cnf 修改内容如下: ssl-ca=/etc/mysql/ssl/cacert.pem ssl-cert=/etc/mysql/ssl/server-cert.pem ssl-key=/etc/mysql/ssl/server-key.pem
  12. 登录到MariaDB并要求对所有登录数据库的SSL。替换192.0.2.255为Web服务器Linode的私有IP: sudo mysql -u root -p #这是输入密码的地方, #每次输入要再输如密码很麻烦,可以: sudo mysql -u root -ppassword GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'192.0.2.255' REQUIRE SSL; FLUSH PRIVILEGES; exit
  13. 重启MariaDB: sudo systemctl restart mysql
  14. 将证书和密钥复制到Web服务器。把example_user替换为Web服务器的用户,把192.0.2.255替换为Web服务器的私有IP: scp cacert.pem client-cert.pem client-key.pem example_user@192.0.2.255:~/certs

在Web服务器上的操作:

  1. 创建目录并将证书和密钥移动到/etc/mysql/sslsudo mkdir /etc/mysql/ssl && sudo mv ~/certs/*.* /etc/mysql/ssl
  2. 配置Web服务器的MariaDB客户端以使用SSL。找到该[mysql]部分并添加证书和密钥的位置: 编辑/etc/mysql/mariadb.conf.d/50-mysql-clients.cnf,内容如下: [mysql] ssl-ca=/etc/mysql/ssl/cacert.pem ssl-cert=/etc/mysql/ssl/client-cert.pem ssl-key=/etc/mysql/ssl/client-key.pem 注意:如果Web服务器使用MySQL,你可以在/etc/mysql/mysql.conf.d/mysqld.cnf中找到
  3. 登录远程数据库,测试SSL登录: mysql -u wpuser -h 192.0.2.100 -p
  4. 检查状态: status;
  5. 退出MariaDB: exit
  6. 在远程连接数据库之前添加一个指令wp-config,强制让WordPress使用SSL进行数据库连接: 编辑配置文件:/var/www/html/example.com/public_html/wp-config.php #前后其它内容省略 ... define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL ); /** 数据库名字 */ define('DB_NAME', 'wordpress'); /** 数据库用户名 */ define('DB_USER', 'wpuser'); /** 该用户名的密码 */ define('DB_PASSWORD', 'password'); /** mysql数据库的IP地址 */ define('DB_HOST', '192.0.2.100'); ...

完成WordPress安装

使用浏览器导航到example.com/wp-admin。如果数据库连接成功,您将看到安装屏幕: 注:被遮住的部分为你的域名,或IP地址(web服务器的),

WordPress安装屏幕
WordPress安装屏幕

下一步

既然数据库已配置为通过安全连接进行通信,请考虑将SSL / TLS用于Web服务器本身。我们的NGINX上的TLS指南详细介绍了保护NGINX和Web服务器的一些最佳实践。有关其他服务器和Linux发行版的信息,请访问Linode Docs 的SSL证书部分。

更多信息

有关此主题的其他信息,您可能需要参考以下资源。虽然提供这些是希望它们有用,但请注意,我们无法保证外部托管材料的准确性或及时性。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 你的准备工作
    • 本指南中使用的变量
    • 在自己的Linode 上安装MariaDB
      • 接受远程连接
      • 从Web服务器连接到远程数据库
      • 配置WordPress以使用远程数据库
        • 添加安全密钥以保护wp-admin登录
        • 使用SSL 保护WordPress数据库流量
        • 完成WordPress安装
        • 使用浏览器导航到example.com/wp-admin。如果数据库连接成功,您将看到安装屏幕: 注:被遮住的部分为你的域名,或IP地址(web服务器的),
        • 下一步
        • 更多信息
        相关产品与服务
        数据库
        云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档