Ultimate Guide to Setting Up PHP Development Environment with Apache on Ubuntu 20.04

This guide provides detailed instructions on how to set up a complete PHP development environment using Apache on Ubuntu 20.04. It covers the installation of necessary dependencies, Apache configuration, enabling SSL, and setting up Laravel with Composer.

Step-by-Step Instructions

  1. Base Image Selection

    Start with the official Ubuntu 20.04 base image.

  2. Set Non-Interactive Environment

    Configure the environment to be non-interactive to avoid prompts during package installation.

  3. Set Working Directory

    Define the working directory as /var/www/html/petradevs.

  4. Install Dependencies

    Update the package list and install required packages, including Apache, PHP, and various PHP modules.

  5. Install MongoDB PHP Extension

    Use pecl to install the MongoDB PHP extension.

  6. Enable Apache Modules

    Enable necessary Apache modules like rewrite and ssl.

  7. Entrypoint Script

    Copy and set permissions for the entrypoint script.

  8. Generate Self-Signed Certificates

    Generate self-signed SSL certificates and Diffie-Hellman parameters for secure communication.

  9. Apache Configuration

    Copy the custom Apache configuration file.

  10. Temporary Directory Permissions

    Ensure the /tmp directory is writable.

  11. Install Composer

    Install Composer using the official Composer Docker image.

  12. Copy Application Files

    Copy application files and set permissions.

  13. Install Composer Dependencies

    Install Composer dependencies for the Laravel application.

  14. Set Permissions

    Set appropriate permissions for Laravel storage and cache directories.

  15. Expose Ports

    Expose ports 80 and 443 for HTTP and HTTPS traffic.

  16. Start Apache

    Start the Apache server in the foreground.

  17. Run Artisan Commands

    Run Laravel Artisan commands to clear and cache the configuration, and set appropriate permissions.

Dockerfile

Base Image and Environment

        
# Use the official PHP image with Apache
# Use the official Ubuntu 20.04 base image
FROM ubuntu:20.04

# Set environment variables to non-interactive
ENV DEBIAN_FRONTEND=noninteractive
        
    

Set Working Directory

        
# Set working directory
WORKDIR /var/www/html/petradevs
        
    

Install Dependencies

        
# Install necessary dependencies
RUN apt-get update && apt-get install -y \
    apache2 \
    php \
    php-dev \
    php-pear \
    libapache2-mod-php \
    php-mongodb \
    php-mbstring \
    php-xml \
    php-dom \
    php-curl \
    nano \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    zip \
    unzip \
    git \
    curl \
    libonig-dev \
    libzip-dev \
    libssl-dev \
    pkg-config \
    certbot \
    python3-certbot-apache \
    && apt-get clean && rm -rf /var/lib/apt/lists/*
        
    

Install MongoDB PHP Extension

        
RUN pecl install mongodb
        
    

Enable Apache Modules

        
# Enable Apache modules
RUN a2ensite 000-default.conf
RUN a2enmod rewrite
RUN a2enmod ssl
        
    

Entrypoint Script

        
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
        
    

Generate Self-Signed Certificates

        
# Generate self-signed certificates
RUN mkdir -p /etc/ssl/certs && mkdir -p /etc/ssl/private && \
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/selfsigned.key -out /etc/ssl/certs/selfsigned.crt -subj "/C=US/ST=California/L=San Francisco/O=Example/OU=IT Department/CN=petradevs.org" && \
    openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
        
    

Apache Configuration

        
# Copy your Apache configuration file
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf
        
    

Temporary Directory Permissions

        
# Ensure the temporary directory is writable
RUN chmod -R 777 /tmp
        
    

Install Composer

        
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
        
    

Copy Application Files

        
# Copy existing application directory contents
COPY . /var/www/html/petradevs

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www/html/petradevs
        
    

Install Composer Dependencies

        
# Install Composer dependencies
RUN composer install --working-dir=/var/www/html/petradevs
        
    

Set Permissions

        
# Set permissions for Laravel
RUN chown -R www-data:www-data /var/www/html/petradevs/storage /var/www/html/petradevs/bootstrap/cache

# Set permissions for Laravel storage directory
RUN chmod -R 777 /var/www/html/petradevs/storage /var/www/html/petradevs/storage/framework /var/www/html/petradevs/storage/logs
        
    

Expose Ports

        
# Expose port 80
EXPOSE 80 443
        
    

Start Apache

        
# Start Apache server
CMD ["apache2ctl", "-D", "FOREGROUND"]
        
    

Run Artisan Commands

        
# Run artisan commands to cache config and routes
RUN php artisan config:clear && php artisan config:cache
RUN chmod -R 777 /var/www/html/petradevs/bootstrap/cache/config.php
        
    

Published By: Krishanu Jadiya
Updated at: 2024-06-29 13:44:49

Card Image

How to Set Up a Local SSL Certificate on Apache: Step-by-Step Guide

Learn how to set up a local SSL certificate on Apache with this comprehensive step-by-step guide. Secure your local development environment with HTTPS.

Card Image

Latest Features of Coding Technology

Explore the latest features and advancements in coding technology, including new programming languages, frameworks, DevOps tools, AI integration, and more.

Card Image

Understanding Laravel Mix Webpack Configuration: Step-by-Step Guide

Step-by-step explanation of a Laravel Mix Webpack configuration file, including asset management for JavaScript, CSS, and Vue.js support.

Card Image

How Emojis Can Enhance Your Git Commits | Gitmoji Guide

Discover how to enhance your Git commits with emojis. Learn about the best practices for creating informative and visually distinctive commit messages.