# LAPP Stack (Linux, Apache, PostgreSQL, PHP)

> LAPP stack for PHP applications with PostgreSQL.

- Category: Full Web Stacks
- Difficulty: intermediate
- Components: apache, php, postgresql, pgadmin
- Tags: lapp, apache, php, postgresql
- Source: https://docker.recipes/full-stacks/apache-php-postgres-stack

## Overview

Apache HTTP Server stands as the world's most widely-used web server software, powering over 30% of all websites globally since its release in 1995. Built from the original NCSA HTTPd server, Apache revolutionized web hosting with its modular architecture, robust security features, and cross-platform compatibility. Its mod_php module enables direct PHP processing within the Apache process, making it a natural choice for PHP-driven applications that require reliable performance and extensive configuration flexibility.

This LAPP stack combines Apache's proven web serving capabilities with PHP 8.2's modern language features and PostgreSQL's advanced relational database engine. Unlike LAMP stacks that rely on MySQL, this configuration leverages PostgreSQL's superior JSON support, ACID compliance, and complex query capabilities, making it ideal for applications requiring both relational integrity and document-style data handling. The stack processes HTTP requests through Apache, executes PHP business logic with full access to PostgreSQL's advanced features like window functions, CTEs, and full-text search.

Developers building content management systems, e-commerce platforms, or data-driven web applications will find this stack particularly valuable when their projects demand PostgreSQL's advanced features over MySQL's simpler approach. The inclusion of pgAdmin provides comprehensive database administration through a web interface, eliminating the need for separate database management tools and enabling efficient development workflows for teams working with complex PostgreSQL schemas and queries.

## Features

- Apache mod_php integration for high-performance PHP execution within the web server process
- PostgreSQL 15 with advanced JSON/JSONB support for hybrid relational-document data models
- pgAdmin 4 web interface with visual query builder and PostgreSQL-specific administration tools
- PHP 8.2 with modern language features including union types, readonly properties, and match expressions
- PostgreSQL full-text search capabilities with ranking and highlighting for content applications
- Apache virtual host support for multi-domain configurations and flexible URL routing
- PostgreSQL table partitioning and logical replication for scalable data architectures
- pgAdmin dashboard statistics and query analysis tools for database performance optimization

## Use cases

- E-commerce platforms requiring complex product catalogs with PostgreSQL's advanced indexing and JSON attributes
- Content management systems leveraging PostgreSQL's full-text search and Apache's mod_rewrite for SEO-friendly URLs
- Business intelligence dashboards utilizing PostgreSQL's window functions and Apache's reliable request handling
- Multi-tenant SaaS applications using PostgreSQL's row-level security and Apache's virtual host configurations
- GIS-enabled web applications combining PostGIS extensions with PHP mapping libraries
- Data analytics platforms processing both structured PostgreSQL data and unstructured JSON documents
- Legacy PHP application modernization projects migrating from MySQL to PostgreSQL's superior feature set

## Prerequisites

- Minimum 2GB RAM available (1GB for PostgreSQL, 512MB for pgAdmin, 512MB for Apache/PHP processes)
- Docker Engine 20.10+ and Docker Compose V2 for proper container orchestration support
- Ports 80 and 8080 available on host system for Apache and pgAdmin web interfaces
- Basic understanding of PostgreSQL concepts like schemas, roles, and ACID transactions
- Familiarity with Apache configuration files and PHP module management
- Knowledge of environment variable configuration for database credentials and service settings

## docker-compose.yml

```yaml
services:
  apache:
    image: php:8.2-apache
    container_name: lapp-apache
    restart: unless-stopped
    ports:
      - "${APACHE_PORT:-80}:80"
    volumes:
      - ${APP_PATH:-./app}:/var/www/html
    depends_on:
      - postgres

  postgres:
    image: postgres:15-alpine
    container_name: lapp-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
    volumes:
      - postgres_data:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: lapp-pgadmin
    restart: unless-stopped
    ports:
      - "${PGADMIN_PORT:-8080}:80"
    environment:
      - PGADMIN_DEFAULT_EMAIL=${PGADMIN_EMAIL}
      - PGADMIN_DEFAULT_PASSWORD=${PGADMIN_PASSWORD}
    depends_on:
      - postgres

volumes:
  postgres_data:
```

## .env

```bash
# LAPP Stack
APACHE_PORT=80
APP_PATH=./app
DB_USER=app
DB_PASSWORD=app_password
DB_NAME=app
PGADMIN_PORT=8080
PGADMIN_EMAIL=admin@example.com
PGADMIN_PASSWORD=admin
```

## Usage notes

- App at http://localhost
- pgAdmin at http://localhost:8080
- Place PHP files in ./app
- PostgreSQL for database

## Troubleshooting

- Apache returns 500 Internal Server Error: Check that PHP files have proper syntax and PostgreSQL connection credentials are correct in environment variables
- pgAdmin shows 'server not found' error: Verify postgres container is running and use 'lapp-postgres' as hostname in pgAdmin server configuration
- PostgreSQL connection refused from PHP: Ensure DB_USER, DB_PASSWORD, and DB_NAME environment variables match PostgreSQL container settings
- Apache serves PHP files as plain text: Confirm php:8.2-apache image includes mod_php and .htaccess files don't override PHP handling
- pgAdmin login fails with authentication error: Double-check PGADMIN_DEFAULT_EMAIL and PGLADMIN_DEFAULT_PASSWORD environment variables are properly set
- PostgreSQL data disappears after container restart: Verify postgres_data volume is properly mounted and not being deleted during container recreation

