安装OpenResty开发环境
OpenResty是一個(gè)基于?Nginx?與 Lua 的高性能 Web 平臺(tái),其內(nèi)部集成了大量精良的 Lua 庫(kù)、第三方模塊以及大多數(shù)的依賴項(xiàng)。用于方便地搭建能夠處理超高并發(fā)、擴(kuò)展性極高的動(dòng)態(tài) Web 應(yīng)用、Web 服務(wù)和動(dòng)態(tài)網(wǎng)關(guān)(摘自官網(wǎng))。本文將會(huì)介紹如何在Centos7上,安裝Nginx+Lua的開(kāi)發(fā)環(huán)境,并運(yùn)行一個(gè)“Hello World”示例。
一、環(huán)境安裝
1.1 創(chuàng)建工作路徑
我計(jì)劃將Openresty安裝到/usr/servers下,首先創(chuàng)建這個(gè)文件夾。
[root@localhost ~]# mkdir -p /usr/servers [root@localhost ~]# cd /usr/servers/ [root@localhost servers]#1.2 安裝依賴庫(kù)
[root@localhost servers]# yum install readline-devel pcre-devel openssl-devel perl make gcc -y1.3 下載Nginx及要安裝的模塊
其中,ngx_cache_purge模塊用于清理nginx緩存,nginx_upstream_check_module用于ustream的健康檢查。
[root@localhost servers]# pwd /usr/servers [root@localhost servers]# wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz [root@localhost servers]# tar -xzvf ngx_openresty-1.7.7.2.tar.gz [root@localhost servers]# cd /usr/servers/ngx_openresty-1.7.7.2/bundle [root@localhost bundle]# pwd /usr/servers/ngx_openresty-1.7.7.2/bundle [root@localhost bundle]# wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz [root@localhost bundle]# tar -xvf 2.3.tar.gz [root@localhost bundle]# cd /usr/servers/ngx_openresty-1.7.7.2/bundle [root@localhost bundle]# pwd /usr/servers/ngx_openresty-1.7.7.2/bundle [root@localhost bundle]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz [root@localhost bundle]# tar -xvf v0.3.0.tar.gz1.4?安裝LuaJIT
[root@localhost bundle]# cd /usr/servers/ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120/ [root@localhost LuaJIT-2.1-20150120]# pwd /usr/servers/ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120 [root@localhost LuaJIT-2.1-20150120]# make clean && make && make install [root@localhost LuaJIT-2.1-20150120]# ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit [root@localhost LuaJIT-2.1-20150120]# lua -v Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio [root@localhost LuaJIT-2.1-20150120]# luajit -v LuaJIT 2.1.0-alpha -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/1.5 安裝ngx_openresty
[root@localhost LuaJIT-2.1-20150120]# cd /usr/servers/ngx_openresty-1.7.7.2 [root@localhost ngx_openresty-1.7.7.2]# pwd /usr/servers/ngx_openresty-1.7.7.2 [root@localhost ngx_openresty-1.7.7.2]# ./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2 [root@localhost ngx_openresty-1.7.7.2]# make && make install至此,基本的環(huán)境已經(jīng)安裝完成了,nginx可執(zhí)行文件為:/usr/servers/nginx/sbin/nginx,可以通過(guò)/usr/servers/nginx/sbin/nginx -V 命令來(lái)查看nginx的版本及安裝的模塊。
二、環(huán)境配置及示例
?2.1 配置nginx.conf
nginx.conf是Nginx的主配置文件,所有的配置都是從這里開(kāi)始的。我的計(jì)劃是將lua腳本、依賴庫(kù)配置、與lua相關(guān)的server、location等配置都放到外部的目錄中,這樣不用每次都動(dòng)到nginx.conf。修改/usr/servers/nginx/conf/nginx.conf,如下:
[root@localhost ~]#?cat?/usr/servers/nginx/conf/nginx.confworker_processes 1;error_log logs/error.log; events {worker_connections 1024; }http {include mime.types; default_type text/html; #lua模塊路徑lua_package_path "/usr/example/lualib/?.lua;;"; #lua 模塊 lua_package_cpath "/usr/example/lualib/?.so;;"; #c模塊 include /usr/example/example.conf; }
2.2 配置example.conf
[root@localhost ~]# mkdir /usr/example [root@localhost ~]# vim /usr/example/example.conf [root@localhost ~]# cat /usr/example/example.conf server {listen 80;server_name _;location /lua {default_type 'text/html';lua_code_cache off;content_by_lua_file /usr/example/lua/test.lua;} }2.3 示例——test.lua
[root@localhost ~]# mkdir /usr/example/lua [root@localhost ~]# vim /usr/example/lua/test.lua [root@localhost ~]# cat /usr/example/lua/test.lua ngx.say("hello world");2.4 驗(yàn)證
首先啟動(dòng)nginx,你會(huì)發(fā)現(xiàn)nginx給了一個(gè)警告,這句警告是因?yàn)樵?usr/example/example.conf的第7行上,我們關(guān)閉了lua_code_cache。這個(gè)配置默認(rèn)是打開(kāi)的,即Nginx在啟動(dòng)的時(shí)候會(huì)將lua腳本都cache起來(lái),這樣系統(tǒng)在調(diào)用這些腳本時(shí)就會(huì)很快。而關(guān)閉這個(gè)配置的話,nginx每次調(diào)用都會(huì)重新load一遍,會(huì)對(duì)性能有一定的影響。因?yàn)槲覀兪窃陂_(kāi)發(fā)環(huán)境,我們不希望更改腳本之后就重啟nginx,所以就把這個(gè)配置給關(guān)閉了。
[root@localhost ~]# /usr/servers/nginx/sbin/nginx nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/example/example.conf:7接下來(lái)在瀏覽器中訪問(wèn)對(duì)應(yīng)的路徑,你會(huì)得到如下的結(jié)果。(提示:若沒(méi)有出現(xiàn),可以檢查一下系統(tǒng)防火墻)
?
備注:
centos下還可以通過(guò)yum的方式安裝openresty,但在國(guó)內(nèi)的網(wǎng)絡(luò)環(huán)境下,這種方式時(shí)靈時(shí)不靈(國(guó)外的主機(jī)上就沒(méi)有問(wèn)題),我懷疑是國(guó)內(nèi)網(wǎng)絡(luò)的問(wèn)題。有興趣的同學(xué)可以嘗試下。具體命令如下:
yum install yum-utils yum-config-manager --add-repo https://openresty.org/yum/centos/OpenResty.repo yum install openresty?
轉(zhuǎn)載于:https://www.cnblogs.com/zhenyuyaodidiao/p/7061706.html
總結(jié)
以上是生活随笔為你收集整理的安装OpenResty开发环境的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java笔试题之《流行的框架与新技术》
- 下一篇: 微赞开发有感