最佳实践wp-config.php版本化?


35

有没有将wp-config.php文件包含在版本控制存储库中的最佳实践?

我正在考虑使用这种类型的配置创建一个新站点(类似于Alex KingMark Jaquith):

/index.php
/local-config.php
/wp-config.php
/wp/ (core)
/wp-content/ (plugins, themes, etc.)

万一该存储库公开,如何在不将密码暴露给git的情况下执行此操作?

尤其是在Mark的帖子中,local-config.php可以存储本地数据库的详细信息和密码,而生产版本则保留在wp-config.php中。这是否太麻烦了,我应该只保留wp-config.php的未版本化吗?


我认为Mark Jaquith的做法并没有多大麻烦,而且运作良好,并且比下面的答案略胜一筹。
Wyck

IMO,所有内容都应置于版本控制之下,并且系统应能够处理不同的环境,而不会产生任何骇人听闻的东西,同时确保事情的安全性和简便性。我刚刚发布了我的操作方法,涵盖了您的所有问题:)如果您对我的设置有任何疑问,请告诉我。
Ashfame

Answers:


45

这是我的操作方式,没有什么比这更好的了。我在版本控制下保留了另一个版本的wp-config.php文件,然后将文件保留在一个目录中,该目录中包含所有数据库凭据和盐/密钥。同样,通过这种方式,我可以区分正在运行的设置类型,并以此为基础进行不同的操作。

这是wp-config.php我保留的githttps://gist.github.com/1923821):

<?php

/**
* Define type of server
*
* Depending on the type other stuff can be configured
* Note: Define them all, don't skip one if other is already defined
*/

define( 'DB_CREDENTIALS_PATH', dirname( ABSPATH ) ); // cache it for multiple use
define( 'WP_LOCAL_SERVER', file_exists( DB_CREDENTIALS_PATH . '/local-config.php' ) );
define( 'WP_DEV_SERVER', file_exists( DB_CREDENTIALS_PATH . '/dev-config.php' ) );
define( 'WP_STAGING_SERVER', file_exists( DB_CREDENTIALS_PATH . '/staging-config.php' ) );

/**
* Load DB credentials
*/

if ( WP_LOCAL_SERVER )
    require DB_CREDENTIALS_PATH . '/local-config.php';
elseif ( WP_DEV_SERVER )
    require DB_CREDENTIALS_PATH . '/dev-config.php';
elseif ( WP_STAGING_SERVER )
    require DB_CREDENTIALS_PATH . '/staging-config.php';
else
    require DB_CREDENTIALS_PATH . '/production-config.php';

/**
* 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.
*/

if ( ! defined( 'AUTH_KEY' ) )
    define('AUTH_KEY', '9*W=5&lt;Rw-)c].9}g?^[:!j]h+Efr&lt;y$&lt;YmV0XOo|lOIujEE}+[R}iAQZ :Sy3wN}');
if ( ! defined( 'SECURE_AUTH_KEY' ) )
    define('SECURE_AUTH_KEY', 'APge3~H;g+b0FyNF&amp;e`$=g?qj9@FQwqFe^Q4(@p#kDa=NR? $Z9|@v*a(tOj*B+.');
if ( ! defined( 'LOGGED_IN_KEY' ) )
    define('LOGGED_IN_KEY', '5l0+:WTpj8#[V|;&lt;Iw;%rkB(A}r++HwT|s[LW!.wt.=5J!b%Z{F1/[LxQ*d7J&gt;Cm');
if ( ! defined( 'NONCE_KEY' ) )
    define('NONCE_KEY', 'zO2cmQX`Kc~_XltJR&amp;T !Uc72=5Cc6`SxQ3;$f]#J)p&lt;/wwX&amp;7RTB2)K1Qn2Y*c0');
if ( ! defined( 'AUTH_SALT' ) )
    define('AUTH_SALT', 'je]#Yh=RN DCrP9/N=IX^,TWqvNsCZJ4f7@3,|@L]at .-,yc^-^+?0ZfcHjD,WV');
if ( ! defined( 'SECURE_AUTH_SALT' ) )
    define('SECURE_AUTH_SALT', '^`6z+F!|+$BmIp&gt;y}Kr7]0]Xb@&gt;2sGc&gt;Mk6,$5FycK;u.KU[Tw$345K9qoF}WV,-');
if ( ! defined( 'LOGGED_IN_SALT' ) )
    define('LOGGED_IN_SALT', 'a|+yZsR-k&lt;cSf@PQ~v82a_+{+hRCnL&amp;|aF|Z~yU&amp;V0IZ}Mrz@ND])YD22iUM[%Oc');
if ( ! defined( 'NONCE_SALT' ) )
    define('NONCE_SALT', '|1.e9Tx{fPv8D#IXO6[&lt;WY*,)+7+URp0~|:]uqiCOzu93b8,h4;iak+eIN7klkrW');

/**
* 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 = 'ft_';

/**
* WordPress Localized Language, defaults to English.
*
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
* language support.
*/

define( 'WPLANG', '' );

/**
* 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.
*/

if ( WP_LOCAL_SERVER || WP_DEV_SERVER ) {

    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true ); // Stored in wp-content/debug.log
    define( 'WP_DEBUG_DISPLAY', true );

    define( 'SCRIPT_DEBUG', true );
    define( 'SAVEQUERIES', true );

} else if ( WP_STAGING_SERVER ) {

    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true ); // Stored in wp-content/debug.log
    define( 'WP_DEBUG_DISPLAY', false );

} else {

    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');

这是本地配置文件,我在WordPress根目录上方保留一个目录,这也将其放置在Web可访问目录之外,因此,如果apache停止解析PHP文件并将其丢弃,我们的数据库凭据仍然是安全的(https:/ /gist.github.com/1923848):

<?php

/**
 * WordPress config file to use one directory above WordPress root, when awesome version of wp-config.php is in use.
 *
 * Awesome wp-config.php file - https://gist.github.com/1923821
 */

/* WordPress Local Environment DB credentials */

define('DB_NAME', 'project_21');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

/* Keys & Salts */

define('AUTH_KEY',         '5H%)s-nQ,+fn0gwg/p1UjBTmCQ?l[8-!>Q{MW&?X3DM,OF;TaI<SOOTrl0+-@) *');
define('SECURE_AUTH_KEY',  '+%rr@,XIt-V+[.B9++uH1L,L+r)uq}5(:~=&4~Lk|.LV|y;R}fEo?G}+Sntf_JN}');
define('LOGGED_IN_KEY',    'Szv!gQm9#(L&TUD OnM`>sXGge:m1j`L2 5sO;hRNVhlN>IUED1/`%<[ly-GxVJ ');
define('NONCE_KEY',        'o-Jo;>G#-%~,[ki@REqXV%4^I.HDnc.3]P;e8];4pJt% $xe5K<aOb|a2*QKV4c-');
define('AUTH_SALT',        '8-tQb3d|W8,;Y_#mfuFB.1&b%U2fnlLD|F&yH).tLRX=ANEdNap{78o|9tqv6JPt');
define('SECURE_AUTH_SALT', 'RSa%^qd~T|@+!-;qgh,qK-GJ}zPpgxz#+@v6-I;BMwqT`TzGTtg_^n*ILxGOdbq4');
define('LOGGED_IN_SALT',   ']+XV)YK.Q-EU1vR [BT!Y$!d(J_[AO37OP[Fg[/esFx;6cI-L[^O|cvtw9F[;_*Q');
define('NONCE_SALT',       'iP{nTQBzy&f^hSbwBgyan.v9<+ErvAMi2ymLhz`Tl-fF?HXa(j<W`wA*8U3R#-|w');

这样,如果上述文件名为local-config.php,则我的系统的行为就像本地安装一样。如果命名为staging-config.php,则其行为类似于分段安装,如果命名为production-config.php。它可以帮助我使某些常量具有不同的值,例如调试在不同的环境下具有不同的值,并且在SCM(git)下仍然具有所有内容。可能性是无止境的,不同环境也不需要黑客。

这样可以确保您无论如何都不会公开任何敏感信息,而我只是用它来启动我正在从事的任何项目,默认情况下,我具有更强的密钥,并且一旦将它们添加到上面的第二个配置文件中,使用这些代替此处定义的那些。极乐!


我喜欢这种方法,我可能最终会做这样的事情。
jjeaton 2012年

如何从主配置文件引用父目录中的本地配置文件?通过符号链接还是通过../(即../filename)某处?- ../在主wp-config.php文件中没有找到任何文件。
KajMagnus

1
@KajMagnus常数可以DB_CREDENTIALS_PATH做到这一点。
Ashfame 2013年

9

万一该存储库公开,如何在不将密码暴露给git的情况下执行此操作?

如果wp-config.php文件处于版本控制中,则它包含的所有密码将处于版本控制中。避免这种情况的唯一方法是将文件置于版本控制中。

这是否太麻烦了,我应该只保留wp-config.php的未版本化吗?

我的直觉是wp-config.php完全保持版本不变。但是有一些解决方法。

wp-config.php将包含您的密码和哈希的部分提取到一个单独的文件中,并将include()其保存在常规wp-config.php文件中。然后,置于wp-config.php版本控制下,但将include()文件分开。

wp-config.php

<?php
/**
 * The base configurations of the WordPress.
 *
 * This file has the following configurations: MySQL settings, Table Prefix,
 * Secret Keys, WordPress Language, and ABSPATH. You can find more information
 * by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
 * wp-config.php} Codex page. You can get the MySQL settings from your web host.
 *
 * This file is used by the wp-config.php creation script during the
 * installation. You don't have to use the web site, you can just copy this file
 * to "wp-config.php" and fill in the values.
 *
 * @package WordPress
 */

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

include( 'conf.php' );    

/**#@-*/

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

/**
 * WordPress Localized Language, defaults to English.
 *
 * Change this to localize WordPress. A corresponding MO file for the chosen
 * language must be installed to wp-content/languages. For example, install
 * de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
 * language support.
 */
define('WPLANG', '');

/**
 * 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.
 */
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

conf.php

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


/**#@+
 * 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');

但老实说,这时您只是在这里添加了冗余级别的抽象。首先,整个原因wp-config.php是分开的,因为它是特定于环境的。您根本不应该将其从本地服务器复制到生产环境中...因此,它根本不应该受版本控制。


看起来确实有些额外的工作,但是我可以看到确保所有wp-config设置在环境之间同步的好处。
jjeaton 2012年

拆分wp-config.php还有一个额外的好处:您可以将其包含conf.php在非WP脚本中,而无需加载整个WordPress。
塔林(Tamlyn)

4

Mark的示例假定您正在使用私有存储库:

if ( file_exists( dirname( __FILE__ ) . '/local-config.php' ) ) {
  include( dirname( __FILE__ ) . '/local-config.php' );
  define( 'WP_LOCAL_DEV', true ); 
} else {
  define( 'DB_NAME',     'production_db'       );
  define( 'DB_USER',     'production_user'     );
  define( 'DB_PASSWORD', 'production_password' );
  define( 'DB_HOST',     'production_db_host'  );
}

除了定义凭据之外,您还可以轻松地创建一个production-config.php文件并将其包含在条件检查中:

if ( file_exists( dirname( __FILE__ ) . '/local-config.php' ) ) {
      include( dirname( __FILE__ ) . '/local-config.php' );
      define( 'WP_LOCAL_DEV', true ); 
    } else {
     include( dirname( __FILE__ ) . '/production-config.php' )
    }

然后在未版本控制的production-config.php中:

  define( 'DB_NAME',     'production_db'       );
  define( 'DB_USER',     'production_user'     );
  define( 'DB_PASSWORD', 'production_password' );
  define( 'DB_HOST',     'production_db_host'  );

即使这是一个私人回购协议,我也不想将密码存储在其中,并且希望在需要时灵活地将回购协议公开。production-config.php可能是一个好方法。
jjeaton 2012年

2

您可以wp-config.php不使用秘密字符串将文件提交到存储库,然后运行:

git update-index --assume-unchanged wp-config.php

这将告诉git假设文件是​​不变的。


4
不错,我不知道assume-unchanged切换,但是我有两点要注意:(1)如果您直接git添加文件,它将被添加到索引中。因此,存在在某个时候意外添加的风险。(2)合并带有此标志的提交将导致合并失败,因此您可以手动处理它,因此这不是一个很好的解决方案。它只能用于在开发会话之类的过程中暂时忽略更改。在这里阅读更多- gitready.com/intermediate/2009/02/18/...
Ashfame
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.