Build a highly available PHP application cluster using Docker Compose, Nginx and MariaDB

PHPz
Release: 2023-10-12 08:40:01
Original
1019 people have browsed it

使用Docker Compose、Nginx和MariaDB构建高可用性的PHP应用程序集群

Build a highly available PHP application cluster using Docker Compose, Nginx and MariaDB

Introduction:

With the popularity of the Internet, Web applications development and deployment become more complex. In order to improve the availability and scalability of web applications, many developers have begun to use containerization technology to build high-availability application clusters. This article will introduce how to use Docker Compose, Nginx and MariaDB to build a highly available PHP application cluster, and give specific code examples.

1. Install Docker and Docker Compose

Before we begin, we need to install Docker and Docker Compose. Docker is a containerization technology that can be used to create and manage containers, and Docker Compose is a tool for defining and running multi-container Docker applications.

First, we need to install Docker. You can find the installation package for your operating system on the Docker official website and install it according to the official instructions.

After the installation is complete, you can open a terminal and run the following command to verify whether Docker is installed normally:

docker --version
Copy after login

Next, we need to install Docker Compose. You can download the latest version of the binaries from the Docker Compose GitHub repository and place them in your system directory.

After the installation is complete, you can run the following command to verify whether Docker Compose is installed normally:

docker-compose --version
Copy after login

If the above commands run normally, Docker and Docker Compose have been successfully installed.

2. Create a Docker Compose file

Next, we need to create a file named docker-compose.yml to define our PHP application cluster. The following is a basic example:

version: '3'

services:
  web:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - php
  php:
    image: php:7.4-fpm
    volumes:
      - ./src:/var/www/html
  db:
    image: mariadb:latest
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - ./data:/var/lib/mysql
Copy after login

In the above example, we defined three services: web, php and db. The web service uses Nginx as a reverse proxy server to forward traffic to the PHP application running in the php service. The php service uses PHP-FPM as the PHP interpreter and mounts the application code to the /var/www/html directory in the container. The db service uses MariaDB as the database server and mounts the data storage directory.

3. Create Nginx configuration file

We need to create a file named nginx.conf to configure the Nginx server. The following is a basic example:

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        server_name localhost;

        location / {
            proxy_pass http://php;
            proxy_set_header Host $host;
        }
    }
}
Copy after login

In the above example, we defined a simple Nginx server, listening on port 80 and proxying traffic to the php service.

4. Writing a PHP application

We need to write a simple PHP application to test whether our cluster is working properly. The following is a basic example:

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

  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  echo "Connected successfully to MariaDB server";

  $conn->close();
?>
Copy after login

In the above example, we created a database named mydb and connected to the MariaDB server using the root user and secret password. If the connection is successful, "Connected successfully to MariaDB server" will be output.

5. Start the application cluster

Navigate to the directory where the docker-compose.yml file is located in the terminal and run the following command to start the application cluster:

docker-compose up -d
Copy after login

The The command will download and start the required image, and create and run the container.

6. Test application cluster

Visit http://localhost in the browser, you should be able to see the output of "Connected successfully to MariaDB server", which indicates that your PHP application The program has successfully connected to the MariaDB database.

7. Expand and manage application clusters

You can easily expand and manage your application by modifying the docker-compose.yml file and running the docker-compose up -d command again Program cluster. You can increase or decrease the number of instances of web, php and db services as well as modify their configuration according to your needs.

Summary:

This article introduces how to use Docker Compose, Nginx and MariaDB to build a high-availability PHP application cluster. We defined a multi-container application consisting of Nginx, PHP and MariaDB through Docker Compose and gave specific code examples. With this cluster, we can easily scale and manage our application and improve its availability and scalability. I hope this article can help you understand how to build a highly available PHP application cluster.

The above is the detailed content of Build a highly available PHP application cluster using 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