- Browser Requests URL (e.g., http://localhost/index.php)
| - Apache/Nginx Receives Request
| - Apache/Nginx Checks Configuration (httpd.conf / nginx.conf)
| - If it’s a PHP file, Apache/Nginx calls the PHP Interpreter (mod_php or php-fpm)
| - PHP Processes the Request (based on code and php.ini settings)
| - PHP Returns HTML to Apache/Nginx
| - Apache/Nginx Sends HTML Response Back to Browser
| - Browser Displays the Web Page
1. Apache/Nginx as Web Server
- Apache and Nginx are web servers responsible for serving web content to clients (browsers).
- They handle HTTP requests and return responses, often serving files like HTML, CSS, JavaScript, and PHP.
- Apache or Nginx is installed on your local machine as part of a LAMP stack (Linux, Apache, MySQL, PHP) or XAMPP/WAMP for Windows.
Apache/Nginx configuration:
- Apache uses a file called
httpd.confto configure how it handles HTTP requests, file paths, directory permissions, and modules. - In the
httpd.conf, you can set up virtual hosts, which map domain names (likelocalhost) to specific directories where your website’s files are located.
# Example VirtualHost in Apache for a PHP website
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs/mysite"
DirectoryIndex index.php
</VirtualHost>
- For Nginx, a similar configuration is done in
/etc/nginx/nginx.confor in site-specific configurations like/etc/nginx/sites-available/default.
2. PHP – Server-Side Language
- PHP is a scripting language used to build dynamic websites.
- When a user visits a PHP-based webpage, the PHP code runs on the server, processes the data (e.g., from a database), and generates HTML, which is sent back to the browser.
- PHP is tightly integrated with Apache/Nginx using modules (
mod_phpfor Apache, orphp-fpmfor Nginx).
3. php.ini – PHP Configuration File
- The
php.inifile configures how PHP behaves. It controls settings like:- Error reporting (e.g., whether PHP errors are shown in the browser).
- Maximum file upload size.
- Time zones, extensions (e.g., MySQL or GD library).
- Memory limits and timeout configurations.
# Sample php.ini settings
display_errors = On
upload_max_filesize = 50M
max_execution_time = 300
date.timezone = "America/New_York"
- When you make changes to
php.ini, you typically need to restart Apache/Nginx for the changes to take effect.
4. httpd.conf – Apache Configuration
- The
httpd.conffile is where Apache’s main settings are configured. - It includes details like:
- ServerName
- DocumentRoot (where your PHP files are stored)
- Directory permissions (controls who can access what files)
- Modules like
mod_phpto ensure PHP is processed.
# Load PHP Module in Apache
LoadModule php_module "C:/php/php7apache2_4.dll"
AddHandler application/x-httpd-php .php
In the configuration, Apache uses this to interpret .php files and run them using the PHP interpreter.
5. How it all works together in a local website
- Apache/Nginx: The server listens for incoming HTTP requests (e.g., from a browser when visiting
http://localhost). - Apache/Nginx Configuration (
httpd.conf): Defines where the website’s files are located (e.g.,C:/xampp/htdocs/mysite). - PHP (
php.ini): When Apache receives a request for a.phpfile, it passes the request to the PHP processor configured inphp.ini. - PHP Processor: PHP reads and executes the PHP code, accessing databases or performing logic as needed. It generates an HTML output.
- Response to Browser: Apache/Nginx sends the processed HTML back to the client (browser), which displays the web page to the user.
6. Nginx and PHP-FPM (FastCGI Process Manager)
- Nginx does not have built-in support for PHP like Apache does with
mod_php. Instead, it uses PHP-FPM (FastCGI Process Manager) to handle PHP requests. - The workflow in Nginx would be:
- Nginx receives the request and forwards the
.phpfile to PHP-FPM. - PHP-FPM processes the PHP file and returns the output to Nginx.
- Nginx sends the response back to the browser.
- Nginx receives the request and forwards the
# Nginx configuration to forward PHP requests to PHP-FPM
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Summary:
- Apache/Nginx handles HTTP requests and determines which files to serve (e.g., HTML, PHP).
httpd.conf(Apache) andnginx.conf(Nginx) define the server’s configuration and how it processes requests.- PHP executes server-side scripts, generating dynamic HTML content.
php.inicontrols the PHP environment, determining how PHP scripts are executed.- Apache/Nginx serves the processed result back to the browser, which displays the final web page.


Leave a Reply