Cross-platform deployment of PHP applications with Docker Compose, Nginx and MariaDB

WBOY
Release: 2023-10-12 15:52:01
Original
854 people have browsed it

通过Docker Compose、Nginx和MariaDB实现PHP应用程序的跨平台部署

Cross-platform deployment of PHP applications through Docker Compose, Nginx and MariaDB requires specific code examples

With the development of cloud computing and containerization technology, cross-platform deployment Platform deployment has become a hot topic. In this article, we will introduce how to use Docker Compose, Nginx and MariaDB to achieve cross-platform deployment of PHP applications and give specific code examples.

Docker Compose is a tool for defining and running multi-container Docker applications. It uses simple YAML files to configure the service, network and storage aspects of the application. Nginx is a high-performance web server and reverse proxy server that can be used to distribute traffic to multiple containerized PHP applications. MariaDB is an open source relational database management system that can be used to store application data.

First, we need to write a Docker Compose configuration file to define the services of our PHP application. Here is a simple example:

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 80:80
    depends_on:
      - db

  db:
    image: mariadb
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:
Copy after login

In the above configuration file, we defined two services: web and db. The web service is our PHP application and uses a file called a Dockerfile to build the image. We map the container's port 80 to the host's port 80 so that our application can be accessed externally. The db service uses the official image of MariaDB, and sets the root password and data volume.

Next, we need to write a Dockerfile to build an image of our PHP application. Here is a simple example:

FROM php:7.4-fpm

WORKDIR /app

COPY . /app

RUN apt-get update 
    && apt-get install -y nginx 
    && docker-php-ext-install pdo_mysql

CMD service nginx start && php-fpm

EXPOSE 80
Copy after login

In the Dockerfile above, we build our image based on the official php:7.4-fpm image. We set the working directory to /app and copy all files in the current directory to the container's /app directory. Next, we installed nginx and pdo_mysql extension using apt-get command. Finally, we set the commands when starting the container, including starting nginx and php-fpm, and opening port 80.

After completing the above configuration, we can use the following command to start our PHP application:

docker-compose up -d
Copy after login

This command will create and start our PHP application based on the Docker Compose configuration file container. The -d parameter indicates running the container in the background.

Once the container is started successfully, we can view our PHP application by accessing http://localhost through the browser. Nginx will distribute traffic to our PHP application container, and the application will then connect to the MariaDB database to fetch and store data.

To sum up, cross-platform deployment of PHP applications can be easily achieved using Docker Compose, Nginx and MariaDB. The docker-compose.yml file can define the application's services, and the Dockerfile can build the application's image. Use the Docker Compose command to easily launch and manage your application's containers. This cross-platform deployment solution is not only simple and efficient, but also has good scalability and maintainability.

The above are specific code examples for using Docker Compose, Nginx and MariaDB to implement cross-platform deployment of PHP applications. Hope this helps!

The above is the detailed content of Cross-platform deployment 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!