Ubuntu 16.04 LTS에서 PHP 7 및 MySQL 5.7(LEMP)과 함께 Nginx 설치
이 튜토리얼은 다음 OS 버전에 대해 존재합니다.
- Ubuntu 14.04 LTS(Trusty Tahr)
이 페이지에서
- 1 서문
- 2 MySQL 5.7 설치
- 3 Nginx 설치
- 4 PHP 7 설치
- 5 nginx 구성
- 6 PHP 7에서 MySQL 지원 받기
- 7 PHP-FPM에서 TCP 연결 사용
- 8 링크
PHP-FPM) 및 MySQL 5.7 지원(LEMP=Linux + nginx(\엔진 x\로 발음) + MySQL + PHP).
1 서문
이 자습서에서는 호스트 이름 server1.example.com을 IP 주소 192.168.1.100과 함께 사용합니다. 이러한 설정은 사용자에 따라 다를 수 있으므로 적절하게 교체해야 합니다.
루트 권한으로 이 튜토리얼의 모든 단계를 실행하고 있으므로 루트로 로그인했는지 확인하십시오.
sudo -s
2 MySQL 5.7 설치
MySQL을 설치하기 위해 다음을 실행합니다.
apt-get -y install mysql-server mysql-client
MySQL 루트 사용자의 비밀번호를 입력하라는 메시지가 표시됩니다. 이 비밀번호는 [이메일 보호] 사용자에게 유효하므로 나중에 수동으로 MySQL 루트 비밀번호를 지정할 필요가 없습니다.
MySQL "root" 사용자의 새 비밀번호: <-- yourrootsqlpassword
MySQL "root" 사용자의 비밀번호 반복: <-- yourrootsqlpassword

데이터베이스 서버를 보호하고 익명 사용자 및 테스트 데이터베이스를 삭제하려면 mysql_secure_installation 명령어를 실행하세요.
mysql_secure_installation
다음과 같은 질문을 받게 됩니다.
:~# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: <-- Enter the MySQL root password
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: <-- Press y if you want this function or press Enter otherwise.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <-- Press enter
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : <-- y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <-- y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <-- y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <-- y
Success.
All done!
MySQL은 이제 안전합니다.
3 Nginx 설치
이미 Apache2를 설치한 경우 다음 명령으로 먼저 제거하고 nginx를 설치합니다.
service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
Nginx는 우리가 설치할 수 있는 Ubuntu 16.04용 패키지로 제공됩니다.
apt-get -y install nginx
나중에 nginx를 시작합니다.
service nginx start
웹 서버 IP 주소 또는 호스트 이름을 브라우저에 입력하면(예: http://192.168.1.100) 다음 페이지가 표시됩니다.

Ubuntu 16.04의 기본 nginx 문서 루트는 /var/www/html입니다.
4 PHP 7 설치
다음과 같이 설치하는 PHP-FPM(PHP-FPM(FastCGI Process Manager)은 모든 규모의 사이트, 특히 사용량이 많은 사이트에 유용한 몇 가지 추가 기능이 포함된 대안적인 PHP FastCGI 구현입니다)을 통해 nginx에서 PHP가 작동하도록 할 수 있습니다.
apt-get -y install php7.0-fpm
PHP-FPM은 소켓 /run/php/php7.0-fpm.sock에서 FastCGI 서버를 실행하는 데몬 프로세스(init 스크립트 php7.0-fpm 포함)입니다.
5 nginx 구성
nginx 구성은 지금 여는 /etc/nginx/nginx.conf에 있습니다.
nano /etc/nginx/nginx.conf
구성은 이해하기 쉽습니다(자세한 내용은 여기에서 확인할 수 있습니다: http://wiki.nginx.org/NginxFullExample2).
먼저(선택 사항) keepalive_timeout을 적절한 값으로 조정합니다.
[...] keepalive_timeout 2; [...]
가상 호스트는 서버 {} 컨테이너에 정의됩니다. 기본 가상 호스트는 /etc/nginx/sites-available/default 파일에 정의되어 있습니다. 다음과 같이 수정할 수 있습니다.
nano /etc/nginx/sites-available/default
[...] 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;
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 php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
} [...]
서버 이름 _; 이것을 기본 포괄 가상 호스트로 만듭니다(물론 여기에 www.example.com과 같은 호스트 이름을 지정할 수도 있습니다).
루트 /var/www/html; 문서 루트가 /var/www/html 디렉토리임을 의미합니다.
PHP에서 중요한 부분은 위치 ~ \\.php${} 스탠자입니다. 활성화하려면 주석을 해제하십시오.
이제 파일을 저장하고 nginx를 다시 로드합니다.
service nginx reload
다음 열기 /etc/php/7.0/fpm/php.ini...
nano /etc/php/7.0/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 다시 로드:
service php7.0-fpm reload
이제 문서 루트 /var/www/html에 다음 PHP 파일을 만듭니다.
nano /var/www/html/info.php
<?php phpinfo(); ?>
이제 브라우저에서 해당 파일을 호출합니다(예: http://192.168.1.100/info.php).

보시다시피 PHP 7이 작동 중이며 서버 API 라인에 표시된 것처럼 FPM/FastCGI를 통해 작동합니다. 더 아래로 스크롤하면 PHP에서 이미 활성화된 모든 모듈이 표시됩니다. MySQL은 거기에 나열되어 있지 않습니다. 즉, 아직 PHP에서 MySQL을 지원하지 않는다는 의미입니다.
6 PHP 7에서 MySQL 지원 받기
PHP에서 MySQL 지원을 받으려면 php7.0-mysql 패키지를 설치할 수 있습니다. 다른 PHP 모듈을 설치하는 것이 좋으며 애플리케이션에 필요할 수도 있습니다. 다음과 같이 사용 가능한 PHP 모듈을 검색할 수 있습니다.
apt-cache search php7.0
필요한 것을 선택하고 다음과 같이 설치하십시오.
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
APCu는 PHP 7과 함께 제공되는 PHP Opcache 모듈의 확장으로, APC 캐시를 지원하는 소프트웨어(예: Wordpress 캐시 플러그인)에 대한 일부 호환성 기능을 추가합니다.
APCu는 다음과 같이 설치할 수 있습니다.
apt-get -y install php-apcu
이제 PHP-FPM을 다시 로드합니다.
service php7.0-fpm reload
이제 브라우저에서 http://192.168.1.100/info.php를 다시 로드하고 모듈 섹션으로 다시 스크롤하십시오. 이제 MySQL 모듈을 포함하여 많은 새 모듈을 찾을 수 있습니다.

7 PHP-FPM이 TCP 연결을 사용하도록 만들기
기본적으로 PHP-FPM은 /var/run/php/php7.0-fpm.sock 소켓에서 수신 대기합니다. PHP-FPM이 TCP 연결을 사용하도록 만드는 것도 가능합니다. 이렇게 하려면 /etc/php/7.0/fpm/pool.d/www.conf...를 엽니다.
nano /etc/php/7.0/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을 다시 로드합니다.
php7.0-fpm reload
다음으로 nginx 구성과 모든 가상 호스트를 살펴보고 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 줄을 변경합니다. to fastcgi_pass 127.0.0.1:9000;, 예. 이와 같이:
nano /etc/nginx/sites-available/default
[...] location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
} [...]
마지막으로 nginx를 다시 로드합니다.
service nginx reload
그게 다야. Nginx LEMP 서버가 설치되었습니다.
8 링크
- 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/