Script to configure PHP variables for development environment.
<?php
$phpDir = '/etc/php';
$backupPath = __DIR__ . DIRECTORY_SEPARATOR . 'config_backup.' . date('Y-m-d_H-i-s') . '.zip';
echo 'Backup config file: ' . $backupPath . PHP_EOL;
system('zip -r ' . $backupPath . ' ' . $phpDir . ' > /dev/null');
chmod($backupPath, 0777);
$values = [
'upload_max_filesize' => '256M',
'post_max_size' => '256M',
'max_file_uploads' => 100,
'memory_limit' => '1024M',
'max_execution_time' => 15,
'error_reporting' => 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
'display_errors' => 'Off',
'log_errors' => 'On',
'xdebug.remote_enable' => '1',
];
setPhpConfigValues($values, $phpDir, ['5.6', '7.0', '7.1', '7.2', '7.3'], ['apache2', 'fpm']);
$values = [
'memory_limit' => -1,
'max_execution_time' => 0,
'error_reporting' => 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
'display_errors' => 'On',
'log_errors' => 'Off',
];
setPhpConfigValues($values, $phpDir, ['5.6', '7.0', '7.1', '7.2', '7.3'], ['cli']);
system('systemctl restart apache2');
foreach(['5.6', '7.0', '7.1', '7.2', '7.3'] as $version) {
system('systemctl restart php' . $version . '-fpm');
}
function setPhpConfigValues($values, $phpDir, $phpVersions, $phpTypes)
{
foreach($phpVersions as $phpVersion) {
foreach($phpTypes as $phpType) {
$filePath = $phpDir . DIRECTORY_SEPARATOR . $phpVersion . DIRECTORY_SEPARATOR . $phpType . DIRECTORY_SEPARATOR . 'php.ini';
if (file_exists($filePath)) {
if (is_readable($filePath)) {
if (is_writable($filePath)) {
$lines = file($filePath);
echo 'FILE: ' . $filePath . PHP_EOL;
foreach($values as $searchKey => $newValue) {
foreach($lines as $line => $text) {
if (substr_count($text, '=') > 0) {
list($lineKey, $lineValue) = explode('=', $text, 2);
if (strpos($lineKey, $searchKey) !== false) {
if (trim($lineValue) != $newValue) {
echo 'Change key "' . $searchKey . '" value from "' . trim($lineValue) . '" to "' . $newValue . '"' . PHP_EOL;
$lines[$line] = $searchKey . ' = ' . $newValue . PHP_EOL;
}
continue 2;
}
}
}
echo 'Add key "' . $searchKey . '" with value"' . $newValue . '"' . PHP_EOL;
$lines[] = $searchKey . ' = ' . $newValue . PHP_EOL;
}
file_put_contents($filePath, implode('', $lines));
echo 'SAVE FILE: ' . $filePath . PHP_EOL;
} else {
echo 'Config file not writable: ' . $filePath . PHP_EOL;
}
} else {
echo 'Config file not readable: ' . $filePath . PHP_EOL;
}
} else {
echo 'Config file not found: ' . $filePath . PHP_EOL;
}
}
}
}