Docker部署php运行环境(php-fpm+nginx+mysql)
后台-插件-广告管理-内容页头部广告(手机) |
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:
- First, create a Dockerfile for your PHP application:
- 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.
- Create a Docker Compose file to define the services that will be used in the application:
- 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.
- Create a nginx.conf file in the same directory as the Compose file:
- 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.
- Create a mysql directory in the current directory, and create a mysql.conf file inside it:
- 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.
- Start the services using Docker Compose:
- 1
This command starts the services in detached mode, so you can close your terminal and the services will keep running in the background.
- 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
后台-插件-广告管理-内容页尾部广告(手机) |