Search
Close this search box.

Nginx和PHP的Brotli扩展

Nginx

官方brotli组件链接:

https://github.com/google/ngx_brotli

安装brotli组件

查看已安装的nginx版本,目前安装的版本为1.26.2。下载对应版本官方包,并解压。

				
					nginx -v
				
			
				
					wget https://nginx.org/download/nginx-1.26.2.tar.gz
tar -xzvf nginx-1.26.2.tar.gz && rm nginx-1.26.2.tar.gz
				
			

安装编译所需环境

				
					apt install -y gcc libpcre3-dev zlib1g zlib1g-dev libpcre2-dev make cmake git
				
			

克隆ngx_brotli仓库及其子模块并构建依赖

				
					git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli
cd ngx_brotli/deps/brotli
mkdir out && cd out
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed ..
cmake --build . --config Release --target brotlienc
cd ../../../..
				
			

编译模块并安装

				
					cd nginx-1.26.2
./configure --with-compat --add-dynamic-module=../ngx_brotli
make modules
cp ./objs/*.so /etc/nginx/modules
				
			

在配置文件中加载brotli模块

				
					vi /etc/nginx/nginx.conf
				
			
				
					load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
				
			
				
					user  www-data;
worker_processes  auto;

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
...
				
			

:wq保存退出并重启nginx

				
					systemctl restart nginx
				
			

PHP

官方brotli组件链接:

https://github.com/kjdev/php-ext-brotli

git克隆仓库并编译

				
					git clone --recursive --depth=1 https://github.com/kjdev/php-ext-brotli.git
cd php-ext-brotli
phpize
./configure
make
make install
				
			

编译完成后启用php的brotili并重启

				
					cat >> /etc/php/8.2/mods-available/brotli.ini <<EOF
extension=brotli.so
EOF
				
			
				
					ln -s /etc/php/8.2/mods-available/brotli.ini /etc/php/8.2/fpm/conf.d/20-brotli.ini
				
			
				
					systemctl restart php8.2-fpm
				
			
Share the Post:

Related Posts