How Does a Web Server Execute PHP Code? From HTTP Request to HTML Response

A web server receives HTTP requests and delegates PHP execution to a PHP interpreter, commonly through PHP-FPM. The generated output is returned to the visitor's browser as HTML, JSON or another supported response format.

Georgios Angelis
By
Georgios Angelis
Georgios Angelis
Web Developer - IT Specialist & Digital Marketing Expert
Georgios Angelis is a Web Developer and Digital Marketing Expert based in Thessaloniki, Greece, with extensive professional experience in website development, search engine optimization (SEO), digital...
- Web Developer - IT Specialist & Digital Marketing Expert
5 Min Read

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 symptomPossible cause
HTTP 500PHP fatal error or server configuration problem
HTTP 502PHP-FPM unavailable or upstream communication failure
HTTP 504Upstream processing timeout
Blank pagePHP error with error display disabled
PHP source code displayedIncorrect 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.

Share This Article
Georgios Angelis
Web Developer - IT Specialist & Digital Marketing Expert
Follow:
Georgios Angelis is a Web Developer and Digital Marketing Expert based in Thessaloniki, Greece, with extensive professional experience in website development, search engine optimization (SEO), digital advertising, e-commerce, and web technologies. He is a Google-certified professional and runs Netclick Digital, a digital marketing agency recognized as a Google Partner. His professional expertise includes Google Ads, Meta Ads, technical SEO, WordPress development, PHP programming, web analytics, marketing automation, and digital growth strategies. Combining technical development skills with data-driven marketing expertise, he helps businesses improve their online visibility, website performance, customer acquisition, and digital presence. As a contributing author at SkillOnPage, Georgios shares practical knowledge and professional insights on web development, programming, digital marketing, SEO, artificial intelligence, e-commerce, and emerging digital technologies.
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *