Install and configure PHP 8.4 with Apache and security hardening

Beginner 25 min Apr 03, 2026 24 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Set up PHP 8.4 with Apache web server using PHP-FPM for optimal performance and security. Learn to configure essential PHP modules, implement security hardening measures, and optimize your LAMP stack for production environments.

Prerequisites

  • Root or sudo access
  • Basic Linux command line knowledge
  • Server with at least 1GB RAM

What this solves

PHP 8.4 brings significant performance improvements and new features that make your web applications faster and more secure. This tutorial shows you how to install PHP 8.4 with Apache web server, configure PHP-FPM for better resource management, and implement security hardening to protect your server from common vulnerabilities.

Step-by-step installation

Update system packages

Start by updating your system to ensure you have access to the latest package repositories and security patches.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Apache web server

Install Apache HTTP server which will serve your PHP applications and handle web requests.

sudo apt install -y apache2 apache2-utils
sudo dnf install -y httpd httpd-tools

Enable and start Apache service

Enable Apache to start automatically on boot and start the service immediately.

sudo systemctl enable --now apache2
sudo systemctl status apache2
sudo systemctl enable --now httpd
sudo systemctl status httpd

Add PHP 8.4 repository

Add the official PHP repository to access PHP 8.4 packages, as they may not be available in default repositories.

sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo dnf install -y epel-release
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module enable php:remi-8.4 -y

Install PHP 8.4 and essential modules

Install PHP 8.4 with commonly needed modules for web development, database connectivity, and security features.

sudo apt install -y php8.4 php8.4-cli php8.4-common php8.4-fpm php8.4-mysql php8.4-pgsql php8.4-sqlite3 php8.4-curl php8.4-gd php8.4-mbstring php8.4-xml php8.4-zip php8.4-bcmath php8.4-intl php8.4-redis php8.4-memcached
sudo dnf install -y php84 php84-php-cli php84-php-common php84-php-fpm php84-php-mysqlnd php84-php-pgsql php84-php-pdo php84-php-curl php84-php-gd php84-php-mbstring php84-php-xml php84-php-zip php84-php-bcmath php84-php-intl php84-php-redis php84-php-memcached

Configure PHP-FPM for Apache

Enable and configure PHP-FPM (FastCGI Process Manager) for better performance and resource isolation compared to mod_php.

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.4-fpm
sudo systemctl enable --now php8.4-fpm
sudo systemctl enable --now php84-php-fpm
sudo setsebool -P httpd_execmem 1

Configure PHP security settings

Modify PHP configuration to implement security best practices and disable dangerous functions.

; Security hardening
expose_php = Off
allow_url_fopen = Off
allow_url_include = Off
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
max_execution_time = 30
max_input_time = 30
memory_limit = 128M
post_max_size = 8M
upload_max_filesize = 8M
max_file_uploads = 20

; Error handling (for production)
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/log/php_errors.log

; Session security
session.cookie_httponly = 1
session.cookie_secure = 1
session.use_strict_mode = 1

Configure PHP-FPM pool settings

Optimize PHP-FPM pool configuration for better performance and security isolation.

[www]
user = www-data
group = www-data
listen = /run/php/php8.4-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

; Process management
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

; Security
security.limit_extensions = .php
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
[www]
user = apache
group = apache
listen = /var/opt/remi/php84/run/php-fpm/www.sock
listen.owner = apache
listen.group = apache
listen.mode = 0660

; Process management
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

; Security
security.limit_extensions = .php
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off

Create Apache virtual host for PHP

Set up a virtual host configuration that properly integrates with PHP-FPM and includes security headers.


    ServerName example.com
    DocumentRoot /var/www/html
    
    # PHP-FPM configuration
    
        SetHandler "proxy:unix:/run/php/php8.4-fpm.sock|fcgi://localhost"
    
    
    # Security headers
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"
    
    # Directory security
    
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    
    
    ErrorLog ${APACHE_LOG_DIR}/php-error.log
    CustomLog ${APACHE_LOG_DIR}/php-access.log combined

    ServerName example.com
    DocumentRoot /var/www/html
    
    # PHP-FPM configuration
    
        SetHandler "proxy:unix:/var/opt/remi/php84/run/php-fpm/www.sock|fcgi://localhost"
    
    
    # Security headers
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"
    
    # Directory security
    
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    
    
    ErrorLog /var/log/httpd/php-error.log
    CustomLog /var/log/httpd/php-access.log combined

Enable Apache modules and site

Enable required Apache modules for security headers and activate the PHP site configuration.

sudo a2enmod headers rewrite
sudo a2ensite php-site.conf
sudo a2dissite 000-default.conf
sudo systemctl reload httpd

Set correct permissions for web directory

Configure proper ownership and permissions for the web root directory. Never use chmod 777 as it compromises security.

Never use chmod 777. It gives every user on the system full access to your files. Instead, fix ownership with chown and use minimal permissions.
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
sudo find /var/www/html -type f -exec chmod 644 {} \;

Create PHP test file

Create a simple PHP file to test your installation and view PHP configuration details.

Restart services

Restart Apache and PHP-FPM to apply all configuration changes.

sudo systemctl restart apache2
sudo systemctl restart php8.4-fpm
sudo systemctl restart httpd
sudo systemctl restart php84-php-fpm

Configure firewall rules

Allow HTTP and HTTPS traffic through the firewall for web access.

sudo ufw allow 'Apache Full'
sudo ufw status
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Verify your setup

Test your PHP installation and verify that all services are running correctly.

php -v
sudo systemctl status apache2
sudo systemctl status php8.4-fpm
curl -I http://localhost/info.php

Visit http://your-server-ip/info.php in your browser to see the PHP configuration page. Remove this file after testing for security.

sudo rm /var/www/html/info.php

Performance optimization

Install and configure OPcache

Enable PHP OPcache for significant performance improvements by caching compiled PHP code.

opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.save_comments=1
opcache.validate_timestamps=1

Configure Apache for better performance

Optimize Apache settings for handling concurrent connections and reduce memory usage.


    StartServers             2
    MinSpareThreads          25
    MaxSpareThreads          75
    ThreadLimit              64
    ThreadsPerChild          25
    MaxRequestWorkers        400
    MaxConnectionsPerChild   1000

Common issues

SymptomCauseFix
PHP files download instead of executingPHP handler not configuredCheck Apache PHP-FPM configuration and restart services
Permission denied errorsIncorrect file ownershipRun sudo chown -R www-data:www-data /var/www/html
PHP-FPM socket connection refusedSocket permissions or path mismatchVerify socket path in both PHP-FPM and Apache configs
High memory usageIncorrect PHP-FPM process settingsAdjust pm.max_children and memory_limit values
Slow page loadingOPcache not enabledInstall and configure OPcache extension

Next steps

Automated install script

Run this to automate the entire setup

#php #apache #php-fpm #lamp-stack #security

Need help?

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.

Talk to an engineer