OpenResty安装

结合其他文章,根据自己实践,记录学习

 

OpenResty 介绍

OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。

OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。

360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。

 

1、 安装编译工具、依赖库

 

居于 ubuntu14.04 安装 OpenResty。

OpenResty 依赖库有: perl 5.6.1+, libreadline, libpcre, libssl。

所以我们需要先安装好这些依赖库,也非常简单:

apt-get install libreadline-dev libpcre3-dev libssl-dev perl

如果你的系统是 Centos 或 RedHat 可以使用以下命令:

yum -y install readline-devel pcre-devel openssl-devel gcc


2、 下载openresty-1.13.6.1.tar.gz 源码包,并解压;

下载ngx_cache_purge模块,该模块用于清理nginx缓存;

下载nginx_upstream_check_module模块,该模块用于ustream健康检查

wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
tar -zxvf openresty-1.13.6.1.tar.gz
cd openresty-1.13.6.1/bundle

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz

wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz


3、配置需安装的模块

# ./configure --help可查询需要安装的模块


./configure --prefix=/usr/local/openresty --with-luajit --with-http_ssl_module --user=root --group=root --with-http_realip_module --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/

其中:add-module安装了刚才下载的两个模块。

 

有些环境会报错:

在的OpenResty®(通过 Lua 扩展 NGINX 实现的可伸缩的 Web 平台)时出现如下错误:

you need to have ldconfig in your PATH env when enabling luajit
解决方法:编辑configure 文件,将can_run("ldconfig")改成can_run("/sbin/ldconfig")


4、编译安装

# you can change the parallism number 2 below to fit the number of spare CPU cores in your machine.

make -j2 && make install


5、制作https证书

mkdir -p /usr/local/openresty/nginx/cert
cd /usr/local/openresty/nginx/cert
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt


6、修改nginx.conf配置

lua_package_path '/usr/local/openresty/lualib/?.lua;/usr/local/openresty/nginx/lua/?.lua;;'; #lua文件默认路径
lua_package_cpath '/usr/local/openresty/lualib/?.so;;'; #so文件默认路径
...
server {
    listen       192.168.0.100:8080;
    listen       192.168.0.100:8443 ssl;
    server_name  localhost;

    ssl_certificate /usr/local/openresty/nginx/cert/server.crt;
    ssl_certificate_key /usr/local/openresty/nginx/cert/server.key;
    ...
    ...
    ...
}


7、启动Nginx服务,查看进程及连接情况

/usr/local/openresty/nginx/sbin/nginx
ps -ef | grep nginx
netstat -ltpe | grep nginx


8、访问测试Nginx

curl -k https://192.168.0.100:8443
curl -i http://192.168.0.100:8080



9、开机自启动

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

#systemctl start nginx.service
systemctl enable nginx.service
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

 

10. 例子

 10.1 lua例子

      location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }

10.2 共享内存例子

定义共享内存100MB,nginx.conf配置文件http域增加如下指令代码:

 http {
     lua_shared_dict share_mem_cache 100m;
     ...
 }

使用共享内存:   

 location /set {
        content_by_lua_block {
        local share_mem_cache  = ngx.shared.share_mem_cache
        share_mem_cache:set("name", "xiaoming") 
        ngx.say("STORED")
        }   
    }   

    location /get {
        content_by_lua_block {
        local share_mem_cache  = ngx.shared.share_mem_cache
        ngx.say(share_mem_cache:get("name"))
        }   
    } 

    location /capacity {
        content_by_lua_block {
            require "resty.core.shdict"
            local share_mem_cache  = ngx.shared.share_mem_cache
            ngx.say(share_mem_cache:capacity())
        }   
    }

另外,ngx_lua还提供很多相对安全的api接口操作共享内存。

get_stale()
safe_set()
safe_add()


结束

参考资料: 
http://jinnianshilongnian.iteye.com/blog/2186270 
https://github.com/thinkerpeng/lua-nginx-module
https://blog.csdn.net/pengweixiang/article/details/79111802 
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值