Nginx 설치 · 2 min read · Feb 14, 2026

CentOS 6.3에서 PHP5 (및 PHP-FPM) 및 MySQL 지원으로 Nginx 설치하기 - 2페이지

6 Nginx 구성

Nginx 구성은 /etc/nginx/nginx.conf에 있으며, 지금 열어보겠습니다:

vi /etc/nginx/nginx.conf

구성은 이해하기 쉽습니다 (여기에서 더 배울 수 있습니다: http://wiki.codemongers.com/NginxFullExample 및 여기: http://wiki.codemongers.com/NginxFullExample2)

먼저 (선택 사항) 작업 프로세스 수를 늘리고 keepalive_timeout을 합리적인 값으로 설정할 수 있습니다:

| [...] worker_processes 4; [...] keepalive_timeout 2; [...] |

가상 호스트는 /etc/nginx/conf.d 디렉토리의 server {} 컨테이너에서 정의됩니다. 기본 vhost ( /etc/nginx/conf.d/default.conf)를 다음과 같이 수정해 보겠습니다:

vi /etc/nginx/conf.d/default.conf

| [...] server { listen 80; server_name _; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/html; try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } |

servername ;는 이것을 기본 catchall vhost로 만듭니다 (물론 www.example.com과 같은 호스트 이름을 지정할 수도 있습니다).

location / 부분에서 index.php를 index 줄에 추가했습니다. root /usr/share/nginx/html;는 문서 루트가 /usr/share/nginx/html 디렉토리임을 의미합니다.

PHP에 중요한 부분은 location ~ .php$ {} 구문입니다. 이를 활성화하려면 주석을 제거하십시오. root 줄을 웹 사이트의 문서 루트로 변경하십시오 (예: root /usr/share/nginx/html;). 제로 데이 익스플로잇을 방지하기 위해 try_files $uri =404; 줄을 추가했습니다 (http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHPhttp://forum.nginx.org/read.php?2,88845,page=3 참조). fastcgi_param 줄을 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;로 변경해야 합니다. 그렇지 않으면 PHP 인터프리터가 브라우저에서 호출하는 PHP 스크립트를 찾지 못합니다 ( $document_root는 /usr/share/nginx/html로 변환됩니다. 이는 우리가 문서 루트로 설정한 것입니다).

기본적으로 PHP-FPM은 127.0.0.1의 포트 9000에서 수신 대기하므로, nginx에 127.0.0.1:9000에 연결하도록 지시합니다. fastcgi_pass 127.0.0.1:9000; 줄로 연결합니다. PHP-FPM이 Unix 소켓을 사용할 수도 있습니다 - 이는 7장에서 설명하겠습니다.

이제 파일을 저장하고 nginx를 다시 로드합니다:

/etc/init.d/nginx reload

이제 문서 루트 /usr/share/nginx/html에 다음 PHP 파일을 생성합니다…

vi /usr/share/nginx/html/info.php

| |

이제 브라우저에서 해당 파일을 호출합니다 (예: http://192.168.0.100/info.php):

보시다시피, PHP5가 작동하고 있으며, Server API 줄에 표시된 대로 FPM/FastCGI를 통해 작동하고 있습니다. 아래로 스크롤하면 MySQL 모듈을 포함하여 PHP5에서 이미 활성화된 모든 모듈을 볼 수 있습니다:

7 PHP-FPM을 Unix 소켓으로 사용하기

기본적으로 PHP-FPM은 127.0.0.1의 포트 9000에서 수신 대기합니다. TCP 오버헤드를 피하기 위해 PHP-FPM이 Unix 소켓을 사용하도록 설정할 수도 있습니다. 이를 위해 /etc/php-fpm.d/www.conf를 엽니다…

vi /etc/php-fpm.d/www.conf

… 그리고 listen 줄을 다음과 같이 변경합니다:

| [...] ;listen = 127.0.0.1:9000 listen = /tmp/php5-fpm.sock [...] |

그런 다음 PHP-FPM을 다시 로드합니다:

/etc/init.d/php-fpm reload

다음으로 nginx 구성을 살펴보고 모든 vhost에서 fastcgi_pass 127.0.0.1:9000; 줄을 fastcgi_pass unix:/tmp/php5-fpm.sock;로 변경합니다. 예를 들어 다음과 같이:

vi /etc/nginx/conf.d/default.conf

| [...] location ~ \.php$ { root /usr/share/nginx/html; try_files $uri =404; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } [...] |

마지막으로 nginx를 다시 로드합니다:

/etc/init.d/nginx reload

8 링크

저자에 대하여

Falko Timme는 Timme Hosting (초고속 nginx 웹 호스팅)의 소유자입니다. 그는 HowtoForge의 주요 유지 관리자 (2005년부터)이며 ISPConfig의 핵심 개발자 중 한 명입니다 (2000년부터). 그는 또한 O’Reilly의 “Linux System Administration” 책에 기여했습니다.

Share: X/Twitter LinkedIn

새 게시물을 받은 편지함에서 받기

스팸은 없습니다. 언제든지 구독 해지 가능합니다.