Whilst viewing a website built with WordPress, a user’s web browser does not run the PHP code. This is run by the server and then sent to the user.
Knowing how PHP is run is useful for identifying server related issues, improving performance and setting up hosting.
What Is Needed To Run PHP
Generally, a PHP hosting environment will comprise;
- Web server – such as Apache, Nginx or LiteSpeed. This accepts and responds to HTTP requests.
- PHP – runs PHP and produces output.
- Database server – typically MySQL or MariaDB. This is used to access and/or save data within applications.
Whilst it is not essential for PHP to access a database, basic PHP can still be used to produce dynamic output without requiring a database connection.
How Does A Browser Access PHP?
Below is a common sequence of events in a PHP-FPM hosting setup.
1. A browser submits an HTTP/HTTPS request to a web server.
2. The web server recognises that the requested resource is PHP and so passes on the request.
3. The request is sent to the PHP runner.
4. PHP runs the application and accesses a database, if required.
5. Output is sent back to the web server.
6. The web server then transmits an HTTP response to the browser.
Users view the resulting output rather than raw PHP code.
PHP In Practice
Within your site’s document root, generate a file called index.php and add the following code:
<?php
$website = “My Website”;
echo “Welcome to $website
“;
echo “Server date:” . date(“Y-m-d”) . “
“;
?>
Open this file in your browser by typing in your website’s URL.
PHP manipulates the variable and runs the date() function to dynamically produce HTML.
If you inspect the source code of the web page in your browser, it will resemble the below:
Welcome to My Website
Server date: 2026-12-06
Whilst the PHP code is retained on the server, the shown date depends on the time zone settings for PHP on the server.
Different PHP Running Methods For Apache And Nginx
Apache can run PHP in a variety of ways. As well as continuing to use mod_php, it can also run PHP via PHP-FPM using FastCGI.
Unlike Apache, Nginx does not run PHP natively. Rather, it delegates PHP requests to an external PHP runner, often PHP-FPM.
PHP-FPM comprises a group of worker processes which run PHP.
Each worker adheres to the pool settings. The pm.max_children parameter specifies the greatest number of requests which can be handled concurrently by a PHP-FPM pool.
Should all workers be busy, extra requests may have to queue. For highly trafficked WordPress sites this can present considerable performance challenges.
Why PHP Can Occasionally Run Slowly
The speed at which PHP runs is influenced by factors such as the intricacy of the application, the number of database requests, server resources and calls to external APIs.
Two key tools can effectively minimise superfluous processing:
PHP OPcache – saves PHP compiled code in shared memory so that there is no need to continually re-interpret and compile PHP.
Full page cache – retains output from a web page. Suitable requests can then be met by simply providing this cached output, without needing to run the initial PHP application.
These are distinct tools which can even be used in conjunction with each other.
Investigating PHP Server Issues
| Error or symptom | Possible cause |
|---|---|
| HTTP 500 | PHP fatal error or server configuration problem |
| HTTP 502 | PHP-FPM unavailable or upstream communication failure |
| HTTP 504 | Upstream processing timeout |
| Blank page | PHP error with error display disabled |
| PHP source code displayed | Incorrect PHP handler configuration |
Initial investigation should involve reviewing both web server and PHP-FPM error logs.
For live sites, ensure that PHP error reporting is switched on, although display of these errors should be turned off. Showing detailed error messages or PHP code can expose sensitive details such as database login details and file system locations.









