Console commands for .htaccess and config.php
For administrators, hosting operators and anyone who scripts installations. Every installation ships three templates
at its root: .htaccess_root and .htaccess_root_static (the rules for Apache and LiteSpeed, the second with the
static cache offload) and config.php-RECOMMENDED (the documented settings of config.php). Seven console commands
put them into place and read and change config.php, so nobody has to copy them by hand or edit PHP in an editor
on a server:
Warning
.htaccess slows down every request. When .htaccess files are enabled (AllowOverride anything but None), let alone
used, Apache looks for them and reads and evaluates them on every request, in every directory of the path, and
page loading times go up drastically (measured). The only real fix is to switch the feature off for
the virtual host: AllowOverride None in the HTTPS virtual host, and the rules in the virtual host's own
configuration as mod_rewrite rules. That is far faster in production speed tests and well worth the time it takes
to optimize a production configuration. Switch over right away: copy the rules of the
example HTTPS virtual host into your virtual host (with the static cache, also
staticcache_offload.conf), set AllowOverride None, run apachectl configtest, reload Apache and
test the site. The same rules for other servers: nginx, LiteSpeed,
Caddy, IIS, Velocity (built in); where virtual host rules go in a control
panel: Plesk, cPanel, DirectAdmin, ISPConfig.
On LiteSpeed or OpenLiteSpeed, the server's own page cache can also answer anonymous visitors and crawlers without PHP: Page caching with LiteSpeed and OpenLiteSpeed.
| Command | Does |
|---|---|
./console exp:setup:status | Says which template .htaccess is a copy of, and whether config.php is a plain copy, sets settings, or does not compile |
./console exp:setup:htaccess [--static] [--force] --apply | Copies .htaccess_root (with --static: .htaccess_root_static) to .htaccess |
./console exp:setup:config [--force] --apply | Copies config.php-RECOMMENDED to config.php |
./console exp:config:list [--only-set] [--format=json] | Every documented setting with the value config.php has, the proposed value and its description |
./console exp:config:get <NAME> | One value, plain, for a script (exit 1 when it is not set) |
./console exp:config:set <NAME> <value> [--type=<t>] --apply | Sets one value in place, takes the comment off its documented line, or adds it |
./console exp:config:unset <NAME> --apply | Comments the setting out again |
What makes them safe
- Dry run first. Without
--applyeach command shows what it would change;--applywith--dry-runis a usage error (exit 2). - No silent overwrite. An existing
.htaccessorconfig.phpthat differs from the template is shown as a diff and kept unless--force. - A backup before every write, to
var/backup/setup/<time>/orvar/backup/config/<time>/, with the file's mode and owner, in directories of mode0700. - Atomic writes through a temporary file in the same directory, renamed into place.
cp -abehavior for owners and modes. A new file gets the template's mode and, run as root, the template's owner (never root's); an existing file keeps its own.--modeand--owner=user[:group]set them;-vprints each copy likecp -v.config.phpis parsed, not searched. It is read as PHP tokens and never run; only the value of onedefine( 'NAME', value )changes, and comments, order and formatting stay. Adefine()inside a condition is reported and left alone.- Checked before it lands. A new
config.phpmust passphp -land read back with the value asked for. Aconfig.phpthat does not compile is refused, and./consolestill starts to repair it. - Typed values.
bool,int,octal(modes such as0640),float,string,null, from--type, the current value or the documented one. - Secrets masked. Values of names with
PASS,SECRET,KEY,TOKEN,CREDENTIALorPWDare********in every output and diff unless--show-secrets; setting one in aconfig.phpeveryone can read prints a warning. - Only known names, those
config.php-RECOMMENDEDdocuments, unless--allow-unknown, so a typing mistake is caught instead of written.
Example
./console exp:setup:htaccess --apply -v
./console exp:setup:config --apply
./console exp:config:set EZP_INI_FILEMTIME_CHECK false --apply
./console exp:config:set EZP_FILE_MODE_MAX 0640 --apply
./console exp:config:list --only-set'.htaccess_root' -> '.htaccess'
PASS: .htaccess_root copied to .htaccess (mode 0644, owner www-data:www-data)
...
config.php: 2 setting(s) set, 30 documented in config.php-RECOMMENDED
INI FILES OPTIMIZATIONS
EZP_INI_FILEMTIME_CHECK false (line 57)
RECOMMENDED false - This new setting controls whether eZINI should check if the settings have been updated...
FILE PERMISSIONS
EZP_FILE_MODE_MAX 0640 (line 112)
RECOMMENDED 0640 - Set EZP_DIR_MODE_MAX and EZP_FILE_MODE_MAX to the widest modes any directory and any fi...The whole walk-through, with every option, the output of each case, the backup and --force rules, scripts,
containers and troubleshooting: Setting up .htaccess and config.php from the console.
For developers
- Code:
kernel/classes/expsetupfilecopy.php(copy, backup, atomic write,php -l),kernel/classes/expconfigphpeditor.php(the token-baseddefine()editor),kernel/classes/expconfigphpcatalogue.php(the settings documented inconfig.php-RECOMMENDED), the commandskernel/private/classes/commands/setupfiles.phpandconfigphp.php, the entry pointsbin/php/setupfiles.phpandbin/php/configphp.php. - A
config.phpthat does not compile would stopautoload.php.bin/php/console,bin/php/setupfiles.phpandbin/php/configphp.phpdefineEXP_CONFIG_PHP_TOLERATEfirst; for them aloneautoload.phpchecks the file with the PHP parser and leaves it unread when it does not compile (EXP_CONFIG_PHP_SKIPholds the error). Every other entry point readsconfig.phpas before. - A new setting becomes known to the commands by documenting it in
config.php-RECOMMENDEDthe same way as the others: a comment block (a heading underlined with dashes starts a section) followed by//define( 'NAME', value );. - Tests:
tests/tests/kernel/private/classes/commands/expSetupFilesAndConfigPhpCommandsTest.php(no database). - Generated reference: exp:setupfiles, exp:configphp.
Related
- Installation: getting the code, Serving the site
- Change settings from the command line: exp:ini, for the INI files in
settings/ - The static cache generator, which
.htaccess_root_staticserves - File modes