|
您是否阅读过要求您编辑 wp-config 文件的教程,但您不知道它是什么?好吧,我们已经为您准备好了。在本文中,我们将向您展示如何在 WordPress 中正确编辑 wp-config.php 文件。
什么是 wp-config.php 文件?
顾名思义,它是一个配置文件,是所有自托管 WordPress 站点的一部分。
与其他文件不同,wp-config.php 文件不是 WordPress 内置的,而是在安装过程中专门为您的站点生成的。

WordPress 将您的数据库信息存储在 wp-config.php 文件中。如果没有此信息,您的 WordPress 网站将无法运行,并且您将收到“建立数据库连接错误”错误。
除了数据库信息,wp-config.php 文件还包含其他几个高级设置。我们将在本文后面解释它们。
由于此文件包含大量敏感信息,建议您不要乱动此文件,除非您别无选择。
但是由于您正在阅读本文,这意味着您必须编辑 wp-config.php 文件。以下是在不弄乱事情的情况下执行此操作的步骤。
视频教程
订阅 WPBeginner
如果您不喜欢该视频或需要更多说明,请继续阅读。
入门
您需要做的第一件事是创建一个完整的WordPress 备份。wp-config.php 文件对 WordPress 站点至关重要,一个小错误就会导致您的站点无法访问。
您将需要一个FTP 客户端来连接到您的网站。Windows 用户可以安装 WinSCP 或 SmartFTP,Mac 用户可以尝试 Transmit 或 CyberDuck。FTP 客户端允许您在服务器和您的计算机之间传输文件。
使用 FTP 客户端连接到您的网站。您将需要 FTP 登录信息,您可以从您的网络托管服务商那里获得这些信息。如果您不知道您的 FTP 登录信息,那么您可以向您的虚拟主机寻求支持。
wp-config.php 文件通常位于您网站的根文件夹中,其他文件夹如 /wp-content/。

只需右键单击该文件,然后从菜单中选择下载。您的 FTP 客户端现在会将 wp-config.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&#39;t have to use the web site, you can * copy this file to &#34;wp-config.php&#34; and fill in the values. * * This file contains the following configurations: * * * MySQL settings * * Secret keys * * Database table prefix * * ABSPATH * * @link https://codex.wordpress.org/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(&#39;DB_NAME&#39;, &#39;database_name_here&#39;); /** MySQL database username */define(&#39;DB_USER&#39;, &#39;username_here&#39;); /** MySQL database password */define(&#39;DB_PASSWORD&#39;, &#39;password_here&#39;); /** MySQL hostname */define(&#39;DB_HOST&#39;, &#39;localhost&#39;); /** Database Charset to use in creating database tables. */define(&#39;DB_CHARSET&#39;, &#39;utf8&#39;); /** The Database Collate type. Don&#39;t change this if in doubt. */define(&#39;DB_COLLATE&#39;, &#39;&#39;); /**#@+ * 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/ http://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(&#39;AUTH_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;SECURE_AUTH_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;LOGGED_IN_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;NONCE_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;AUTH_SALT&#39;, &#39;put your unique phrase here&#39;);define(&#39;SECURE_AUTH_SALT&#39;, &#39;put your unique phrase here&#39;);define(&#39;LOGGED_IN_SALT&#39;, &#39;put your unique phrase here&#39;);define(&#39;NONCE_SALT&#39;, &#39;put your unique phrase here&#39;); /**#@-*/ /** * 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 = &#39;wp_&#39;; /** * 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 Codex. * * @link https://codex.wordpress.org/Debugging_in_WordPress */define(&#39;WP_DEBUG&#39;, false); /* That&#39;s all, stop editing! Happy blogging. */ /** Absolute path to the WordPress directory. */if ( !defined(&#39;ABSPATH&#39;) ) define(&#39;ABSPATH&#39;, dirname(__FILE__) . &#39;/&#39;); /** Sets up WordPress vars and included files. */require_once(ABSPATH . &#39;wp-settings.php&#39;); |
wp-config.php 文件的每个部分都在文件本身中有详细记录。这里几乎所有设置都是使用 PHP 常量定义的。
1 | define( &#39;constant_name&#39; , &#39;value&#39;); |
让我们仔细看看 wp-config.php 文件中的每个部分。
wp-config.php 文件中的 MySQL 设置
您的 WordPress 数据库连接设置显示在 wp-config.php 文件的“MySQL 设置”部分下。您将需要您的 MySQL 主机、数据库名称、数据库用户名和密码来填写此部分。
// ** MySQL settings - You can get this info from your web host ** ///** The name of the database for WordPress */define(&#39;DB_NAME&#39;, &#39;database_name_here&#39;); /** MySQL database username */define(&#39;DB_USER&#39;, &#39;username_here&#39;); /** MySQL database password */define(&#39;DB_PASSWORD&#39;, &#39;password_here&#39;); /** MySQL hostname */define(&#39;DB_HOST&#39;, &#39;localhost&#39;); /** Database Charset to use in creating database tables. */define(&#39;DB_CHARSET&#39;, &#39;utf8&#39;); /** The Database Collate type. Don&#39;t change this if in doubt. */define(&#39;DB_COLLATE&#39;, &#39;&#39;); |
您可以从您的虚拟主机帐户的 cPanel 中标有数据库的部分下获取您的数据库信息。

如果您找不到您的 WordPress 数据库或 MySQL 用户名和密码,那么您需要联系您的虚拟主机。
身份验证密钥和盐
身份验证唯一密钥和盐是有助于提高 WordPress 站点安全性的安全密钥。这些密钥为 WordPress 生成的用户会话和 cookie 提供强大的加密。有关更多信息,请参阅我们的WordPress 安全密钥指南。
/**#@+ * 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/ http://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(&#39;AUTH_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;SECURE_AUTH_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;LOGGED_IN_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;NONCE_KEY&#39;, &#39;put your unique phrase here&#39;);define(&#39;AUTH_SALT&#39;, &#39;put your unique phrase here&#39;);define(&#39;SECURE_AUTH_SALT&#39;, &#39;put your unique phrase here&#39;);define(&#39;LOGGED_IN_SALT&#39;, &#39;put your unique phrase here&#39;);define(&#39;NONCE_SALT&#39;, &#39;put your unique phrase here&#39;); /**#@-*/ |
您可以生成 WordPress 安全密钥并将它们粘贴到此处。如果您怀疑您的 WordPress 网站可能已被入侵,这将特别有用。更改安全密钥将注销您 WordPress 网站上所有当前登录的用户,迫使他们再次登录。
WordPress 数据库表前缀
默认情况下,WordPress 将 wp_ 前缀添加到 WordPress 创建的所有表中。建议您将 WordPress 数据库表前缀更改为随机的。这将使黑客很难猜测您的 WordPress 表,并使您免受一些常见的 SQL 注入攻击。
/** * 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 = &#39;wp_&#39;; |
请注意,您不能为现有的 WordPress 站点更改此值。按照我们如何更改 WordPress 数据库前缀一文中的说明更改现有 WordPress 站点上的这些设置。
WordPress 调试模式
此设置对于尝试学习 WordPress 开发的用户和尝试实验性功能的用户特别有用。默认情况下,WordPress 会隐藏 PHP 在执行代码时生成的通知。只需将调试模式设置为 true 即可向您显示这些通知。这为开发人员查找错误提供了重要信息。
1 | define(&#39;WP_DEBUG&#39;, false); |
绝对路径设置
wp-config 文件的最后一部分定义了绝对路径,然后用于设置 WordPress 变量和包含的文件。你根本不需要在这里改变任何东西。
/** Absolute path to the WordPress directory. */if ( !defined(&#39;ABSPATH&#39;) ) define(&#39;ABSPATH&#39;, dirname(__FILE__) . &#39;/&#39;);/** Sets up WordPress vars and included files. */require_once(ABSPATH . &#39;wp-settings.php&#39;); |
有用的 wp-config.php 技巧和设置
还有一些其他 wp-config.php 设置可以帮助您排除错误并解决许多常见的 WordPress 错误。
在 WordPress 中更改 MySQL 端口和套接字
如果您的 WordPress 托管服务提供商为 MySQL 主机使用备用端口,那么您将需要更改 DB_HOST 值以包含端口号。请注意,这不是新行,但您需要编辑现有的 DB_HOST 值。
1 | define( &#39;DB_HOST&#39;, &#39;localhost:5067&#39; ); |
不要忘记将端口号 5067 更改为您的网络主机提供的任何端口号。
如果您的主机为 MySQL 使用套接字和管道,那么您需要像这样添加它:
1 | define( &#39;DB_HOST&#39;, &#39;localhost:/var/run/mysqld/mysqld.sock&#39; ); |
使用 wp-config.php 文件更改 WordPress URL
将 WordPress 站点移动到新域名或新 Web 主机时,您可能需要更改 WordPress URL。您可以通过访问设置 » 常规页面来更改这些 URL 。

您还可以使用 wp-config.php 文件更改这些 URL。如果您由于错误太多直接问题而无法访问 WordPress 管理区域,这将派上用场。只需将这两行添加到您的 wp-config.php 文件中:
define(&#39;WP_HOME&#39;,&#39;http://example.com&#39;);define(&#39;WP_SITEURL&#39;,&#39;http://example.com&#39;); |
不要忘记用您自己的域名替换 http://example.com。您还需要记住,搜索引擎将 http://www.example.com 和 http://example.com 视为两个不同的位置(参见www 与非 www – 哪个更适合 SEO?)。如果您的站点以 www 前缀编入索引,那么您需要相应地添加您的域名。
使用 wp-config.php 更改上传目录
默认情况下,WordPress 将所有媒体上传存储在 /wp-content/uploads/ 目录中。如果您想将媒体文件存储在其他位置,则可以通过在 wp-config.php 文件中添加这行代码来实现。
define( &#39;UPLOADS&#39;, &#39;wp-content/media&#39; ); |
请注意,上传目录路径是相对于 WordPress 中自动设置的 ABSPATH 的。在这里添加绝对路径是行不通的。有关更多信息,请参阅有关如何在 WordPress 中更改默认媒体上传位置的详细指南。
在 WordPress 中禁用自动更新
WordPress 在 WordPress 3.7 中引入了自动更新。它允许 WordPress 网站在有可用的小更新时自动更新。虽然自动更新对于安全性非常重要,但在某些情况下,它们可能会破坏 WordPress 网站,使其无法访问。
将这一行代码添加到您的 wp-config.php 文件将禁用您的 WordPress 网站上的所有自动更新。
define( &#39;WP_AUTO_UPDATE_CORE&#39;, false ); |
有关更多信息,请参阅我们关于如何在 WordPress 中禁用自动更新的教程。
限制 WordPress 中的发布修订
WordPress 带有内置的自动保存和修订功能。请参阅我们的教程,了解如何使用后期修订撤消 WordPress 中的更改。但是,如果您运行大型站点,修订会增加您的 WordPress 数据库备份大小。
将这行代码添加到您的 wp-config.php 文件以限制为帖子存储的修订数量。
define( &#39;WP_POST_REVISIONS&#39;, 3 ); |
将 3 替换为您要存储的修订数量。WordPress 现在将自动丢弃旧版本。但是,您较早的帖子修订仍存储在您的数据库中。请参阅我们的教程,了解如何删除 WordPress 中的旧帖子修订。 |
|