보통 웹을 찍먹해보고 싶거나, 이 일을 시작한지 얼마 안된 주니어 분들은 아주 밑의 명령어들을 남발을 했을 것이라고 생각함
# dnf install nginx mysql
# dnf install php php-fpm
nginx와 php 간에는 유닉스 소켓 방식과 TCP/IP 기반 소켓으로 통신을 할 수 있는데
이론상? 소켓 통신이 속도가 상대적으로 빠르다고는 하지만 유의미한 차이는 크게 느끼기 어렵다고 나는 생각하고
만약 느리다?라고 느껴진다면 차라리 환경 구성이나 코드를 개선을 하는게 맞지 않나 싶다
어쨌든 nginx와 php가 통신을 하려면 nginx.conf와 www.conf를 설정을 해줘야함
이유는 글을 작성하면서 설명하겠다리
일단 어떤 블로그들을 보면 소켓통신이 실제 속도 차이가 많이 난다고 적어놓은 곳이 있는데, 처음부터 개념을 이렇게 잡아가면 힘들지 않을까라고 생각함
먼저 nginx와 php, php-fpm이 준비가 되어있다고 가정한 상태에서 진행한다
나는 호스트가 여러개 나눠질 것을 대비해서 관리를 위해 nginx.conf 파일에는 이렇게 적었다
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream test {
server 127.0.0.1:9000;
}
include vhosts/*.conf;
}
일단 upstream쪽은 무시해라, TCP/IP 기반으로 포트 통신을 하는 글을 포스팅을 할 때 다시 언급할 예정이당
일단 핵심은 include 쪽에 있는데 실질적인 소켓 통신 파일을 vhosts 디렉터리를 만들어서 그 안에서 구성을 했다.
/usr/local/nginx/conf/vhosts/9001.conf (conf 파일명은 자기가 관리하는거니 알아서)
server {
listen 80;
server_name localhost;
root /usr/local/nginx/html;
index index.php index.html index.htm;
access_log logs/9001_access_log;
error_log logs/9001_error_log;
location / {
root html;
index index.php index.html index.htm;
}
location ~* \.php$ {
fastcgi_pass unix:/usr/local/php7/var/run/9001.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300s;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
include /usr/local/nginx/conf/fastcgi_params;
}
}
TCP/IP 기반 통신 포스팅에서도 언급할건데, 주의해야 할 점은 locaion ~* \.php$ 부분이다.
그냥저냥 내 글 포함 글들만 보고 베껴쓰지말고 적어도 개념은 알고가라
~* : 모든 문자에 대해
\. : ' . ' 문자
php$ : php로 끝난다
=> .php로 끝나는 모든 파일에 대한 블록을 뜻함
이 블록에서는
fastcgi_pass 부분이 있는데, 소켓 통신을 할 때에는 소켓 경로를 직접 지정해준다.
때문에 TCP/IP를 사용할때와는 다르게 로드밸런싱의 까다로움이 있음
이렇게 해서 nginx 쪽은 설정을 마쳤다.
그럼 이제 php 설정을 해줘야함
각자 설정해놓은 etc 경로에 가서 php-fpm.conf던, 새로 생성하던 상관 없다
나는 php-fpm.d 안에 9001.conf라고 새로 만들었다
[php9001]
user = nobody
group = nobody
listen = /usr/local/php7/var/run/9001.sock
listen.owner = nobody
listen.group = nobody
listen.mode = 0660
pm = dynamic
pm.max_children = 8192
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 15
pm.max_requests = 8192
php_admin_value[error_log] = /usr/local/php7/var/log/fpm-php9901.log
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 2G
php_admin_value[date.timezone] = Asia/Seoul
각 옵션은 php.net에 fpm configuration을 보면 잘 나와있긴한데
pm string
Choose how the process manager will control the number of child processes. Possible values: static, ondemand, dynamic. This option is mandatory.
static - the number of child processes is fixed (pm.max_children).
ondemand - the processes spawn on demand (when requested, as opposed to dynamic, where pm.start_servers are started when the service is started.
dynamic - the number of child processes is set dynamically based on the following directives: pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers.
라고 나와있다.
스태틱은 환경 변화에 대응하기가 어려워서 탈락,
온디맨드는 그때그때 리스타트를 해주면서 옵션값을 주면 기존에 남아있던 세션은 어떡하지? 라는 생각에 탈락,
결국 남은건 동적으로 관리하겠다이고 pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers 옵션 설정에 따라 영향을 받는다고 되어있다
pm.max_children int
The number of child processes to be created when pm is set to static and the maximum number of child processes to be created when pm is set to dynamic. This option is mandatory.
This option sets the limit on the number of simultaneous requests that will be served. Equivalent to the ApacheMaxClients directive with mpm_prefork and to the PHP_FCGI_CHILDREN environment variable in the original PHP FastCGI.
위에 pm 옵션을 dynamic으로 했을 때에 최대 프로세스 수 설정이다. 보면 This option is mandatory라고 되어있다
필수라고 하는데 그 앞에 보면 dynamic으로 설정했을 때에 필수라고 되어있다.
pm.start_servers int
The number of child processes created on startup. Used only when pm is set to dynamic. Default Value: (min_spare_servers + max_spare_servers) / 2.
이것도 동적으로 설정되어있는 경우에 사용된다고 나와있다. 시작할 때 생성되는 프로세스 수
pm.min_spare_servers int
The desired minimum number of idle server processes. Used only when pm is set to dynamic. Also mandatory in this case.
pm.max_spare_servers int
The desired maximum number of idle server processes. Used only when pm is set to dynamic. Also mandatory in this case.
둘다 최소 최대 유휴 프로세스 값을 설정해주는거다. 동적일때 사용되고 필수값이라고 적혀있다
이렇게 설정하고 저장하고 나가자
그리고 실질적인 실행파일인 sbin을 보자
꼭 옵션을 잘 읽어봐라,
내 기준에서 필요한 옵션은 -t (--test)와 -y (--fpm-config)다.
conf 파일 경로를 직접 지정해주고 테스트를 해보자
에러가 나면 에러를 보고 그냥 해결해라, 무슨 에러가 나올지도 모른다
successful이 나오면 이제 -t를 빼고 실행해라
했으면 프로세스를 확인해보자
프로세스가 잘 올라와있다
그럼 테스트를 해보자
nginx의 루트 디렉터리에 test.php 파일을 하나 만들자
# vim /usr/local/nginx/html/test.php
<?php
phpinfo();
?>
바로 접속 ㄱㄱ
접속이 잘 되는걸 볼 수 있다.
다음 포스팅은 TCP/IP 기반 포트로 통신하는 내용을 정리할 예정
'Linux > Rocky 8.10' 카테고리의 다른 글
[Rocky] NGINX PHP TCP/IP 통신 (0) | 2024.12.10 |
---|---|
ssh 키 등록 후에도 접속안됨 (0) | 2024.11.21 |
[Rocky] ssh 패스워드 없이 접속 - 공개키 접속 (1) | 2024.11.04 |
[Rocky] iptables 방화벽 설정 (0) | 2024.09.05 |
[Rocky] firewall 아웃바운드 설정 (0) | 2024.09.02 |