


Best Practices for Docker Compose, Nginx, and MariaDB: Highly Available PHP Application Architectural Design
Best Practices for Docker Compose, Nginx and MariaDB: Highly Available PHP Application Architecture Design
Introduction:
In today’s Internet era, building high availability applications are becoming increasingly important. As the number of Internet users increases, application performance, reliability, and scalability become key considerations. This article will introduce how to use Docker Compose, Nginx and MariaDB to design a highly available PHP application architecture, and provide specific code examples.
Part One: Architecture Overview
We want to build a highly available PHP application that needs to meet the following requirements:
- High reliability: able to provide high availability The application can automatically switch to the backup server when the server fails or the network is abnormal.
- Scalability: Able to dynamically scale the capacity of the application according to demand to cope with sudden high concurrent access.
- Performance Optimization: Improve application performance and response speed by using Nginx as a reverse proxy server and load balancer.
- Database Reliability: Use MariaDB as the application's database and ensure data reliability and consistency.
Part 2: Building a Docker Image
Before building a highly available PHP application, we first need to package the application and all dependencies into a Docker image. You can use a Dockerfile to define the build process of a Docker image.
The following is a sample Dockerfile:
# 使用基础PHP镜像 FROM php:7.4-fpm # 安装PHP依赖项 RUN apt-get update && apt-get install -y git zip curl libpng-dev libonig-dev libxml2-dev && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd # 设置工作目录 WORKDIR /var/www/html # 将应用程序复制到工作目录 COPY . . # 安装依赖项 RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer install --no-scripts --no-autoloader # 自动生成Autoload文件 RUN composer dump-autoload --optimize # 修改文件权限 RUN chown -R www-data:www-data /var/www/html/storage # 启动PHP-FPM服务器 CMD ["php-fpm"] # 暴露端口 EXPOSE 9000
Using the above Dockerfile, we can build a Docker image that contains the application and all dependencies. The image can be built using the following command:
docker build -t myapp .
Part 3: Building the architecture using Docker Compose
Next, we will use Docker Compose to define and manage the entire high-availability PHP application architecture. Docker Compose is a tool for defining and running multi-container Docker applications.
The following is an example docker-compose.yaml file:
version: '3' services: app: build: context: . dockerfile: Dockerfile volumes: - .:/var/www/html ports: - 9000 networks: - my-network nginx: image: nginx volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./site.conf:/etc/nginx/conf.d/default.conf ports: - 80:80 depends_on: - app networks: - my-network db: image: mariadb restart: always environment: MYSQL_ROOT_PASSWORD: 'password' networks: - my-network networks: my-network:
In the docker-compose.yaml file of the above example, we define three services: app, nginx and db. Among them, the app service is the PHP application image we built before. The nginx service uses the official image of Nginx. The db service is the official image of MariaDB.
We also defined a network named my-network to connect these three services.
Part 4: Configure Nginx as a reverse proxy server and load balancer
In order to improve the performance and response speed of the application, we will use Nginx as a reverse proxy server and load balancer.
The following is an example nginx.conf file:
user www-data; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log combined; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; include /etc/nginx/conf.d/*.conf; }
In the above example, we specified the configuration of Nginx. Next, we need to define a site configuration for Nginx.
The following is an example site.conf file:
server { listen 80; server_name myapp.example.com; location / { proxy_pass http://app: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; client_max_body_size 100M; proxy_buffering off; proxy_request_buffering off; } }
In the above example, we defined an Nginx virtual host to proxy all HTTP requests to the 9000 port of the app service.
Part 5: Running Architecture
Use the following command to start the highly available PHP application architecture we defined:
docker-compose up -d
Now, our highly available PHP application has successfully run . The usability and performance of the application can be tested by visiting http://myapp.example.com.
Conclusion:
By using Docker Compose, Nginx and MariaDB, we can design and build a highly available PHP application architecture. Docker Compose can help us define and manage multi-container applications, Nginx can provide high-performance reverse proxy and load balancing functions, and MariaDB can provide reliable and stable database services. Through this architecture, we can improve the availability, reliability and performance of applications and meet user needs for high-quality applications.
The above is the detailed content of Best Practices for Docker Compose, Nginx, and MariaDB: Highly Available PHP Application Architectural Design. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).
