您现在的位置是:首页 > 技术教程 正文

Docker部署php运行环境(php-fpm+nginx+mysql)

admin 阅读: 2024-03-15
后台-插件-广告管理-内容页头部广告(手机)

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"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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:
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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; } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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
  • 1

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.

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索