当前位置: 首页 > 工具软件 > lua-epoll > 使用案例 >

linux中安装openresty+lua+sqlite3

单修德
2023-12-01

安装

本文章基于centos8安装

环境安装

这里openresty是已源码的方式安装,需要安装依赖环境。

您必须将这些库 perl 5.6.1+, libpcre, libssl安装在您的电脑之中。 对于 Linux来说, 您需要确认使用 ldconfig 命令,让其在您的系统环境路径中能找到它们

yum -y install openssl perl* gcc pcre* wget

下载安装openresty

如果需要最新的openresty,请点这里下载

# 下载openresty源码包
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
# 将文件解压出来
tar xvf  openresty-1.19.3.1.tar.gz
# 进入到文件中
cd openresty-1.19.3.1
# 开始编译安装
./configure
gmake -j 2
gmake install -j 2
# 然后创建软连
ln -s /usr/local/openresty/bin/openresty /usr/bin/

下载安装lua

官方下载地址: https://www.lua.org/download.html

我这是使用的是5.1.5,更高可能不兼容sqlite3,如果不适应sqlite3,可以到官方下载最新源码包

# 下载源码安装包
curl -R -O http://www.lua.org/ftp/lua-5.1.5.tar.gz
# 解压文件
tar xvf lua-5.1.5.tar.gz
# 切换目录
cd lua-5.4.3
# 编译安装
make linux test
make install -j 2
# 使用lua
[root@fxeye-nginx bin]# lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio

> print("hello world")
hello world

下载安装luarocks

lua安装管理luarocks,官方地址: https://luarocks.org/

# 下载源码包
wget http://luarocks.org/releases/luarocks-2.2.1.tar.gz
# 解压
tar xvf luarocks-2.2.1.tar.gz
# 进入到文件目录
cd luarocks-2.2.1
# 编译安装
 ./configure
# 注意不要用make build
make bootstrap

下载安装sqlite3

sqlite官方地址: https://www.sqlite.org/index.html
官方下载地址: https://www.sqlite.org/download.html

# 下载源码包
wget https://www.sqlite.org/2021/sqlite-autoconf-3350300.tar.gz
# 解压
tar xvf sqlite-autoconf-3350300.tar.gz
# 切换到sqlite文件目录
cd sqlite-autoconf-3350300
# 编译安装
./configure 
make -j 2 && make install -j 2

lua安装luasql-sqlite3模块

luarocks install luasql-sqlite3

如果无法安装报一下错误

Warning: falling back to curl - install luasec to get native HTTPS support
Warning: Failed searching manifest: Failed extracting manifest file
Installing https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/luasql-sqlite3-2.6.0-1.rockspec...
Using https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/luasql-sqlite3-2.6.0-1.rockspec... switching to 'build' mode

Error: Could not find expected file sqlite3.h, or sqlite3.h for SQLITE -- you may have to install SQLITE in your system and/or pass SQLITE_DIR or SQLITE_INCDIR to the luarocks command. Example: luarocks install luasql-sqlite3 SQLITE_DIR=/usr/local

使用find / -name "sqlite3.h"

[root@fxeye-nginx sqlite-autoconf-3230100]# find / -name "sqlite3.h"
/usr/local/sqlite3/include/sqlite3.h
/root/sqlite-autoconf-3230100/sqlite3.h

然后指定路径安装

luarocks install luasql-sqlite3 SQLITE_DIR=/usr/local/sqlite3/

最后就大功告成了

配置优化nginx

在/usr/local/openresty/nginx/

cat << EOF > /usr/local/openresty/nginx/conf/nginx.conf
user  root;
worker_processes auto;
error_log  /var/log/nginx/nginx_error.log  crit;
pid        /var/log/nginx/nginx.pid;
worker_rlimit_nofile 51200;
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }


http{
        include       mime.types;
        include       /etc/nginx/conf/*.conf;
        log_format  main  '访问时间: [$time_local] 客户端IP: $remote_addr,$http_x_forwarded_for 请求记录: $request 请求状态: $status 访问路由: $http_referer 客户端详情: $http_user_agent 连接数: $connection_requests 请求时长: $request_time/s 响应时间: $upstream_response_time/s';
	map $http_x_forwarded_for  $clientRealIp {
        ""      $remote_addr;
        ~^(?P<firstAddr>[0-9\.]+),?.*$  $firstAddr;
	}
        #include proxy.conf;
        access_log /var/log/nginx/access.log main;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        server_tokens off;
        access_log on;
}
EOF

使用openresty -t

[root@fxeye-nginx nginx]# openresty -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

目录初始化

mkdir -pv /etc/nginx/{conf,lua}

使用

这里只使用一些基本使用

编辑一个hello world程序

cat << EOF >> /etc/nginx/conf/hello.conf
server{
        listen 80;
        server_name _;
        location / {
           default_type text/html;
           content_by_lua_block {
                ngx.say("hello world!")
           }
        }
}
EOF

检测并启动openresty程序

[root@fxeye-nginx conf]# openresty -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@fxeye-nginx conf]# openresty -s reload 

访问,出现hello world!

[root@fxeye-nginx nginx]# curl -X GET 127.0.0.1
hello world!

常规用法

流程控制

Created with Raphaël 2.2.0 if 条件 确认? 结束 yes no

实例

# 语法比较像shell的流程控制
if (true)
then
	print("true")
end

循环事件

实例

# while使用
local number = 10
while (number < 1)
do
	print(number)
	number -= 1
end
# for 使用
a = {"one", "two", "three"}
for i, v in ipairs(a) do
    print(i, v)
end 

总结

通过本篇文章可以快速上手使用lua

 类似资料: