(WordPress) PHP accelerator OPcache+APCu 설치해 성능 개선하기

Linux

PHP파일은 실행할 때 다음과 같은 순서로 처리됩니다.

  1. PHP파일 액세스
  2. 컴파일
  3. 중간 코드 생성
  4. 실행

WordPress에서 사용하는 PHP파일을 액세스 할 때 마다 매번 위의 과정을 매번 거쳐야 하기 때문에 처리 시간이 많이 걸립니다. 이 글에서는 이 성능 문제를 개선하는 방법을 소개합니다.

PHP accelerator에 대해서

PHP accelerator는 PHP 액세스를 고속화 하기 위한 확장 기능입니다.

PHP accelerator의 여러 확장 모듈 중 OPcacheAPCu의 확장 기능을 이용하면 PHP파일에 처음 액세스 할 때는 컴파일을 해서 생성된 중간 코드를 메모리 캐쉬에 저장하고 실행합니다.

그러나 두 번째 액세스부터는 PHP파일 액세스, 컴파일 , 중간 코드 저장을 생략하고 메모리 캐쉬에 저장된 중간 코드실행하기만 하기 때문에 많은 처리 시간을 단축할 수 있습니다.

참고로 OPcache는 소스 코드 전반을 관리하며 APCu는 객체, 변수를 관리합니다.

현재 웹사이트 성능을 측정하기

다음 명령의 마지막 인수 사이트 URL https://www.sample.com/ 는 예시이므로, 실제로 성능 개선을 하고자 하는 사이트 URL을 지정합니다.

$ ab -n 100 -c 10 https://www.sample.com/

결과는 이 글의 마지막 부분에서 성능 개선 후 측정치와 비교하겠습니다.

현재 확장 모듈 확인하기

다음 명령으로 10-opcache.ini, 40-apcu.ini가 설치되어 있지 않음을 확인합니다.

$ sudo php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:
/etc/php.d/20-bz2.ini,
/etc/php.d/20-calendar.ini,
... 중간 생략 ...
/etc/php.d/30-xmlrpc.ini,
/etc/php.d/40-imagick.ini,
/etc/php.d/40-zip.ini

현재 PHP버전 확인하기

다음 명령으로 현재 PHP버전을 확인합니다.

$ php -v
PHP 7.4.9 (cli) (built: Aug  4 2020 08:28:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

확장 모듈 설치하기

다음 명령으로 확장 모듈 opcacheapcu 를 설치합니다.
[주의] 위에서 확인한 php버전에 맞는 저장소(Repository) remi-php74를 입력해야 합니다.

$ sudo yum install --enablerepo=epel,remi,remi-php74 php-opcache php-pecl-apcu

설치 결과 로그는 여기를 확인 하십시오. 참고로 확장 모듈 opcacheapcu 설치와 동시에 php버전이 PHP 7.4.9 에서 PHP 7.4.23 으로 업데이트 되었음을 확인할 수 있습니다. 업데이트는 사용자 환경에 따라 다를 수 있습니다.

설치 후 PHP버전 확인하기

$ php -v
PHP 7.4.23 (cli) (built: Aug 24 2021 16:33:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.23, Copyright (c), by Zend Technologies

설치 후 확장 모듈 확인하기

$ php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:
/etc/php.d/10-opcache.ini,
/etc/php.d/20-bz2.ini,

/etc/php.d/30-xmlrpc.ini,
/etc/php.d/40-apcu.ini,
/etc/php.d/40-imagick.ini,
/etc/php.d/40-zip.ini

서버 재기동 & 적용 결과 확인

모듈을 적용하기 위해 서버를 재기동합니다.

$ sudo systemctl restart httpd

https://워드프레스호스트명/phpinfo.php를 사용하여 php 환경 정보에 zip가 존재하는 것을 확인합니다.

  • 확장 기능 적용 전 phpinfo는 여기를 확인하십시오
  • 확장 기능 적용 후 phpinfo는 여기를 확인하십시오

성능 측정 비교

다음과 같이 확장 기능 적용 전 성능확장 기능 적용 후 성능을 비교하면 비약적인 성능 향상이 이루어진 것을 확인할 수 있습니다.

확장 기능 적용 전 성능

다음은 OPcache+APCu확장 기능 설치 전 측정한 성능입니다.

$ ab -n 100 -c 10 https://tech.zinnunkebi.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking tech.zinnunkebi.com (be patient).....done


Server Software:        Apache/2.4.6
Server Hostname:        tech.zinnunkebi.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256

Document Path:          /
Document Length:        56681 bytes

Concurrency Level:      10
Time taken for tests:   263.220 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      5710800 bytes
HTML transferred:       5668100 bytes
Requests per second:    0.38 [#/sec] (mean)
Time per request:       26322.005 [ms] (mean)
Time per request:       2632.200 [ms] (mean, across all concurrent requests)
Transfer rate:          21.19 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        4  107 133.0     59     673
Processing:  2565 25307 14923.0  23612   65457
Waiting:      353 21136 13617.5  19589   56946
Total:       2569 25415 14955.0  24048   65488

Percentage of the requests served within a certain time (ms)
  50%  24048
  66%  30794
  75%  36268
  80%  39989
  90%  45961
  95%  53937
  98%  63731
  99%  65488
 100%  65488 (longest request)

확장 기능 적용 후 성능

다음은 OPcache+APCu확장 기능 설치 후 & 서버 재기동 후 측정한 성능입니다.

$ ab -n 100 -c 10 https://tech.zinnunkebi.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking tech.zinnunkebi.com (be patient).....done

Server Software:        Apache/2.4.6
Server Hostname:        tech.zinnunkebi.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256

Document Path:          /
Document Length:        56681 bytes

Concurrency Level:      10
Time taken for tests:   18.225 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      5711000 bytes
HTML transferred:       5668100 bytes
Requests per second:    5.49 [#/sec] (mean)
Time per request:       1822.527 [ms] (mean)
Time per request:       182.253 [ms] (mean, across all concurrent requests)
Transfer rate:          306.01 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        4   20  71.1     11     718
Processing:   999 1741 297.1   1736    2615
Waiting:       65  402 200.6    461     705
Total:       1004 1761 313.7   1757    2647

Percentage of the requests served within a certain time (ms)
  50%   1757
  66%   1854
  75%   1938
  80%   1991
  90%   2084
  95%   2461
  98%   2633
  99%   2647
 100%   2647 (longest request)

전체 목록

AWS EC2 웹 서비스(Wordpress) 구축 전체 글 목록
EC2(Elastic Compute Cloud) 웹 서비스 구축 전체 글 워드프레스 설치 , 환경 설정 전체 글
제목과 URL을 복사했습니다