이 글은 2년 이상 지난 이야기에요. 읽으실때 참고!
AWS EC2 인스턴스에 PHP 설치 후 Nginx와 연동하기

이전 'AWS EC2 인스턴스에 Nginx 설치하기'에서 이어지는 내용입니다.

 

 

Nginx를 설치했으므로 그 위에서 돌아갈 언어 중 하나인 PHP를 설치해봅시다. Nginx와 연동할 php는 php7.2-fpm 입니다.

 

1
2
3
4
5
6
7
8
$ 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
 
cs

 

위 명령줄의 의미는 아래와 같습니다.  실제로는 line01과 02 그리고 03 사이에 무수한 설치 관련 내용이 표시됩니다.

line01: PHP 기본 패키지를 설치합니다.
line02: PHP-FPM 패키지를 설치합니다.
line03: 설치된 PHP 버전을 확인해 설치가 정상적으로 되었음을 확인합니다.

 

이어서 Nginx 서버 설정에 금방 설치한 PHP-FPM을 연동시켜봅시다. (vi 에디터 이용하셔도 됩니다.)

 

1
2
$ sudo nano /etc/nginx/sites-available/default
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
        #}
}
 
 
 
cs

 

기존 설정과 다른점은 아래와 같습니다. 확인 후 설정값을 위의 그림처럼 바꿔주세요. 코드 에디터는 nano 에디터를 이용했는데 여러가지 단축키와 기능이 있다만, 가장중요한 코드 작성 후 control+o -> enter로 저장, control+x로 나가기만 잘하시면 됩니다.

line24: 주석처리
line25: index.php를 포함한 새로운 루트 인덱스 작성
line32: 주석처리
line33: php-fpm용 파일 리퀘스트 작성
line36~43: php 연동에 대한 전체 내용 작성

 

여기까지 작성이 완료 되었으면 Nginx 서버를 재로딩 합니다. 앞으로 서버설정을 바꾸실때 꼭 아래와 같이 리로드 해주셔야 바꾼 설정값이 적용됩니다.

 

1
2
$ sudo service nginx reload
 
cs

 

마지막으로 위의 PHP 설정이 제대로 먹혔는지 확인하기위해 아래와 같은 파일을 웹 서버 루트 위치에 index.php란 이름으로 새로 작성해봅시다.

 

1
2
3
4
5
#sudo nano /var/www/html/index.php
 
<?php
phpinfo();
 
cs

 

그리고 브라우저를 통해 인스턴스 IP로 접속요청을 했을때 아래와 같이 phpinfo() 내용이 보이게 된다면 완벽히 연동에 성공했다고 봅니다!

 

 

여기까지 서버 설정을 마치셧다면 AWS EC2 인스턴스를 이용해 Ubuntu 운영체제에 설치된 Nginx 서버에서 PHP 코드를 구동할 수 있는 개발환경이 완성된것입니다.

고생하셨습니다!

 

D.2020-02-10 V.477