[php] PHP 압축 설정

웹 애플리케이션을 개발 및 배포할 때, 웹 서버의 성능을 향상시키기 위해 PHP 압축을 설정할 수 있습니다. PHP 압축을 활성화하면 파일 크기를 줄이고 전송 속도를 향상시킬 수 있습니다.

PHP 내장 압축

zlib 압축 활성화

PHP의 내장 압축 기능인 zlib를 사용하여 압축을 활성화할 수 있습니다. PHP 설정 파일 (php.ini)을 열고 다음 구성을 추가합니다.

zlib.output_compression = On

이렇게 설정하면 PHP는 출력 버퍼링을 사용하여 페이지 출력을 압축하고, 클라이언트에게 압축된 콘텐츠를 전달합니다.

웹 서버 설정

Apache mod_deflate 모듈

만약 웹 서버로 Apache를 사용하고 있다면, mod_deflate 모듈을 활성화하여 HTTP 응답을 압축할 수 있습니다. Apache 설정 파일(httpd.conf 또는 .htaccess)을 열고 다음 구성을 추가합니다.

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/json

  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/atom_xml

  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/x-component
</IfModule>

위 설정은 특정 유형의 콘텐츠를 압축하여 전송합니다.

nginx 설정

Nginx를 사용하는 경우 gzip 모듈을 활성화하여 HTTP 응답을 압축할 수 있습니다. Nginx 설정 파일을 열고 다음과 같이 구성합니다.

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss;

마무리

PHP 압축 설정은 웹 서버와 PHP 설정의 조합으로 작동하며, 실제로 웹 페이지를 로드할 때 네트워크 대역폭을 절약하고 빠른 페이지 응답 시간을 제공합니다.

참고 문헌: PHP.net - Output Control Functions