如何编译一个高性能 OpenResty

介绍

本教程将介绍如何一步一步手动编译 OpenResty,OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,拥有非常好的拓展性让服务器发挥更好性能。
教程中将全部依赖 Linux 发行版组建中的依赖,而免除编译带来的后期维护成本。

如何编译一个高性能 OpenResty

拓展

  1. OpenSSL 1.0.2,提供 ALPN 支持,支持 HTTP/2
  2. Nginx-CT,透明证书提高 HTTPS 网站的安全性和浏览器支持
  3. ngx_PageSpeed,Google 家的网站性能优化工具
  4. Brotli,实现比 Gzip 更高的压缩率
  5. Jemalloc,优化内存管理

环境

本教程以,Ubuntu 16.04 LTS 64位版 为例。

依赖包

更新系统软件源缓存顺便升级组件

apt update
apt upgrade -y

条件

您应该在系统中安装了perl 5.6.1 +,libpcre,libssl。对于Linux,还应确保ldconfig在PATH环境中。

设定版本变量

如果软件版本更新后,为了方便起见,后续修改版本号只需修改下面的变量即可。在 SSH终端 中输入:

# Version
# OpenSSLVersion='openssl-1.0.2n'//Ubuntu 16.04 系统默认为 1.0.2
NginxCTVersion='1.3.2';
PageSpeedVersion='1.12.34.3';
SystemBit='X64';
OpenRestyVersion='openresty-1.13.6.1';

上述软件版本更新查看:OpenSSL、Nginx-CT、PageSpeed、OpenResty

安装依赖组件

sudo apt-get install libpcre3-dev libssl-dev perl make build-essential curl
apt install build-essential libreadline-dev libncurses5-dev libpcre3 libpcre3-dev libssl-dev zlib1g-dev unzip git perl make libjemalloc1 libjemalloc-dev libxml2 libxml2-dev libxslt-dev libxml2 libxml2-dev libxslt-dev

下载源码

这里将 OpenResty 所需的源代码均放置在 /root/src 目录下,方便管理。

cd /root
mkdir src
cd src

下载 OpenResty 和其拓展的源代码:

## Ubuntu 16.04 默认已经提供 1.0.2
##wget https://www.openssl.org/source/$OpenSSLVersion.tar.gz
##tar xzf $OpenSSLVersion.tar.gz

wget https://github.com/grahamedgecombe/nginx-ct/archive/v$NginxCTVersion.tar.gz
tar xzf v$NginxCTVersion.tar.gz

git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
cd ../

wget https://github.com/apache/incubator-pagespeed-ngx/archive/v$PageSpeedVersion-stable.zip
unzip v$PageSpeedVersion-stable.zip
cd incubator-pagespeed-ngx-$PageSpeedVersion-stable/
wget https://dl.google.com/dl/page-speed/psol/1.12.34.2-$SystemBit.tar.gz
tar -xzvf 1.12.34.2-$SystemBit.tar.gz
cd ../

wget -c https://openresty.org/download/$OpenRestyVersion.tar.gz
tar zxf $OpenRestyVersion.tar.gz

编译

./configure 配置

cd $OpenRestyVersion

./configure  
 --prefix=/usr/local/openresty 
 --user=www 
 --group=www 
 --with-pcre-jit 
 --with-stream  
 --with-stream_ssl_module 
 --with-stream=dynamic  
 --with-file-aio  
 --with-threads  
 --with-cc-opt="-O3"  
 --with-http_v2_module  
 --with-http_realip_module  
 --with-http_mp4_module  
 --with-http_gzip_static_module  
 --with-http_ssl_module 
 --with-http_stub_status_module  
 --with-http_xslt_module 
 --with-http_iconv_module   
 --without-http_redis2_module 
 --with-openssl-opt=enable-tlsext 
 -j8

错误一

./configure: error: the HTTP XSLT module requires the libxml2/libxslt

解决办法:sudo apt-get install libxml2 libxml2-dev libxslt-dev

git clone https://github.com/google/ngx_brotli.git

编译

现在您可以使用命令编译所有内容

make

如果您的机器有多个内核并且您的 make 支持 jobserver 功能,您可以像这样并行编译:

make -j2

假设您有 2 个 CPU 内核。

安装

如果前面的所有步骤都没有问题

sudo make install

在 Linux 上,通常需要 sudo 才能获得 root 访问权限。

设置变量

默认情况下,OpenResty安装在前缀/usr/local/openresty/中。最后,您需要将OpenResty提供的命令行实用程序添加到PATH环境中,如下所示:

export PATH=/usr/local/openresty/bin:/usr/local/openresty/nginx/sbin:$PATH

如果您正在使用bash。最好将此行添加到Shell的启动脚本中,例如〜/ .bashrc〜/ .bash_profile

或者以下方式

[ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=/usr/local/openresty/nginx/sbin:$PATH" >> /etc/profile
[ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep /usr/local/openresty/ /etc/profile`" ] && sed -i "s@^export PATH=(.*)@export PATH=/usr/local/openresty/nginx/sbin:1@" /etc/profile
. /etc/profile

后面就可以用,nginx -t 检测配置是否正确,nginx -s reload 重载 Nginx 了。

设置服务和开机启动

创建 /etc/systemd/system/openresty.service 文件,内容:

cd /etc/systemd/system/
wget https://gist.githubusercontent.com/ivmm/dbf03e6c7970488652878bb8ddc3a775/raw/48436d911d08e57774c759bdb50548dec31dc86f/openresty.service

下载内容如下

# Stop dance for OpenResty
# A modification of the Nginx systemd script
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the Nginx process.
# If, after 5s (--retry QUIT/5) OpenResty is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if OpenResty is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# Nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A dynamic web platform based on Nginx and LuaJIT.
After=network.target

[Service]
Type=forking
#PIDFile=/run/openresty.pid
ExecStartPre=/usr/local/openresty/bin/openresty -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;'
ExecReload=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/openresty.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

编辑 /usr/local/openresty/nginx/conf/nginx.conf 文件为:

cd /usr/local/openresty/nginx/conf/
rm nginx.conf -rf
wget https://gist.githubusercontent.com/ivmm/ab81dee184b64036bd4b8d5abe676264/raw/1cbfbc387aa956f6d9afe39d60e2b8c988a10688/nginx.conf

重新加载 systemd 服务,以便它可以找到我们的文件:

systemctl daemon-reload

通过 systemd 重启 OpenResty:

systemctl restart openresty

设置开机启动:

systemctl enable openresty

打开你的服务器 IP,就能看到安装好的 OpenResty 提示页了

如何编译一个高性能 OpenResty


原文始发于微信公众号(开源技术小栈):如何编译一个高性能 OpenResty

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/248435.html

(0)
李, 若俞的头像李, 若俞

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!