Home  >  Article  >  Backend Development  >  Best Practices for Docker Compose, Nginx, and MariaDB: Process Integration for Deploying PHP Applications

Best Practices for Docker Compose, Nginx, and MariaDB: Process Integration for Deploying PHP Applications

王林
王林Original
2023-10-12 14:03:151077browse

Docker Compose、Nginx和MariaDB的最佳实践:部署PHP应用程序的流程整合

Best Practices for Docker Compose, Nginx and MariaDB: Process Integration for Deploying PHP Applications

Introduction:
With the advancement of cloud computing and containerization technology Rapidly developing, Docker has become an important way to modernize the development and deployment of applications. Among the many Docker tools, Docker Compose, as a tool for orchestrating container services, is widely used in the deployment and management of multi-container applications. Nginx and MariaDB, as commonly used open source software, also play an important role in many application scenarios.

This article will introduce how to integrate Nginx and MariaDB through Docker Compose to deploy PHP applications. We will explain it in the following steps: preparation, creating Docker Compose file, writing Nginx configuration, writing PHP application, starting the container, and accessing the application.

1. Preparation
Before starting, we need to install Docker and Docker Compose in the local environment. Please ensure that the system has the latest version of Docker and Docker Compose correctly installed and running normally.

2. Create a Docker Compose file
Create a file named docker-compose.yml in the root directory of the project. This file will define all the container services we need to integrate the application.

version: '3'
services:
  web:
    build: .
    ports:
      - 80:80
    volumes:
      - ./app:/var/www/html
    depends_on:
      - db
  db:
    image: mariadb:latest
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - ./db:/var/lib/mysql

In the above Docker Compose file, two services are defined: web and db. Serving web will build the code in the current directory and map the local port 80 to the container's port 80. The service db uses the latest version of the MariaDB image, and the environment variable MYSQL_ROOT_PASSWORD is set to define the root password of the database.

3. Write Nginx configuration
Create a file named nginx.conf in the root directory of the project and write the Nginx configuration content. The following is a basic Nginx configuration example:

server {
    listen 80;
    server_name localhost;

    root /var/www/html/public;
    index index.php;

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

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

In the above Nginx configuration file, we defined the listening port as 80 and the server name as localhost. The root directory is designated as /var/www/html/public, and index.php is set as the default index file. At the same time, we defined two location blocks, which are used to process static files and process requests for PHP files.

4. Write PHP application
Write the PHP application code in the app directory. Here is a simple Hello World program as an example. Create a new file named index.php and add the following code:

<?php
echo "Hello World!";

5. Start the container
In the terminal, enter the root directory of the project, And execute the following command to start the container:

docker-compose up -d

This command will build and start the container according to the definition in the Docker Compose file. Wait a moment. After the container is started successfully, you will see log information similar to the following:

Creating network "docker_default" with the default driver
Creating docker_web_1 ... done
Creating docker_db_1  ... done

6. Access the application
Access http://localhost in the browser, you will You see the output "Hello World!", which means that the PHP application has been deployed successfully.

7. Summary
Through the introduction of this article, we have learned how to integrate and deploy PHP applications through Docker Compose, Nginx and MariaDB. This integration makes it easy to build and manage complex container services and to easily extend and adjust application components. At the same time, this deployment method also provides good portability, allowing us to easily migrate and deploy applications in different environments.

Please note that this article only provides a basic example, and there may be more configuration and adjustments in actual application deployment. Through this example, I hope readers can have a basic understanding of the integration of Docker Compose, Nginx and MariaDB, and make further flexible adjustments in practical applications.

The above is the detailed content of Best Practices for Docker Compose, Nginx, and MariaDB: Process Integration for Deploying 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