如何在WordPress中编辑wp-config.php文件

Did you read a tutorial that asks you to edit your wp-config file, and you have no idea what it is? Well we’ve got you covered. In this article, we will show you how to properly edit the wp-config.php file in WordPress.

您是否读过要求您编辑wp-config文件的教程,却不知道它是什么? 好吧,我们已经覆盖了您。 在本文中,我们将向您展示如何在WordPress中正确编辑wp-config.php文件。

什么是wp-config.php文件? (What is wp-config.php File?)

As the name suggests, it is a configuration file that is part of all self-hosted WordPress sites.

顾名思义,它是所有自托管WordPress网站一部分的配置文件。

Unlike other files, wp-config.php file does not come built-in with WordPress rather it’s generated specifically for your site during the installation process.

与其他文件不同,wp-config.php文件不是WordPress内置的,而是在安装过程中专门为您的站点生成的。

WordPress creating wp-config.php file during the installation

WordPress stores your database information in the wp-config.php file. Without this information your WordPress website will not work, and you will get the ‘error establishing database connection‘ error.

WordPress将您的数据库信息存储在wp-config.php文件中。 没有这些信息,您的WordPress网站将无法正常工作,并且您将收到“ 建立数据库连接错误”错误。

Apart from database information, wp-config.php file also contains several other high-level settings. We will explain them later in this article.

除了数据库信息外,wp-config.php文件还包含其他几个高级设置。 我们将在本文后面解释它们。

Since this file contains a lot of sensitive information, it is recommended that you don’t mess with this file unless you have absolutely no other choice.

由于此文件包含很多敏感信息,因此建议您不要搞乱该文件,除非您别无选择。

But since you’re reading this article, it means that you have to edit wp-config.php file. Below are the steps to do it without messing things up.

但是由于您正在阅读本文,所以这意味着您必须编辑wp-config.php文件。 以下是在不造成混乱的情况下执行此操作的步骤。

影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

入门 (Getting Started)

First thing you need to do is to create a complete WordPress backup. The wp-config.php file is so crucial to a WordPress site that a tiny mistake will make your site inaccessible.

您需要做的第一件事是创建一个完整的WordPress备份 。 wp-config.php文件对于WordPress网站是如此重要,以至于微小的错误将使您的网站无法访问。

You will need an FTP client to connect to your website. Windows users can install WinSCP or SmartFTP and Mac users can try Transmit or CyberDuck. An FTP client allows you to transfer files between a server and your computer.

您将需要一个FTP客户端来连接到您的网站。 Windows用户可以安装WinSCP或SmartFTP,而Mac用户可以尝试传输或Cyber​​Duck。 FTP客户端使您可以在服务器和计算机之间传输文件。

Connect to your website using the FTP client. You will need FTP login information which you can get from your web host. If you don’t know your FTP login information, then you can ask your web host for support.

使用FTP客户端连接到您的网站。 您将需要从Web主机获取的FTP登录信息。 如果您不知道FTP登录信息,则可以向Web主机寻求支持。

The wp-config.php file is usually located in the root folder of your website with other folders like /wp-content/.

wp-config.php文件通常与其他文件夹(如/ wp-content /)一起位于网站的根文件夹中。

wp-config file is located in the root directory of your WordPress site

Simply right click on the file and then select download from the menu. Your FTP client will now download wp-config.php file to your computer. You can open and edit it using a plain text editor program like Notepad or Text Edit.

只需右键单击文件,然后从菜单中选择下载。 您的FTP客户端现在将把wp-config.php文件下载到您的计算机上。 您可以使用纯文本编辑器程序(如记事本或文本编辑)打开和编辑它。

了解wp-config.php文件 (Understanding wp-config.php file)

Before you start, let’s take a look at the full code of the default wp-config.php file. You can also see a sample of this file here.

在开始之前,让我们看一下默认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');

Each section of wp-config.php file is well documented in the file itself. Almost all settings here are defined using PHP Constants.

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


define( 'constant_name' , 'value'); 

Let’s take a closer look at each section in wp-config.php file.

让我们仔细看看wp-config.php文件中的每个部分。

wp-config.php文件中MySQL设置 (MySQL Settings in wp-config.php File)

Your WordPress database connection settings appear under ‘MySQL Settings’ section of the wp-config.php file. You will need your MySQL host, database name, database username and password to fill in this section.

您的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', '');

You can get your database information from your web hosting account’s cPanel under the section labeled databases.

您可以从网络托管帐户的cPanel下的“数据库”部分获取数据库信息。

MySQL databases in cPanel

If you cannot find your WordPress database or MySQL username and password, then you need to contact your web host.

如果找不到您的WordPress数据库或MySQL用户名和密码,则需要联系您的虚拟主机。

身份验证密钥和盐 (Authentication Keys and Salts)

Authentication unique keys and salts are security keys that help improve security of your WordPress site. These keys provide a strong encryption for user sessions and cookies generated by WordPress. See our guide on WordPress Security Keys for more information.

身份验证唯一密钥和盐是安全密钥,可帮助提高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/ 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');

/**#@-*/

You can generate WordPress security keys and paste them here. This is particularly useful if you suspect your WordPress site may have been compromised. Changing security keys will logout all currently logged in users on your WordPress site forcing them to login again.

您可以生成WordPress安全密钥并将其粘贴到此处。 如果您怀疑自己的WordPress网站可能已遭到入侵,这特别有用。 更改安全密钥将注销WordPress网站上所有当前登录的用户,从而迫使他们再次登录。

WordPress数据库表前缀 (WordPress Database Table Prefix)

By default WordPress adds wp_ prefix to all the tables created by WordPress. It is recommended that you change your WordPress database table prefix to something random. This will make it difficult for hackers to guess your WordPress tables and will save you from some common SQL injection attacks.

默认情况下,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  = 'wp_';

Please note that you cannot change this value for an existing WordPress site. Follow the instructions in our how to change the WordPress database prefix article to change these settings on an existing WordPress site.

请注意,您不能为现有的WordPress网站更改此值。 请按照我们如何更改WordPress数据库前缀一文中的说明在现有WordPress网站上更改这些设置。

WordPress调试模式 (WordPress Debugging Mode)

This setting is particularly useful for users trying to learn WordPress development, and users trying experimental features. By default WordPress hides notices generated by PHP when executing code. Simply setting the debug mode to true will show you these notices. This provides crucial information to developers to find bugs.

该设置对于尝试学习WordPress开发的用户以及尝试实验功能的用户特别有用。 默认情况下,WordPress在执行代码时会隐藏PHP生成的通知。 只需将调试模式设置为true即可向您显示这些通知。 这为开发人员提供了发现错误的关键信息。


define('WP_DEBUG', false);

绝对路径设置 (Absolute Path Settings)

The last part of wp-config file defines the absolute path which is then used to setup WordPress vars and included files. You don’t need to change anything here at all.

wp-config文件的最后一部分定义了绝对路径,该绝对路径随后用于设置WordPress var和包含的文件。 您根本不需要在此处进行任何更改。


/** 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技巧和设置 (Useful wp-config.php Hacks and Settings)

There are some other wp-config.php settings that can help you troubleshoot errors and solve many common WordPress errors.

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

在WordPress中更改MySQL端口和套接字 (Changing MySQL Port and Sockets in WordPress)

If your WordPress hosting provider uses alternate ports for MySQL host, then you will need to change your DB_HOST value to include the port number. Note, that this is not a new line but you need to edit the existing DB_HOST value.

如果您的WordPress托管服务提供商将备用端口用于MySQL主机,那么您将需要更改DB_HOST值以包括端口号。 注意,这不是新行,但是您需要编辑现有的DB_HOST值。


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

Don’t forget to change the port number 5067 to whatever port number is provided by your web host.

不要忘记将端口号5067更改为Web主机提供的任何端口号。

If your host uses sockets and pipes for MySQL, then you will need to add it like this:

如果您的主机使用MySQL的套接字和管道,则需要像这样添加它:


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

使用wp-config.php文件更改WordPress URL (Changing WordPress URLs Using wp-config.php File)

You may need to change WordPress URLs when moving a WordPress site to a new domain name or a new web host. You can change these URLs by visiting Settings » General page.

将WordPress网站移至新域名或新的虚拟主机时,可能需要更改WordPress URL。 您可以通过访问设置»常规页面来更改这些URL。

WordPress Address and Site Address settings

You can also change these URLs using wp-config.php file. This comes handy if you are unable to access the WordPress admin area due to error too many directs issue. Simply add these two lines to your wp-config.php file:

您也可以使用wp-config.php文件更改这些URL。 如果由于错误而导致无法访问WordPress管理区域,这将非常方便。 只需将这两行添加到wp-config.php文件中:


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

Don’t forget to replace example.com with your own domain name. You also need to keep in mind that search engines treat www.example.com and example.com as two different locations (See www vs non-www – Which one is better for SEO?). If your site is indexed with www prefix then you need to add your domain name accordingly.

不要忘记用您自己的域名替换example.com。 您还需要记住,搜索引擎将www.example.com和example.com视为两个不同的位置(请参阅www与非www –哪个更适合SEO? )。 如果您的网站以www前缀编入索引,那么您需要相应地添加域名。

使用wp-config.php更改上传目录 (Change Uploads Directory Using wp-config.php)

By default WordPress stores all your media uploads in /wp-content/uploads/ directory. If you want to store your media files in someother location then you can do so by adding this line of code in your wp-config.php file.

默认情况下,WordPress将所有媒体上传内容存储在/ wp-content / uploads /目录中。 如果要将媒体文件存储在其他位置,则可以通过在wp-config.php文件中添加以下代码行来实现。


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

Note that the uploads directory path is relative to the ABSPATH automatically set in WordPress. Adding an absolute path here will not work. See out detailed guide on how to change default media upload location in WordPress for more information.

请注意,上载目录路径是相对于WordPress中自动设置的ABSPATH而言的。 在此处添加绝对路径将不起作用。 请参阅有关如何在WordPress中更改默认媒体上传位置的详细指南,以获取更多信息。

禁用WordPress中的自动更新 (Disable Automatic Updates in WordPress)

WordPress introduced automatic updates in WordPress 3.7. It allowed WordPress sites to automatically update when there is a minor update available. While automatic updates are great for security, but in some cases they can break a WordPress site making it inaccessible.

WordPress在WordPress 3.7中引入了自动更新。 当有较小的更新可用时,它允许WordPress网站自动更新。 虽然自动更新对于安全性很有用,但是在某些情况下,它们可能会破坏WordPress网站,使其无法访问。

Adding this single line of code to your wp-config.php file will disable all automatic updates on your WordPress site.

将这行代码添加到wp-config.php文件将禁用WordPress网站上的所有自动更新。


define( 'WP_AUTO_UPDATE_CORE', false );

See our tutorial on how to disable automatic updates in WordPress for more information.

有关更多信息,请参见有关如何在WordPress中禁用自动更新的教程。

限制WordPress中的帖子修订 (Limit Post Revisions in WordPress)

WordPress comes with built-in autosave and revisions. See our tutorial on how to undo changes in WordPress with post revisions. However, if you run a large site revisions can increase your WordPress database backup size.

WordPress带有内置的自动保存和修订。 请参阅我们的教程,了解如何使用后期修订撤消WordPress中的更改 。 但是,如果您运行大型网站,则修订可能会增加WordPress数据库的备份大小。

Add this line of code to your wp-config.php file to limit the number of revisions stored for a post.

将这行代码添加到wp-config.php文件中,以限制为帖子存储的修订版本数。


define( 'WP_POST_REVISIONS', 3 );

Replace 3 with the number of revisions you want to store. WordPress will now automatically discard older revisions. However, your older post revisions are still stored in your database. See our tutorial on how to delete old post revisions in WordPress.

将3替换为要存储的修订版本号。 WordPress现在将自动放弃较早的修订。 但是,您较早的修订版仍存储在数据库中。 请参阅我们的教程,了解如何在WordPress中删除旧的帖子修订

We hope this article helped you learn how to edit wp-config.php file in WordPress and all the cool things you can do with it. You may also want to see our article on 25+ extremely useful tricks for WordPress functions file.

我们希望本文能帮助您学习如何在WordPress中编辑wp-config.php文件以及您可以使用它进行的所有出色操作。 您可能还想看一下有关WordPress函数文件的25种极其有用的技巧的文章。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/beginners-guide/how-to-edit-wp-config-php-file-in-wordpress/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值