源码编译openresty(Compiling openresty/nginx from source)

源码编译openresty(Compiling openresty/nginx from source)

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

# 当然如果自带的够新,也可以用apt search libssl,
# 找到libssl-dev来安装(centos自己找是哪个吧,名称未必相同)
apt install libssl-dev

设置符号链接到/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

如果没安装openssl,会显示如下报错

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl= option.


PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。

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

没有安装pcre会报错:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre= option.


有些系统不带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

如果没有安装,会报以下错误

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using –without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using –with-zlib= option.


with-ld-opt=-ljemallocld是个命令,man ld可以查到它的意思是“linker”,中文翻译为“链接器”,链接器是用于把目标文件链接起来生成可执行文件。

这里ld-opt就是“链接器选项”,-ljemalloc其中的-l表示library,而jemalloc才是真正要连接的一个库的名称,它是一个开源内存管理工具(github)。

malloc其实是memory allocation的缩写,就是说它是一个内存分配管理工具,用它来管理nginx的内存分配,可以优化nginx对内存的使用,事实上这个工具还可以用来管理mysql等很多软件(只要那些软件支持用jemalloc来管理),至于为什么要叫jemalloc,这只是个项目名称,可能只有作者才知道。

要使用jemalloc,要先编译安装jemalloc

# 安装依赖
yum -y install gcc gcc-c++
# 下载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的编译安装。

安装好之后的配置

创建以下符号链接

ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/bin/nginx

以后nginx和openresty就是一个意思(openresty太长,我建议还是用nginx好点),因为openresty命令也是nginx的一个符号链接

> which openresty
/usr/local/bin/openresty
> ll /usr/local/bin/openresty
lrwxrwxrwx 1 root root 34 10月 22 18:58 /usr/local/bin/openresty -> /usr/local/openresty/bin/openresty
> ll /usr/local/openresty/bin/openresty
lrwxrwxrwx 1 root root 37 10月 22 18:40 /usr/local/openresty/bin/openresty -> /usr/local/openresty/nginx/sbin/nginx

创建一个名为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 /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 /etc/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=/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

参考:安装Nginx依赖环境和库、Nginx安装,Nginx服务命令

打赏

订阅评论
提醒
guest

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x

扫码在手机查看
iPhone请用自带相机扫
安卓用UC/QQ浏览器扫

源码编译openresty(Compiling openresty/nginx from source)