Home >Backend Development >PHP Tutorial >Continuous delivery of PHP applications with Docker Compose, Nginx and MariaDB

Continuous delivery of PHP applications with Docker Compose, Nginx and MariaDB

WBOY
WBOYOriginal
2023-10-12 12:06:271372browse

通过Docker Compose、Nginx和MariaDB实现PHP应用程序的持续交付

Continuous delivery of PHP applications through Docker Compose, Nginx and MariaDB

Overview:
With the rapid development of cloud computing and containerization technology, more and more More and more applications are adopting containerization to achieve rapid delivery and deployment. This article will introduce how to use Docker Compose, Nginx and MariaDB to build a simple PHP application and implement the continuous delivery process. At the same time, we will give specific code examples to help readers better understand this process.

1. Preparation

  1. Install Docker and Docker Compose.

2. Create a Docker Compose configuration file
Create a file named docker-compose.yml and fill it in according to the following example content.

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: mariadb:latest
    environment:
      MYSQL_ROOT_PASSWORD: example

3. Create Nginx configuration file
Create a file named nginx.conf and fill it in according to the following example content.

server {
    listen 80;
    server_name localhost;
    root /var/www/html;
    index index.php index.html;

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

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

4. Create a PHP application file
Create a file named index.php in the project root directory and fill it in according to the following example content.

<?php
echo "Hello, World!";
?>

5. Create Dockerfile
Create a file named Dockerfile in the project root directory and fill in the following example content.

FROM php:7.4-fpm

WORKDIR /var/www/html

COPY . /var/www/html

RUN docker-php-ext-install mysqli pdo pdo_mysql

CMD ["php-fpm"]

EXPOSE 9000

6. Build and start the container
Execute the following commands in the project root directory to build and start the container.

docker-compose up -d

7. Verify the running results
Access http://localhost in the browser. If you see the words "Hello, World!", it means that the container has run successfully.

8. Continuous delivery process
When we update the application, we need to follow the following steps to achieve continuous delivery.

  1. Modify the code file
    Before modifying the code file, we need to stop the currently running container.
docker-compose down

Then, we can modify the index.php file, for example, change "Hello, World!" to "Hello, Docker!".

  1. Rebuild and start the container
    Execute the following command in the project root directory to rebuild and start the container.
docker-compose up -d --build
  1. Verify the running results
    Visit http://localhost in the browser. If you see the words "Hello, Docker!", it means that the update has been successfully deployed.

Conclusion:
By using Docker Compose, Nginx and MariaDB, we can quickly build a PHP application and implement the continuous delivery process. The advantage of containerization technology is that it can provide consistent development, testing and production environments, greatly simplifying deployment and maintenance. At the same time, using Nginx as a reverse proxy and load balancer can better manage request traffic. Through the above code examples, readers can further understand how to use these tools to implement the continuous delivery process.

The above is the detailed content of Continuous delivery of PHP applications with 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