Skip to content

Instantly share code, notes, and snippets.

@adbrsln
Last active September 8, 2025 04:42
Show Gist options
  • Select an option

  • Save adbrsln/fbcbb56965f953f683f1b97322508dca to your computer and use it in GitHub Desktop.

Select an option

Save adbrsln/fbcbb56965f953f683f1b97322508dca to your computer and use it in GitHub Desktop.
HTTPD TUNING

Guide For Installing and Tuning Apache + PHP FPM

#Links

32 GB Tuning

16 GB Tuning

8 GB Tuning

Base Configuration and installation

1. Installation & Integration of PHP-FPM

First, we will install a modern version of PHP-FPM and configure Apache to use it, replacing the outdated mod_php.

A. Install a Modern PHP Version via Remi Repository

The default PHP in RHEL 7 is too old. The Remi repository is the standard for getting up-to-date PHP versions.

  1. Install the Remi Repository:

    sudo yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
  2. Install yum-utils:

    sudo yum install -y yum-utils
  3. Enable the PHP 7.4 Repository (a stable and widely used version):

    sudo yum-config-manager --enable remi-php74
  4. Install PHP-FPM and Common Modules:

    sudo yum install -y php-fpm php-mysqlnd php-gd php-xml php-mbstring php-json php-opcache

B. Configure Apache and PHP-FPM to Work Together

  1. Configure PHP-FPM to Use a Unix Socket: A socket is more efficient than a TCP port for local communication.

    • Edit /etc/php-fpm.d/www.conf.
    • Find the listen directive and change it:
    ; Find and comment out this line:
    ; listen = 127.0.0.1:9000
    
    ; Add this line to use a socket:
    listen = /var/run/php-fpm/www.sock
    • A few lines below, set the socket permissions so Apache can access it. Uncomment these lines:
    listen.owner = apache
    listen.group = apache
    listen.mode = 0660
  2. Disable the Old mod_php: This module conflicts with the event MPM and PHP-FPM.

    sudo mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.disabled

    (It's okay if this command fails; it just means mod_php was never installed.)

  3. Configure Apache to Forward PHP Requests:

    • Edit your site's virtual host file (e.g., /etc/httpd/conf.d/your-site.conf).
    • Inside your <VirtualHost> block, add the following FilesMatch directive:
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php-fpm/www.sock|fcgi://localhost/"
    </FilesMatch>

Guide For 32 GB RAM

This guide provides a complete walkthrough for setting up and performance-tuning a dedicated web application server.

Server Specifications & Goal

  • Operating System: RHEL 7 / CentOS 7
  • Total RAM: 32 GB
  • Services: Apache (httpd) 2.4 and PHP-FPM
  • Workload: A "heavy usage" application (e.g., e-commerce, complex CMS, data processing) where individual PHP scripts are memory and CPU intensive.
  • Database: Hosted on a separate server.

1. The Memory Allocation Plan

With installation complete, we partition the 32 GB of RAM for optimal performance.

  • PHP-FPM (The Application Engine): 16 GB
  • Apache httpd (The Web Server): 8 GB
  • Operating System & File Cache: 8 GB

2. Tuning PHP-FPM for a Heavy Application

We will now tune PHP-FPM based on our 16 GB budget, assuming each process in a heavy application uses ~100 MB of RAM.

A. Tune the PHP-FPM Process Manager

File: /etc/php-fpm.d/www.conf

Goal: Limit the total number of PHP processes to stay within budget.

Calculation: 16384 MB / 100 MB per process ≈ 160 processes

; /etc/php-fpm.d/www.conf

pm = dynamic

; CRITICAL: Max processes, recalculated for a heavy app (~100MB/process).
pm.max_children = 160

; The number of servers to start with.
pm.start_servers = 40

; The minimum number of idle "ready" processes.
pm.min_spare_servers = 20

; The maximum number of idle processes.
pm.max_spare_servers = 60

; Recycle processes more frequently to ensure stability.
pm.max_requests = 200

B. Tune PHP Core Settings for Long-Running Scripts

File: /etc/php.ini

Goal: Increase resource limits for individual scripts to prevent failures.

; /etc/php.ini

; A heavy script needs more memory.
memory_limit = 512M

; Heavy tasks need more time. Give scripts up to 2 minutes.
max_execution_time = 120

; Allow for larger file uploads.
upload_max_filesize = 128M
post_max_size = 128M

; Set this to your server's timezone.
date.timezone = Asia/Kuala_Lumpur

3. Tuning Apache httpd

Apache is tuned for its 8 GB budget to handle a high volume of connections.

File: /etc/httpd/conf.modules.d/00-mpm.conf

# /etc/httpd/conf.modules.d/00-mpm.conf

<IfModule mpm_event_module>
    StartServers             4
    MinSpareThreads          75
    MaxSpareThreads          250
    ServerLimit              20
    ThreadsPerChild          25

    # CRITICAL: Max simultaneous connections, calculated for an 8GB RAM budget.
    MaxRequestWorkers        400

    # Recycle child processes to prevent memory leaks over time.
    MaxConnectionsPerChild   4000
</IfModule>

4. Applying Configuration and Verifying

  1. Test Apache Configuration Syntax:

    sudo apachectl configtest

    This command must return Syntax OK before you proceed.

  2. Start and Enable Services:

    # Start and enable PHP-FPM
    sudo systemctl start php-fpm
    sudo systemctl enable php-fpm
    
    # Restart Apache to apply all changes
    sudo systemctl restart httpd
  3. Verify the Setup:

    • Create a test file to confirm PHP is being handled by FPM.
    echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
    • Navigate to http://your-server-ip/info.php in a browser.
    • Look for the Server API line. It must say FPM/FastCGI.
    • For security, delete the test file when you are done:
    sudo rm /var/www/html/info.php

Your server is now fully installed, integrated, and tuned as a high-performance machine for your heavy-usage application.

Guide For 16 GB RAM

This guide provides a complete walkthrough for setting up and performance-tuning a dedicated web application server with 32 GB of total RAM, with a strong focus on PHP-FPM's 16 GB memory allocation for "heavy usage" applications where PHP scripts are memory and CPU intensive.

Server Specifications & Goal

  • Operating System: RHEL 7 / CentOS 7
  • Total RAM: 32 GB
  • Services: Apache (httpd) 2.4 and PHP-FPM
  • Workload: A "heavy usage" application (e.g., e-commerce, complex CMS, data processing).
  • Database: Hosted on a separate server.`

1. The Memory Allocation Plan (16 GB Server)

With 16 GB of RAM, we carefully partition the memory to ensure stability and high performance for the heavy application.

  • PHP-FPM (The Application Engine): 10 GB
    • This gets the largest share, as the heavy application code is the primary bottleneck and usually the most memory-intensive.
  • Apache httpd (The Web Server): 3 GB
    • Sufficient for handling a good number of connections with the event MPM, which is very memory-efficient.
  • Operating System & File Cache: 3 GB
    • A necessary reservation to keep the OS running smoothly and to cache files from disk, improving I/O performance.

2. Tuning PHP-FPM for a Heavy Application (10 GB Budget)

We will tune PHP-FPM based on our 10 GB budget, assuming each process in a heavy application uses ~100 MB of RAM.

A. Tune the PHP-FPM Process Manager

File: /etc/php-fpm.d/www.conf Goal: Limit the total number of PHP processes to stay within budget and ensure stability. Calculation: 10240 MB / 100 MB per process ≈ 102 processes

; /etc/php-fpm.d/www.conf
pm = dynamic
; CRITICAL: Max processes, recalculated for a heavy app (~100MB/process) on a 16GB server.
pm.max_children = 102
; The number of servers to start with.
pm.start_servers = 25
; The minimum number of idle "ready" processes.
pm.min_spare_servers = 15
; The maximum number of idle processes.
pm.max_spare_servers = 40
; Recycle processes more frequently to ensure stability and prevent memory leaks.
pm.max_requests = 200

B. Tune PHP Core Settings for Long-Running Scripts

File: /etc/php.ini Goal: Increase resource limits for individual scripts to prevent failures during heavy tasks. [1]

; /etc/php.ini
; A heavy script needs more memory. 512M is a good balance for this server size.
memory_limit = 512M
; Heavy tasks need more time. Give scripts up to 2 minutes to complete.
max_execution_time = 120
; Allow for larger file uploads.
upload_max_filesize = 128M
post_max_size = 128M
; Set this to your server's timezone.
date.timezone = Asia/Kuala_Lumpur

3. Tuning Apache httpd (8 GB Budget)

Apache is tuned for its 8 GB budget to handle a good volume of connections. We assume a lightweight event MPM process uses ~20 MB of RAM. [1]

File: /etc/httpd/conf.modules.d/00-mpm.conf Goal: Handle a respectable number of simultaneous connections without exhausting memory. Calculation: 8192 MB / 20 MB per process ≈ 400 processes [1]

# /etc/httpd/conf.modules.d/00-mpm.conf
<IfModule mpm_event_module>
    StartServers          4
    MinSpareThreads       60
    MaxSpareThreads       200
    ServerLimit           16
    ThreadsPerChild       25
    # CRITICAL: Max simultaneous connections, calculated for an 8GB RAM budget.
    MaxRequestWorkers     400
    # Recycle child processes to prevent memory leaks over time.
    MaxConnectionsPerChild  3000
</IfModule>

4. Applying Configuration and Verifying

  1. Test Apache Configuration Syntax: [1]

    sudo apachectl configtest

    This command must return Syntax OK before you proceed.

  2. Start and Enable Services: [1]

    # Start and enable PHP-FPM
    sudo systemctl start php-fpm
    sudo systemctl enable php-fpm
    
    # Restart Apache to apply all changes
    sudo systemctl restart httpd
  3. Verify the Setup: [1]

    • Create a test file to confirm PHP is being handled by FPM.
      echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
    • Navigate to http://your-server-ip/info.php in a browser.
    • Look for the Server API line. It must say FPM/FastCGI.
    • For security, delete the test file when you are done:
      sudo rm /var/www/html/info.php

Your server is now fully installed, integrated, and tuned as a high-performance machine for your heavy-usage application on 32 GB RAM, with a 16 GB allocation for PHP-FPM.


Guide For 8 GB RAM

This guide provides a complete walkthrough for setting up and performance-tuning a dedicated web application server with limited memory.

Server Specifications & Goal

  • Operating System: RHEL 7 / CentOS 7
  • Total RAM: 8 GB
  • Services: Apache (httpd) 2.4 and PHP-FPM
  • Workload: A "heavy usage" application (e.g., e-commerce, complex CMS, data processing).
  • Database: Hosted on a separate server.

1. The Memory Allocation Plan (8 GB Server)

With only 8 GB of RAM, our budget must be carefully balanced to prevent the server from running out of memory (OOM).

  • PHP-FPM (The Application Engine): 4 GB
    • This gets the largest share, as the heavy application code is the primary bottleneck.
  • Apache httpd (The Web Server): 2 GB
    • Sufficient for handling a good number of connections while being very memory-efficient.
  • Operating System & File Cache: 2 GB
    • A necessary reservation to keep the OS running smoothly and to cache files from disk.

2. Tuning PHP-FPM for a Heavy Application (4 GB Budget)

We'll tune PHP-FPM for our 4 GB budget, assuming each heavy process uses ~100 MB of RAM.

A. Tune the PHP-FPM Process Manager

File: /etc/php-fpm.d/www.conf

Goal: Prioritize stability by limiting the number of memory-intensive processes.

Calculation: 4096 MB / 100 MB per process ≈ 40 processes

; /etc/php-fpm.d/www.conf

pm = dynamic

; CRITICAL: Max processes, recalculated for a heavy app on an 8GB server.
pm.max_children = 40

; The number of servers to start with.
pm.start_servers = 10

; The minimum number of idle "ready" processes.
pm.min_spare_servers = 5

; The maximum number of idle processes.
pm.max_spare_servers = 15

; Recycle processes more frequently on a smaller server to ensure stability.
pm.max_requests = 200

B. Tune PHP Core Settings for a Smaller Server

File: /etc/php.ini

Goal: Set reasonable limits that prevent a single script from consuming too much of our limited RAM.

; /etc/php.ini

; On a smaller server, a lower memory_limit is safer.
; 256M is still sufficient for most heavy tasks but prevents runaway scripts.
memory_limit = 256M

; Heavy tasks need more time. 120 seconds remains a reasonable limit.
max_execution_time = 120

; Reduce upload sizes slightly to conserve memory during file processing.
upload_max_filesize = 64M
post_max_size = 64M

; Set this to your server's timezone.
date.timezone = Asia/Kuala_Lumpur

3. Tuning Apache httpd (2 GB Budget)

Apache is tuned for its 2 GB budget. We assume a lightweight event MPM process uses ~20 MB of RAM.

File: /etc/httpd/conf.modules.d/00-mpm.conf

Goal: Handle a respectable number of connections without exhausting memory.

Calculation: 2048 MB / 20 MB per process ≈ 100 processes

# /etc/httpd/conf.modules.d/00-mpm.conf

<IfModule mpm_event_module>
    StartServers             2
    MinSpareThreads          25
    MaxSpareThreads          75
    ServerLimit              8
    ThreadsPerChild          25

    # CRITICAL: Max simultaneous connections, calculated for a 2GB RAM budget.
    MaxRequestWorkers        100

    # Recycle child processes to prevent memory leaks over time.
    MaxConnectionsPerChild   2000
</IfModule>

4. Applying Configuration and Verifying

  1. Test Apache Configuration Syntax:

    sudo apachectl configtest

    This command must return Syntax OK before you proceed.

  2. Start and Enable Services:

    # Start and enable PHP-FPM
    sudo systemctl start php-fpm
    sudo systemctl enable php-fpm
    
    # Restart Apache to apply all changes
    sudo systemctl restart httpd
  3. Verify the Setup:

    • Create a test file to confirm PHP is being handled by FPM.
    echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
    • Navigate to http://your-server-ip/info.php in a browser.
    • Look for the Server API line. It must say FPM/FastCGI.
    • For security, delete the test file when you are done:
    sudo rm /var/www/html/info.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment