Installing Nginx with PHP 7 and MySQL 5.7 (LEMP) on Ubuntu 16.04 LTS

Nginx(발음 “engine x”)는 무료이며 오픈소스이고 높은 성능의 HTTP 서버이다. Nginx는 매우 적은 자원을 사용하면서 안정적이고 풍부한 기능들을 가지고 있다. 또한 설정도 매우 간단하다. 본 지침서는 우분투 16.04 서버에서 PHP 7(PHP-FPM), MySQL 5.7을 지원하는 Nginx 설치를 설명하고 있다. LEMP = Linux + nginx(engine x) + MySQL + Linux

1. 준비 단계

이 지침서에 호스트 이름은 server1.example.com, 아이피 주소는 192.168.1.100을 사용한다. 이 설정은 사용자마다 다르므로 적절하게 수정 후 사용해야 한다. 이 지침서의 모든 단계는 루트 권한으로 진행하였다. 다음과 같이 루트(root)로 :

sudo -s

2. MySQL 5.7 설치

MySQL을 설치하기 위해서 다음과 같이 실행한다. [root@server1 ~]#은 터미널의 프롬프트이다. 타이핑하는 것은 아니다.

[root@server1 ~]# apt-get -y install mysql-server mysql-client

New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword

데이터베이스의 보안을 위해 익명사용자와 테스트 데이터베이스를 제거하기 위해 다음을 실행한다.

[root@server1 ~]# mysql_secure_installation

위의 명령을 실행 후 다음의 몇 가지 질문에 답을 하자.

Enter password for user root: <-- Enter the MySQL root password
...
Press y|Y for Yes, any other key for No: <-- Press y if you want this function or press Enter otherwise.
...
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <-- Press enter
...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : <-- y
...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <-- y
...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <-- y
...
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <-- y

3. Nginx 설치

Apache2가 이미 설치되어 있다면 Nginx를 설치하기 전에 Apache2를 제거한다.

[root@server1 ~]# service apache2 stop
[root@server1 ~]# update-rc.d -f apache2 remove
[root@server1 ~]# apt-get remove apache2

이제 Nginx를 설치한다.

[root@server1 ~]# apt-get -y install nginx
// 시작은 다음과 같이
[root@server1 ~]# service nginx start

제대로 설치되었는지 확인하기 위해 웹 브라우저에서 주소를 입력해 본다 (예, http://192.168.1.100). 기본 페이지가 보이면 설치 성공이다. 그리고 Nginx의 웹 문서의 기본 디렉터리는 /var/www/html 이다.

4. PHP 7 설치

PHP-FPM(FastCGI Process Manager)를 이용한다1. FastCGI 데몬 소켓은 /run/php/php7.0-fpm.sock 에서 확인할 수 있다.

[root@server1 ~]# apt-get -y install php7.0-fpm

5. Nginx 설정

vi(또는 nano)편집기를 이용하여 다음의 설정파일을 편집한다2.

[root@server1 ~]# vi /etc/nginx/nginx.conf
[root@server1 ~]# vi /etc/nginx/sites-available/default

예를들면 기본 디렉터리는 설정은 root /var/www/html;이다. 그리고 php 연동을 위해 다음과 같이:

[root@server1 ~]# vi /etc/php/7.0/fpm/php.ini
// 다음과 같이 설정한다.
cgi.fix_pathinfo=0:
[root@server1 ~]# service nginx reload
[root@server1 ~]# service php7.0-fpm reload

이제 /var/www/html/에 info.php 파일을 하나 만들고 웹에서 확인 : <?php phpinfo(); ?>

6. PHP에서 MySQL 지원 및 기타 주요기능 설치

[root@server1 ~]# apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache  php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext

[root@server1 ~]# service php7.0-fpm reload

7. 기타 설정 (옵션)

TCP Connection

[root@server1 ~]# vi /etc/php/7.0/fpm/pool.d/www.conf
...
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
...
[root@server1 ~]# php7.0-fpm reload
[root@server1 ~]# vi /etc/nginx/sites-available/default
...
fastcgi_pass 127.0.0.1:9000;
...
[root@server1 ~]# service nginx reload
  1. PHP-FPM의 자세한 내용은 https://php-fpm.org/를 참고하자. ↩︎
  2. 설정파일 예제, https://www.nginx.com/resources/wiki/start/topics/examples/full/ ↩︎
2017/05/21 12:11 2017/05/21 12:11

Trackback Address :: https://youngsam.net/trackback/1878