源码编译openresty(Compiling openresty/nginx from source)
Table of Contents
openresty相当于是支持lua的nginx(它本质还是nginx),最后创建一个符号链接叫openresty。
准备工作
先安装一些编译工具
# debian / ubuntu
apt install -y gcc make autoconf automake g++ git
# redhat / centos
yum install -y gcc make autoconf automake g++ git
gmake是GNU Make的缩写。 Linux系统环境下的make就是GNU Make,之所以有gmake,是因为在别的平台上,make一般被占用,GNU make只好叫gmake了。 比如在安装二进制文件进行编译时要使用make命令,但如果在Solaris或其他非GNU系统中运行,必须使用GNU make,而不是使用系统自带的make版本,这时要用gmake代替make进行编译。
如果make报错,则可以执行make clean
后重新编译(当然要在解决报错原因后再编译)。
编译openresty
下载最新源码包:openresty releases
解压
tar -zxf openresty-1.15.8.3.tar.gz
进入解压后的openresty目录里
cd openresty-1.15.8.3
查看有什么编译选项
./configure --help
确定要填写哪些编译选项(具体要什么还是靠经验,不然只能网上查),然后再编译
./configure \
--prefix=/usr/local/openresty \
--user=www \
--group=www \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-openssl=./openssl-OpenSSL_1_1_1g \
--with-pcre=./pcre-8.43 \
--with-pcre-jit \
--with-ld-opt=-ljemalloc \
--with-stream \
--with-stream_ssl_preread_module
# gmake(如果没有gmake那就用make)
gmake
# gmake install(如果没有gmake那就用make install)
gmake install
上边configure命令如果你直接运行肯定报错,因为以下两个表示当前目录(即openresty源码目录)下的文件夹,然而你去看看,并没有这两个目录,这需要自己下载
./openssl-OpenSSL_1_1_1g
./pcre-8.43
openssl在这里,用wget下载后解压,进入目录,然后用以下命令编译安装
./Configure --prefix=/usr/local/openssl linux-x86_64
make && make install
设置符号链接到/usr/local/bin/
,这样就添加到环境变量了
ln -s /usr/local/openssl/bin/openssl /usr/local/bin/openssl
直接输入openssl
回车运行,应该会报错
openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
上边的报错是因为找不到动态链接库,我们添加一下就行
echo '/usr/local/openssl/lib' >> /etc/ld.so.conf.d/local.conf
# 再运行一下这个ldconfig,相当于配置文件更新了,要重新加载一下
ldconfig
其中/usr/local/openssl/lib
就是openssl动态链接库的路径,如果你不知道在哪而,直接全局搜索吧
sudo find / -name libssl.so.1.1
然后再次运行openssl
,回车,应该就没问题了,使用version看看对不对,我这里编译的是1.1.1g
,所以没错,最后用exit
退出
OpenSSL> version
OpenSSL 1.1.1g 21 Apr 2020
OpenSSL> exit
pcre在这里,用wget下载后,解压、编译、make&&make install,不过更方便的是直接安装,直接安装就可以去掉这个选项--with-pcre=./pcre-8.43
,它会自动搜索
#redhat/centos
yum install -y pcre pcre-devel
# debian/ubuntu
sudo apt -y install libpcre3 libpcre3-dev
有些系统不带zlib,需要安装zlib,--with-http_gzip_static_module
选项需要使用(gzip压缩用)
#redhat/centos
yum install -y zlib zlib-devel
# debian/ubuntu
sudo apt -y install zlib1g zlib1g-dev
with-ld-opt=-ljemalloc
,ld
是个命令,man ld
可以查到它的意思是“linker”,中文翻译为“链接器”,链接器是用于把目标文件链接起来生成可执行文件。
这里ld-opt
就是“链接器选项”,-ljemalloc
其中的-l
表示library,而jemalloc
才是真正要连接的一个库的名称,它是一个开源内存管理工具(github)。
malloc其实是memory allocation的缩写,就是说它是一个内存分配管理工具,用它来管理nginx的内存分配,可以优化nginx对内存的使用,事实上这个工具还可以用来管理mysql等很多软件(只要那些软件支持用jemalloc
来管理),至于为什么要叫jemalloc
,这只是个项目名称,可能只有作者才知道。
要使用jemalloc,要先编译安装jemalloc
# 下载jemalloc,版本可能不是最新,要自己去下载最新的
wget http://soft.xiaoz.org/linux/jemalloc-5.2.0.tgz
tar -zxvf jemalloc-5.2.0.tgz
cd jemalloc-5.2.0
# 安装jemalloc
./configure
make && make install
# 这一步看情况,有可能local.conf本来就已经存在/usr/local/lib了
echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf
# 这是一个命令,用于配置动态链接库运行时的绑定(相当于安装了个新的东西,要绑定一下)
ldconfig
把openssl、pcre、zlib、jemalloc等依赖全部安装好之后再运行一遍前面的编译安装命令
./configure \
--prefix=/usr/local/openresty \
--user=www \
--group=www \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-openssl=./openssl-OpenSSL_1_1_1g \
--with-pcre=./pcre-8.43 \
--with-pcre-jit \
--with-ld-opt=-ljemalloc \
--with-stream \
--with-stream_ssl_preread_module
# gmake(如果没有gmake那就用make)
gmake
# gmake install(如果没有gmake那就用make install)
gmake install
如果一切正常,到这里就完成了openresty的编译安装。
安装好之后的配置
创建一个名为www的用户和www组(以下命令是创建www用户,但组会自动伴随创建)
useradd -s /sbin/nologin -M www
创建用于放置网站源码的wwwroot
目录以及存放openresty(nginx)日志的wwwlogs
(目录放哪儿是个人习惯,我这里是参考oneinstack的)
mkdir -p /data/wwwroot
mkdir -p /data/wwwlogs
# 修改所有者和所属组为www,以后放到/data/里的网站文件也都必须属于www用户和组
chown -R www:www /data/
修改nginx配置文件(前面说过openresty本质运行的还是nginx)
vim /usr/local/openresty/nginx/conf/nginx.conf
在顶部修改以下配置(其中http模块那里表示在http模块最后添加include
指令)
user www www;
error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/openresty.pid;
http {
……
include vhost/*.conf;
}
创建vhost目录,用于放置网站配置
sudo mkdir /usr/local/openresty/nginx/conf/vhost/
放置的网站配置必须以.conf
结尾,如果不以.conf
结尾,则不会被使用,因为前面用的是(include vhost/*.conf;
)
www.test.com.conf
www.test2.com.conf
创建systemd service文件,用于开机自启动
vim /usr/lib/systemd/system/openresty.service
:set paste
进入粘贴模式,然后按i
进入插入模式,然后把以下内容粘贴进去,按esc退出粘贴模式,最后:x
保存
[Unit]
Description=OpenResty® is a dynamic web platform based on NGINX and LuaJIT.
Documentation=https://openresty.org/en/
After=network.target
[Service]
Type=forking
PIDFile=/var/run/openresty.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000
[Install]
WantedBy=multi-user.target
启动openresty
# 启动
systemctl start openresty
# 查看启动状态
systemctl status openresty
# 重启
systemctl start openresty
# 停止
systemctl stop openresty

