WEB & WAS/Nginx

CentOS 7 Nginx 패키지 설치 및 PHP-FPM 연동 (PHP-FPM 8.2)

서버엔지니어 2023. 4. 9.
728x90

yum install -y vim wget epel-release 

yum install -y  http://rpms.remirepo.net/enterprise/remi-release-7.rpm

yum install -y yum-utils

yum update -y

echo "alias vi='vim'" >> /etc/bashrc

source /etc/bashrc


echo "[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true" > /etc/yum.repos.d/nginx.repo

nginx는 stable 라인과, mainline 최신버전이 있는데 안정성을 위해 stable 이 기본으로 되어있습니다.
혹여 모르니 nginx-stable를 선택합시다.
yum-config-manager --enable nginx-stable
yum-config-manager --disable nginx-mainline

yum clean all
yum update -y 
yum install -y nginx

 

# nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

 

yum install -y tree

 

tree /etc/nginx/
/etc/nginx/
├── conf.d
│   └── default.conf
├── fastcgi_params
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params

 

 

vi /etc/nginx/conf.d/default.conf

========================================================

server {
    listen       80;
    server_name  localhost;
    root /usr/share/nginx/html;
    index index.html index.php index.htm

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

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;

        #fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

========================================================

 

PHP 설치 (PHP-FPM)

 

yum-config-manager --enable remi-php82

yum install -y php-bcmath php-bz2 php-cgi php-cli php-curl php-dba php-enchant php-fpm php-gd php-gmp php-imap php-intl php-json php-ldap php-mbstring php-mysql php-odbc php-opcache php-pgsql php-pspell php-readline php-snmp php-soap php-sqlite3 php-tidy php-xml php-xmlrpc php-xsl php-zip php-mysqlnd php-gettext php-common php-pear 

 

# ll /etc/ | grep php
drwxr-xr-x   2 root root     4096 Apr  9 01:02 php.d
-rw-r--r--   1 root root     4908 Mar 15 15:43 php-fpm.conf
drwxr-xr-x   2 root root       21 Apr  9 01:01 php-fpm.d
-rw-r--r--   1 root root    63144 Mar 15 15:43 php.ini
drwxr-xr-x   2 root root     4096 Apr  9 01:02 php-zts.d

 

PHP-FPM 설정파일 확인 주요 설정

 

vi /etc/php-fpm.conf 

 

pid = /run/php-fpm/php-fpm.pid

error_log = /var/log/php-fpm/error.log

syslog.facility = daemon

syslog.ident = php-fpm

log_level = warning

log_limit = 4096

emergency_restart_threshold = 10

emergency_restart_interval = 10

process_control_timeout = 10

systemd_interval = 10

 

PHP-FPM www.conf 설정파일 주요 설정

 

vi /etc/php-fpm.d/www.conf

user = nginx       -> apache 데몬이나 nginx 데몬에 설정된 유저를 같이 적는게 맞다. 

group = nginx     -> apache 데몬이나 nginx 데몬에 설정된 그룹를 같이 적는게 맞다.  (nginx는 없다.)

 

PHP-FPM를 어떻게 운영하는가에 따라 TCP/IP or UDS (Unix Domain Socket)

listen = 127.0.0.1:9000 

또는

listen = /var/run/php-fpm/php-fpm.sock

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

process.dumpable = yes

listen.allowed_clients = 127.0.0.1

 

pm = dynamic

 

공식

min_spare_servers + (max_spare_servers - min_spare_servers) / 2

 

pm.max_children = CPU 코어(쓰레드) 수 × 4, RAM 용량(GB)의 8배 (DB도 돌린다면 4~6배) 중 적은 쪽이 적정값입니다. 
또는 pm.max_children은 (동접수 ÷ 100)

pm.start_servers = pm.max_children의 절반 정도
pm.min_spare_servers = pm.start_servers와 같은 값
pm.max_spare_servers = pm.max_children과 같은 값

 

8코어 16쓰레드 기준

pm.max_children = 64

pm.start_servers = 32

pm.min_spare_servers = 32

pm.max_spare_servers = 64

;pm.process_idle_timeout = 10s; -> pm = ondemand일때만 사용합니다.

pm.max_requests = 500

 

access.log = /var/log/php-fpm/php-access.log

slowlog = /var/log/php-fpm/www-slow.log

request_slowlog_timeout = 10

request_slowlog_trace_depth = 20

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

 

 

 

 

systemctl restart nginx

systemctl enable nginx

systemctl restart php-fpm

systemctl enable php-fpm

'WEB & WAS > Nginx' 카테고리의 다른 글

CentOS 7 Nginx 소스설치 1.22.1  (0) 2023.04.09
Nginx 정리  (0) 2023.03.12

댓글