본 슬라이드는 Windows환경에서 NginX구동을 실습하기 위해, PHP를 예로 들어 진행하고 있습니다. NginX는 PHP 동적웹페이지에 대한 처리보다, 정적 HTTP 서버에 적합 합니다.
본 슬라이드는 시작과 구동에 초점을 맞추고 있습니다. 설정관련 내용은 아래 공식 문서를 참조할 수 있습니다.
http://nginx.org/en/docs/beginners_guide.html
1. PHP를 위한 NginX 시작과 설정
2015. 7. 22
Jinwook Jeong
HTTP and reverse proxy server
2. NginX
실습전 안내사항
• 본 슬라이드는 Windows환경에서 NginX구동을 실습하기 위해, PHP
를 예로 들어 진행하고 있습니다.
– NginX는 PHP 동적웹페이지에 대한 처리보다, 정적 HTTP 서버에 적합
합니다.
• 본 슬라이드는 시작과 구동에 초점을 맞추고 있습니다. 설정관련
내용은 아래 공식 문서를 참조할 수 있습니다.
– http://nginx.org/en/docs/beginners_guide.html
3. NginX
NginX 소개
• NginX 아키텍쳐 관점
– scalability 문제에 최적화된, cost effective architeture
– 기존 방식은 user당 1개의 Thread를 생성했다면, NginXㄴ는 Event-
Driven aynchronous, non-blocking , Single threaded architecture를 채
택함으로 요청에 대한 프로세스 생성을 방지함
– 단일 master에 대해, n개의 worker process가 존재함
– 1개의 worker process는 다수의 유저를 handling할 수 있음
Single Threaded 관점
4. NginX
NginX와 Apache
NginX Apache
아키텍쳐 Event Driven
multitasking
multi-processing
요청시 프로세스 생성 안함 요청마다 생성함
메모리 적게듬 (Low) 많이듬 (High)
적합 리소스 Static Content 처리 Dynamic Content 처리
주요단점
Dynamic Interpreter
가 worker process에 없
어, dynamic content 생
성에 overhead 발생
모듈이 process에 삽입되
어 있어 dynamic
processing 능력이 좋다.
OS지원 일부 (OpenVMS, IBMi 등 지원
하지 못함)
대부분
모듈 적음 많음
개발시작 2002 1995
Scalability시 H/W의존성 없음 있음
5. NginX
NginX 설정
• nginx.conf 파일설정
– c:servernginx-1.9.3confnginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.php;
}
}
index.php를 추가
7. NginX
NginX 설정
• location php 설정부분을 찾아 주석 ‘#’을 모두 제거함
• root 디렉터리 설정
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
주석 ‘#’을 제거한 결과
c:/server/nginx-1.9.3/html
root 디렉터리를 설정
8. NginX
NginX 실행전 PHP 시작
• 시스템 변수 등록
– 환경변수 설정에서 시스템 변수인 Path에 PHP 설치경로를 입력함
• 프로세스 실행
– PHP를 이용하기 위해서는 php-cgi를 프로세스 형태로 실행해야함
– php-cgi –b 127.0.0.1:9000
• 주의 : c:serverphp 에 설치되어있고 Path가 잡혀있지 않다면, 해당 Dir에 가서 실행
• 서비스로 등록
– sc create SERVER_PHP binPath= "service.exe "c:serverPHP5php-cgi.exe -b
127.0.0.1:9000 -c C:serverPHP5php.ini"" type= own start= auto error= ignore
DisplayName= SERVER_PHP
9. NginX
NginX 실행
• 프로세스 실행
– start nginx
• 프로세스 종료
– nginx –s stop
• 프로세스 종료가 되지 않을때 작업관리자프로세스에서 종료
• tasklist /fi "imagename eq nginx.exe" or tasklist /svc
• master process와 worker process를 확인할 수 있음
– 만약, 프로세스 종료를 하고자 할때 다음과 같이 함
» taskkill /f /pid 프로세스아이디
10. NginX
NginX 실행
• 프로세스 종료 배치파일 (nginx_stop.bat)
– 위의 예는 프로세스 ID로 종료하는 방식이며, 개선의 여지가 있음
– master와 slave로 구성되어, 프로세스 확인후 종료가 필요함
– 시간소요를 줄이기 위해 bat 파일을 생성을 고려할 수 있음
tasklist /fi "imagename eq nginx.exe"
set /p Input=Enter some text:
taskkill /f /pid %Input%
pause
11. NginX
NginX 실행 as a Service
• 사용이유
– 부팅시 마다 nginx와 php를 구동시키는것은 번거롭다.
• 콘솔창 기반 백그라운드 실행
– 실행방법
• START /b c:serverPHP5php-cgi.exe -b 127.0.0.1:9000 -c
c:serverPHP5php.ini
• cd c:servernginx-1.9.3
• START /b nginx.exe
– 단점 : 화면에 나타나지 않지만, 콘솔창이 닫히면 종료
12. NginX
NginX 실행 as a Service
• SC
– 서비스컨트롤 명령어실행
• sc create SERVER_NGINX binpath= "service.exe "C:servernginx-
1.9.3nginx.exe -p c:servernginx-1.9.3 "" type= own start= auto error=
ignore DisplayName= SERVER_NGINX
• 주의 : 관리자권한으로 실행해줘야한다.
13. NginX
NginX 실행 as a Service
• SC 명령어
– 실행
• sc start SERVER_PHP
• sc start SERVER_NGINX
– 기타
• 삭제 : start 대신 delete, 중지 : start 대신 stop 입력
services.msc
14. NginX
서비스 등록후 실행이 안된다면...
• service wrapper 다운로드
– http://iroolab.blogspot.kr/2015/08/service-wrapper.html
c:windows 폴더로 복사
15. NginX
PHP 구동확인
• PHP 연동사항을 브라우저에서 확인
– c:/server/nginx-1.9.3/html/phpninfo.php
– phpinfo.php 내용저장
<?php
date_default_timezone_set('Asia/Seoul');
phpinfo();
?>
– 브라우저 오픈후
• localhost/phpinfo.php 로 실행