Creating a Docker Image for PHP Stress Testing with Nginx
This guide details the steps to create a Docker image optimized for running PHP applications with Nginx. The provided configuration ensures compatibility with PHP 8.2 and includes essential settings for handling stress testing.
Nginx Configuration
The Nginx configuration (default
) is set up to serve PHP files and handle common web server scenarios. Below is the configuration file:
server {
listen 80;
server_name _;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Dockerfile
The Dockerfile builds an image with the following characteristics:
- Base image:
ubuntu:latest
- Installed software: PHP 8.2, Nginx, and essential PHP extensions.
- Configured to serve files from
/var/www/html
. - Includes
php8.2-fpm
for PHP processing and a properly configured Nginx server.
Here is the Dockerfile:
# Start from Ubuntu base image
FROM ubuntu:latest
# Update package list and install necessary dependencies
RUN apt-get update && \
apt-get install -y curl gnupg2 ca-certificates lsb-release software-properties-common git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install Nginx
RUN apt-get update && \
apt-get install -y nginx && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install PHP 8.2 and basic extensions
RUN add-apt-repository ppa:ondrej/php && \
apt-get update && \
apt-get install -y php8.2 php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-gd php8.2-zip php8.2-redis && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Copy Nginx Config
COPY default /etc/nginx/sites-available/default
# Set working directory
WORKDIR /var/www/html
# Copy application code
RUN rm -rf /var/www/html/*
COPY . /var/www/html/
RUN chown -R www-data:www-data /var/www/html/
# Expose ports
EXPOSE 80
# Start Nginx and PHP-FPM in the foreground
CMD service php8.2-fpm start && nginx -g 'daemon off;'
Building the Docker Image
-
Place the
Dockerfile
and thedefault
Nginx configuration file in the root of your application directory. -
Run the following command to build the Docker image:
docker build -t stress-php-nginx .
-
Once the image is built, you can run a container using:
docker run -p 8080:80 stress-php-nginx
Access the application at
http://localhost:8080
.
Conclusion
This Docker image setup allows you to deploy a PHP application with Nginx seamlessly. It ensures compatibility with modern PHP versions and provides a robust environment for stress testing or general application hosting. For more resources, visit docs.ahmadraza.in (opens in a new tab).