Home  >  Article  >  Backend Development  >  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 Architectural Design

WBOY
WBOYOriginal
2023-10-12 11:33:11757browse

Docker Compose、Nginx和MariaDB的最佳实践:高可用PHP应用程序架构设计

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:

  1. 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.
  2. Scalability: Able to dynamically scale the capacity of the application according to demand to cope with sudden high concurrent access.
  3. Performance Optimization: Improve application performance and response speed by using Nginx as a reverse proxy server and load balancer.
  4. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn