Install PHP & Apache on Linux: A Fast, Step-by-Step Guide

Install PHP & Apache on Linux: A Fast, Step-by-Step Guide

Want to install PHP and Apache on your Linux server quickly and correctly? This fast, step-by-step guide walks you through the essential commands, configuration tips, and performance tweaks to get a reliable Apache + PHP stack running for everything from blogs to high‑traffic apps.

Setting up a reliable web stack on Linux often begins with installing Apache and PHP. Whether you’re deploying a simple blog, a high-traffic web application, or serving APIs for internal services, a correctly configured Apache + PHP environment is foundational. This guide provides a fast, step-by-step walkthrough with practical commands, configuration tips, performance considerations, and buying advice for choosing a VPS to host your stack.

Why Apache + PHP still matters

Apache remains a ubiquitous web server due to its stability, extensive module ecosystem, and compatibility with many PHP applications (notably WordPress, Drupal, and legacy PHP apps). PHP is still the dominant language for server-side web development, and modern PHP (7.4, 8.0, 8.1, 8.2+) offers significant performance and language improvements.

Combining Apache with PHP gives you:

  • Wide compatibility with popular CMS and frameworks.
  • Flexible deployment options (mod_php, PHP-FPM + mod_proxy_fcgi).
  • Extensive modules and configuration for security, caching, and logging.

Prerequisites and environment assumptions

This guide assumes you are using a Debian/Ubuntu or RHEL/CentOS-based Linux distribution on a VPS or dedicated server. Commands will vary slightly across distros. You need a user with sudo privileges and basic familiarity with the shell. For production, choose a server with adequate RAM and CPU—PHP-FPM prefers at least 1–2 GB RAM for moderate traffic.

Core concepts: how Apache and PHP interact

There are two common ways Apache runs PHP:

  • mod_php: PHP runs as an Apache module. Simpler to set up, but less efficient for high concurrency and less flexible for user isolation.
  • PHP-FPM (FastCGI Process Manager): PHP runs as a separate service, and Apache forwards requests via mod_proxy_fcgi. This is recommended for performance, process isolation, and tuning.

For new deployments, choose PHP-FPM + Apache (with the proxy_fcgi module). It provides better resource control and works well with opcode caches like OPcache.

Step-by-step installation (Debian/Ubuntu)

Below are quick commands to install and enable Apache and PHP-FPM on Debian/Ubuntu. Replace php8.1 with your desired PHP version.

Update package lists: sudo apt update

Install Apache: sudo apt install -y apache2

Install PHP-FPM and common extensions: sudo apt install -y php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl php8.1-xml php8.1-mbstring php8.1-zip

Enable required Apache modules and restart: sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.1-fpm && sudo systemctl restart apache2

Confirm PHP-FPM running: sudo systemctl status php8.1-fpm

Place a test PHP file at /var/www/html/info.php with content <?php phpinfo(); ?> and visit http://your-server-ip/info.php to confirm PHP is served through Apache.

Step-by-step installation (RHEL/CentOS/AlmaLinux)

On RHEL-based systems, use yum/dnf and EPEL/Remi repositories to get recent PHP versions.

Install EPEL & Remi, enable PHP module, and install Apache (httpd) and PHP-FPM. Example sequence:

sudo dnf install -y epel-release

sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

sudo dnf module reset php

sudo dnf module enable php:remi-8.1

sudo dnf install -y httpd php-fpm php-mysqlnd php-curl php-xml php-mbstring php-zip

Enable and start services: sudo systemctl enable –now httpd php-fpm

Configure Apache to proxy PHP requests to php-fpm by enabling the relevant configuration (often handled by the distro package) and restart httpd.

Essential configuration details

Apache virtual hosts

Create an Apache virtual host for each site instead of placing files in the default root. Example key directives:

  • ServerName and ServerAlias to identify the site.
  • DocumentRoot pointing to /var/www/example.com/public_html.
  • Directory rules with AllowOverride and proper permissions for .htaccess if used.

Remember to set correct ownership (www-data on Debian/Ubuntu or apache on RHEL) and tighten permissions (e.g., directories 755, files 644).

PHP-FPM pool tuning

Edit the pool configuration (e.g., /etc/php/8.1/fpm/pool.d/www.conf) to set:

  • pm = dynamic or ondemand — dynamic is common, ondemand saves RAM on low-traffic sites.
  • pm.max_children — max concurrent PHP processes; calculate as available RAM / average PHP memory per process.
  • pm.start_servers, pm.min_spare_servers, pm.max_spare_servers for dynamic mode.

Example: if average process uses 40 MB and you have 1 GB free for PHP, pm.max_children ≈ 25. Always keep headroom for OS and other services.

OPcache and performance

Enable OPcache (bundled with PHP) to cache compiled bytecode and reduce CPU and I/O. Typical php.ini settings:

  • opcache.enable=1
  • opcache.memory_consumption=128
  • opcache.max_accelerated_files=10000

Also consider HTTP-level caching with Varnish or reverse-proxy caching, and a persistent object cache (Redis or Memcached) for applications like WordPress.

Security best practices

Security is crucial for public-facing servers. Key recommendations:

  • Keep software up to date: apply security patches for OS, Apache, and PHP promptly.
  • Run PHP-FPM as an unprivileged user and use separate pools per site for isolation.
  • Disable unused PHP functions (e.g., exec, shell_exec) in php.ini if not required.
  • Use HTTPS with certificates from Let’s Encrypt; enable HSTS and strong TLS ciphers.
  • Limit directory access with proper Require / AllowOverride configuration and avoid world-writable directories.

Common application scenarios and recommended setups

Single WordPress site

For a single WordPress instance on moderate traffic, a small VPS with PHP-FPM, MariaDB, and OPcache is sufficient. Configure PHP-FPM with conservative pm.max_children and enable object caching (Redis). Use a WordPress cache plugin and a CDN for static assets.

Multiple small sites / shared hosting

Use separate PHP-FPM pools per site for process isolation and separate system users. Consider a control panel (or custom scripts) to manage vhosts and permissions. Monitor memory to avoid overcommitment.

High-traffic dynamic application

Scale horizontally: use a load balancer (HAProxy or cloud LB) with multiple Apache/PHP-FPM backends, a dedicated database server, and a shared object cache. Offload static files to a CDN or object storage. Optimize PHP with OPcache and tune FPM pools for high concurrency.

Advantages and trade-offs versus alternatives

Apache + PHP-FPM pros:

  • Proven stability and feature-rich modules (mod_rewrite, mod_security).
  • Good compatibility with PHP applications and .htaccess support.
  • Flexible configuration and per-site isolation via PHP-FPM pools.

Potential cons and alternatives:

  • Nginx + PHP-FPM typically offers slightly better performance for static file serving and lower memory footprint when configured as a reverse proxy.
  • Lighttpd and other lightweight servers exist for constrained environments.
  • If you need async PHP handling or long-running workers, combine with a queue system (RabbitMQ, Redis queues) and decouple web requests from heavy jobs.

Choose Apache when you need its module ecosystem, .htaccess compatibility, or are migrating legacy apps. Choose Nginx for leaner static serving and slightly improved concurrency with fewer resources.

Monitoring, logging and troubleshooting

Monitor key metrics: CPU, memory, disk I/O, response times, PHP-FPM process usage, and slow logs (PHP-FPM slowlog and Apache access/error logs). Useful tools include top/htop, vmstat, iostat, and application performance tools like New Relic or open-source alternatives (Prometheus + Grafana).

If PHP pages return 502/504 errors, check PHP-FPM is running and that Apache’s proxy configuration matches the php-fpm socket or port. For permission problems, verify file ownership and SELinux contexts on RHEL systems.

Choosing a VPS for hosting Apache + PHP

When selecting a VPS, prioritize:

  • CPU performance: single-thread speed matters for PHP execution.
  • RAM: enough memory to run PHP-FPM pools, database, and caching layers.
  • Disk type: SSD or NVMe for fast I/O; prefer dedicated IOPS for databases.
  • Network throughput: important for serving assets and handling concurrent visitors.
  • Managed backups, snapshots, and an easy OS/recovery workflow.

For US-based projects or users targeting American audiences, consider a reliable provider with US VPS locations for lower latency. For a production-ready entry, a provider that offers flexible scaling and predictable pricing reduces operational friction.

Summary

Installing Apache and PHP on Linux is straightforward, but building a performant, secure, and maintainable environment requires attention to configuration: use PHP-FPM with well-tuned pools, enable OPcache, secure PHP and Apache settings, and plan for monitoring and scaling. For most modern deployments, Apache + PHP-FPM provides a balanced mix of compatibility and performance. Choose a VPS with sufficient CPU, RAM, and fast storage to match your workload and traffic expectations.

If you’re ready to deploy, consider starting with a reliable provider that offers flexible US-based VPS options—see VPS.DO’s USA VPS offerings at https://vps.do/usa/ and general hosting plans at https://vps.do/. These plans are convenient for quickly provisioning a Linux server to host Apache and PHP with predictable performance and support.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!