Home  >  Article  >  Backend Development  >  Automate the deployment process of PHP applications using Docker Compose, Nginx and MariaDB

Automate the deployment process of PHP applications using Docker Compose, Nginx and MariaDB

WBOY
WBOYOriginal
2023-10-12 08:27:111007browse

使用Docker Compose、Nginx和MariaDB实现PHP应用程序的自动化部署流程

Title: Automating the deployment process of PHP applications using Docker Compose, Nginx and MariaDB

Abstract: This article aims to introduce how to use Docker Compose, Nginx and MariaDB to Automate deployment of PHP applications. We will use Docker Compose to manage containerized applications and Nginx as a reverse proxy server while integrating with the MariaDB database. The article will provide specific code examples to help readers implement the automated deployment process.


  1. Introduction

In traditional deployment methods, configuring, installing, and managing the server environment can be very cumbersome. Using Docker Compose can simplify this process, allowing us to deploy applications quickly and reliably.

  1. Install Docker and Docker Compose

First, we need to install Docker and Docker Compose on the target server. Please refer to Docker official documentation for specific installation methods.

  1. Create Docker Compose file

Create a file named docker-compose.yaml in the root directory of the project, which will define the services we will deploy and interdependence between them.

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
    depends_on:
      - php
  php:
    image: php:7.4-fpm
    volumes:
      - ./php/app:/var/www/html
  db:
    image: mariadb:latest
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=app
      - MYSQL_USER=app_user
      - MYSQL_PASSWORD=app_password
    volumes:
      - ./db/data:/var/lib/mysql

The above Docker Compose file defines three services: nginx, php and db. The Nginx service is responsible for the reverse proxy and static file serving, the PHP service runs our application, and the DB service is the MariaDB database.

  1. Create Nginx configuration file

Create a directory named nginx in the root directory of the project, and create a conf.d directory in this directory. Create a file named default.conf in the conf.d directory, which will define Nginx's reverse proxy and static file service.

server {
    listen 80;
    server_name localhost;

    root /var/www/html/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ .php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

The key to the above Nginx configuration file is to forward the request to the 9000 port on the PHP container, thereby enabling the parsing and running of the PHP code.

  1. Create a PHP application

Create a directory named php in the root directory of the project and write our PHP application in this directory.

  1. Start the application

Now that we have completed all the preparations for the automated deployment process, we can use the following command to start the application.

docker-compose up -d

This command will read the configuration from the docker-compose.yaml file and automatically create and run the required Docker containers.

  1. Access the application

Enter the IP address or domain name of the server in the browser, you will be able to access our application.


This article details how to use Docker Compose, Nginx and MariaDB to implement automated deployment of PHP applications. We use Docker Compose to manage containerized applications and use Nginx as a reverse proxy server to integrate with the MariaDB database. Through specific code examples, readers can better understand and implement the automated deployment process. This automated deployment solution can improve development efficiency, reduce the difficulty of system configuration and management, and is an important part of modern software development.

The above is the detailed content of Automate the deployment process of 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