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.
Start with the official Ubuntu 20.04 base image.
Configure the environment to be non-interactive to avoid prompts during package installation.
Define the working directory as /var/www/html/petradevs
.
Update the package list and install required packages, including Apache, PHP, and various PHP modules.
Use pecl
to install the MongoDB PHP extension.
Enable necessary Apache modules like rewrite
and ssl
.
Copy and set permissions for the entrypoint script.
Generate self-signed SSL certificates and Diffie-Hellman parameters for secure communication.
Copy the custom Apache configuration file.
Ensure the /tmp
directory is writable.
Install Composer using the official Composer Docker image.
Copy application files and set permissions.
Install Composer dependencies for the Laravel application.
Set appropriate permissions for Laravel storage and cache directories.
Expose ports 80 and 443 for HTTP and HTTPS traffic.
Start the Apache server in the foreground.
Run Laravel Artisan commands to clear and cache the configuration, and set appropriate permissions.
# 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
WORKDIR /var/www/html/petradevs
# 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/*
RUN pecl install mongodb
# Enable Apache modules
RUN a2ensite 000-default.conf
RUN a2enmod rewrite
RUN a2enmod ssl
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
# 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
# Copy your Apache configuration file
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf
# Ensure the temporary directory is writable
RUN chmod -R 777 /tmp
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# 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
RUN composer install --working-dir=/var/www/html/petradevs
# 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 port 80
EXPOSE 80 443
# Start Apache server
CMD ["apache2ctl", "-D", "FOREGROUND"]
# 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