Home>Article>Backend Development> Horizontal scaling of PHP applications with Docker Compose, Nginx and MariaDB
Horizontal expansion of PHP applications through Docker Compose, Nginx and MariaDB
As the Internet develops and the complexity of applications increases, how to efficiently expand applications Program performance becomes an important issue. Horizontal scaling is a solution that increases the processing power of an application by adding server instances. In this article, I will introduce how to use Docker Compose, Nginx and MariaDB to achieve horizontal scaling of PHP applications, and provide specific code examples.
Environment preparation:
Before we start, we need to prepare some environments, including machines with Docker and Docker Compose installed. If you haven't installed these tools yet, you can find the relevant installation guide on the official website.
Step 1: Create Docker Compose file
First, create a file named docker-compose.yml and define our service in it. The following is the content of a sample compose file:
version: '3.7' services: web: build: ./web ports: - 80:80 networks: - app-net depends_on: - db db: image: mariadb environment: - MYSQL_ROOT_PASSWORD=your_password networks: - app-net networks: app-net:
In the above example, we defined two services: web and db. The web service is used to run Nginx and PHP applications, and the db service is used to run the MariaDB database.
Step 2: Create the Dockerfile for Nginx and PHP applications
In the previous step, we specified the build path of the web service as ./web. Now, we need to create a Dockerfile in this directory for building images of Nginx and PHP applications. The following is a simple Dockerfile example:
FROM nginx:latest # 安装PHP和一些扩展 RUN apt-get update && apt-get install -y php7.4-cli php7.4-fpm php7.4-mysql # 复制PHP应用程序到容器中 COPY app /var/www/html # 配置Nginx COPY nginx.conf /etc/nginx/nginx.conf # 启动Nginx和PHP-FPM CMD service php7.4-fpm start && nginx -g "daemon off;"
In the above example, we use nginx:latest as the base image and install PHP and some common extensions in it. We then copied the PHP application into the container and copied a customized nginx.conf file.
Step 3: Create Nginx configuration file
In the Dockerfile in the previous step, we used a customized nginx.conf file. Now, we need to create this file in the ./web directory and define the Nginx configuration. The following is the content of an example nginx.conf file:
worker_processes 1; events { worker_connections 1024; } http { server { listen 80; server_name localhost; root /var/www/html; index index.php index.html; location ~ .php$ { fastcgi_pass web:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location / { try_files $uri $uri/ /index.php?$query_string; } } }
In the above example, we define a virtual host for Nginx and forward the request to the PHP-FPM container named web.
Step 4: Create a PHP application
In the nginx.conf configuration file in the previous step, we specified the root directory of the PHP application as /var/www/html. Now, we need to create a folder named app under the ./web directory and put the code of the PHP application into it.
Step 5: Start the container cluster
After completing the above preparations, we can now start the container cluster. In the terminal, enter the directory where docker-compose.yml is located and execute the following command:
docker-compose up -d
This command will start the web and db services and connect them to the app-net in the network. The web service will bind port 80 of the host machine to receive HTTP requests from the outside.
So far, we have successfully used Docker Compose, Nginx and MariaDB to achieve horizontal expansion of PHP applications. Now, we can use the following command to expand the application:
docker-compose up --scale web=n
where n is the number of web service instances to be expanded.
Summary:
This article introduces how to use Docker Compose, Nginx and MariaDB to achieve horizontal expansion of PHP applications, and provides specific code examples. By using these tools, we can quickly and easily scale the performance of our applications to meet growing user needs. I hope this article can be helpful to everyone in practice.
The above is the detailed content of Horizontal scaling of PHP applications with Docker Compose, Nginx and MariaDB. For more information, please follow other related articles on the PHP Chinese website!