이전에 포스팅을 한 대로 PHP와 Nginx까지 설치를 다 하셨으면 이제는 PHP와 Nginx를 연동해 줘야 합니다. 그래야 본인이 작성하는 PHP 코드도 로컬 환경에서 테스트 가능하게 됩니다.
Nginx와 PHP 연동의 경우 윈도우 환경이든 리눅스 환경이든 설정하는 방법은 거의 비슷하다고 생각해 주셔도 괜찮습니다. 실행을 설정하고 하는 부분이 차이가 조금 있을 뿐이지 문서를 수정하거나 하는 부분은 거의 비슷합니다.
1. Nginx의 폴더에서 conf 폴더를 열어 nginx.conf 파일을 연다. 아마 이게 아파치에서는 .htaccess와 비슷할 겁니다.
1) server 부분에 location 첫번째에 index.php 추가
2) 아래쪽 location에 php FastCGI 부분에 주석 전부 해제
3) fastcgi_param에 SCRIPT_FILENAME 뒷부분에 $document_root로 경로 수정
4) fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 이렇게 수정되면 됩니다.
5) cmd로 다시 nginx 폴더를 열고 서비스를 재시작합니다. -> nginx -s reload
6) 예시는 아래와 같습니다.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root F:/Documents/Development/Projects/example;
#root html;
index index.php index.html index.htm index.php;
}
#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 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$ {
root F:/Documents/Development/Projects/example;
#root html;
fastcgi_pass 127.0.0.1:9000;
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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
7) 기본 nginx.conf에서 변경한 부분으로 # 처리된 부분은 주석처리입니다.
2. PHP를 설치한 곳으로 이동하여 cmd를 열고 명령어를 실행합니다.
1) 명령어는 php-cgi.exe -b "127.0.0.1:9000" -c php.ini
2) 작업관리자를 실행시켜 프로세스에 CGI / FastCGI가 활성화 되어있는지 확인합니다.
3) nginx의 html 폴더로 이동하여 index.php를 작성해서(localthost/index.php로 작동시켜도 됩니다.) 연동을 확인합니다.
4) 해당 폴더로 매번 이동하여 명령어 입력을 하는 부분이 번거로웠기 때문에 이전 포스팅 중 PHP 설치에 배치 파일로 만들어서 실행하는 방법을 올려 놓았으니 그걸 참고하셔도 됩니다.
3. nginx 기본 폴더 변경
1) nginx를 설치하면 기본 폴더가 nginx 하위 폴더에 있는 html로 되어 있는데 이 기본 폴더를 변경해야 할 때 쓰는 설정입니다.
2) nginx.conf를 열어서 location에 root 경로(50x.html 부분 제외)를 원하는 폴더가 있는 경로로 수정합니다. 하지만 경로를 그대로 복사해서 붙여넣으면 안 되고 역슬래시(\)를 슬래시(/)로 바꿔서 입력합니다.
3) nginx 및 php-cgi 서비스를 재시작합니다.
4) 위의 코드 형식으로 붙여 넣은 부분을 참고하시면 더 쉽게 바꾸실 수 있을 거라 생각되네요.
PHP와 Nginx 연동에 대해 윈도우에서도 간단하게 할 수 있으니 처음 설치를 하면서 힘드셨던 분들에게 도움이 되었으면 좋겠습니다.
'프로그래밍 & 개발 > PHP' 카테고리의 다른 글
phpMyAdmin 설치(윈도우 11 환경) (0) | 2025.04.07 |
---|---|
윈도우 환경(윈도우 11)에서 Nginx 설치 (0) | 2025.04.04 |
윈도우 환경(윈도우 11 사용)에서 PHP 설치 (0) | 2025.04.03 |