Introduction
In this tech support guide, we will walk you through the process of setting up two separate services using PHP-FPM (FastCGI Process Manager) and Nginx (pronounced engine-x) on a single server. The first container will act as the backend admin, while the second container will serve as the frontend, fetching information first from the backend before delivering it to the user.
Creating Containers for PHP-FPM Backend and Nginx Frontend
To get started, we'll create two separate Docker containers: one for the PHP-FPM backend and another for the Nginx frontend.
PHP-FPM Backend
First, let's create a Dockerfile for the PHP-FPM backend:
Dockerfile
FROM php:7.4-fpm
Set the working directory
WORKDIR /var/www/html
Copy the PHP files
COPY . /var/www/html
Next, build the container:
$ docker build -t php-fpm .
Nginx Frontend
Now, let's create a Dockerfile for the Nginx frontend:
Dockerfile
FROM nginx:alpine
Set the working directory
WORKDIR /usr/src/nginx
Copy the Nginx configuration file
COPY nginx.conf /etc/nginx/nginx.conf
Copy the HTML files
COPY . /usr/src/nginx/html
Build the container:
$ docker build -t nginx .
Networking the Containers
To allow the Nginx container to access the PHP-FPM backend, we need to set up a network:
$ docker network create my-network
Now, run the containers using the network:
$ docker run --name php-fpm --network my-network -d php-fpm
$ docker run --name nginx --network my-network -d nginx
Nginx Configuration
Update the Nginx configuration file (nginx.conf) to proxy requests to the PHP-FPM backend:
server {
listen 80;
server_name localhost;
location / {
root /usr/src/nginx/html;
index index.html index.htm;
}
location ~ .php$ {
proxy_pass http://php-fpm:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Testing the Setup
To test the setup, visit http://localhost in your web browser. The Nginx frontend should now be serving content from the PHP-FPM backend.
- References: