개발
AWS EC2 인스턴스에 PHP 설치 후 Nginx와 연동하기
⚠ 이 글은 2년 이상 지난 이야기에요. 읽으실때 참고!
이전 글에서 이어지는 내용입니다.
Nginx를 설치했으므로 그 위에서 돌아갈 언어 중 하나인 PHP를 설치해봅시다. Nginx와 연동할 php는 php7.2-fpm 입니다.
$ sudo apt install php7.2
$ sudo apt install php7.2-fpm
$ php -v
PHP 7.2.24-0ubuntu0.18.04.2 (cli) (built: Jan 13 2020 18:39:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
Copy
위 명령줄의 의미는 아래와 같습니다. 실제로는 명령어 사이사이에 무수한 설치 관련 내용이 표시됩니다.
1. PHP 기본 패키지를 설치합니다.
2. PHP-FPM 패키지를 설치합니다.
3. 설치된 PHP 버전을 확인해 설치가 정상적으로 되었음을 확인합니다.
이어서 Nginx 서버 설정에 금방 설치한 PHP-FPM을 연동시켜봅시다. (vi 에디터 이용하셔도 됩니다.)
$ sudo nano /etc/nginx/sites-available/default
Copy
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
#index index.html index.htm index.nginx-debian.html;
index index.php index.html index.htm;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Copy
기존 설정과 다른점은 아래와 같습니다. 확인 후 설정값을 위의 그림처럼 바꿔주세요. 코드 에디터는 nano 에디터를 이용했는데 여러가지 단축키와 기능이 있다만, 가장중요한 코드 작성 후 control+o -> enter로 저장, control+x로 나가기만 잘하시면 됩니다.
여기까지 작성이 완료 되었으면 Nginx 서버를 재로딩 합니다. 앞으로 서버설정을 바꾸실때 꼭 아래와 같이 리로드 해주셔야 바꾼 설정값이 적용됩니다.
$ sudo service nginx reload
Copy
마지막으로 위의 PHP 설정이 제대로 먹혔는지 확인하기위해 아래와 같은 파일을 웹 서버 루트 위치에 index.php란 이름으로 새로 작성해봅시다.
#sudo nano /var/www/html/index.php
<?php
phpinfo();
Copy
그리고 브라우저를 통해 인스턴스 IP로 접속요청을 했을때 아래와 같이 phpinfo() 내용이 보이게 된다면 완벽히 연동에 성공했다고 봅니다!

여기까지 서버 설정을 마치셧다면 AWS EC2 인스턴스를 이용해 Ubuntu 운영체제에 설치된 Nginx 서버에서 PHP 코드를 구동할 수 있는 개발환경이 완성된것입니다.
고생하셨습니다!
#AWS #php
0
개의 댓글
개발 카테고리의 다른 글
02/10/2020
Ubuntu에서 환경변수 PATH 추가 적용하는 방법
우분투 환경에서 개발을 하다보면 PATH에 로컬에 설치한 패키지 폴더를 추가로 입력해야 하는 상황이 발생한다. 그것에 대한 기...

02/10/2020
AWS EC2 인스턴스에 Nginx 설치하기
이전글에서 이어지는 내용입니다. AWS EC2 인스턴스 환경 뿐만이 아니라 리눅스를 기반으로하는 운영체제에서 모두 적용 가능한...

02/10/2020
AWS EC2 인스턴스 생성부터 접속까지
이 포스팅을 시작으로 앞으로 서술될 3개의 포스팅은 AWS EC2 인스턴스를 이용해 Ubuntu 운영체제에 설치된 Nginx 서버에서 PHP...