Sure, here’s an example of how to deploy a PHP application using Docker, along with Nginx as the reverse proxy server and MySQL as the database server:

  1. First, create a Dockerfile for your PHP application:
FROM php:8.0-fpm

RUN apt-get update && apt-get install -y curl

WORKDIR /var/www/html

COPY index.php /var/www/html/

CMD ["php-fpm"]

This Dockerfile uses the php:8.0-fpm image as the base image, installs curl, sets the working directory to /var/www/html, copies the index.php file to that directory, and sets the default command to php-fpm.

  1. Create a Docker Compose file to define the services that will be used in the application:
version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql

  php:
    build: .
    volumes:
      - .:/var/www/html
    environment:
      - COMPOSER_HOME=app
      - COMPOSER_CACHE_DIR=app/cache
    depends_on:
      - nginx
  mysql:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=database
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:

This Compose file defines three services: nginx, php, and mysql. The nginx service uses the nginx:latest image, maps port 80 to port 80, and mounts a configuration file (./nginx.conf) to /etc/nginx/conf.d/default.conf. The php service builds the Docker image using the Dockerfile in the current directory, mounts the current directory to /var/www/html, and sets environment variables for Composer. The mysql service uses the mysql:latest image, sets environment variables for the MySQL root password, database name, user, and password, and mounts a volume to persist data.

  1. Create a nginx.conf file in the same directory as the Compose file:
http {
    upstream php {
        server localhost:9000;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }
    }
}

This configuration file defines a single server block that listens on port 80 and responds to requests for the example.com server name. It also defines a location block that handles PHP files, using the fastcgi_pass directive to pass the request to the PHP-FPM service.

  1. Create a mysql directory in the current directory, and create a mysql.conf file inside it:
[mysqld]
server_id = 1
bind_address = 0.0.0.0
character_set_system = UTF8mb4
collation_system = utf8mb4_unicode_ci

[mysqld_safe]
log_error = /var/log/mysql/error.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log

[mysql]
!includedir /etc/mysql/conf.d/

This configuration file defines the MySQL server configuration, including the server ID, bind address, character set, and collation. It also enables the slow query log.

  1. Start the services using Docker Compose:
docker-compose up -d

This command starts the services in detached mode, so you can close your terminal and the services will keep running in the background.

  1. Access your PHP application by visiting http://example.com in your web browser.

That’s it! You’ve successfully deployed a PHP application using Docker, along with Nginx as the reverse proxy server and MySQL as the database server.

更多推荐