Dockerizing PHP 8.1 and Nginx for Drupal: A Step-by-Step Guide
Dockerizing your web application is becoming increasingly popular due to the numerous benefits it offers, including scalability, portability, and ease of deployment. If you're looking to Dockerize your PHP 8.1 and Nginx environment for Drupal, you're in luck. In this article, we'll walk you through the process step-by-step, providing you with a scalable and efficient infrastructure that can improve your website's performance and user experience.
Firstly, we'll create a new directory for our project and a Docker-comopse file that will contain all the necessary services and Dockerfile php image requirement, We'll install required PHP extensions, Change the default configuration of nginx server.
Next, we'll use composer to install Drupal and set the working directory to the Drupal root directory. Finally, we'll expose port 80 and start supervisord to run Nginx and PHP-FPM.
By following this guide, you'll have a Dockerized PHP 8.1 and Nginx environment for Drupal that is easily scalable and deployable, making it easier to maintain and optimize your website. Dockerizing your web application is a great way to improve its performance, reduce its downtime, and ensure its availability to your users.
In conclusion, Dockerizing PHP 8.1 and Nginx for Drupal is a straightforward process that can provide numerous benefits to your website. By following this step-by-step guide, you'll have a scalable and efficient infrastructure that can enhance your website's performance and user experience.
Create a directory for your project
$ mkdir my-drupal-project
$ cd my-drupal-project
Create Dockerfile for php image
$ touch Dockerfile
Open the Dockerfile in your preferred text editor and add the following content:
# Dockerfile
# Base image
FROM php:8.1-fpm
ARG user
ARG uid
# Install dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
zip \
unzip \
libonig-dev \
libxml2-dev \
libzip-dev \
libpq-dev \
libpng-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath xml zip gd
# Copy the Laravel project to the working directory
# COPY . /var/www/html
# Set working directory
WORKDIR /usr/share/nginx/html
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
USER $user
CMD ['composer install']
# Expose port 9000 for PHP-FPM
EXPOSE 9000
# Start PHP-FPM
CMD ["php-fpm"]
Create a new file named nginx.conf in the project directory and add the following content:
server {
listen 80;
server_name drupal.local;
root /usr/share/nginx/html/web;
index index.php index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Create a new file named docker-compose.yml in the project directory and add the following content:
version: "3.8"
services:
app:
container_name: drupal
restart: unless-stopped
build:
args:
user: webserver
uid: '1000'
context: .
dockerfile: Dockerfile
tty: true
volumes:
- ./drupal/:/usr/share/nginx/html
depends_on:
- db
networks:
- mynet
db:
container_name: drupal_mysql
image: mysql:5.7
environment:
MYSQL_DATABASE: db_drupal
MYSQL_ROOT_PASSWORD: root
restart: always
volumes:
- dbdata:/var/lib/mysql
networks:
- mynet
web:
container_name: drupal_nginx
image: nginx:latest
volumes:
- ./drupal/:/usr/share/nginx/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
depends_on:
- app
networks:
- mynet
# Database management
pma:
image: phpmyadmin:5.1
environment:
- PMA_HOST=drupal_mysql
depends_on:
- db
restart: always
ports:
- 8888:80
networks:
- mynet
volumes:
dbdata:
networks:
mynet:
driver: bridge
from the root of your project create a directory for drupal instance
$ mkdir drupal
Run the Docker containers using the following command:
$ docker-compose up -d
Navigate to php docker container shell and run following command to install drupal using composer:
$ docker exec -it drupal bash
Finally install drupal using composer command
$ composer create-project drupal/recommended-project .
Now it Done, navigate to link http://localhost:8000/