@@ -825,6 +825,10 @@ function update($event_name,$data) {
825825 }
826826
827827 $ vhost_file = escapeshellcmd ($ web_config ["vhost_conf_dir " ].'/ ' .$ data ["new " ]["domain " ].'.vhost ' );
828+ //* Make a backup copy of vhost file
829+ copy ($ vhost_file ,$ vhost_file .'~ ' );
830+
831+ //* Write vhost file
828832 file_put_contents ($ vhost_file ,$ tpl ->grab ());
829833 $ app ->log ("Writing the vhost file: $ vhost_file " ,LOGLEVEL_DEBUG );
830834 unset($ tpl );
@@ -879,14 +883,36 @@ function update($event_name,$data) {
879883 if ($ data ["new " ]["stats_type " ] == 'awstats ' && $ data ["new " ]["type " ] == "vhost " ) {
880884 $ this ->awstats_update ($ data ,$ web_config );
881885 }
886+
887+ if ($ web_config ['check_apache_config ' ] == 'y ' ) {
888+ //* Test if apache starts with the new configuration file
889+ $ apache_online_status_before_restart = $ this ->_checkTcp ('localhost ' ,80 );
890+ $ app ->log ("Apache status is: " .$ apache_online_status_before_restart ,LOGLEVEL_DEBUG );
882891
883-
884- if ($ apache_chrooted ) {
885- $ app ->services ->restartServiceDelayed ('httpd ' ,'restart ' );
892+ $ app ->services ->restartService ('httpd ' ,'restart ' );
893+
894+ //* Check if apache restarted successfully if it was online before
895+ $ apache_online_status_after_restart = $ this ->_checkTcp ('localhost ' ,80 );
896+ $ app ->log ("Apache online status after restart is: " .$ apache_online_status_after_restart ,LOGLEVEL_DEBUG );
897+ if ($ apache_online_status_before_restart && !$ apache_online_status_after_restart ) {
898+ $ app ->log ("Apache did not restart after the configuration change for website " .$ data ["new " ]["domain " ].' Reverting the configuration. Saved not working config as ' .$ vhost_file .'.err ' ,LOGLEVEL_WARN );
899+ copy ($ vhost_file ,$ vhost_file .'.err ' );
900+ copy ($ vhost_file .'~ ' ,$ vhost_file );
901+ $ app ->services ->restartService ('httpd ' ,'restart ' );
902+ }
886903 } else {
887- // request a httpd reload when all records have been processed
888- $ app ->services ->restartServiceDelayed ('httpd ' ,'reload ' );
904+ //* We do not check the apache config after changes (is faster)
905+ if ($ apache_chrooted ) {
906+ $ app ->services ->restartServiceDelayed ('httpd ' ,'restart ' );
907+ } else {
908+ // request a httpd reload when all records have been processed
909+ $ app ->services ->restartServiceDelayed ('httpd ' ,'reload ' );
910+ }
889911 }
912+
913+ // Remove the backup copy of the config file.
914+ unlink ($ vhost_file .'~ ' );
915+
890916
891917 //* Unset action to clean it for next processed vhost.
892918 $ this ->action = '' ;
@@ -1283,6 +1309,18 @@ private function _exec($command) {
12831309 $ app ->log ("exec: " .$ command ,LOGLEVEL_DEBUG );
12841310 exec ($ command );
12851311 }
1312+
1313+ private function _checkTcp ($ host ,$ port ) {
1314+
1315+ $ fp = @fsockopen ($ host , $ port , $ errno , $ errstr , 2 );
1316+
1317+ if ($ fp ) {
1318+ fclose ($ fp );
1319+ return true ;
1320+ } else {
1321+ return false ;
1322+ }
1323+ }
12861324
12871325
12881326} // end class
0 commit comments