問題描述
是否有最佳實踐將您的 wp-config.php 檔案包含在版本控制儲存庫中?
我正在考慮用這種型別的配置建立一個新的站點 (類似於 Alex King 和 Mark Jaquith):
/index.php
/local-config.php
/wp-config.php
/wp/ (core)
/wp-content/ (plugins, themes, etc.)
如果這個儲存庫被公開,我該怎麼辦?
特別是在馬克的帖子中,看起來 local-config.php 可以儲存本地資料庫的詳細資訊和密碼,但是生產的那些留在 wp-config.php 中。這是太麻煩了,我應該馬上離開 wp-config.php unversioned 嗎?
最佳解決方案
這是我如何做,我沒有遇到任何比這更好的東西。我在版本控制下保留不同版本的 wp-config.php 檔案,然後在其上儲存一個檔案,其中包含所有資料庫憑據和鹽/鍵。同樣這樣,我能夠區分我正在執行的設定型別,並以此為基礎做不同的事情。
這是 wp-config.php 我保持 git(https://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<Rw-)c].9}g?^[:!j]h+Efr<y$<YmV0XOo|lOIujEE}+[R}iAQZ :Sy3wN}');
if ( ! defined( 'SECURE_AUTH_KEY' ) )
define('SECURE_AUTH_KEY', 'APge3~H;g+b0FyNF&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|;<Iw;%rkB(A}r++HwT|s[LW!.wt.=5J!b%Z{F1/[LxQ*d7J>Cm');
if ( ! defined( 'NONCE_KEY' ) )
define('NONCE_KEY', 'zO2cmQX`Kc~_XltJR&T !Uc72=5Cc6`SxQ3;$f]#J)p</wwX&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>y}Kr7]0]Xb@>2sGc>Mk6,$5FycK;u.KU[Tw$345K9qoF}WV,-');
if ( ! defined( 'LOGGED_IN_SALT' ) )
define('LOGGED_IN_SALT', 'a|+yZsR-k<cSf@PQ~v82a_+{+hRCnL&|aF|Z~yU&V0IZ}Mrz@ND])YD22iUM[%Oc');
if ( ! defined( 'NONCE_SALT' ) )
define('NONCE_SALT', '|1.e9Tx{fPv8D#IXO6[<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 root 以上的一個目錄,這也使得它在 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) 下的一切。可能性是無止境的,不同的環境不需要駭客。
這確保你永遠不會向公眾透露任何敏感資訊,我使用這個只是為了開始我工作的任何專案,我有更強的鍵在預設情況下,一旦我把它們新增到上面的第二個配置檔案,那些被使用而不是這裡定義的那些。幸福!
次佳解決方案
How can I do this without exposing my passwords to git, in case this repository ever becomes public?
如果您的 wp-config.php 檔案處於版本控制中,則其包含的任何密碼也將在版本控制中。避免這種情況的唯一方法是將檔案放在版本控制中。
Is this too much trouble and should I just leave wp-config.php unversioned?
我的直覺會是讓 wp-config.php 完全不受版權保護。但有一些方法。
將包含您的密碼和雜湊的 wp-config.php 部分解壓縮到常規 wp-config.php 檔案中的 include()檔案中。然後,將 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 首先分開的原因是因為它是 environment-specific 。您不應該將它從本地伺服器複製到所有的生產… 所以它不應該在版本控制下。
第三種解決方案
Mark’s example 假設您正在使用私人回購:
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' );
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。