Skip to content

Commit 7522f64

Browse files
author
Marius Burkard
committed
Merge branch '6177-php-cache-collisions-for-chroot-mode-php-fpm' into 'develop'
Resolve "php cache collisions for chroot mode php-fpm" Closes #6177 See merge request ispconfig/ispconfig3!1507
2 parents bd55918 + 7faccfc commit 7522f64

File tree

3 files changed

+114
-10
lines changed

3 files changed

+114
-10
lines changed

server/mods-available/web_module.inc.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class web_module {
5454
'web_backup_insert',
5555
'web_backup_update',
5656
'web_backup_delete',
57+
'server_php_insert',
58+
'server_php_update',
59+
'server_php_delete',
5760
'aps_instance_insert',
5861
'aps_instance_update',
5962
'aps_instance_delete',
@@ -112,6 +115,7 @@ class that contains the function functionname.
112115
$app->modules->registerTableHook('web_folder', 'web_module', 'process');
113116
$app->modules->registerTableHook('web_folder_user', 'web_module', 'process');
114117
$app->modules->registerTableHook('web_backup', 'web_module', 'process');
118+
$app->modules->registerTableHook('server_php', 'web_module', 'process');
115119
$app->modules->registerTableHook('aps_instances', 'web_module', 'process');
116120
$app->modules->registerTableHook('aps_instances_settings', 'web_module', 'process');
117121
$app->modules->registerTableHook('aps_packages', 'web_module', 'process');
@@ -167,6 +171,11 @@ function process($tablename, $action, $data) {
167171
if($action == 'u') $app->plugins->raiseEvent('web_backup_update', $data);
168172
if($action == 'd') $app->plugins->raiseEvent('web_backup_delete', $data);
169173
break;
174+
case 'server_php':
175+
if($action == 'i') $app->plugins->raiseEvent('server_php_insert', $data);
176+
if($action == 'u') $app->plugins->raiseEvent('server_php_update', $data);
177+
if($action == 'd') $app->plugins->raiseEvent('server_php_delete', $data);
178+
break;
170179
case 'aps_instances':
171180
if($action == 'i') $app->plugins->raiseEvent('aps_instance_insert', $data);
172181
if($action == 'u') $app->plugins->raiseEvent('aps_instance_update', $data);

server/plugins-available/apache2_plugin.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ function update($event_name, $data) {
11781178
$app->system->chgrp('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log', 'root');
11791179
}
11801180

1181-
//* Write the custom php.ini file, if custom_php_ini fieled is not empty
1181+
//* Write the custom php.ini file, if custom_php_ini field is not empty
11821182
$custom_php_ini_dir = $web_config['website_basedir'].'/conf/'.$data['new']['system_user'];
11831183
if($data['new']['type'] == 'vhostsubdomain' || $data['new']['type'] == 'vhostalias') $custom_php_ini_dir .= '_' . $web_folder;
11841184
if(!is_dir($web_config['website_basedir'].'/conf')) $app->system->mkdir($web_config['website_basedir'].'/conf');

server/plugins-available/webserver_plugin.inc.php

Lines changed: 104 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class webserver_plugin {
3737
* This function is called during ispconfig installation to determine
3838
* if a symlink shall be created for this plugin.
3939
*/
40-
41-
4240
public function onInstall() {
4341
global $conf;
4442

@@ -56,7 +54,11 @@ public function onLoad() {
5654
global $app;
5755

5856
$app->plugins->registerAction('server_plugins_loaded', $this->plugin_name, 'check_phpini_changes');
57+
5958
$app->plugins->registerEvent('server_update', $this->plugin_name, 'server_update');
59+
60+
$app->plugins->registerEvent('server_php_insert', $this->plugin_name, 'server_php_update');
61+
$app->plugins->registerEvent('server_php_update', $this->plugin_name, 'server_php_update');
6062
}
6163

6264
/**
@@ -92,10 +94,6 @@ public function check_phpini_changes() {
9294
'mode' => 'mod',
9395
'php_version' => 0); // default;
9496

95-
$check_files[] = array('file' => $web_config['php_ini_path_cgi'],
96-
'mode' => '', // all but 'mod' and 'fast-cgi'
97-
'php_version' => 0); // default;
98-
9997
if($fastcgi_config["fastcgi_phpini_path"] && $fastcgi_config["fastcgi_phpini_path"] != $web_config['php_ini_path_cgi']) {
10098
$check_files[] = array('file' => $fastcgi_config["fastcgi_phpini_path"],
10199
'mode' => 'fast-cgi',
@@ -106,6 +104,16 @@ public function check_phpini_changes() {
106104
'php_version' => 0); // default;
107105
}
108106

107+
$check_files[] = array('file' => $web_config['php_fpm_ini_path'],
108+
'mode' => 'php-fpm',
109+
'php_version' => 0); // default;
110+
111+
if(!array_search($web_config['php_ini_path_cgi'], array_column($check_files, 'file'))) {
112+
$check_files[] = array('file' => $web_config['php_ini_path_cgi'],
113+
'mode' => '', // all but 'mod' and 'fast-cgi'
114+
'php_version' => 0); // default;
115+
}
116+
109117

110118
//** read additional php versions of this server
111119
$php_versions = $app->db->queryAllRecords('SELECT server_php_id, php_fastcgi_ini_dir, php_fpm_ini_dir FROM server_php WHERE server_id = ?', $conf['server_id']);
@@ -114,7 +122,8 @@ public function check_phpini_changes() {
114122
$check_files[] = array('file' => $php['php_fastcgi_ini_dir'] . '/php.ini',
115123
'mode' => 'fast-cgi',
116124
'php_version' => $php['server_php_id']);
117-
} elseif($php['php_fpm_ini_dir'] && $php['php_fpm_ini_dir'] . '/php.ini' != $web_config['php_ini_path_cgi']) {
125+
}
126+
if($php['php_fpm_ini_dir'] && $php['php_fpm_ini_dir'] . '/php.ini' != $web_config['php_fpm_ini_path']) {
118127
$check_files[] = array('file' => $php['php_fpm_ini_dir'] . '/php.ini',
119128
'mode' => 'php-fpm',
120129
'php_version' => $php['server_php_id']);
@@ -134,6 +143,13 @@ public function check_phpini_changes() {
134143
}
135144
if(!is_array($php_ini_md5)) $php_ini_md5 = array();
136145

146+
// verify needed php file settings if that hasn't been done since 15 minutes
147+
$now = time();
148+
$verify_php_ini=false;
149+
if(!isset($php_ini_md5['last_verify_php_ini']) || ($now - intval($php_ini_md5['last_verify_php_ini']) > 15*60)) {
150+
$verify_php_ini=true;
151+
}
152+
137153
$processed = array();
138154
foreach($check_files as $file) {
139155
$file_path = $file['file'];
@@ -145,6 +161,11 @@ public function check_phpini_changes() {
145161
if(in_array($ident, $processed) == true) continue;
146162
$processed[] = $ident;
147163

164+
//** check that needed php.ini settings/changes are made
165+
if($verify_php_ini) {
166+
$this->verify_php_ini($file);
167+
}
168+
148169
//** check if md5sum of file changed
149170
$file_md5 = md5_file($file_path);
150171
if(array_key_exists($file_path, $php_ini_md5) == false || $php_ini_md5[$file_path] != $file_md5) {
@@ -158,13 +179,62 @@ public function check_phpini_changes() {
158179
$new_php_ini_md5[$file_path] = $file_md5;
159180
}
160181

182+
$new_php_ini_md5['last_verify_php_ini'] = time();
183+
161184
//** write new md5 sums if something changed
162-
if($php_ini_changed == true) $app->system->file_put_contents(SCRIPT_PATH . '/temp/php.ini.md5sum', base64_encode(serialize($new_php_ini_md5)));
185+
if($php_ini_changed == true || $verify_php_ini == true) $app->system->file_put_contents(SCRIPT_PATH . '/temp/php.ini.md5sum', base64_encode(serialize($new_php_ini_md5)));
163186
unset($new_php_ini_md5);
164187
unset($php_ini_md5);
165188
unset($processed);
166189
}
167190

191+
/**
192+
* The method runs each php.ini file through verify_php_ini()
193+
*/
194+
function server_php_update($event_name, $data) {
195+
global $app, $conf;
196+
197+
if(isset($data['new']['php_fastcgi_ini_dir'])) {
198+
$php_ini = $data['new']['php_fastcgi_ini_dir'] . '/php.ini';
199+
if(file_exists($php_ini)) {
200+
$this->verify_php_ini(array('file' => $php_ini,
201+
'mode' => 'fast-cgi',
202+
'php_version' => $data['new']['server_php_id'])
203+
);
204+
} else {
205+
$app->log("Cannot verify php.ini, file not found: $php_ini", LOGLEVEL_WARN);
206+
}
207+
}
208+
if(isset($data['new']['php_fpm_ini_dir'])) {
209+
$php_ini = $data['new']['php_fpm_ini_dir'] . '/php.ini';
210+
if(file_exists($php_ini)) {
211+
$this->verify_php_ini(array('file' => $php_ini,
212+
'mode' => 'php-fpm',
213+
'php_version' => $data['new']['server_php_id'])
214+
);
215+
} else {
216+
$app->log("Cannot verify php.ini, file not found: $php_ini", LOGLEVEL_WARN);
217+
}
218+
}
219+
}
220+
221+
/**
222+
* The method checks/sets needed php.ini settings
223+
*/
224+
public function verify_php_ini($file) {
225+
global $app;
226+
227+
if(isset($file['file']) && is_file($file['file'])) {
228+
$php_ini = $file['file'];
229+
// ensure opcache.validate_root = 1
230+
$app->system->exec_safe('grep ^opcache.validate_root ?', $php_ini);
231+
if($app->system->last_exec_retcode() != 0) {
232+
$app->log('verify_php_ini(): php.ini '.$php_ini.' is missing validate_root', LOGLEVEL_DEBUG);
233+
$sed_script='s/; *opcache\.validate_root *= *.+$/opcache.validate_root = 1/g';
234+
$app->system->exec_safe('sed -E -i ? ?', $sed_script, $php_ini);
235+
}
236+
}
237+
}
168238

169239
/*
170240
* Checks for changes to jailkit settings in server config and schedules affected jails to be updated.
@@ -245,7 +315,32 @@ function server_update($event_name, $data) {
245315
}
246316
}
247317
}
318+
319+
$check_files = array();
320+
if ($old['php_ini_path_apache'] != $new['php_ini_path_apache']) {
321+
$check_files[] = array('file' => $new['php_ini_path_apache'],
322+
'mode' => 'mod',
323+
'php_version' => 0);
324+
}
325+
326+
if ($old['fastcgi_phpini_path'] != $new['fastcgi_phpini_path']) {
327+
$check_files[] = array('file' => $new['fastcgi_phpini_path'],
328+
'mode' => 'fast-cgi',
329+
'php_version' => 0);
330+
}
331+
if ($old['php_ini_path_cgi'] != $new['php_ini_path_cgi']) {
332+
$check_files[] = array('file' => $new['php_ini_path_cgi'],
333+
'mode' => 'fast-cgi',
334+
'php_version' => 0);
335+
}
336+
if ($old['php_fpm_ini_path'] != $new['php_fpm_ini_path']) {
337+
$check_files[] = array('file' => $web_config['php_fpm_ini_path'],
338+
'mode' => 'php-fpm',
339+
'php_version' => 0);
340+
}
341+
foreach ($check_files as $file) {
342+
$this->verify_php_ini($file);
343+
}
248344
}
249345
}
250346

251-
?>

0 commit comments

Comments
 (0)