This error happens because of either the php taking too long to process the script or the database process large query. The more insights will be there in the Apache error log file.
If its due to the PHP execution time limit, we need to increase the value in php.ini file.
max_execution_time = 600 max_input_time = 600
If its due to the MySQL execution time limit, we need to increase the value in my.cnf file.
wait_timeout = 28000
Please note that these values might required to change according to the environments.