CentOS7, PHP7 설치 및 nginx와 연동하기
"CentOS7, PHP7 설치 및 nginx와 연동하기"
---------------------------------------------------------------------------------------------------
* 테스트 환경
- CentOS7 + nginx 1.12.0 + php7
---------------------------------------------------------------------------------------------------
1. PHP설치를 위해 repository 추가
- IUS repo: 엔터프라이즈 환경을 위한 최신 버전의 Php, Python, Mysql 등 패키지를 제공
# curl 'https://setup.ius.io/' -o setup-ius.sh |
- ius 설치 스크립트 실행
# bash setup-ius.sh |
2. PHP7 설치
# yum install php71u-fpm-nginx php71u-cli php71u-mysqlnd php71u-gd php71u-mbstring php71u-xml php71u-opcache |
3. php-fpm/www.conf 파일 수정
- php-fpm: fast CGI 모듈
# vim /etc/php-fpm.d/www.conf ... ... listen = /run/php-fpm/www.sock // 주석 삭제, socket 통신을 하겠다고 설정함 ... ... listen.acl_users = nginx // 주석 삭제, 사용자 설정 (웹 데몬 설정) |
4. nginx의 php-fpm.conf 파일 수정
# vim /etc/nginx/conf.d/php-fpm.conf upstream php-fpm { #server 127.0.0.1:9000; // 주석처리 server unix:/run/php-fpm/www.socket; // 주석제거 } |
5. /etc/nginx/conf.d/default.conf 파일 수정
# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name 192.168.0.111; root /usr/share/nginx/html; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { 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 { } # 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 php-fpm; 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; #} }
|
참고) nginx 1.10 일 경우
- /etc/nginx/nginx.conf.default파일을 conf.d로 복사 (하나하나 다 입력하기 귀찮으니까..)
# cd /etc/nginx/ # cp nginx.conf.defaul conf.d/default.conf |
- server {} 부분을 제외하고 모두 주석처리 또는 삭제! (nginx.conf파일과 중복되기때문)
# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
// locations ~\.php$ {} 주석 삭제 location ~ \.php$ { root html; fastcgi_pass php-fpm; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } } |
6. php-fpm 과 nginx 재시작
# systemctl restart php-fpm # systemctl restart nginx |
7. php-fpm, nginx 동작 확인
# systemctl status php-fpm # systemctl status nginx |
8. php 테스트 파일 생성
# vim /usr/share/nginx/html/info.php <?php phpinfo(); ?> |
9. php파일 실행
http://서버IP/info.php |