본문 바로가기

리눅스

[draft] NGINX와 PHP-FPM을 연동하여 PHP 스크립트를 처리하는 방법

728x90

NGINX와 PHP-FPM을 연동하여 PHP 스크립트를 처리하는 방법

1. NGINX 설치

https://sangchul.kr/754

 

2. PHP-FPM 설치

https://sangchul.kr/763

3. NGINX 설정

nginx(fastcgi_params) 설정

cat <<'EOF' >> /etc/nginx/fastcgi_params

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  PATH_INFO          $fastcgi_path_info;
EOF

(or)

vim /etc/nginx/fastcgi_params
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  PATH_INFO          $fastcgi_path_info;

nginx(default.conf) 설정

  • fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
vim /etc/nginx/conf.d/default.conf
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        try_files $uri =404;
	    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /\.ht {
        deny all;
    }
728x90

4. PHP-FPM(php-fpm.conf, www.conf) 설정

php-fpm.conf 설정

vim /etc/php-fpm.conf
include = /etc/php-fpm.d/*.conf

[global]
pid = /run/php-fpm/php-fpm.pid
;log_level = debug
error_log = /var/log/php-fpm/error.log
daemonize = yes

www.conf 설정

  • listen = /var/run/php-fpm/php-fpm.sock
vim /etc/php-fpm.d/www.conf
[www]
user = nginx
group = nginx

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
;listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

;ping.path = /ping
pm.status_path = /status

request_terminate_timeout = 30
request_slowlog_timeout = 10
slowlog = /var/log/php-fpm/www-slow.log

access.log = /var/log/php-fpm/www-access.log
access.format = "[%t] %m %{REQUEST_SCHEME}e://%{HTTP_HOST}e%{REQUEST_URI}e %f pid:%p TIME:%ds MEM:%{mega}Mmb CPU:%C%% status:%s {%{REMOTE_ADDR}e|%{HTTP_USER_AGENT}e}"

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on

php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache

5. 웹 브라우저에서 테스트

http://localhost
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

 

728x90