본문 바로가기

리눅스

우분투에서 NGINX와 PHP-FPM을 설치하고 연동하는 방법

반응형

우분투에서 NGINX와 PHP-FPM을 설치하고 연동하는 방법

테스트 환경

구분 호스트 이름 아이피 주소 비고
NGINX node1 192.168.10.111  
PHP-FPM node2 192.168.10.112  
PHP-FPM node3 192.168.10.113  

NGINX 서버 설정

NGINX 설치

sudo apt-get update
sudo apt-get install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring apt-transport-https
curl -fsSL https://nginx.org/keys/nginx_signing.key \
  | gpg --dearmor | sudo tee /etc/apt/keyrings/nginx_signing.asc >/dev/null
echo "deb [signed-by=/etc/apt/keyrings/nginx_signing.asc] \
  http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \
  | sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt-get update
sudo apt-get install -y nginx
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf_$(date +"%Y%m%d-%H%M%S")
sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf_$(date +"%Y%m%d-%H%M%S")

NGINX 설정

nginx.conf 편집

sudo vim /etc/nginx/nginx.conf
더보기

---

sudo tee /etc/nginx/nginx.conf > /dev/null << 'EOF'
user www-data www-data;
worker_processes auto;

error_log /var/log/nginx/error.log notice;

pid /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include /etc/nginx/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"';

    access_log /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
EOF

---

default.conf 편집

sudo vim /etc/nginx/conf.d/default.conf
더보기

---

sudo tee /etc/nginx/conf.d/default.conf > /dev/null << 'EOF'
server {
    listen 80 default_server;
    server_name _;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;
    
    charset utf-8;

    access_log /var/log/nginx/default.access.log main;
    error_log /var/log/nginx/default.error.log;

    location / {
        try_files $uri $uri/ =404;
    }

    #location ~ \.php$ {
    location ~ \.(php|html|htm)$ {
        root /usr/share/nginx/html;
        fastcgi_index index.php;
        fastcgi_pass 192.168.0.112:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
        access_log /var/log/nginx/nginx_php.access.log;
        error_log /var/log/nginx/nginx_php.error.log;
    }

    location ~ /\.ht {
        deny  all;
    }

    location ~ ^/(nginx_status)$ {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        allow 192.168.0.0/24;
        deny all;
    }

    location ~ ^/(status|ping)$ {
        fastcgi_pass 192.168.0.112:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        access_log off;
        allow 127.0.0.1;
        allow 192.168.0.0/24;
        deny all;
    }
}
EOF

---

NGINX 설정 테스트

sudo nginx -t

NGINX 재시작

sudo systemctl restart nginx

NGINX 서비스 상태 확인

sudo systemctl status nginx

NGINX 상태 페이지 호출

curl http://localhost/nginx_status
728x90

PHP-FPM 서버 설정

PHP-FPM 설치

export PHP_VERSION="8.3"
sudo apt-get update
sudo apt-get install -y zlib1g-dev software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php${PHP_VERSION}-fpm php${PHP_VERSION}-cli \
  php${PHP_VERSION}-common php${PHP_VERSION}-dev
sudo apt-get install -y php-pear php${PHP_VERSION}-gd php${PHP_VERSION}-xml \
  php${PHP_VERSION}-curl php${PHP_VERSION}-igbinary php${PHP_VERSION}-zip
sudo cp /etc/php/${PHP_VERSION}/fpm/php-fpm.conf \
  /etc/php/${PHP_VERSION}/fpm/php-fpm.conf_$(date +"%Y%m%d-%H%M%S")
sudo cp /etc/php/${PHP_VERSION}/fpm/pool.d/www.conf \
  /etc/php/${PHP_VERSION}/fpm/pool.d/www.conf_$(date +"%Y%m%d-%H%M%S")
sudo mkdir -p /var/log/php-fpm

PHP-FPM 설정 파일

php-fpm.conf 편집

sudo vim /etc/php/${PHP_VERSION}/fpm/php-fpm.conf
더보기

---

sudo tee /etc/php/${PHP_VERSION}/fpm/php-fpm.conf > /dev/null << EOF
include = /etc/php/${PHP_VERSION}/fpm/pool.d/*.conf

[global]
pid = /run/php/php-fpm.pid
error_log = /var/log/php-fpm/php-fpm.log
daemonize = yes
EOF

---

www.conf 편집

sudo vim /etc/php/${PHP_VERSION}/fpm/pool.d/www.conf
더보기

---

sudo tee /etc/php/${PHP_VERSION}/fpm/pool.d/www.conf > /dev/null << 'EOF'
[www]
; 사용자와 그룹 설정
user = www-data
group = www-data

; 소켓과 권한 설정
listen = 0.0.0.0:9000
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
;listen.allowed_clients = 127.0.0.1

; 프로세스 관리 설정
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

; Health check 경로 설정
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
EOF

---

phpinfo() 파일 생성

sudo mkdir -p /usr/share/nginx/html
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/phpinfo.php

nginx 및 php-fpm 정보 페이지

sudo vim /usr/share/nginx/html/index.php
더보기

---

sudo tee /usr/share/nginx/html/index.php > /dev/null << 'EOF'
<?php
// NGINX 정보
$nginx_ip = $_SERVER['SERVER_ADDR'];
$nginx_root = $_SERVER['DOCUMENT_ROOT'];

// PHP-FPM 정보
$php_fpm_ip = gethostbyname(gethostname());
$php_fpm_version = phpversion();
$php_fpm_root = realpath(dirname(__FILE__));

// PHP-FPM 모듈 정보
ob_start();
phpinfo(INFO_MODULES);
$php_fpm_modules = ob_get_clean();

// 출력
echo "<h1>Server Information</h1>";
echo "<h2>NGINX Information</h2>";
echo "<p><strong>- NGINX IP Address:</strong> {$nginx_ip}</p>";
echo "<p><strong>- NGINX Root Directory:</strong> {$nginx_root}</p>";

echo "<h2>PHP-FPM Information</h2>";
echo "<p><strong>- PHP-FPM IP Address:</strong> {$php_fpm_ip}</p>";
echo "<p><strong>- PHP-FPM Version:</strong> {$php_fpm_version}</p>";
echo "<p><strong>- PHP-FPM Root Directory:</strong> {$php_fpm_root}</p>";

echo "<h2>PHP-FPM Modules</h2>";
echo "<pre>{$php_fpm_modules}</pre>";
?>
EOF

---

PHP-FPM 권한 설정(파일 권한 설정)

sudo chown -R www-data:www-data /usr/share/nginx/html
sudo chmod -R 755 /usr/share/nginx/html

PHP_FPM 설정 테스트

sudo php-fpm${PHP_VERSION} -t

PHP-FPM 서비스 재시작

sudo systemctl restart php${PHP_VERSION}-fpm

PHP-FPM 서비스 상태 확인

sudo systemctl status php${PHP_VERSION}-fpm

FastCGI 서버의 IP와 포트 확인

sudo netstat -plnt | grep 9000

PHP-FPM 상태 페이지 호출

curl http://localhost/status

NGINX, PHP-FPM 삭제

export PHP_VERSION="8.3"
sudo tee remove_package.sh > /dev/null << 'EOF'
#!/bin/bash

sudo apt purge -y nginx
sudo rm -rf /etc/nginx /var/log/nginx /usr/share/nginx

sudo apt purge -y php${PHP_VERSION}-fpm php${PHP_VERSION}-common php${PHP_VERSION}-cli php${PHP_VERSION}-* php-*
sudo rm -rf /etc/php/${PHP_VERSION} /var/log/php-fpm /usr/share/nginx

sudo apt autoremove -y
sudo apt autoclean
EOF
bash remove_package.sh

 

728x90
반응형