2011-09-14

Debian(wheezy) + nginx + php-fpm(FastCGI) + php-apc

얼마전부터 Apache 대신, nginx 를 사용하고 있다. Django 하고 연동해서 사용하고 있지만, PHP 도 사용할 일이 생겨서 FascCGI 구성하여 설정하는 법을 정리하게 되었다. 가장 기본적인 설정법만 있기 때문에, 구체적으로 특별한 어플리케이션을 사용하는 경우 연구를 더 해봐야 한다. 그에 대한 사항은 나중에 알아보도록 하자.


  1. /etc/apt/sources.list 에 contrib non-free 추가
    deb http://ftp.daum.net/debian/ wheezy main contrib non-free
    deb-src http://ftp.daum.net/debian/ wheezy main contrib non-free
    
    deb http://security.debian.org/ wheezy/updates main contrib non-free
    deb-src http://security.debian.org/ wheezy/updates main contrib non-free
    위와 같이 파일을 수정한 후에 apt-get update 실행한다.
  2. 필요한 패키지 설치
    # apt-get install nginx php5-fpm php-apc
  3. Project 디렉토리 생성
    # mkdir -p /opt/project/phpfpm
  4. index.php 생성 (테스트용 페이지)
    # cd /opt/project/phpfpm
    # vi index.php
    <html>
    <head>
    <title>PHP-FPM Test</title>
    </head>
    <body>
    Hello~!<br/>
    <?php
    echo "PHP-FPM Test";
    ?>
    </body>
    </html>
  5. nginx 설정파일 작성
    # cd /etc/nginx/sites-available
    # vi phpfpm 
    server {
        listen *:80;
        server_name phpfpm.test.com; # 자신이 원하는 도메인주소 입력
        root /opt/project/phpfpm/;
        index index.php;
    
        location ~ \.php$ {
    
            # index index.php;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
        }
    }
    # cd ../sites-enabled
    # ln -s /etc/nginx/sites-available/phpfpm phpfpm
  6. nginx, php-fpm 시작
    # /etc/init.d/nginx start
    # /etc/init.d/php-fpm start  
  7. PC의 hosts 파일에 phpfpm.test.com 을 설정한 후에, 브라우저에서 http://phpfpm.test.com 을 입력하면 출력화면을 볼 수 있을 것이다.