如何编辑WordPress的配置文件wp-config.php

作为一个WordPress小白,你可能在很多WordPress教程中都看到过需要在wp-config.php中修改一些东西,但是wp-config.php到底是个什么鬼?我们将在本文中向你展示如何正确的编辑WordPress的wp-config.php文件。

wp-config.php文件是干什么的?

从文件的名字就可以看出来,它是所有自托管WordPress站点的配置文件,是网站不可或缺的一部分。

跟其他文件不同的是,wp-config.php文件并不是WordPress自带的,而是在网站安装过程中针对你的网站生成的一个文件。

WordPress会把数据库的相关信息存在wp-config.php文件中,没有这个信息你的网站就无法运行,页面会显示“建立数据库连接时出错”的错误提示。

除了数据库的信息,wp-config.php文件还包含了很多其他的高级设置,后面我们会对这些设置进行解释。

由于该文件包含了大量的敏感信息,所以强烈建议你不要随便编辑该文件以防出错,除非你不得不对其进行修改。

不过既然你读到了这篇文章,那说明你可能已经面临不得不编辑 wp-config.php 的情况了。下面就是一步步的教你如何编辑该文件。

准备工作

首先你需要将网站进行完整的备份。wp-config.php文件对WordPress网站来说至关重要,一点小小的错误都可能导致网站无法打开。

你需要用FTP客户端连接到网站。Windows用户可以安装WinSCP或者SmartFTP,Mac用户可以试试Transmit或者CyberDuck,不过我用FileZilla。FTP客户端让你在服务器和电脑之间传输文件。

使用FTP客户端连接网站需要你输入FTP登录信息,你可以从主机服务商那里获取该信息。如果你不知道你的FTP登录信息,去咨询你的主机服务商。

wp-config.php文件通常存放在网站的根目录,跟/wp-content/等文件夹在同一级目录。

选择该文件,在文件上点击鼠标右键,在弹出的菜单中选择“下载”,FTP会将该文件下载到电脑上。你可以使用纯文本编辑器来打开以及编辑该文件,例如 Notepad或者TextEdit。

了解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://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('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 Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define('WP_DEBUG', false);
 
/* That's all, stop editing! Happy blogging. */
 
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
 
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

wp-config.php文件的每个部分都在文件本身中有详细说明。 这里的几乎所有设置都是使用PHP常量定义的。

define( 'constant_name' , 'value'); 

我们再进一步看一下 wp-config.php 各个部分的内容。

wp-config.php 中的MySQL设置

WordPress网站的数据库连接设置在wp-config.php的“MySQL Settings”部分,你需要将MySQL主机、数据库名称、数据库用户名和密码填进去。

// ** 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', '');

你可以在主机服务商的cPanel界面中,找到数据库部分,然后查看数据库信息。如果找不到的话,请联系你的主机服务商进行索要。

认证秘钥和盐

身份认证唯一秘钥和盐是可以帮助提升网站安全性的安全秘钥。这些秘钥会对WordPress生成的用户session和cookies进行强加密。

/**#@+
 * 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安全秘钥,然后粘贴到这里。如果你怀疑自己的网站被盗用了,这些安全秘钥就显得非常有用了。修改安全秘钥后会强制将已登录的全部用户都注销,要求他们重新登录。

WordPress数据表前缀

WordPress默认会将 wp_ 这个前缀添加到所有数据表名称的前面。我们推荐你把默认的前缀修改成其他的,这样的话黑客就很难猜到你的数据库表,也就无法对你实施常见的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  = 'wp_';

需要注意的是,如果直接在wp-config.php文件中修改前缀,对已经安装完毕正在运行的WordPress站点是无效的。你可以按照这个教程来修改正在运行的WordPress站点的数据表前缀

WordPress 调试模式

对于想学习WordPress开发和尝试实验性功能的用户来说,这个设置非常有用。WordPress默认是会将执行代码时PHP输出的log隐藏起来,将调试模式改为“true”就可以将这些log显示出来,为开发人员去查找bug提供关键的信息。

define('WP_DEBUG', false);

绝对路径设置

wp-config文件的最后一部分定义了绝对路径,这些用于设置WordPress变量和包含的文件。 你不需要碰这里的任何设置。

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

有用的wp-config.php技巧和设置

还有一些其他wp-config.php设置可以帮助你解决许多常见的WordPress错误。

修改MySQL的端口和Socket

如果你的主机服务商使用其他的端口号代替了MySQL默认端口号,那么你需要修改DB_HOST的值,在数据库地址后面加上端口号。注意,不是另写一行新代码,是编辑DB_HOST的值。

define( 'DB_HOST', 'localhost:5067' );

别忘了把端口号5067改成你的主机服务商提供的端口号。

如果你的主机使用了sockets和pipes,那么应该这样填写DB_HOST:

define( 'DB_HOST', 'localhost:/var/run/mysqld/mysqld.sock' );

使用wp-config.php文件修改WordPress的URL地址

当你将WordPress站点迁移至一个新的域名下或者新主机中,你可能需要修改WordPress的URL地址。你可以在管理员后台的“设置” – “常规”中进行修改。

你也可以在wp-config.php中修改这两个地址。如果由于太多的跳转错误导致无法进入管理员面板,直接修改wp-config.php会非常好用,只需要将下面两行代码添加至wp-config.php中即可:

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');

别忘了将 example.com 替换成你自己的域名。还有,搜索引擎是将 www.example.com和example.com当做两个不同的地址看待的(了解 域名带www和不带www,哪个更有利于SEO?)。如果你的网站被索引的时候是带www前缀的,那么你需要根据实际情况来添加域名。

使用wp-config.php修改上传目录

WordPress默认将上传的媒体存储在/wp-content/uploads/这个目录。如果你想把媒体文件存放在其他位置,你可以通过在wp-config.php中添加以下代码来实现。

define( 'UPLOADS', 'wp-content/media' );

注意:上传目录的路径是相对于WordPress自动设置的ABSPATH的,在这里直接添加绝对路径是不起作用的。查看关于如何修改WordPress中默认媒体上传位置的详细指南。

禁用自动更新

WordPress在3.7版本汇总引入了自动更新,可以让WordPress站点在有可用的小更新时自动更新。虽然自动更新有利于安全性,但是有些情况下会让整个站点崩溃,导致无法访问。

将下面这一行代码添加到wp-config.php中可以禁用所有的自动更新。

define( 'WP_AUTO_UPDATE_CORE', false );

想了解更多信息,请查看如何禁用WordPress的自动更新

限制文章修定版本数量

WordPress本身自带了自动保存和版本控制的功能,但是,如果你的站点比较大,大量的修定版本内容会增大数据库备份的体积。

将下面的代码加入到wp-config.php中可以限制每篇文章的修定版本数量。

define( 'WP_POST_REVISIONS', 3 );

将 3 替换成你希望的版本数量。WordPress会自动丢弃掉老的版本,但是你的老版本的修定内容仍然存储在数据库中。

希望这篇文章可以让学习到如何编辑WordPress的wp-config.php文件,以及所有可以用它来做的比较酷的事情。

为您推荐

发表回复

您的电子邮箱地址不会被公开。