@@ -961,6 +961,41 @@ function update($event_name,$data) {
961961 $ tpl ->setVar ('fastcgi_config_syntax ' ,$ fastcgi_config ['fastcgi_config_syntax ' ]);
962962
963963 }
964+
965+ /**
966+ * PHP-FPM
967+ */
968+ // Support for multiple PHP versions
969+ if (trim ($ data ['new ' ]['fastcgi_php_version ' ]) != '' ){
970+ $ default_php_fpm = false ;
971+ list ($ custom_php_fpm_name , $ custom_php_fpm_init_script , $ custom_php_fpm_ini_dir , $ custom_php_fpm_pool_dir ) = explode (': ' , trim ($ data ['new ' ]['fastcgi_php_version ' ]));
972+ if (substr ($ custom_php_fpm_ini_dir ,-1 ) != '/ ' ) $ custom_php_fpm_ini_dir .= '/ ' ;
973+ } else {
974+ $ default_php_fpm = true ;
975+ }
976+
977+ if ($ default_php_fpm ){
978+ $ pool_dir = escapeshellcmd ($ web_config ['php_fpm_pool_dir ' ]);
979+ } else {
980+ $ pool_dir = $ custom_php_fpm_pool_dir ;
981+ }
982+ if (substr ($ pool_dir ,-1 ) != '/ ' ) $ pool_dir .= '/ ' ;
983+ $ pool_name = 'web ' .$ data ['new ' ]['domain_id ' ];
984+ $ socket_dir = escapeshellcmd ($ web_config ['php_fpm_socket_dir ' ]);
985+ if (substr ($ socket_dir ,-1 ) != '/ ' ) $ socket_dir .= '/ ' ;
986+
987+ if ($ data ['new ' ]['php_fpm_use_socket ' ] == 'y ' ){
988+ $ use_tcp = 0 ;
989+ $ use_socket = 1 ;
990+ } else {
991+ $ use_tcp = 1 ;
992+ $ use_socket = 0 ;
993+ }
994+ $ tpl ->setVar ('use_tcp ' , $ use_tcp );
995+ $ tpl ->setVar ('use_socket ' , $ use_socket );
996+ $ fpm_socket = $ socket_dir .$ pool_name .'.sock ' ;
997+ $ tpl ->setVar ('fpm_socket ' , $ fpm_socket );
998+ $ tpl ->setVar ('fpm_port ' , $ web_config ['php_fpm_start_port ' ] + $ data ['new ' ]['domain_id ' ] - 1 );
964999
9651000 /**
9661001 * install cgi starter script and add script alias to config.
@@ -1153,6 +1188,8 @@ function update($event_name,$data) {
11531188 $ this ->awstats_update ($ data ,$ web_config );
11541189 }
11551190
1191+ $ this ->php_fpm_pool_update ($ data ,$ web_config ,$ pool_dir ,$ pool_name ,$ socket_dir );
1192+
11561193 if ($ web_config ['check_apache_config ' ] == 'y ' ) {
11571194 //* Test if apache starts with the new configuration file
11581195 $ apache_online_status_before_restart = $ this ->_checkTcp ('localhost ' ,80 );
@@ -1256,6 +1293,11 @@ function delete($event_name,$data) {
12561293 exec ('rm -rf ' .$ fastcgi_starter_path );
12571294 }
12581295 }
1296+
1297+ // remove PHP-FPM pool
1298+ if ($ data ['old ' ]['php ' ] == 'php-fpm ' ) {
1299+ $ this ->php_fpm_pool_delete ($ data ,$ web_config );
1300+ }
12591301
12601302 //remove the php cgi starter script if available
12611303 if ($ data ['old ' ]['php ' ] == 'cgi ' ) {
@@ -1879,6 +1921,212 @@ private function awstats_delete ($data,$web_config) {
18791921 }
18801922 }
18811923
1924+ //* Update the PHP-FPM pool configuration file
1925+ private function php_fpm_pool_update ($ data ,$ web_config ,$ pool_dir ,$ pool_name ,$ socket_dir ) {
1926+ global $ app , $ conf ;
1927+ //$reload = false;
1928+
1929+ if (trim ($ data ['new ' ]['fastcgi_php_version ' ]) != '' ){
1930+ $ default_php_fpm = false ;
1931+ list ($ custom_php_fpm_name , $ custom_php_fpm_init_script , $ custom_php_fpm_ini_dir , $ custom_php_fpm_pool_dir ) = explode (': ' , trim ($ data ['new ' ]['fastcgi_php_version ' ]));
1932+ if (substr ($ custom_php_fpm_ini_dir ,-1 ) != '/ ' ) $ custom_php_fpm_ini_dir .= '/ ' ;
1933+ } else {
1934+ $ default_php_fpm = true ;
1935+ }
1936+
1937+ $ app ->uses ("getconf " );
1938+ $ web_config = $ app ->getconf ->get_server_config ($ conf ["server_id " ], 'web ' );
1939+
1940+ if ($ data ['new ' ]['php ' ] != 'php-fpm ' ){
1941+ if (@is_file ($ pool_dir .$ pool_name .'.conf ' )){
1942+ unlink ($ pool_dir .$ pool_name .'.conf ' );
1943+ //$reload = true;
1944+ }
1945+ if ($ data ['old ' ]['php ' ] == 'php-fpm ' ){
1946+ if (!$ default_php_fpm ){
1947+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ custom_php_fpm_init_script );
1948+ } else {
1949+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ conf ['init_scripts ' ].'/ ' .$ web_config ['php_fpm_init_script ' ]);
1950+ }
1951+ }
1952+ //if($reload == true) $app->services->restartService('php-fpm','reload');
1953+ return ;
1954+ }
1955+
1956+ $ app ->load ('tpl ' );
1957+ $ tpl = new tpl ();
1958+ $ tpl ->newTemplate ('php_fpm_pool.conf.master ' );
1959+
1960+ if ($ data ['new ' ]['php_fpm_use_socket ' ] == 'y ' ){
1961+ $ use_tcp = 0 ;
1962+ $ use_socket = 1 ;
1963+ if (!is_dir ($ socket_dir )) exec ('mkdir -p ' .$ socket_dir );
1964+ } else {
1965+ $ use_tcp = 1 ;
1966+ $ use_socket = 0 ;
1967+ }
1968+ $ tpl ->setVar ('use_tcp ' , $ use_tcp );
1969+ $ tpl ->setVar ('use_socket ' , $ use_socket );
1970+
1971+ $ fpm_socket = $ socket_dir .$ pool_name .'.sock ' ;
1972+ $ tpl ->setVar ('fpm_socket ' , $ fpm_socket );
1973+
1974+ $ tpl ->setVar ('fpm_pool ' , $ pool_name );
1975+ $ tpl ->setVar ('fpm_port ' , $ web_config ['php_fpm_start_port ' ] + $ data ['new ' ]['domain_id ' ] - 1 );
1976+ $ tpl ->setVar ('fpm_user ' , $ data ['new ' ]['system_user ' ]);
1977+ $ tpl ->setVar ('fpm_group ' , $ data ['new ' ]['system_group ' ]);
1978+ $ tpl ->setVar ('pm_max_children ' , $ data ['new ' ]['pm_max_children ' ]);
1979+ $ tpl ->setVar ('pm_start_servers ' , $ data ['new ' ]['pm_start_servers ' ]);
1980+ $ tpl ->setVar ('pm_min_spare_servers ' , $ data ['new ' ]['pm_min_spare_servers ' ]);
1981+ $ tpl ->setVar ('pm_max_spare_servers ' , $ data ['new ' ]['pm_max_spare_servers ' ]);
1982+ $ tpl ->setVar ('document_root ' , $ data ['new ' ]['document_root ' ]);
1983+ $ tpl ->setVar ('security_level ' ,$ web_config ['security_level ' ]);
1984+ $ php_open_basedir = ($ data ['new ' ]['php_open_basedir ' ] == '' )?escapeshellcmd ($ data ['new ' ]['document_root ' ]):escapeshellcmd ($ data ['new ' ]['php_open_basedir ' ]);
1985+ $ tpl ->setVar ('php_open_basedir ' , $ php_open_basedir );
1986+ if ($ php_open_basedir != '' ){
1987+ $ tpl ->setVar ('enable_php_open_basedir ' , '' );
1988+ } else {
1989+ $ tpl ->setVar ('enable_php_open_basedir ' , '; ' );
1990+ }
1991+
1992+ // Custom php.ini settings
1993+ $ final_php_ini_settings = array ();
1994+ $ custom_php_ini_settings = trim ($ data ['new ' ]['custom_php_ini ' ]);
1995+ if ($ custom_php_ini_settings != '' ){
1996+ // Make sure we only have Unix linebreaks
1997+ $ custom_php_ini_settings = str_replace ("\r\n" , "\n" , $ custom_php_ini_settings );
1998+ $ custom_php_ini_settings = str_replace ("\r" , "\n" , $ custom_php_ini_settings );
1999+ $ ini_settings = explode ("\n" , $ custom_php_ini_settings );
2000+ if (is_array ($ ini_settings ) && !empty ($ ini_settings )){
2001+ foreach ($ ini_settings as $ ini_setting ){
2002+ list ($ key , $ value ) = explode ('= ' , $ ini_setting );
2003+ if ($ value ){
2004+ $ value = escapeshellcmd (trim ($ value ));
2005+ $ key = escapeshellcmd (trim ($ key ));
2006+ switch (strtolower ($ value )) {
2007+ case 'on ' :
2008+ case 'off ' :
2009+ case '1 ' :
2010+ case '0 ' :
2011+ // PHP-FPM might complain about invalid boolean value if you use 0
2012+ $ value = 'off ' ;
2013+ case 'true ' :
2014+ case 'false ' :
2015+ case 'yes ' :
2016+ case 'no ' :
2017+ $ final_php_ini_settings [] = array ('ini_setting ' => 'php_admin_flag[ ' .$ key .'] = ' .$ value );
2018+ break ;
2019+ default :
2020+ $ final_php_ini_settings [] = array ('ini_setting ' => 'php_admin_value[ ' .$ key .'] = ' .$ value );
2021+ }
2022+ }
2023+ }
2024+ }
2025+ }
2026+
2027+ $ tpl ->setLoop ('custom_php_ini_settings ' , $ final_php_ini_settings );
2028+
2029+ file_put_contents ($ pool_dir .$ pool_name .'.conf ' ,$ tpl ->grab ());
2030+ $ app ->log ('Writing the PHP-FPM config file: ' .$ pool_dir .$ pool_name .'.conf ' ,LOGLEVEL_DEBUG );
2031+ unset($ tpl );
2032+
2033+ // delete pool in all other PHP versions
2034+ $ default_pool_dir = escapeshellcmd ($ web_config ['php_fpm_pool_dir ' ]);
2035+ if (substr ($ default_pool_dir ,-1 ) != '/ ' ) $ default_pool_dir .= '/ ' ;
2036+ if ($ default_pool_dir != $ pool_dir ){
2037+ if ( @is_file ($ default_pool_dir .$ pool_name .'.conf ' ) ) {
2038+ unlink ($ default_pool_dir .$ pool_name .'.conf ' );
2039+ $ app ->log ('Removed PHP-FPM config file: ' .$ default_pool_dir .$ pool_name .'.conf ' ,LOGLEVEL_DEBUG );
2040+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ conf ['init_scripts ' ].'/ ' .$ web_config ['php_fpm_init_script ' ]);
2041+ }
2042+ }
2043+ $ php_versions = $ app ->db ->queryAllRecords ("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = " .$ conf ["server_id " ]);
2044+ if (is_array ($ php_versions ) && !empty ($ php_versions )){
2045+ foreach ($ php_versions as $ php_version ){
2046+ if (substr ($ php_version ['php_fpm_pool_dir ' ],-1 ) != '/ ' ) $ php_version ['php_fpm_pool_dir ' ] .= '/ ' ;
2047+ if ($ php_version ['php_fpm_pool_dir ' ] != $ pool_dir ){
2048+ if ( @is_file ($ php_version ['php_fpm_pool_dir ' ].$ pool_name .'.conf ' ) ) {
2049+ unlink ($ php_version ['php_fpm_pool_dir ' ].$ pool_name .'.conf ' );
2050+ $ app ->log ('Removed PHP-FPM config file: ' .$ php_version ['php_fpm_pool_dir ' ].$ pool_name .'.conf ' ,LOGLEVEL_DEBUG );
2051+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ php_version ['php_fpm_init_script ' ]);
2052+ }
2053+ }
2054+ }
2055+ }
2056+ // Reload current PHP-FPM after all others
2057+ sleep (1 );
2058+ if (!$ default_php_fpm ){
2059+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ custom_php_fpm_init_script );
2060+ } else {
2061+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ conf ['init_scripts ' ].'/ ' .$ web_config ['php_fpm_init_script ' ]);
2062+ }
2063+
2064+ //$reload = true;
2065+
2066+ //if($reload == true) $app->services->restartService('php-fpm','reload');
2067+ }
2068+
2069+ //* Delete the PHP-FPM pool configuration file
2070+ private function php_fpm_pool_delete ($ data ,$ web_config ) {
2071+ global $ app , $ conf ;
2072+
2073+ if (trim ($ data ['old ' ]['fastcgi_php_version ' ]) != '' ){
2074+ $ default_php_fpm = false ;
2075+ list ($ custom_php_fpm_name , $ custom_php_fpm_init_script , $ custom_php_fpm_ini_dir , $ custom_php_fpm_pool_dir ) = explode (': ' , trim ($ data ['old ' ]['fastcgi_php_version ' ]));
2076+ if (substr ($ custom_php_fpm_ini_dir ,-1 ) != '/ ' ) $ custom_php_fpm_ini_dir .= '/ ' ;
2077+ } else {
2078+ $ default_php_fpm = true ;
2079+ }
2080+
2081+ if ($ default_php_fpm ){
2082+ $ pool_dir = escapeshellcmd ($ web_config ['php_fpm_pool_dir ' ]);
2083+ } else {
2084+ $ pool_dir = $ custom_php_fpm_pool_dir ;
2085+ }
2086+
2087+ if (substr ($ pool_dir ,-1 ) != '/ ' ) $ pool_dir .= '/ ' ;
2088+ $ pool_name = 'web ' .$ data ['old ' ]['domain_id ' ];
2089+
2090+ if ( @is_file ($ pool_dir .$ pool_name .'.conf ' ) ) {
2091+ unlink ($ pool_dir .$ pool_name .'.conf ' );
2092+ $ app ->log ('Removed PHP-FPM config file: ' .$ pool_dir .$ pool_name .'.conf ' ,LOGLEVEL_DEBUG );
2093+
2094+ //$app->services->restartService('php-fpm','reload');
2095+ }
2096+
2097+ // delete pool in all other PHP versions
2098+ $ default_pool_dir = escapeshellcmd ($ web_config ['php_fpm_pool_dir ' ]);
2099+ if (substr ($ default_pool_dir ,-1 ) != '/ ' ) $ default_pool_dir .= '/ ' ;
2100+ if ($ default_pool_dir != $ pool_dir ){
2101+ if ( @is_file ($ default_pool_dir .$ pool_name .'.conf ' ) ) {
2102+ unlink ($ default_pool_dir .$ pool_name .'.conf ' );
2103+ $ app ->log ('Removed PHP-FPM config file: ' .$ default_pool_dir .$ pool_name .'.conf ' ,LOGLEVEL_DEBUG );
2104+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ conf ['init_scripts ' ].'/ ' .$ web_config ['php_fpm_init_script ' ]);
2105+ }
2106+ }
2107+ $ php_versions = $ app ->db ->queryAllRecords ("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = " .$ data ['old ' ]['server_id ' ]);
2108+ if (is_array ($ php_versions ) && !empty ($ php_versions )){
2109+ foreach ($ php_versions as $ php_version ){
2110+ if (substr ($ php_version ['php_fpm_pool_dir ' ],-1 ) != '/ ' ) $ php_version ['php_fpm_pool_dir ' ] .= '/ ' ;
2111+ if ($ php_version ['php_fpm_pool_dir ' ] != $ pool_dir ){
2112+ if ( @is_file ($ php_version ['php_fpm_pool_dir ' ].$ pool_name .'.conf ' ) ) {
2113+ unlink ($ php_version ['php_fpm_pool_dir ' ].$ pool_name .'.conf ' );
2114+ $ app ->log ('Removed PHP-FPM config file: ' .$ php_version ['php_fpm_pool_dir ' ].$ pool_name .'.conf ' ,LOGLEVEL_DEBUG );
2115+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ php_version ['php_fpm_init_script ' ]);
2116+ }
2117+ }
2118+ }
2119+ }
2120+
2121+ // Reload current PHP-FPM after all others
2122+ sleep (1 );
2123+ if (!$ default_php_fpm ){
2124+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ custom_php_fpm_init_script );
2125+ } else {
2126+ $ app ->services ->restartService ('php-fpm ' ,'reload: ' .$ conf ['init_scripts ' ].'/ ' .$ web_config ['php_fpm_init_script ' ]);
2127+ }
2128+ }
2129+
18822130 function client_delete ($ event_name ,$ data ) {
18832131 global $ app , $ conf ;
18842132
0 commit comments