Home  >  Article  >  Backend Development  >  The combination of Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications

The combination of Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications

WBOY
WBOYOriginal
2023-10-12 14:14:031689browse

Docker Compose、Nginx和MariaDB的搭配之道:高效运维PHP应用程序

How to match Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications

Introduction:

In today's Internet era, applications The demand and scale of the network continue to grow, so efficient operation and maintenance and deployment solutions are crucial. Docker is a popular containerization platform that solves the complexities of application deployment and management. Docker Compose is a powerful tool of Docker that allows us to define and manage a combination of multiple containers through a simple configuration file. In this article, we will focus on how to use Docker Compose with Nginx and MariaDB to efficiently operate and maintain PHP applications, and provide specific code examples.

1. Introduction to Docker Compose

Docker Compose is a tool officially launched by Docker for defining and managing multiple Docker containers. Through a simple YAML file, you can easily define multiple containers, dependencies between containers, network configuration, etc.

Benefits of using Docker Compose include:

  1. Simplified deployment process: By defining configuration files, the entire application can be deployed quickly and consistently.
  2. Improve scalability: Applications can be easily scaled across multiple hosts.
  3. Easy to manage and monitor: You can start, stop, restart and view logs of containers through Docker Compose commands.

2. Use of Nginx

Nginx is a lightweight, high-performance web server that can be used as a reverse proxy server, load balancing server, static resource server, etc. use. In our PHP application, we can use Nginx as a reverse proxy server to forward requests to the PHP-FPM server on the backend.

The following is a sample configuration file (docker-compose.yml) using Docker Compose and Nginx:

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./public:/var/www/html

  php:
    image: php:7.4-fpm
    volumes:
      - ./public:/var/www/html

networks:
  default:
    external:
      name: my-network

In the above configuration, we define a service named nginx and a name For php services. Among them, the nginx service uses the official nginx image and maps the container's port 80 to the host's port 80. At the same time, we mount the nginx configuration file (nginx.conf) and the public directory where our PHP application is located into the container.

3. Use of MariaDB

MariaDB is a free, open source relational database management system and a branch of MySQL, providing high-performance and high-reliability database solutions. In our PHP application we can use MariaDB as backend database.

The following is a sample configuration file (docker-compose.yml) using Docker Compose and MariaDB:

version: '3'

services:
  db:
    image: mariadb:latest
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=my_database
      - MYSQL_USER=my_user
      - MYSQL_PASSWORD=my_password
    volumes:
      - ./data:/var/lib/mysql

  php:
    image: php:7.4-fpm
    volumes:
      - ./public:/var/www/html

networks:
  default:
    external:
      name: my-network

In the above configuration, we define a service named db and a service named For php services. Among them, the db service uses the official MariaDB image and maps the container's 3306 port to the host's 3306 port. We also set the relevant configuration of the database through environment variables, such as root password, database name, user and password, etc. At the same time, we mount the database folder into the container to ensure persistent storage of data.

4. Complete sample configuration

The following is a complete sample configuration for efficient operation and maintenance of PHP applications using Docker Compose, Nginx and MariaDB:

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./public:/var/www/html
    depends_on:
      - php
    networks:
      - my-network

  php:
    image: php:7.4-fpm
    volumes:
      - ./public:/var/www/html
    networks:
      - my-network

  db:
    image: mariadb:latest
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=my_database
      - MYSQL_USER=my_user
      - MYSQL_PASSWORD=my_password
    volumes:
      - ./data:/var/lib/mysql
    networks:
      - my-network

networks:
  my-network:
    external: true

In the above configuration, we have defined a service named nginx, a service named php and a service named db. At the same time, we also defined an external network named my-network to connect these three services. The nginx service depends on the php service, so we use the depends_on keyword to specify this dependency.

5. Summary

The above are methods and specific code examples for using Docker Compose, Nginx and MariaDB to efficiently operate and maintain PHP applications. By using the Docker Compose tool, we can quickly build and manage applications containing multiple containers, improving deployment efficiency and operation and maintenance convenience. As a high-performance web server, Nginx can help us implement functions such as reverse proxy and load balancing. MariaDB, as a reliable database management system, provides high-performance and high-availability data storage solutions for our applications.

I hope this article will help you understand and use Docker Compose, Nginx and MariaDB. I wish you better results and experience in the process of operating and maintaining PHP applications!

The above is the detailed content of The combination of Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications. 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