Home  >  Article  >  Backend Development  >  Containerize PHP applications using Docker Compose, Nginx, and MariaDB

Containerize PHP applications using Docker Compose, Nginx, and MariaDB

王林
王林Original
2023-10-12 14:40:561291browse

使用Docker Compose、Nginx和MariaDB实现PHP应用程序的容器化

Title: Containerization of PHP applications using Docker Compose, Nginx and MariaDB

Introduction: With the rapid development of containerization technology, more and more Developers are beginning to pay attention to using Docker for application deployment and management. This article will introduce how to use Docker Compose, Nginx and MariaDB to implement containerization of PHP applications, and give specific code examples to help readers better understand and practice.

  1. Introduction to Docker Compose
    Docker Compose is a tool for defining and running multiple Docker containers. It uses YAML files to configure an application's services, network, storage, etc. With Docker Compose, we can more easily define and manage complex multi-container applications.
  2. Nginx introduction
    Nginx is a high-performance HTTP and reverse proxy server, which can be used to serve static content and proxy dynamic content. In containerized applications, Nginx is often used as a front-end proxy server, responsible for accepting client requests and forwarding them to the back-end application service.
  3. MariaDB introduction
    MariaDB is an open source relational database management system based on MySQL. It provides reliable performance and security features and is widely used in web applications and cloud computing environments.
  4. Architecture Design
    Our containerized PHP application consists of three services: Nginx, PHP-FPM and MariaDB. Nginx is used to receive and process the client's HTTP request, then forward the dynamic request to PHP-FPM, and the static request is returned to the client. PHP-FPM is the PHP FastCGI process manager, responsible for processing and interpreting PHP code. MariaDB is used to store application data.

The following is an example of our Docker Compose configuration file (docker-compose.yml):

version: '3'
services:
  webserver:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - php
  php:
    image: php:7.4-fpm
    volumes:
      - ./php.ini:/usr/local/etc/php/php.ini:ro
      - ./code:/var/www/html
    depends_on:
      - db
  db:
    image: mariadb:latest
    environment:
      MYSQL_ROOT_PASSWORD: your_password
      MYSQL_DATABASE: your_database
      MYSQL_USER: your_username
      MYSQL_PASSWORD: your_password
    volumes:
      - ./data:/var/lib/mysql

We have three services defined through this configuration file: webserver, php and db . The webserver service uses the Nginx image and maps port 80 of the host to port 80 of the container. The php service uses the PHP-FPM image and mounts the code directory into the container. The db service uses MariaDB mirroring, and the user name, password and data storage directory of the database are set.

  1. Configuration files and code examples
    We also need to provide the Nginx configuration file (nginx.conf) and the PHP configuration file (php.ini). Examples are given below:

nginx.conf:

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        
        location / {
            root /var/www/html;
            index index.php index.html index.htm;
            try_files $uri $uri/ /index.php?$query_string;
        }
        
        location ~ .php$ {
            fastcgi_pass php:9000;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";
        }
    }
}

php.ini:

date.timezone = Asia/Shanghai
upload_max_filesize = 2M
post_max_size = 8M
memory_limit = 128M

In the above example, nginx.conf configures Nginx monitoring Port and proxy rules to forward dynamic requests to PHP services. php.ini configures some common PHP parameters, such as time zone and upload file restrictions.

  1. Run the containerized application
    After the configuration file is ready, we can start the containerized application through the following command:
$ docker-compose up -d

After running this command, Docker The container will be automatically downloaded and started based on the configuration file. You can use the docker-compose ps command to view running containers.

  1. Summary
    This article introduces how to use Docker Compose, Nginx and MariaDB to implement containerization of PHP applications, and gives specific code examples. We hope that by reading this article, readers can better understand and practice containerization technology and improve the efficiency of application deployment and management.

The above is just a simple example. The actual situation may be more complex. Readers can make appropriate adjustments and expansions according to their own needs. I hope this article can provide some help to readers!

The above is the detailed content of Containerize PHP applications using Docker Compose, Nginx, and MariaDB. 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