Home > Article > Backend Development > Containerize PHP applications using Docker Compose, Nginx, and MariaDB
Title: Containerization of PHP applications using Docker Compose, Nginx and MariaDB
Introduction: With the rapid development of containerization technology, more and more Developers are beginning to pay attention to using Docker for application deployment and management. This article will introduce how to use Docker Compose, Nginx and MariaDB to implement containerization of PHP applications, and give specific code examples to help readers better understand and practice.
The following is an example of our Docker Compose configuration file (docker-compose.yml):
version: '3' services: webserver: image: nginx:latest ports: - 80:80 volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - php php: image: php:7.4-fpm volumes: - ./php.ini:/usr/local/etc/php/php.ini:ro - ./code:/var/www/html depends_on: - db db: image: mariadb:latest environment: MYSQL_ROOT_PASSWORD: your_password MYSQL_DATABASE: your_database MYSQL_USER: your_username MYSQL_PASSWORD: your_password volumes: - ./data:/var/lib/mysql
We have three services defined through this configuration file: webserver, php and db . The webserver service uses the Nginx image and maps port 80 of the host to port 80 of the container. The php service uses the PHP-FPM image and mounts the code directory into the container. The db service uses MariaDB mirroring, and the user name, password and data storage directory of the database are set.
nginx.conf:
worker_processes auto; events { worker_connections 1024; } http { server { listen 80; location / { root /var/www/html; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$query_string; } location ~ .php$ { fastcgi_pass php:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log"; } } }
php.ini:
date.timezone = Asia/Shanghai upload_max_filesize = 2M post_max_size = 8M memory_limit = 128M
In the above example, nginx.conf configures Nginx monitoring Port and proxy rules to forward dynamic requests to PHP services. php.ini configures some common PHP parameters, such as time zone and upload file restrictions.
$ docker-compose up -d
After running this command, Docker The container will be automatically downloaded and started based on the configuration file. You can use the docker-compose ps
command to view running containers.
The above is just a simple example. The actual situation may be more complex. Readers can make appropriate adjustments and expansions according to their own needs. I hope this article can provide some help to readers!
The above is the detailed content of Containerize PHP applications using Docker Compose, Nginx, and MariaDB. For more information, please follow other related articles on the PHP Chinese website!