맛집 여행 캠핑 일상 생활

[Linux] Docker nginx with php-fpm 설치 방법 본문

LINUX

[Linux] Docker nginx with php-fpm 설치 방법

영은파더♥ 2025. 5. 20. 11:48
728x90

도커 허브에는 nginx + php-fpm이 같이 있는 이미지를 못찾겠네요~

따로 따로 조합해서 사용하는 방법 말고 하나의 이미지에서 서비스하는 방법입니다.

1. docker-compose.yml

services:
  nginx:
    #image: nginx:latest
    container_name: nginx-php82
    build:
      context: .
      dockerfile: ./Dockerfile-nginx
    restart: always
    ports:
      - "8080:80"
    volumes:
      - ./www:/var/www/html
      - ./nginx/logs:/var/log/nginx
      - ./nginx/conf:/etc/nginx/conf.d

2. Dockerfile-nginx

FROM nginx:latest

RUN apt update
RUN apt-get -y install nginx php-fpm php-mysqli
RUN apt clean

RUN sed -i 's/www-data/nginx/g' /etc/php/8.2/fpm/pool.d/www.conf
RUN sed -i '/^;security.limit_extensions/asecurity.limit_extensions = .php .php3 .php4 .php5 .php7 .htm .html' /etc/php/8.2/fpm/pool.d/www.conf
RUN echo "/usr/sbin/php-fpm8.2 -D" > /docker-entrypoint.d/40-php82-fpm.sh
RUN chmod +x /docker-entrypoint.d/40-php82-fpm.sh

3. nginx/conf/example.conf

server {
    listen 80;
    server_name www.example.com;
    access_log "/var/log/nginx/example.access.log";
    error_log  "/var/log/nginx/example.error.log";
    root /var/www/html;
    index index.php index.html;

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

    location ~ [^/]\.(php|htm|html)(/|$) {
        try_files $uri =404;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

위 3개의 파일을 생성한 다음에
docker compose up -d --build

해주시면 됩니다.

다음글에는 php-apache 설치 방법에 대해서 알아보겠습니다.

 

728x90
반응형
Trackback : | Comments :