


Build a highly available PHP application cluster using Docker Compose, Nginx and MariaDB
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
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
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
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; } } }
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(); ?>
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).
