백엔드 개발 PHP 튜토리얼 Nginx and PHP-FastCGI on Debian 6 (Squeeze)

Nginx and PHP-FastCGI on Debian 6 (Squeeze)

Jun 23, 2016 pm 02:28 PM

Nginx and PHP-FastCGI on Debian 6 (Squeeze)

Published: Monday, May 9th, 2011 by Phil Paradis

The nginx web server is a fast, lightweight server designed to efficiently handle the needs of both low and high traffic websites. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you get nginx up and running with PHP via FastCGI on your Debian 6 (Squeeze) Linux VPS.

It is assumed that you've already followed the steps outlined in our getting started guide. These steps should be performed via a root login to your Linode VPS over SSH.

Contents

Set the Hostname Install Required Packages Configure Virtual HostingTest PHP with FastCGI Create Directories UNIX Sockets Configuration Example TCP Sockets Configuration Example Important Security Considerations Enable and Start Services More Information

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname
로그인 후 복사
hostname -f 
로그인 후 복사

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install Required Packages

Issue the following commands to update your system and install the nginx web server, PHP, and compiler tools:

apt-get update
로그인 후 복사
apt-get upgrade
로그인 후 복사
apt-get install nginx php5-cli php5-cgi spawn-fcgi 
로그인 후 복사

Configure Virtual Hosting

Create Directories

In this guide, the domain "example.com" is used as an example site. You should substitute your own domain name in the configuration steps that follow. First, create directories to hold content and log files:

mkdir -p /srv/www/www.example.com/public_html
로그인 후 복사
mkdir /srv/www/www.example.com/logs
로그인 후 복사
chown -R www-data:www-data /srv/www/www.example.com 
로그인 후 복사

UNIX Sockets Configuration Example

Next, you'll need to define the site's virtual host file. This example uses a UNIX socket to connect to fcgiwrap. Be sure to change all instances of "example.com" to your domain name.

File:/etc/nginx/sites-available/www.example.com

server {
로그인 후 복사
로그인 후 복사
  server_name www.example.comexample.com; 
로그인 후 복사
  access_log /srv/www/www.example.com/logs/access.log; 
로그인 후 복사
  error_log /srv/www/www.example.com/logs/error.log;
로그인 후 복사
로그인 후 복사
  root /srv/www/www.example.com/public_html;
로그인 후 복사
로그인 후 복사
  location / {
로그인 후 복사
로그인 후 복사
    index index.htmlindex.htm;
로그인 후 복사
  }
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  location ~ \.php$ {
로그인 후 복사
로그인 후 복사
    include /etc/nginx/fastcgi_params;
로그인 후 복사
로그인 후 복사
    fastcgi_passunix:/var/run/php-fastcgi/php-fastcgi.socket;
로그인 후 복사
    fastcgi_index index.php;
로그인 후 복사
로그인 후 복사
    fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
로그인 후 복사
로그인 후 복사
  }
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
} 
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

Create a file named /usr/bin/php-fastcgi with the following contents:

File:/usr/bin/php-fastcgi

#!/bin/bash
로그인 후 복사
로그인 후 복사
FASTCGI_USER=www-data
로그인 후 복사
로그인 후 복사
FASTCGI_GROUP=www-data
로그인 후 복사
로그인 후 복사
SOCKET=/var/run/php-fastcgi/php-fastcgi.socket
로그인 후 복사
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
로그인 후 복사
CHILDREN=6 PHP5=/usr/bin/php5-cgi
로그인 후 복사
/usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5
로그인 후 복사

Make it executable by issuing the following command:

chmod +x /usr/bin/php-fastcgi 
로그인 후 복사
로그인 후 복사

TCP Sockets Configuration Example

Alternately, you may wish to use TCP sockets instead. If so, modify your nginx virtual host configuration file to resemble the following example. Again, make sure to replace all instances of "example.com" with your domain name.

File:/etc/nginx/sites-available/www.example.com

server {
로그인 후 복사
로그인 후 복사
  server_name www.example.com example.com; 
로그인 후 복사
  access_log /srv/www/www.example.com/logs/access.log;
로그인 후 복사
  error_log /srv/www/www.example.com/logs/error.log;
로그인 후 복사
로그인 후 복사
  root /srv/www/www.example.com/public_html;
로그인 후 복사
로그인 후 복사
  location / {
로그인 후 복사
로그인 후 복사
    index  index.html index.htm;
로그인 후 복사
  }
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  location ~ \.php$ {
로그인 후 복사
로그인 후 복사
    include /etc/nginx/fastcgi_params;
로그인 후 복사
로그인 후 복사
    fastcgi_pass 127.0.0.1:9000;
로그인 후 복사
    fastcgi_index index.php;
로그인 후 복사
로그인 후 복사
    fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
로그인 후 복사
로그인 후 복사
  }
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
} 
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

Create a file named /usr/bin/php-fastcgi with the following contents:

File:/usr/bin/php-fastcgi

#!/bin/bash 
로그인 후 복사
FASTCGI_USER=www-data 
로그인 후 복사
FASTCGI_GROUP=www-data 
로그인 후 복사
ADDRESS=127.0.0.1 PORT=9000 
로그인 후 복사
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid 
로그인 후 복사
CHILDREN=6 
로그인 후 복사
PHP5=/usr/bin/php5-cgi  
로그인 후 복사
/usr/bin/spawn-fcgi -a $ADDRESS -p $PORT -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5 
로그인 후 복사

Make it executable by issuing the following command:

chmod +x /usr/bin/php-fastcgi 
로그인 후 복사
로그인 후 복사

Important Security Considerations

If you're planning to run applications that support file uploads (images, for example), the above configurations may expose you to a security risk by allowing arbitrary code execution. The short explanation for this behavior is that a properly crafted URI which ends in ".php", in combination with a malicious image file that actually contains valid PHP, can result in the image being processed as PHP.

To mitigate this issue, you may wish to modify your configuration to include a try_files directive. Please note that this fix requires nginx and the php-fcgi workers to reside on the same server.

location ~ \.php$ {
로그인 후 복사
로그인 후 복사
  try_files $uri =404;
로그인 후 복사
  include /etc/nginx/fastcgi_params;
로그인 후 복사
로그인 후 복사
  fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
로그인 후 복사
  fastcgi_index index.php;
로그인 후 복사
로그인 후 복사
  fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
로그인 후 복사
로그인 후 복사
} 
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

Additionally, it's a good idea to secure any upload directories your applications may use. The following configuration excerpt demonstrates securing an "/images" directory.

location ~ \.php$ {
로그인 후 복사
로그인 후 복사
  include /etc/nginx/fastcgi_params;
로그인 후 복사
로그인 후 복사
  if ($uri !~ "^/images/") {
로그인 후 복사
    fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
로그인 후 복사
  }
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  fastcgi_index index.php;
로그인 후 복사
로그인 후 복사
  fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
로그인 후 복사
로그인 후 복사
} 
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

Enable and Start Services

Issue the following commands to enable the site:

cd /etc/nginx/sites-enabled/
로그인 후 복사
ln -s /etc/nginx/sites-available/www.example.com 
로그인 후 복사

Create a file named /etc/init.d/php-fastcgi with the following contents:

File:/etc/init.d/php-fastcgi

#!/bin/bash
로그인 후 복사
로그인 후 복사
PHP_SCRIPT=/usr/bin/php-fastcgi
로그인 후 복사
FASTCGI_USER=www-data
로그인 후 복사
로그인 후 복사
FASTCGI_GROUP=www-data
로그인 후 복사
로그인 후 복사
PID_DIR=/var/run/php-fastcgi
로그인 후 복사
PID_FILE=/var/run/php-fastcgi/php-fastcgi.pid
로그인 후 복사
RET_VAL=0
로그인 후 복사
case "$1" in
로그인 후 복사
  start)
로그인 후 복사
    if [[ ! -d $PID_DIR ]]
로그인 후 복사
    then
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
      mkdir $PID_DIR
로그인 후 복사
      chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
로그인 후 복사
      chmod 0770 $PID_DIR
로그인 후 복사
    fi
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    if [[ -r $PID_FILE ]]
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    then
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
      echo "php-fastcgi already running with PID `cat $PID_FILE`"
로그인 후 복사
      RET_VAL=1
로그인 후 복사
로그인 후 복사
    else
로그인 후 복사
로그인 후 복사
로그인 후 복사
      $PHP_SCRIPT
로그인 후 복사
      RET_VAL=$?
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    fi
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  ;;
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  stop)
로그인 후 복사
    if [[ -r $PID_FILE ]]
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    then
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
      kill `cat $PID_FILE`
로그인 후 복사
      rm $PID_FILE
로그인 후 복사
로그인 후 복사
      RET_VAL=$?
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
      else  echo "Could not find PID file $PID_FILE"
로그인 후 복사
      RET_VAL=1
로그인 후 복사
로그인 후 복사
    fi
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  ;;
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  restart)
로그인 후 복사
    if [[ -r $PID_FILE ]]
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    then  kill `cat $PID_FILE`
로그인 후 복사
      rm $PID_FILE
로그인 후 복사
로그인 후 복사
      RET_VAL=$?
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    else
로그인 후 복사
로그인 후 복사
로그인 후 복사
      echo "Could not find PID file $PID_FILE"
로그인 후 복사
    fi
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    $PHP_SCRIPT
로그인 후 복사
    RET_VAL=$?
로그인 후 복사
  ;;
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  status)
로그인 후 복사
    if [[ -r $PID_FILE ]]
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    then
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
      echo "php-fastcgi running with PID `cat $PID_FILE`"
로그인 후 복사
      RET_VAL=$?
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
    else
로그인 후 복사
로그인 후 복사
로그인 후 복사
      echo "Could not find PID file $PID_FILE, php-fastcgi does not appear to be running"
로그인 후 복사
    fi
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  ;;
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
  *)
로그인 후 복사
    echo "Usage: php-fastcgi {start|stop|restart|status}"
로그인 후 복사
    RET_VAL=1
로그인 후 복사
  ;;
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
esac
로그인 후 복사
exit $RET_VAL 
로그인 후 복사

Start php-fastcgi and nginx by issuing the following commands:

chmod +x /etc/init.d/php-fastcgi
로그인 후 복사
update-rc.d php-fastcgi defaults
로그인 후 복사
/etc/init.d/php-fastcgi start
로그인 후 복사
/etc/init.d/nginx start 
로그인 후 복사

Test PHP with FastCGI

Create a file called "test.php" in your site's "public_html" directory with the following contents:

File:/srv/www/example.com/public_html/test.php

<?php phpinfo(); ?> 
로그인 후 복사

When you visit http://www.example.com/test.php in your browser, the standard "PHP info" output is shown. Congratulations, you've configured the nginx web server to use PHP-FastCGI for dynamic content!

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

 

The nginx Homepage FastCGI Project Homepage PHP Documentation Basic Ngnix Configuration
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Laravel의 플래시 세션 데이터로 작업합니다 Laravel의 플래시 세션 데이터로 작업합니다 Mar 12, 2025 pm 05:08 PM

Laravel은 직관적 인 플래시 방법을 사용하여 임시 세션 데이터 처리를 단순화합니다. 응용 프로그램에 간단한 메시지, 경고 또는 알림을 표시하는 데 적합합니다. 데이터는 기본적으로 후속 요청에만 지속됩니다. $ 요청-

PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법 PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법 Mar 14, 2025 am 11:42 AM

PHP 클라이언트 URL (CURL) 확장자는 개발자를위한 강력한 도구이며 원격 서버 및 REST API와의 원활한 상호 작용을 가능하게합니다. PHP CURL은 존경받는 다중 프로모토콜 파일 전송 라이브러리 인 Libcurl을 활용하여 효율적인 execu를 용이하게합니다.

Laravel 테스트에서 단순화 된 HTTP 응답 조롱 Laravel 테스트에서 단순화 된 HTTP 응답 조롱 Mar 12, 2025 pm 05:09 PM

Laravel은 간결한 HTTP 응답 시뮬레이션 구문을 제공하여 HTTP 상호 작용 테스트를 단순화합니다. 이 접근법은 테스트 시뮬레이션을보다 직관적으로 만들면서 코드 중복성을 크게 줄입니다. 기본 구현은 다양한 응답 유형 단축키를 제공합니다. Illuminate \ support \ Facades \ http를 사용하십시오. http :: 가짜 ([ 'google.com'=> ​​'Hello World', 'github.com'=> ​​[ 'foo'=> 'bar'], 'forge.laravel.com'=>

Codecanyon에서 12 개의 최고의 PHP 채팅 스크립트 Codecanyon에서 12 개의 최고의 PHP 채팅 스크립트 Mar 13, 2025 pm 12:08 PM

고객의 가장 긴급한 문제에 실시간 인스턴트 솔루션을 제공하고 싶습니까? 라이브 채팅을 통해 고객과 실시간 대화를 나누고 문제를 즉시 해결할 수 있습니다. 그것은 당신이 당신의 관습에 더 빠른 서비스를 제공 할 수 있도록합니다.

PHP에서 늦은 정적 결합의 개념을 설명하십시오. PHP에서 늦은 정적 결합의 개념을 설명하십시오. Mar 21, 2025 pm 01:33 PM

기사는 PHP 5.3에 도입 된 PHP의 LSB (Late STATIC BING)에 대해 논의하여 정적 방법의 런타임 해상도가보다 유연한 상속을 요구할 수있게한다. LSB의 실제 응용 프로그램 및 잠재적 성능

프레임 워크 보안 기능 : 취약점 보호. 프레임 워크 보안 기능 : 취약점 보호. Mar 28, 2025 pm 05:11 PM

기사는 입력 유효성 검사, 인증 및 정기 업데이트를 포함한 취약점을 방지하기 위해 프레임 워크의 필수 보안 기능을 논의합니다.

프레임 워크 사용자 정의/확장 : 사용자 정의 기능을 추가하는 방법. 프레임 워크 사용자 정의/확장 : 사용자 정의 기능을 추가하는 방법. Mar 28, 2025 pm 05:12 PM

이 기사에서는 프레임 워크에 사용자 정의 기능 추가, 아키텍처 이해, 확장 지점 식별 및 통합 및 디버깅을위한 모범 사례에 중점을 둡니다.

See all articles