Deployment
Optimization
Before deploying your CodeIgniter application to production, there are several things you can do to make your application run more efficiently.
This section describes the optimization features that CodeIgniter provides.
spark optimize
New in version 4.5.0.
The spark optimize
command performs the following optimizations:
Enabling Config Caching
Enabling FileLocator Caching
Composer Optimization
Removing Dev Packages
When you deploy, don’t forget to run the following command:
composer install --no-dev
The above command will remove the Composer packages only for development that are not needed in the production environment. This will greatly reduce the vendor folder size.
Specifying Packages to Discover
If Composer Package Auto-Discovery is enabled, all Composer packages are scanned when needed. But there is no need to scan packages that are not CodeIgniter packages, so specifying the packages to be scanned prevents unnecessary scanning.
Config Caching
Important
Once cached, configuration values are never changed until the cache is deleted, even if the configuration file or .env is changed.
Caching the Config objects can improve performance. However, the cache must be manually deleted when changing Config values.
See Config Caching.
FileLocator Caching
Caching the file paths that FileLocator found can improve performance. However, the cache must be manually deleted when adding/deleting/changing file paths.
See FileLocator Caching.
PHP Preloading
With PHP preloading, you can instruct the server to load essential files like functions and classes into memory during startup. This means these elements are readily available for all requests, skipping the usual loading process and boosting your application’s performance. However, this comes at the cost of increased memory usage and requires restarting the PHP engine for changes to take effect.
Note
If you want to use Preloading, we provide a preload script.
Requirement
Using preloading requires one dedicated PHP handler. Normally, web servers are configured to use one PHP handler, so one app requires a dedicated web server.
If you want to use preloading for multiple apps on one web server, configure your server to use virtual hosts with multiple PHP handlers like multiple PHP-FPMs, with each virtual host using one PHP handler.
Preloading keeps the relevant definitions in memory by reading the files specified in opcache.preload
.
Note
See Running Multiple Applications with one CodeIgniter Installation to make one core CodeIgniter4 to handle your multiple apps.
Configuration
Open php.ini
or xx-opcache.ini
if you have split INI configuration in PHP, and recommend to set opcache.preload=/path/to/preload.php
and opcache.preload_user=myuser
.
Note
myuser
is user running in your web server. If you want to find the location of the split INI configuration, just run php --ini
or open file phpinfo()
and search Additional .ini files parsed.
Make sure you use the appstarter installation. If using manual installation, you must change the directory in include
path.
<?php
// ...
class preload
{
/**
* @var array Paths to preload.
*/
private array $paths = [
[
'include' => __DIR__ . '/system', // <== change this line to where CI is installed
// ...
],
];
// ...
}
// ...