CentOS 编译安装 OpenResty

安装约定

openresty源码路径:/usr/local/src
openresty安装路径:/usr/local/openresty
openresty配置文件路径:/usr/local/openresty/nginx/conf/nginx.conf
openresty虚拟主机路径:/usr/local/openresty/nginx/conf/vhosts

源码包下载地址

http://openresty.org/cn/download.html

下载源代码包

# cd /usr/local/src/
# wget https://openresty.org/download/openresty-1.13.6.2.tar.gz

安装gcc、make等

# yum -y install gcc gcc-c++ make

安装编译nginx所需的库

# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel

添加nginx用户和组

# groupadd -g 500 nginx
# useradd -u 500 -g nginx -c "Nginx web server" -d /var/lib/nginx -s /sbin/nologin nginx

安装nginx

# cd ..
# tar zxvf openresty-1.13.6.2.tar.gz
# cd openresty-1.13.6.2
# ./configure --prefix=/usr/local/openresty \
--user=nginx \
--group=nginx \
--with-luajit \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_v2_module \
--with-http_addition_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-pcre \
--with-stream
# make
# make install

配置nginx启动脚本

# cat >/usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

启动nginx

# systemctl start nginx

配置nginx开机启动

# systemctl enable nginx

设置nginx环境变量

# vim /etc/profile

在其文件末尾添加如下变量

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

或者用以下命令添加

# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/openresty/nginx/sbin' /etc/profile

运行如下命令使环境变量生效

# source /etc/profile