웹사이트 검색

PHP 및 MariaDB로 Nginx 설치(MySQL 대체) - LEMP - Ubuntu 15.10(Wiley Werewolf)


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • Ubuntu 14.04 LTS(Trusty Tahr)

이 페이지에서

  1. 1 서문
  2. 2 MariaDB 설치하기\n
  3. 3 Nginx 설치
  4. 4 PHP5 설치
  5. 5 Nginx 구성
  6. 6 PHP에서 MariaDB/MySQL 지원 받기
  7. 7 PHP-FPM에서 TCP 연결 사용
  8. 8 이 튜토리얼의 가상 머신 이미지 다운로드
  9. 9개의 링크

Nginx("엔진 x"로 발음)는 무료 오픈 소스 고성능 HTTP 서버입니다. Nginx는 안정성, 풍부한 기능 세트, 간단한 구성 및 낮은 리소스 소비로 유명합니다. 이 튜토리얼은 PHP 지원(PHP-FPM을 통해) 및 MariaDB(MySQL 대체)를 사용하여 Ubuntu 15.10 서버에 Nginx를 설치하는 방법을 보여줍니다. LEMP라는 용어는 Linux + Nginx + MySQL + PHP를 의미합니다.

1 서문

이 자습서에서는 호스트 이름 server1.example.com을 IP 주소 192.168.1.100과 함께 사용합니다. 이러한 설정은 사용자에 따라 다를 수 있으므로 적절하게 교체해야 합니다.

2 MariaDB 설치하기

MySQL 대신 MariaDB를 설치하겠습니다. MariaDB는 원래 MySQL 설립자 Monty Widenius가 유지 관리하는 MySQL 포크로 MySQL에 비해 속도와 기능이 약간 향상되었습니다. MariaDB를 설치하려면 다음을 실행하십시오.

sudo apt-get -y install mariadb-server mariadb-client

MariaDB에는 현재 루트 사용자에 대한 비밀번호가 설정되어 있지 않습니다. mysql_secure_installation 명령을 실행하여 비밀번호를 구성하고 테스트 데이터베이스를 제거하십시오.

sudo mysql_secure_installation

다음과 같은 질문을 받게 됩니다.

Enter current password for root (enter for none): <-- press enter
Set root password? [Y/n] <-- y
New password: <-- Enter the new MariaDB root password here
Re-enter new password: <-- Repeat the password
Remove anonymous users? [Y/n] <-- y
Disallow root login remotely? [Y/n] <-- y
Reload privilege tables now? [Y/n] <-- y

3 Nginx 설치

Nginx는 Ubuntu 15.10용 패키지로 제공됩니다. Apache 웹 서버가 서버에 설치되어 충돌이 발생할 수 있습니다. Apache가 실행되고 있지 않은지 확인합니다.

ps aux | grep apache2

이 명령이 결과를 반환하지 않으면 Nginx 설치를 진행합니다.

위의 명령이 다음 명령을 사용하여 일부 Apache 프로세스를 표시하는 경우 Apache를 제거하십시오.

sudo systemctl stop apache2.service
sudo systemctl disable apache2.service
sudo apt-get remove apache2

이제 nginx를 설치할 수 있습니다.

sudo apt-get install nginx

나중에 Nginx를 시작합니다.

sudo systemctl start nginx.service
sudo systemctl enable nginx.service

웹 서버 IP 주소 또는 호스트 이름을 브라우저에 입력하면(예: http://192.168.1.100) 다음 페이지가 표시됩니다.

해당 페이지에 Nginx 대신 "Apache2"가 표시되는 이유는 무엇입니까? 페이지의 텍스트는 실제 실행 중인 웹 서버를 반영하지 않습니다. 위 페이지는 Apache 및 Nginx에서 사용하는 문서 루트 /var/www/html/의 Ubuntu 기본 페이지일 뿐입니다. Nginx가 실제로 이 페이지를 제공하는지 확인하려면 Firefox 네트워크 분석 콘솔에서 HTTP 헤더를 살펴보십시오.

또는 nginx가 실행 중인 셸을 확인합니다.

ps aux | grep nginx

결과에는 다음과 같은 nginx 프로세스가 표시되어야 합니다.

4 PHP5 설치하기

PHP-FPM을 통해 Nginx에서 PHP5가 작동하도록 만들 수 있습니다. PHP-FPM(FastCGI Process Manager)은 다음과 같이 설치하는 모든 크기의 사이트, 특히 더 바쁜 사이트에 유용한 몇 가지 추가 기능이 있는 대체 PHP FastCGI 구현입니다.

sudo apt-get install php5-fpm

PHP-FPM은 소켓 /var/run/php5-fpm.sock에서 FastCGI 서버를 실행하는 데몬 프로세스(init 스크립트 php5-fpm 포함)입니다.

5 Nginx 구성

Nginx 구성은 이제 nano 편집기에서 여는 /etc/nginx/nginx.conf 파일에 있습니다.

sudo nano /etc/nginx/nginx.conf

구성은 이해하기 쉽습니다(자세한 내용은 여기에서 확인할 수 있습니다: http://wiki.nginx.org/NginxFullExample2).

먼저(선택 사항임) keepalive_timeout을 적절한 값으로 설정합니다.

[...]
    keepalive_timeout   2;
[...]

가상 호스트는 서버 {} 컨테이너에 정의됩니다. 기본 가상 호스트는 /etc/nginx/sites-available/default 파일에 정의되어 있습니다. 다음과 같이 수정할 수 있습니다.

sudo nano /etc/nginx/sites-available/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
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.php index.html index.htm index.nginx-debian.html;

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;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

서버 이름 _; 이것을 기본 포괄 가상 호스트로 만듭니다(물론 여기에 www.example.com과 같은 호스트 이름을 지정할 수도 있습니다).

인덱스 라인에 index.php를 추가했습니다. 루트 /usr/share/nginx/html; 문서 루트가 /usr/share/nginx/html 디렉토리임을 의미합니다.

PHP에서 중요한 부분은 위치 ~ \\.php${} 스탠자입니다. 활성화하려면 주석을 해제하십시오. 제로데이 악용을 방지하려면 PHP 위치 스탠자 내에서 "include snippets/fastcgi-php.conf;" 줄을 활성화하는 것이 중요합니다(http://forum.nginx.org/read.php? 2,88845,페이지=3).

이제 파일을 저장하고 nginx를 다시 로드합니다.

sudo service nginx reload

다음 열기 /etc/php5/fpm/php.ini...

sudo nano /etc/php5/fpm/php.ini

... 그리고 cgi.fix_pathinfo=0을 설정합니다:

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

PHP-FPM 다시 로드:

sudo service php5-fpm reload

이제 문서 루트 /var/www/html에 다음 PHP 파일을 만듭니다.

sudo nano /var/www/html/info.php
<?php
phpinfo();
?>

이제 브라우저에서 해당 파일을 호출합니다(예: http://192.168.1.100/info.php).

보시다시피 PHP5는 작동 중이며 서버 API 라인에 표시된 것처럼 FPM/FastCGI를 통해 작동합니다. 더 아래로 스크롤하면 PHP5에서 이미 활성화된 모든 모듈이 표시됩니다. MySQL은 목록에 없습니다. 즉, PHP5에서 아직 MySQL을 지원하지 않습니다.

6 PHP에서 MariaDB/MySQL 지원 받기

PHP에서 MySQL 지원을 받으려면 php5-mysqlnd 패키지를 설치할 수 있습니다. 다른 PHP5 모듈을 설치하는 것이 좋으며 애플리케이션에 필요할 수도 있습니다. 다음과 같이 사용 가능한 PHP5 모듈을 검색할 수 있습니다.

apt-cache search php5

필요한 것을 선택하고 다음과 같이 설치하십시오.

sudo apt-get install php5-mysqlnd php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl

APCu는 PHP 중간 코드를 캐싱하고 최적화하기 위한 무료 PHP opcode 캐셔입니다. eAccelerator 및 Xcache와 같은 다른 PHP opcode 캐셔와 유사합니다. PHP 페이지 속도를 높이려면 이 중 하나를 설치하는 것이 좋습니다.

APC는 다음과 같이 설치할 수 있습니다.

sudo apt-get install php5-apcu

이제 PHP-FPM을 다시 로드합니다.

sudo service php5-fpm reload

이제 브라우저에서 http://192.168.1.100/info.php를 다시 로드하고 모듈 섹션으로 다시 스크롤하십시오. 이제 MySQL 모듈을 포함하여 많은 새 모듈을 찾을 수 있습니다.

7 PHP-FPM이 TCP 연결을 사용하도록 만들기

기본적으로 PHP-FPM은 소켓 /var/run/php5-fpm.sock에서 수신 대기합니다. PHP-FPM이 TCP 연결을 사용하도록 만드는 것도 가능합니다. 이렇게 하려면 /etc/php5/fpm/pool.d/www.conf...를 엽니다.

sudo nano /etc/php5/fpm/pool.d/www.conf

... 다음과 같이 청취 라인을 만듭니다.

[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]

이렇게 하면 PHP-FPM이 IP 127.0.0.1(localhost)의 포트 9000에서 수신 대기하게 됩니다. 시스템에서 사용하지 않는 포트를 사용하고 있는지 확인하십시오.

그런 다음 PHP-FPM을 다시 로드합니다.

sudo php5-fpm reload

다음으로 nginx 구성과 모든 가상 호스트를 살펴보고 fastcgi_pass unix:/var/run/php5-fpm.sock; 줄을 변경합니다. to fastcgi_pass 127.0.0.1:9000;, 예. 이와 같이:

sudo nano /etc/nginx/sites-available/default
[...]
 
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
}
[...]

마지막으로 nginx를 다시 로드합니다.

sudo service nginx reload

8 이 튜토리얼의 가상 머신 이미지 다운로드

이 튜토리얼은 VMWare 및 Virtualbox와 호환되는 ovf/ova 형식의 가상 머신 이미지를 사용할 준비가 된 상태로 제공됩니다. 가상 머신 이미지는 다음 로그인 세부 정보를 사용합니다.

SSH/쉘 로그인

사용자 이름: administrator
비밀번호: howtoforge

이 사용자는 sudo 권한이 있습니다.

마리아DB 로그인

사용자 이름: root
비밀번호: howtoforge

VM의 IP는 192.168.1.100이며 /etc/network/interfaces 파일에서 변경할 수 있습니다. 위의 모든 암호를 변경하여 가상 머신을 보호하십시오.

9 링크

  • nginx: http://nginx.net/
  • nginx 위키: http://wiki.codemongers.com/Main
  • PHP: http://www.php.net/
  • PHP-FPM: http://php-fpm.org/
  • MySQL: http://www.mysql.com/
  • 우분투: http://www.ubuntu.com/