Nginx 는 러시아에서 두번째로 방문자수가 많은 Rambler.ru 에서 사용하기 위해 Igor Sysoev 가 만들었다고 합니다.

1. PHP5 설치
FastCGI를 사용하기 위해 설치 합니다.
5.2.x 버전에서는 FastCGI 옵션이 별도로 있었는데 5.3.x 부터는 CGI 옵션으로 통합 되었나 봅니다.

[root@yongbok ~]# cd /usr/ports/lang/php5 ; make install clean


php.ini 파일 복사하구요..

[root@yongbok ~]# cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini

– PHP5 Extensions 설치 (옵션은 필요한 것만.. 선택하시길..)

[root@yongbok ~]# cd /usr/ports/lang/php5-extensions ; make install clean

2. spawn-fcgi 설치

[root@yongbok ~]# cd /usr/ports/www/spawn-fcgi ; make install clean

FastCGI 를 실행 해봅니다.

[root@yongbok ~]# echo ‘spawn_fcgi_enable=”YES”‘ >> /etc/rc.conf
[root@yongbok ~]# /usr/local/etc/rc.d/spawn-fcgi start
Starting spawn_fcgi.
spawn-fcgi: child spawned successfully: PID: 4241

소켓 확인도 해보구요..

[root@yongbok ~]# sockstat -4 | grep php-cgi
www php-cgi 99996 0 tcp4 127.0.0.1:9000 *:*

3. Nginx 설치
안정 버전은 nginx 로 설치 하시고 개발자 버전은 nginx-devel 로 설치 하시면 됩니다. (모듈은 필요한 것만)

[root@yongbok ~]# cd /usr/ports/www/nginx ; make install clean


/usr/local/etc/nginx 디렉토리에 nginx.conf 설정 파일을 사용자 환경에 맞게 수정 합니다.

[root@yongbok ~]# ee /usr/local/etc/nginx/nginx.conf

## 기본 설정 ##
# nginx 가 실행될 사용자입니다. (UID) 기본값은 nobody 입니다.
user www;

# 프로세스 ID
pid logs/nginx.pid;

# 처음에 띄울 프로세스 갯수입니다. 기본값은 1 입니다.
worker_processes 4;

# 연결 허용 설정
events {
worker_connections 1024;
}

# 에러 관련 로그 설정입니다.
error_log logs/error.log; # 전체 다 보여줄꺼냐..
#error_log logs/error.log notice; # notice 만 보여줄꺼냐
#error_log logs/error.log info; # 걍 정보만 보여줄거냐.. 인듯?

# http 설정
http {
include mime.types;
default_type application/octet-stream;

# 로그 포멧 방식
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

sendfile on;
tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;

# 기본 사이트 설정
server {
listen 80; # 포트 설정
server_name www2.yongbok.net; # 도메인 설정

charset utf-8; # 기본 언어셋 (utf-8, euc-kr)
# charset 에 관한 자세한 건 위키 참고 http://wiki.nginx.org/NginxHttpCharsetModule

# 로그 관련
access_log logs/static.access.log main;

# 문서 설정
location / {
root /home/www;
index index.php index.html index.htm;
}

# FastCGI 설정 – php.ini 파일에 cgi.fix_pathinfo=1 구문이 있어야 합니다.
location ~ \.php$ {
root /home/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www$fastcgi_script_name;
include fastcgi_params;
}

# 아파치에서 사용하는 .htaccess 파일에 대한 접근 금지
location ~ /\.ht {
deny all;
}
}

# 가상 호스트 (아래 형식 처럼 계속 늘려 가면 됩니다.)
server {
listen 80;
server_name ftp.yongbok.net;

charset euc-kr;
#source_charset euc-kr;

access_log logs/ftp.access.log main;

location / {
root /home/ftp;
index index.php index.html index.htm;
fancyindex on; # fancy indexes 활성화 (Directory Listening)
fancyindex_exact_size off; # 파일 제한 해제
}

location ~ \.php$ {
root /home/ftp;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/ftp$fastcgi_script_name;
include fastcgi_params;
}
}
}

나머지 설정 및 옵션들은 위키를 참고 하시면 됩니다.

http://wiki.nginx.org/Main

nginx 실행

[root@yongbok ~]# echo ‘nginx_enable=”YES”‘ >> /etc/rc.conf

[root@yongbok ~]# /usr/local/etc/rc.d/nginx start
Performing sanity check on nginx configuration:
the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.

phpinfo 로 PHP 정상 작동 확인

[root@yongbok ~]# echo ‘<?php phpinfo(); ?>’ >> /home/www/phpinfo.php


참고
http://www.cyberciti.biz/faq/freebsd-configure-nginx-php-fastcgi-server/