在安装 WordPress 的过程中,会创建一个名为 wp-config.php 的配置文件。该文件包含 WordPress 网站的许多重要配置内容,比如与数据库建立连接的数据库、用户及密码等重要信息。此外,在 wp-config.php 文件中,还有许多用于配置安全密钥和开发人员选项的设置。
在本文中,我们将深入探讨一些 wp-config.php 文件的高级功能配置。
什么是 wp-config.php 文件?
在 WordPress 的默认安装包中,并没有 wp-config.php 文件,但可以在其中找到一个名为 wp-config-sample.php 的文件。该文件位于 WordPress 根目录中,它包含站点的数据库配置等重要信息。
在安装 WordPress 时,需要输入数据库名称、用户名、密码、数据库主机和表前缀等信息。然后,Wordpress 安装程序将根据提供的信息创建一个 wp-config.php 文件。
如果想要手动创建配置文件,可以使用 wp-config-sample.php 文件的内容。只需将其重命名为 wp-config.php 并编辑其内容,将数据库名称、用户名、密码、数据库主机和表前缀等信息添加到相应位置。
注意:不要更改默认代码的顺序,因为更改后可能会导致网站上出现错误。 wp-config.php 文件的默认配置内容如下所示:
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the installation.
* You don't have to use the web site, you can copy this file to "wp-config.php"
* and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://wordpress.org/support/article/editing-wp-config-php/
*
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/**#@+
* 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' );
/**#@-*/
/**
* WordPress database table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the documentation.
*
* @link https://wordpress.org/support/article/debugging-in-wordpress/
*/
define( 'WP_DEBUG', false );
/* Add any custom values between this line and the "stop editing" line. */
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
wp-config.php 文件的基本配置
在示例配置文件中,您可以看到以下配置内容:
MySQL 配置
MySQL 配置区块包含 WordPress 数据库相关配置,例如数据库名称、用户名、密码和主机名等内容。
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
一般情况下,用户在配置服务器的过程中,会对数据库进行相关配置,只需记录创建的数据库名称、用户名和密码等内容待用即可。如果您安装了宝塔面板或 cPanel 面板,可以在数据库相关内容中找到这些配置信息。此外,该区域中还包含数据库字符集和数据库整理类型设置。
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
配置这些设置时,可以使用适当的字符集 (charset) 定义数据库表。WordPress 分配 UTF8 作为默认字符集,它支持任何语言。数据库排序规则确定数据库如何对数据进行排序。大多数情况下,数据库排序规则值应该留空, MySQL 将根据 charset 指定的数据库字符集自动分配。通常没有理由更改 DB_CHARSET 和 DB_COLLATE 的默认值,但是,如果需要,您可以根据 MySQL 支持的字符集和排序规则更改它们。
密钥
在 wp-config.php 文件的密钥配置区域中,WordPress 存储了一组唯一的身份验证密钥和盐,它们通过向密码中添加随机字符为您的站点提供额外的安全保护,以防止网站遭受攻击。该区块通常包含八个由随机数据串组成的变量。
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' );
用户可以通过访问WordPress 密钥服务来生成唯一的密钥。
数据库表前缀
数据库表前缀是 WordPress 数据库表前面的值。默认情况下,WordPress 在 wp-config 文件中将表前缀设置为“ wp_ ”。
/**
* WordPress database table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
对数据库前缀进行自定义设置,是提高数据库安全性以抵御 SQL 注入攻击的重要方式。此外,使用唯一的表前缀,让我们可以将多个 WordPress 站点的数据存储在一个数据库中。
WordPress 调试模式
在 wp-config.php 文件中,可以为 WordPress 网站开启调试模式。一旦开启了调试模式,当 WordPress 网站发生错误时,会在网站上醒目的位置提示错误内容,这样有助于网站管理人员快速确定和解决问题。
在 wp-config.php 文件中,存在下面的代码行,表示系统关闭了调试模式。
define( 'WP_DEBUG', false );
要打开调试模式,只需要将“ false ”值更改为“ true ”:
define( 'WP_DEBUG', true );
注意:建议在正常运营的站点上禁用调试模式,因为网站访问者可能通过网站的的错误和警告提示,获得有关您站点的重要信息。我们将在本文后面介绍如何安全地使用调试模式。
ABSPATH
ABSPATH 是在 wp-config.php 文件底部定义的 PHP 常量,它说明了 WordPress 目录的绝对路径。
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
此代码段位于以下注释之后:
/* That's all, stop editing! Happy publishing. */
这意味着 WordPress 不建议修改其后的代码行。
高级 wp-config.php 配置
在上面的内容中,我们介绍了 wp-config.php 文件的一些默认设置。除了这些基本配置外,用户还可以根据需要添加高级的配置内容。需要注意的是,建议将自定义配置内容,添加到下面的代码行之上,否则自定义设置可能不起作用。
/* That's all, stop editing! Happy publishing. */
设置 WordPress 网址
如果要更换网站的域名,则需要更改 WordPress 的 URL 设置。可以在 WordPress 仪表盘的设置 > 常规部分更改它。
有时因为特殊情况,无法在仪表盘中编辑这些字段,可以通过在 wp-config.php 文件,中添加以下代码来设置 WordPress 的 URL。
define( 'WP_HOME', 'http://your-new-domain' );
define( 'WP_SITEURL', 'http://your-new-domain' );
WP_SITEURL 的值是您的 WordPress 核心文件所在的地址。WP_HOME 的值是访问者在浏览器中键入的网站的地址。两个常量值都应包含“ http:// ”或“ https:// ” ,并且末尾不应有斜线“ / ”。在 wp-config.php 中设置 WP_SITEURL 和 WP_HOME ,可以减少网站加载时数据库的调用次数。
设置上传目录
默认情况下,WordPress 将上传路径设置为“ wp-content/uploads ”。该目录存储着用户在媒体库上传的所有文件。我们可以通过在 wp-config.php 中添加以下代码,来定义媒体文件的上传路径。
define( 'UPLOADS', 'wp-content/custom-folder' );
要将媒体文件存储在 wp-content 文件夹之外的其他目录中,可以使用以下代码:
define( 'UPLOADS', ''.'media' );
注意:该值不需要前导斜杠,因为此路径是相对于 ABSPATH 的。
设置 wp-content 目录
WordPress 允许用户将存储主题、插件和上传内容的 wp-content 目录,移动到 WordPress 应用程序目录之外。这是保护 WordPress 网站免受恶意软件注入攻击的方法之一。要设置 wp-content 目录,需要重新定义 WP_CONTENT_DIR:
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/new-folder/wp-content' );
并通过更改 WP_CONTENT_URL 值来更改 wp-content URL 的位置:
define( 'WP_CONTENT_URL', 'http://yourdomain/new-folder/wp-content' );
设置主题目录
不能简单地通过 wp-config 更改主题目录,因为它的路径是相对于 wp-content 文件夹的:
$theme_root = WP_CONTENT_DIR . '/themes';
在这种情况下,可以按照官方文档注册其他主题目录来进行更改。
设置插件目录
与更改主题目录一样,我们也可以通过 wp-config.php 文件设置 plugins 插件目录。设置代码如下所示:
define( 'WP_PLUGIN_DIR', dirname(__FILE__) . '/wp-content/new-folder/plugins' );
define( 'WP_PLUGIN_URL', 'http://yourdomain/wp-content/new-folder/plugins' );
第一行代码行设置 WP_PLUGIN_DIR,第二行代码行设置 WP_PLUGIN_URL 值。
自定义用户表和用户元表
在 WordPress 数据库中,使用 wp_user 表来存储用户数据。如果想要自定义用户表,可以在 wp-config.php 中添加以下代码。
define( 'CUSTOM_USER_TABLE', $table_prefix.'my_users' );
如果您创建了自定义用户表,则还需要创建自定义用户元数据表。
define( 'CUSTOM_USER_META_TABLE', $table_prefix.'my_usermeta' );
设置语言和语言目录
在 WordPress 4.0 之后的版本,用户可以在仪表盘 > 设置 > 常规l中设置语言。
也可以在 WordPress 安装过程中通过 wp-config.php 文件设置语言。可以通过以下代码设置语言并定义将存储语言文件的语言目录。
define( 'WPLANG', 'it_IT' );
define( 'WP_LANG_DIR', dirname(__FILE__) . 'wordpress/languages' );
设置内存大小限制
在 WordPress 中,可以使用 WP_MEMORY_LIMIT 选项,设置执行脚本所需的 PHP 内存量。此选项仅对 WordPress 程序起作用。默认情况下,WordPress 为单个站点分配 40M 内存,为多站点分配 64M 内容。如果要将内存限制增加到 256M,只需要在 wp-config.php 中添下面的代码。
define( 'WP_MEMORY_LIMIT', '256M' );
此外,还可以通过定义 WP_MAX_MEMORY_LIMIT ,为 WP_MEMORY_LIMIT 增加或减少 PHP 内存。
define( 'WP_MAX_MEMORY_LIMIT', '256M' );
设置文件权限
文件权限设置决定了谁可以查看、修改和执行网站的文件和文件夹。每个权限级别由一个 3 位代码表示,该代码由以下内容组成:
无法访问 | 0 (-) |
执行 | 1 (x) |
写入 | 2 (w) |
读取 | 4 (r) |
在 wp-config 中,您可以借助两个常量覆盖默认文件权限:FS_CHMOD_DIR 和 FS_CHMOD_FILE。在下面的示例中,我们将目录权限设置为“755”(所有者读取、写入和执行;组读取和执行;其他人读取和执行),文件设置为“644”(所有者读取、写入和执行;组成员只能阅读;为他人只能阅读)。
define( 'FS_CHMOD_DIR', ( 0755 & ~ umask() ) );
define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );
设置 WordPress 自动更新
我们可以为 WordPress 设置自动更新。但是,自动更新有时可能会导致出现异常情况。在这种情况下,可以通过添加以下代码来关闭自动更新。
define( 'AUTOMATIC_UPDATER_DISABLED', true );
此外,从 WordPress 3.7 版开始,可以使用 WP_AUTO_UPDATE_CORE 来管理核心更新。要禁用所有核心更新,可以使用下面的代码。
define( 'WP_AUTO_UPDATE_CORE', false );
还可以为次要版本启用核心更新。
define( 'WP_AUTO_UPDATE_CORE', 'minor' );
内容相关设置(自动保存间隔、修订)
在编辑文章时,WordPress 会在文章表中添加一个条目,以便用户可以将文章或页面恢复到之前的修订版本。虽然这个功能很好,但随着网站内容的增长,这些保存的修订版本可能过多。在这种情况下,可以禁用或减少修订的数量,并自定义自动保存的时间间隔。WordPress 每 60 秒自动保存一次修订,可以使用以下代码进行自定义(例如,改为 180 秒):
define( 'AUTOSAVE_INTERVAL', 180 );
通过以下代码设置修订的数量(设置为 5):
define( 'WP_POST_REVISIONS', 5 );
或使用以下代码完全禁用它们:
define( 'WP_POST_REVISIONS', false );
调试模式高级设置
在本文前面,我们介绍了如何在 wp-config.php 文件中启用调试模式。但是,在这种情况下,我们网站的访问者也可以看到所有错误和通知,这会带来安全风险。为了避免这种情况发生,在将 WP_DEBUG 常量设置为“true”后,可以在下面添加另外几行代码:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
这样,所有错误、通知和警告都将被存储到名为 debug.log 的文件中。该文件将在 wp-content 目录中生成,而网站上不会显示任何错误。
锁定 wp-config.php 文件
综上所述,wp-config.php 文件不仅包含数据库设置,还包含许多 WordPress 网站的其他重要配置,因此有必要确保该文件的安全性。一种方法是在 Web 服务器设置中使用以下指令:
对于 Apache Web 服务器,将下面的代码添加到 .htaccess 文件中:
<files wp-config.php>
order allow,deny
deny from all
</files>
对于 Nginx Web 服务器,将下面的代码添加到配置文件中:
location ~* wp-config.php { deny all; }
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/wordpress/the-advanced-guide-to-the-wp-config-file/