Disaster recovery and recovery of PHP applications with Docker Compose, Nginx and MariaDB

WBOY
Release: 2023-10-12 10:44:02
Original
1039 people have browsed it

通过Docker Compose、Nginx和MariaDB实现PHP应用程序的灾备和恢复

Disaster recovery and recovery of PHP applications through Docker Compose, Nginx and MariaDB

Disaster recovery and recovery is an important topic for any application All very critical. In modern cloud computing environments, application disaster recovery and recovery can be easily achieved using containerization technology. This article will introduce how to use Docker Compose, Nginx and MariaDB to implement disaster recovery and recovery of PHP applications, and provide specific code examples.

First, we need to define a Docker Compose file that describes the various components of our application and the relationships between them. The following is a simple example:

version: '3'
services:
  web:
    build: .
    ports:
      - 80:80
    depends_on:
      - db
    networks:
      - app-network
  db:
    image: mariadb:latest
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - app-network
networks:
  app-network:
volumes:
  db-data:
Copy after login

The above Docker Compose file defines two services: web and db. The web service is our PHP application and it will run through Nginx. The db service is a MariaDB database used to store the application's data.

Next, we need to write an Nginx configuration file to forward requests to our PHP application. The following is a sample configuration file:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    location ~ .php$ {
        fastcgi_pass web:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
Copy after login

In the above configuration file, we use the fastcgi_pass directive to forward the request to the Docker service named web, with the port number being 9000.

Finally, we need to write a PHP application and package it into a container image together with the above code. The following is a simple PHP application example:

<?php
$servername = "db";
$username = "root";
$password = "secret";
$dbname = "mydb";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 执行查询
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

// 输出结果
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 结果";
}

// 关闭连接
$conn->close();
?>
Copy after login

The above PHP application is a simple database query example, which connects to the MariaDB database named db and executes a query. The query results will be output to the browser.

In order to achieve disaster recovery and recovery, we can use Docker Compose commands to start and stop our applications. The following are some commonly used command examples:

# 启动应用程序
docker-compose up -d

# 停止应用程序
docker-compose down
Copy after login

By using these commands, we can easily create and destroy our application instances to achieve disaster recovery and recovery goals.

Summary:

Through Docker Compose, Nginx and MariaDB, we can easily implement disaster recovery and recovery of PHP applications. Using containerization technology, we can quickly deploy and destroy application instances and use Nginx as a reverse proxy to achieve load balancing of traffic. MariaDB provides powerful database functions for storing and managing application data. The code examples provided above can help readers understand how to implement these functions and customize their own applications based on them.

The above is the detailed content of Disaster recovery and recovery of PHP applications with Docker Compose, Nginx and MariaDB. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!