Khi cài đặt WordPress bằng Docker, trong nhiều trường hợp mình cần phải cài thêm PHP Extensions theo yêu cầu của một số plugins đặc biệt.
Bài viết này mình sẽ hướng dẫn cách cài đặt PHP Extensions cho PHP Docker Image, sử dụng script docker-php-extension-installer. Ưu điểm của khi dùng script cài đặt này so với cách cài đặt PHP Extensions thông thường:
-
Tiêu chuẩn hóa các bước cài đặt thêm PHP Extension bằng 1 lệnh duy nhất
install-php-extensions -
Tương thích cho mọi phiên bản PHP từ 5.5 đến 8.3.
-
Tương thích với nhiều hệ điều hành: Ubuntu, Alpine, Debain và nhiều PHP Image khác nhau.
-
Hỗ trợ 146 PHP Extensions, xem chi tiết ở đây.
1. Tạo website nháp
Để demo, mình sẽ tạo 1 website WordPress theo hướng dẫn ở đây
https://thuanbui.me/wordpress-caddy-docker/
Kiểm tra danh sách PHP Extension đã được cài mặc định
docker compose exec wordpress php -m[PHP Modules]bcmathCorectypecurldatedomexiffileinfofiltergdhashiconvimagickintljsonlibxmlmbstringmysqlimysqlndopensslpcrePDOpdo_sqlitePharposixrandomreadlineReflectionsessionSimpleXMLsodiumSPLsqlite3standardtokenizerxmlxmlreaderxmlwriterZend OPcachezipzlib
[Zend Modules]Zend OPcache2. Cài đặt ionCube Loader
Mình muốn cài đặt thêm PHP Extension: Ioncube Loader cho WordPress.
Tạo file Dockerfile trong cùng thư mục với file compose.yml và nhập vào nội dung sau
FROM wordpress:fpm-alpineADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/RUN install-php-extensions ioncube-loaderChỉnh sửa lại file compose.yml, thay thế mục image: wordpress:fpm-alpine bằng build: .. Phần khai báo woordpress sẽ tương tự như sau
wordpress: build: . container_name: wordpress restart: always depends_on: - db #- redis volumes: - ./wordpress/html:/var/www/html - ./wordpress/custom.ini:/usr/local/etc/php/conf.d/custom.ini env_file: .envCấu hình này sẽ yêu cầu Docker tạo 1 Container mới dựa trên thông số đã được cấu hình trong file Dockerfile
Kích hoạt lại hệ thống
docker compose builddocker compose up -dSau đó, kiểm tra lại thông số PHP Extensions. Giờ đã thấy ionCube Loader được bổ sung trong danh sách
[PHP Modules]bcmathCorectypecurldatedomexiffileinfofilterftpgdhashiconvimagickintlionCube LoaderjsonlibxmlmbstringmysqlimysqlndopensslpcrePDOpdo_sqlitePharposixreadlineReflectionsessionSimpleXMLsodiumSPLsqlite3standardtokenizerxmlxmlreaderxmlwriterZend OPcachezipzlib
[Zend Modules]Zend OPcachethe ionCube PHP Loader + ionCube24Để cài thêm cùng lúc nhiều PHP Extensions, bạn sửa lại dòng thứ 3 trong Dockerfile, và thêm vào tên các extensions mong muốn
RUN install-php-extensions ioncube-loader xdebugMình đã thử cài đặt thêm PHP Extension cho nhiều phiên bản WordPress khác nhau: wordpress:php8.1-fpm-alpine, wordpress:php7.4-apache, … Tất cả đều hoạt động ngon lành, bất kể Docker OS là Debian, Ubuntu hay Alpine.