Skip to content

trong291990/phpdocker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tổng quan

Đây là bộ docker-compose dùng để phát triển các ứng dụng trên nền PHP/MySQL. Gồm có:

  • PHP PHP 8.1,8.2,8.4

  • MySQL 8.2

  • Postgres 17

  • Nginx

Kiến trúc

Hệ thống tách thành các service độc lập:

  • PHP-FPM: php81, php82, php84
  • Web: nginx
  • Database: mysql (MySQL 8.x), postgres (tuỳ chọn)
  • Cache: redis

Ưu điểm: linh hoạt chọn phiên bản PHP, có thể chạy nhiều phiên bản song song, dễ mở rộng và debug.

Bắt đầu nhanh

  1. Tạo file .env (nếu chưa có), ví dụ các biến hay dùng:
USER_ID=1000
GROUP_ID=1000
HTTP_PORT=8080
PROJECTS_PATH=/home/youruser/Projects
MYSQL_PWD=root
POSTGRES_PWD=postgres
REDIS_PORT=6379
MYSQL_PORT=3306
POSTGRES_PORT=5432
  1. Chọn phiên bản PHP cho Nginx

Sửa fastcgi_pass trong site để trỏ đúng upstream đã định nghĩa sẵn:

# trong nginx/sites/default.conf
fastcgi_pass php82-fpm; # hoặc php81-fpm / php84-fpm
  1. Build và chạy
docker compose build php81 php82 php84 nginx
docker compose up -d
  1. Thêm hosts (Adminer nếu dùng):
127.0.0.1 adminer.local
::1       adminer.local

Truy cập: http://adminer.local:${HTTP_PORT} (mặc định 8080 nếu bạn đặt như trên)

Ghi chú

  • Tất cả PHP-FPM containers listen nội bộ trên cổng 9000; không xung đột vì nằm trong các container khác nhau. Nginx kết nối qua tên service + 9000.
  • Mount toàn bộ PROJECTS_PATH không làm tăng dung lượng container; nếu thư mục quá lớn, cân nhắc mount chọn lọc để giảm overhead khi tools quét file.
  • Trên Ubuntu (Linux), bind mount là native, hiệu năng tốt. Nếu dùng nhiều watcher, có thể tăng inotify:
    • sudo sysctl -w fs.inotify.max_user_watches=524288
    • sudo sysctl -w fs.inotify.max_user_instances=1024

Cách cài đặt

Cài đặt chung

  • Copy file .env.example sang một file mới và đặt tên là .env, sửa các giá trị nếu muôn

  • Copy file docker-compose.yml.example sang một file mới và đặt tên là docker-compose.yml, sửa các giá trị tại các row có dòng // need to edit cho chính xác

Cài đặt web

  • Thêm hai dòng này vào hosts file
127.0.0.1 adminer.local
::1 adminer.local

Hoàn thành

  • Tại thư mục root (ngang hàng với file docker-composer.yml), gõ lệnh docker-compose up -d

  • Vào trình duyệt gõ http:://adminer.local:9090. Servername nhập mariadb, username/pass thì tìm ở file .env. Nếu login thành công vào adminer thì đã setup thành công

Database

  • Khi import file Data (SQL) có dung lượng lớn, hãy copy vào thư mục data/shared (được mount vào /var/shared ở container mariadb) và dùng command line để import
  • Khi setup biến môi trường cho ứng dụng php, ví dụ file .env của dự án Laravel, hãy chú ý
DB_HOST=mariadb # không phải là localhost
DB_PORT=3306 # không phải là 33069

Setup một site mới

Ví dụ muốn setup site Laravel mysite:

  1. Thêm domain vào hosts trên máy local
127.0.0.1 mysite.local
::1       mysite.local
  1. Đặt mã nguồn
  • Đảm bảo dự án nằm trong ${PROJECTS_PATH}/mysite trên host.
  • Trong container, đường dẫn sẽ là /var/www/Projects/mysite.
  1. Tạo virtual host Nginx

Tạo file nginx/sites/mysite.conf với nội dung:

server {
    listen 80;
    server_name mysite.local;

    root /var/www/Projects/mysite/public; # Laravel public
    index index.php index.html;

    access_log /var/log/nginx/mysite.access.log;
    error_log  /var/log/nginx/mysite.error.log;

    client_max_body_size 0;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass php82-fpm; # hoặc php81-fpm / php84-fpm
        fastcgi_index index.php;
    }
}
  1. Khởi động lại Nginx
docker compose restart nginx
  1. Truy cập

Mở http://mysite.local:${HTTP_PORT} (ví dụ 8080).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors