Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

Contents

PHP를 사용하지 않은지 10년은 된 것 같지만, 어쩌다 보니 nginx 서버에 php를 조합해야 할 일이 생겨서 정리한다.

LEMP

예전에는 LAMP(Linux,Apache,Mysql,PHP)였는데, nginx의 사용이 늘어나면서 LEMP(Linux, nginx, MySQL, PHP)가 대세인 것 같다. 나도 요즘에는 nginx만 사용한다. 처음에는 성능 때문에 사용하다가, 가벼움, 단순함 때문에 계속사용하게 됐다. 대략 DevOps 적인 일을 하다보니, 컨테이너와 같은 동적이고 가벼운 환경에 잘 적응 할 수 있는 reverse proxy가 필요했는데, nginx가 필요를 잘 충족시켜줬다.

설치 환경

우분투리눅스 17.04를 기준으로 한다. 모든 명령은 (귀찮아서)루트 권한으로 실행을 한다. /etc/hosts 에 테스트를 위한 도메인을 설정했다.
127.0.0.1 my.example.priv

Nginx 웹서버 설치

거의 5년전 부터 apache를 아예 사용하지 않은 것 같다. 지금은 모든 사이트에 nginx를 이용하고 있다. nginx를 설치하자.
# apt-get install nginx
우분투 리눅스의 경우 설치 즉시, nginx 서버가 실행된다. 브라우저를 이용 http://my.example.priv에 접근해보자. 아래와 같이 ngix 기본 설치화면을 볼 수 있을 것이다.

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

mysql 서버 설치

Mysql은 인터넷 서비스를 통해서 유저에게 제공하기 위한 데이터를 저장하는 저장소의 역할을 한다. 유저정보, 유저의 상품정보, 로그인 정보등 모든 데이터들을 mysql에 저장한다. 간단하게 설치 할 수 있다.
# apt-get install mysql-server
설치 마지막 단계에서 mysql 서버의 root 계정을 위한 패스워드를 묻는다. 적당한 패스워드로 설정한다. root 계정은 시스템의 root 계정과는 상관 없다. mysql의 관리자 계정이라고 보면 된다. 데이터베이스 생성, 삭제, 데이터베이스 유저관리와 같은 모든 일들을 할 수 있다.

php-fpm 설치

# apt-get install php-fpm
# php-fpm7.0 -version
PHP 7.0.22-0ubuntu0.17.04.1 (fpm-fcgi) (built: Aug  8 2017 22:03:30)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.22-0ubuntu0.17.04.1, Copyright (c) 1999-2017, by Zend Technologies
mysql 연동을 위해서 php-mysql 모듈도 설치했다.
# apt-get install php-mysql
php-ftpm 서비스를 실행한다.
# service php7.0-fpm start

Nginx 설정

지금 nginx 설정은 아래와 같을 것이다. 설정파일은 /etc/ngix/site-available/default 이다.
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
     
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;
    
    location / {
        try_files $uri $uri/ =404;
    }
}
php 프로세스를 적용하기 위해서 아래의 작업을 수행해야 한다.
  • 페이지를 명시하지 않을 경우 index.php를 찾도록 한다.
  • server_name을 my.example.priv로 변경한다.
nginx 설정파일을 변경했다.
server {
   listen 80 default_server;
   listen [::]:80 default_server;

   root /var/www/html;

   # Add index.php to the list if you are using PHP
   index index.html index.htm index.nginx-debian.html;

   server_name my.example.com;

   location / {
       try_files $uri $uri/ =404;
   }
   location ~ \.php$ {
   include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
   }
}
test.php 파일을 만들어서 테스트를 했다.
# cat /var/www/html/test.php
<?php
phpinfo();
?>
실행화면이다.