Error 503 Service Temporarily Unavailable

  PHP, Web hosting

If your site returns this error “503 Service Temporarily Unavailable“, it usually indicates a problem with the server overloading by your PHP scripts.

Each webhosting server has preserved 5 PHP processes, which can operate simultaneously. This means that no more than 5 requirements for executing PHP script on one site at one time may be processed. To the contrary, it does not mean that the other requests were rejected – they are placed in a queue and waiting for processing. Usually, 5 PHP processes are entirely sufficient – if you have a properly optimized site and using caching, you will not find this problem.

It may happen that other requests are unable to gain free process PHP in timeout. That means that all 5 PHP processes are doing something for a long time. Then there are pending requests rejected with error 503 after some time.

Causes of the problem and the solution

The essence of the problem is that PHP scripts on your site are not able to perform requests – either because there are too much of them (too busy web), or because they have been waiting for something for a too long time and for some reason (unavailable external sources).

If you’re a programmer, try to find out which activity in your PHP scripts spend most of time (which action is blocked)

The goal is to shorten the execution time of a PHP request as much as possible. This allows you to “check in” multiple requests over time.

Possible causes and solutions:

  1. Incorrect optimized PHP application. Your application is not adapted for a large number of requests. Determine whether you are using a CMS which allows caching. If so, turn it on. This significantly shortens the period of the script execution, often it can radically reduce the number of carried SQL queries to the database and overall significantly speed up your site – for example caching in WordPress.

  2. Too many records in the database – a typical problem is that the application records all visitors accesses or some similar extensive statistics to the database, which is completely inappropriate, database is very busy and your site over time significantly slows.

  3. Inappropriate database tables, incorrect or missing database indexes – PHP waiting for the results a very long time for SQL queries(viz. MySQL – optimization of performance, indexes).

  4. PHP application trying to connect to some external resource which is unavailable – if you are downloading external data via RSS, SOAP, etc., but the destination server could not be connected or does not respond or overloaded – then your PHP script awaits too

  5. A long time running script called by cron – also PHP script called by cron will occupy a PHP process when it is executed. If you have more cron jobs or cron runs for a long time(this may be caused when script downloading data from external source, which is slow or not availlable), could also cause blocking of all PHP processes for your web.

  6. Files download via PHP script – If you offer to your visitors to download any large files such as the ones you refer to the PHP script that reads the file and sends it to the output (eg, ReadFile () or file_get_contents ()), then you will occupy the PHP process during the period of downloading. Therefore if something will be downloaded by 5 people at same time, there is no more free PHP process. Solve downloads by referring to direct or redirect URL adress of the file, do not use downloading via PHP.

  7. A large number of files in the wrong directory structure – in one directory should not be located more than thousands of items. For larger numbers, especially if you have incorrectly set the caching or incorrect stored e-shop images in one directory, operations with these files and folders can be radically slowed. With hundreds of thousands or millions of items such a directory is completely unusable.

  8. The administration of CMS is under attack in order to break the admin password. A large number of brute-force password cracking attempts burdens of applications and occupies PHP processes. This situation ыоу цан uncover in AccessLog (see below), then the attacker may be filtered out(for example, by blocking certain IP addresses in htaccess or allow just your IP address for administration).

The solution to this situation can be also upgrading to a higher version of webhosting NoLimit Extra – this tariff offers 10 PHP processes, also higher performance.

However, be warned that this option does not accelerate your scripts, ie shorten the execution time of your PHP script. Just allows running more scripts simultaneously one time (in parallel). So it can deal with the consequences, but not the causes. If the problem is that the execution time of your script is too long (eg in seconds), it is necessary to optimize the particular application.

When this error appears suddenly

Error 503 does not have to be related to the actual number of visitors or changes on the site. More frequent causes are loading some data from a slow or unavailable external source or run a cron job, something that has long retrieved, updates, performs maintenance etc. It might also be that randomly gather larger amount of complex requirements(search in extensive database).

AccessLog, errorlog and how to look for the cause

We do not recognize the cause of specific problem from error log of the server. We will only see the records about the error 503 = timeout while waiting for free process of PHP on webhosting.

For us, the PHP scripts of our customers are like black boxes. We do not know what’s going on inside, what exactly PHP scripts have been waiting for so long or what are they doing. The analyzing and solving of the problem should be done by the customer. For example, it is appropriate to introduce a PHP script with detailed logging of which requirements the are processed and how long did it run the PHP script. Then you can sequentially detect what’s taking so long and what is delaying the entire site.

In the analysis of the problem, it can be useful accesslog – to log accesses to the site. There you will see the specific requirements and period of execution. Accesslog can be activated free for 24 hours in customer administration (in additional services in detail of a particular webhosting), eventually, it can be activated permanently for an additional fee.

Byl pro Vás tento návod užitečný?