서버 설정 · 2 min read · Dec 13, 2025

Fedora 13에서 PHP5 및 MySQL 지원으로 Nginx 설치하기 - 페이지 2

4 PHP5 설치하기

Nginx에서 FastCGI를 통해 PHP5를 작동시킬 수 있습니다. Fedora에는 독립 실행형 FastCGI 데몬 패키지가 없으므로, lighttpd의 FastCGI 패키지(lighttpd-fastcgi)를 사용하고 php-cli 및 MySQL을 PHP 스크립트에서 사용하려면 필요한 php-mysql과 같은 몇 가지 PHP5 모듈과 함께 설치합니다:

yum install lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

그런 다음 /etc/php.ini를 열고 파일 끝에 cgi.fix_pathinfo = 1을 추가합니다:

vi /etc/php.ini

| [...] cgi.fix_pathinfo = 1 |

lighttpd-fastcgi 패키지는 FastCGI 프로세스를 시작하는 데 사용할 수 있는 실행 파일 /usr/bin/spawn-fcgi를 포함합니다. 자세한 내용을 보려면

spawn-fcgi --help

를 확인하세요.

localhost의 포트 9000에서 nginx 사용자 및 그룹으로 실행되는 PHP FastCGI 데몬을 시작하려면 다음 명령을 실행합니다:

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid

물론, 시스템을 부팅할 때마다 이 명령을 수동으로 입력하고 싶지는 않으므로, 시스템이 부팅 시 자동으로 명령을 실행하도록 하려면 /etc/rc.local을 열고…

vi /etc/rc.local

… 파일 끝에 명령을 추가합니다:

| [...] /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid |

5 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 5; [...] keepalive_timeout 2; [...] |

가상 호스트는 server {} 컨테이너에서 정의됩니다. 기본 vhost를 다음과 같이 수정해 보겠습니다:

| [...] 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 /usr/share/nginx/html$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를 인덱스 라인에 추가했습니다. root /usr/share/nginx/html;는 문서 루트가 /usr/share/nginx/html 디렉토리임을 의미합니다.

PHP에 중요한 부분은 location ~ .php$ {} 구문입니다. 이를 활성화하려면 주석을 제거하세요. root 라인을 웹 사이트의 문서 루트로 변경하세요(예: root /usr/share/nginx/html;). fastcgi_param 라인을 fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;로 변경해야 합니다. 그렇지 않으면 PHP 인터프리터가 브라우저에서 호출하는 PHP 스크립트를 찾지 못합니다.

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

/etc/init.d/nginx restart

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

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

| |

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

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

6 링크

Share: X/Twitter LinkedIn

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

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