1 安裝ubuntu
首先我們需要實(shí)現(xiàn)的第一個(gè)功能是讀取本地配置,并且實(shí)時(shí)一個(gè)各個(gè)生命周期里讀取配置的插件.并運(yùn)行.
如果你是windows 用戶的話,我并不建議用windows 去安裝部署openresty ,應(yīng)為后期我們的openresty 肯定是要部署在linux 上的,你用來開發(fā)一些插件的時(shí)候需要用到一些庫(kù),這些庫(kù)如果要編譯在windows上的話,意味著你要折騰兩遍.
所以我建議你創(chuàng)建的時(shí)候在ubuntu子系統(tǒng)上去搞.
進(jìn)入 microsoft store 安裝ubuntu
[圖片上傳失敗...(image-17b5f-1624273861648)]
搜索ubuntu 進(jìn)行安裝 ,安裝完成后重啟系統(tǒng).
[圖片上傳失敗...(image-fd26e1-1624273861648)]
Win10中安裝Ubuntu子系統(tǒng)后默認(rèn)是沒有開啟SSH服務(wù)的榕茧,需要手動(dòng)配置開啟崎坊,
如何安裝win10的linux子系統(tǒng)可以參考這篇文章:https://blog.csdn.net/zhouzme/article/details/78780479
2 開通ssh
先通過 bash 進(jìn)入子系統(tǒng)修改配置
vi /etc/ssh/sshd_config
如果文件不存在說明尚未安裝,則執(zhí)行安裝
apt-get install openssh-server
繼續(xù)修改配置蚪战,下面以密碼登錄的配置作說明:
Port = 22 # 默認(rèn)是22端口皱炉,如果和windows端口沖突或你想換成其他的否則不用動(dòng)
ListenAddress 0.0.0.0 # 如果需要指定監(jiān)聽的IP則去除最左側(cè)的井號(hào)庸推,并配置對(duì)應(yīng)IP蒜茴,默認(rèn)即監(jiān)聽PC所有IP
PermitRootLogin no # 如果你需要用 root 直接登錄系統(tǒng)則此處改為 yes
PasswordAuthentication no # 將 no 改為 yes 表示使用帳號(hào)密碼方式登錄
主要配置以上幾項(xiàng)即可
然后啟動(dòng) ssh 服務(wù)
service ssh start
這個(gè)時(shí)候再用 xshell 就能連接子系統(tǒng)了
[圖片上傳失敗...(image-2acd49-1624273861648)]
windows 還能直接訪問ubuntu 里的文件系統(tǒng)
C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\usr\local\apps\openresty
3 此處略過openresty 安裝
4 共享目錄調(diào)整
他在這個(gè)目錄下.
我們只需要把我們的openresty 安裝在ubuntu里
luarocks 也安裝在子系統(tǒng)里
然后在idea 里去編輯這個(gè)目錄里的文件就可以了
在安裝的時(shí)候要注意,我們的這個(gè)ubuntu子系統(tǒng)是沒有 make gcc 的
需要手動(dòng)安裝的
我們把我們的luademo 工程遷移到
C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\usr\local\apps\openresty下
修改nginx 的配置
增加如下配置
lua_code_cache off;
include /usr/local/apps/openresty/luademo/conf/nginx-lua.conf;
將日志定位到工程目錄下
access_log /usr/local/apps/openresty/luademo/logs/access_8089.txt;
error_log /usr/local/apps/openresty/luademo/logs/error_8089.txt info;
修改
vi ~/.bash_profile
export PATH=$PATH:/usr/local/apps/openresty/nginx/sbin
nginx -s reload 執(zhí)行成功
5 日志轉(zhuǎn)移
為了方便開發(fā)我們需要把日志轉(zhuǎn)移到工程目錄下 方便用idea 打開日志查看
http {
include mime.types;
default_type application/octet-stream;
log_format main '
remote_user [
request" '
'
body_bytes_sent "$http_referer" '
'"
http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
access_log /usr/local/apps/openresty/luademo/logs/access.txt;
error_log /usr/local/apps/openresty/luademo/logs/error.txt info;
server {
listen 8080;
server_name localhost;
charset koi8-r;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
6 luarocks 安裝 以及l(fā)fs 安裝使用
安裝前先備份 openresty
sudo tar -cvf openresty.tar openresty
zip
sudo apt-get install zip
sudo mkdir /usr/local/apps/luarocks
sudo chmod -R 777 /usr/local/apps/luarocks
cd /usr/local/apps/luarocks
wget https://luarocks.org/releases/luarocks-3.3.1.tar.gz
tar -xvf luarocks-3.3.1.tar.gz
cd luarocks-3.3.1
./configure --prefix=/usr/local/apps/openresty/luajit \
--with-lua=/usr/local/apps/openresty/luajit/ \
--lua-suffix=jit \
--with-lua-include=/usr/local/apps/openresty/luajit/include/luajit-2.1
[圖片上傳失敗...(image-73e5ec-1624273861647)]
這個(gè)時(shí)候在
/usr/local/apps/openresty/luajit/bin 下能看到我們安裝好的 luarocks
[圖片上傳失敗...(image-dc87f8-1624273861647)]
安裝lfs
進(jìn)入到/usr/local/apps/openresty/luajit/bin目錄中
./luarocks install luafilesystem
dozenx@DESKTOP-93QKS01:/usr/local/apps/openresty/luajit/bin$ sudo ./luarocks install luafilesystem
Installing https://luarocks.org/luafilesystem-1.8.0-1.src.rock
luafilesystem 1.8.0-1 depends on lua >= 5.1 (5.1-1 provided by VM)
gcc -O2 -fPIC -I/usr/local/apps/openresty/luajit/include/luajit-2.1 -c src/lfs.c -o src/lfs.o
gcc -shared -o lfs.so src/lfs.o
No existing manifest. Attempting to rebuild...
luafilesystem 1.8.0-1 is now installed in /usr/local/apps/openresty/luajit (license: MIT/X11)
[圖片上傳失敗...(image-ac27c3-1624273861647)]
在/usr/local/apps/openresty/luajit/lib/lua/5.1目錄中,會(huì)看到編譯好的lfs.so疆栏,不用再拷貝什么的曾掂,openresty就直接可以用了。
ozenx@DESKTOP-93QKS01:/usr/local/apps/openresty/luajit/bin ls
lfs.so
[圖片上傳失敗...(image-768aa7-1624273861647)]
測(cè)試方法
在nginx-lua.conf里添加一段代碼段
location /demo/lfs/test {
content_by_lua_file /usr/local/apps/openresty/luademo/src/demo/lfsTest.lua;
}
lfsTest.lua 內(nèi)容如下
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Administrator.
--- DateTime: 2021/6/10 19:24
local lfs = require "lfs"
print("開始測(cè)試lfs的功能")
print(lfs._VERSION)
請(qǐng)求測(cè)試
curl 127.0.0.1:8089/demo/lfs/test
2021/06/21 17:26:16 [notice] 2754#0: *871 [lua] lfsTest.lua:9: 開始測(cè)試lfs的功能, client: 127.0.0.1, server: localhost, request: "GET /demo/lfs/test HTTP/1.1", host: "127.0.0.1:8089"
2021/06/21 17:26:16 [notice] 2754#0: *871 [lua] lfsTest.lua:10: LuaFileSystem 1.8.0, client: 127.0.0.1, server: localhost, request: "GET /demo/lfs/test HTTP/1.1", host: "127.0.0.1:8089"
[圖片上傳失敗...(image-231e89-1624273861647)]