Kolostov

install-openresty-termux.sh

Dec 11th, 2025 (edited)
77
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 27.13 KB | Software | 0 0
  1. #!/bin/bash
  2. # install-openresty-termux.sh
  3. # Install OpenResty on Termux with all patches
  4. set -e
  5. echo "=== OpenResty Installation for Termux ==="
  6. echo "Setting up directories..."
  7. BUILD_DIR=~/openresty-build
  8. BACKUP_DIR=~/openresty-backup
  9. echo ""
  10. echo "Do you want to delete everything and start fresh? (y/N)"
  11. read -r DELETE_ALL
  12. DELETE_ALL=${DELETE_ALL:-N}
  13. if [[ "$DELETE_ALL" =~ ^[Yy]$ ]]; then
  14.     echo "Deleting all build directories..."
  15.     rm -rf "$BUILD_DIR" "$BACKUP_DIR"
  16.     mkdir -p "$BUILD_DIR" "$BACKUP_DIR"
  17.     FRESH_START=true
  18. else
  19.     echo "Using existing files if available..."
  20.     if [ ! -d "$BUILD_DIR" ]; then
  21.         mkdir -p "$BUILD_DIR"
  22.         FRESH_START=true
  23.     else
  24.         FRESH_START=false
  25.     fi
  26.     if [ ! -d "$BACKUP_DIR" ]; then
  27.         mkdir -p "$BACKUP_DIR"
  28.     fi
  29. fi
  30. # Function to backup source
  31. backup_source() {
  32.     local src_dir="$1"
  33.     local backup_name="$2"
  34.    
  35.     if [ ! -d "$BACKUP_DIR/$backup_name" ] || [ "$FRESH_START" = true ]; then
  36.         echo "Backing up $backup_name..."
  37.         rm -rf "$BACKUP_DIR/$backup_name"
  38.         cp -r "$src_dir" "$BACKUP_DIR/$backup_name"
  39.     fi
  40. }
  41. # Function to restore from backup
  42. restore_source() {
  43.     local src_dir="$1"
  44.     local backup_name="$2"
  45.    
  46.     if [ -d "$BACKUP_DIR/$backup_name" ]; then
  47.         echo "Restoring $backup_name from backup..."
  48.         rm -rf "$src_dir"
  49.         cp -r "$BACKUP_DIR/$backup_name" "$src_dir"
  50.     fi
  51. }
  52. echo "Checking for existing installations..."
  53. if [ -f "$PREFIX/bin/nginx-lua" ]; then
  54.     echo "Removing existing nginx-lua..."
  55.     pkill -f nginx-lua 2>/dev/null || true
  56.     rm -f $PREFIX/bin/nginx-lua
  57.     rm -rf $PREFIX/etc/nginx-lua 2>/dev/null || true
  58.     echo "✓ nginx-lua removed"
  59. fi
  60. if pkg list-installed | grep -q nginx; then
  61.     echo "Removing system nginx package..."
  62.     pkg uninstall -y nginx
  63.     echo "✓ System nginx removed"
  64. fi
  65. echo "Updating Termux packages..."
  66. yes | pkg update -y
  67. echo "Installing build dependencies..."
  68. pkg install -y binutils build-essential clang make git wget tar patch openssl pcre2 zlib perl
  69. cd "$BUILD_DIR"
  70. # Download or restore OpenResty
  71. if [ ! -d "openresty-1.27.1.2" ] || [ "$FRESH_START" = true ]; then
  72.     echo "Downloading OpenResty source (1.27.1.2)..."
  73.     rm -f openresty-1.27.1.2.tar.gz 2>/dev/null || true
  74.     wget -q https://openresty.org/download/openresty-1.27.1.2.tar.gz
  75.     tar xzf openresty-1.27.1.2.tar.gz
  76.     backup_source "openresty-1.27.1.2" "openresty-1.27.1.2-orig"
  77. else
  78.     echo "OpenResty source already exists, restoring from backup..."
  79.     restore_source "openresty-1.27.1.2" "openresty-1.27.1.2-orig"
  80. fi
  81. cd openresty-1.27.1.2
  82. # First, let's patch LuaJIT before anything else
  83. echo "=== PATCHING LUAJIT ==="
  84. # Find LuaJIT source directory
  85. LUAJIT_SRC=""
  86. if [ -d "build/LuaJIT-2.1-20250117" ]; then
  87.     LUAJIT_SRC="build/LuaJIT-2.1-20250117"
  88. elif [ -d "bundle/LuaJIT-2.1-20250117" ]; then
  89.     LUAJIT_SRC="bundle/LuaJIT-2.1-20250117"
  90. else
  91.     LUAJIT_SRC=$(find build/ -type d -name "LuaJIT-*" | head -1)
  92. fi
  93. if [ -n "$LUAJIT_SRC" ] && [ -d "$LUAJIT_SRC" ]; then
  94.     echo "Found LuaJIT source at: $LUAJIT_SRC"
  95.    
  96.     # Backup original LuaJIT
  97.     backup_source "$LUAJIT_SRC" "luajit-orig"
  98.    
  99.     cd "$LUAJIT_SRC"
  100.     echo "Now in LuaJIT directory: $(pwd)"
  101.    
  102.     # Apply LuaJIT patches for Android/Termux
  103.     echo "Applying LuaJIT patches for Termux..."
  104.    
  105.     # Patch 1: Add lua_getexdata and lua_setexdata symbols
  106.     # Create a patch file for src/lj_api.c
  107.     if [ -f "src/lj_api.c" ]; then
  108.         echo "Patching src/lj_api.c for exdata functions..."
  109.         cp src/lj_api.c src/lj_api.c.backup
  110.        
  111.         # Add exdata functions after lua_getextraspace
  112.         awk '
  113.        /LUA_API void \*lua_getextraspace\(lua_State \*L\)/ {
  114.            print $0
  115.            print ""
  116.            print "LUA_API void *lua_getexdata(lua_State *L)"
  117.            print "{"
  118.            print "  global_State *g = G(L);"
  119.            print "  return g->exdata;"
  120.            print "}"
  121.            print ""
  122.            print "LUA_API void lua_setexdata(lua_State *L, void *exdata)"
  123.            print "{"
  124.            print "  global_State *g = G(L);"
  125.            print "  g->exdata = exdata;"
  126.            print "}"
  127.            next
  128.        }
  129.        { print }
  130.        ' src/lj_api.c.backup > src/lj_api.c
  131.        
  132.         # Also need to declare these functions in lua.h
  133.         if [ -f "src/lua.h" ]; then
  134.             echo "Patching src/lua.h for exdata declarations..."
  135.             cp src/lua.h src/lua.h.backup
  136.            
  137.             # Add declarations after lua_getextraspace
  138.             awk '
  139.            /LUA_API void \*lua_getextraspace \(lua_State \*L\);/ {
  140.                print $0
  141.                print ""
  142.                print "#define LUA_EXDATA_IDX\t(-1001000)"
  143.                print ""
  144.                print "LUA_API void *lua_getexdata(lua_State *L);"
  145.                print "LUA_API void lua_setexdata(lua_State *L, void *exdata);"
  146.                next
  147.            }
  148.            { print }
  149.            ' src/lua.h.backup > src/lua.h
  150.         fi
  151.        
  152.         # Patch lj_state.c to initialize exdata
  153.         if [ -f "src/lj_state.c" ]; then
  154.             echo "Patching src/lj_state.c for exdata initialization..."
  155.             cp src/lj_state.c src/lj_state.c.backup
  156.            
  157.             # Find the lj_state_newstate function and add exdata initialization
  158.             awk '
  159.            /lj_state_newstate/ {
  160.                in_function = 1
  161.            }
  162.            in_function && /g->gc\.state = GCSpause/ {
  163.                print $0
  164.                print "  g->exdata = NULL;"
  165.                next
  166.            }
  167.            { print }
  168.            ' src/lj_state.c.backup > src/lj_state.c
  169.         fi
  170.        
  171.         # Patch lj_obj.h to add exdata to global_State struct
  172.         if [ -f "src/lj_obj.h" ]; then
  173.             echo "Patching src/lj_obj.h to add exdata field..."
  174.             cp src/lj_obj.h src/lj_obj.h.backup
  175.            
  176.             # Find global_State struct and add exdata field
  177.             awk '
  178.            /typedef struct global_State/ {
  179.                in_struct = 1
  180.                print $0
  181.                getline
  182.                print $0
  183.                print "  void *exdata;  /* for lua_getexdata/lua_setexdata */"
  184.                next
  185.            }
  186.            in_struct && /^\}/ {
  187.                in_struct = 0
  188.            }
  189.            { print }
  190.            ' src/lj_obj.h.backup > src/lj_obj.h
  191.         fi
  192.     fi
  193.    
  194.     # Patch 2: Fix for Android/Termux build
  195.     echo "Applying Android/Termux build fixes..."
  196.    
  197.     # Patch Makefile to use correct flags for Termux
  198.     if [ -f "Makefile" ]; then
  199.         cp Makefile Makefile.backup
  200.         sed -i 's/^BUILDMODE= mixed$/BUILDMODE= static/' Makefile
  201.         sed -i 's/^CC= gcc$/CC= clang/' Makefile
  202.         sed -i 's/^HOST_CC= gcc$/HOST_CC= clang/' Makefile
  203.         sed -i 's/^CFLAGS= -O2 -fomit-frame-pointer$/CFLAGS= -O2 -fomit-frame-pointer -fPIC/' Makefile
  204.     fi
  205.    
  206.     # Patch src/Makefile
  207.     if [ -f "src/Makefile" ]; then
  208.         cp src/Makefile src/Makefile.backup
  209.         sed -i 's/^CC= gcc$/CC= clang/' src/Makefile
  210.         sed -i 's/^CCOPT= -O2 -fomit-frame-pointer$/CCOPT= -O2 -fomit-frame-pointer -fPIC -DLUAJIT_ENABLE_LUA52COMPAT/' src/Makefile
  211.     fi
  212.    
  213.     cd ../..
  214.     echo "✓ LuaJIT patches applied"
  215. else
  216.     echo "WARNING: Could not find LuaJIT source directory"
  217. fi
  218. echo "=== FINDING AND PATCHING NGINX SOURCE ==="
  219. echo "Current directory: $(pwd)"
  220. # Find the nginx source directory
  221. NGINX_SRC=""
  222. if [ -d "build/nginx-1.27.1" ]; then
  223.     NGINX_SRC="build/nginx-1.27.1"
  224.     echo "Found nginx source at: $NGINX_SRC"
  225. elif [ -d "bundle/nginx-1.27.1" ]; then
  226.     NGINX_SRC="bundle/nginx-1.27.1"
  227.     echo "Found nginx source at: $NGINX_SRC"
  228. else
  229.     echo "Searching for nginx source..."
  230.     NGINX_SRC=$(find build/ -type d -name "nginx-*" | head -1)
  231.     if [ -n "$NGINX_SRC" ]; then
  232.         echo "Found nginx source at: $NGINX_SRC"
  233.     else
  234.         echo "ERROR: Could not find nginx source!"
  235.         echo "Contents of build directory:"
  236.         ls -la build/ || true
  237.         exit 1
  238.     fi
  239. fi
  240. if [ -n "$NGINX_SRC" ] && [ -d "$NGINX_SRC" ]; then
  241.     echo "Entering nginx source: $NGINX_SRC"
  242.     cd "$NGINX_SRC"
  243.     echo "Now in: $(pwd)"
  244.    
  245.     EPOLL_FILE="src/event/modules/ngx_epoll_module.c"
  246.     echo "Patching file: $EPOLL_FILE"
  247.    
  248.     # Check if file exists
  249.     if [ ! -f "$EPOLL_FILE" ]; then
  250.         echo "ERROR: File not found!"
  251.         exit 1
  252.     fi
  253.    
  254.     # Create backup
  255.     cp "$EPOLL_FILE" "$EPOLL_FILE.backup"
  256.     echo "Created backup: $EPOLL_FILE.backup"
  257.    
  258.     echo "Applying epoll patch..."
  259.    
  260.     # Create a properly patched version
  261.     awk '
  262.    BEGIN { skip_next_endif = 0 }
  263.    
  264.    /#if \(NGX_READ_EVENT != EPOLLIN\|EPOLLRDHUP\)/ {
  265.        print "        events = EPOLLIN|EPOLLRDHUP;  /* PATCHED1 */"
  266.        skip_next_endif = 1
  267.        next
  268.    }
  269.    
  270.    /#if \(NGX_WRITE_EVENT != EPOLLOUT\)/ {
  271.        print "        events = EPOLLOUT;  /* PATCHED2 */"
  272.        skip_next_endif = 1
  273.        next
  274.    }
  275.    
  276.    /#endif/ && skip_next_endif {
  277.        skip_next_endif = 0
  278.        next
  279.    }
  280.    
  281.    { print }
  282.    ' "$EPOLL_FILE.backup" > "$EPOLL_FILE.temp"
  283.    
  284.     # Check if patch was applied
  285.     if grep -q "PATCHED" "$EPOLL_FILE.temp"; then
  286.         echo "✓ Epoll patch applied successfully"
  287.         mv "$EPOLL_FILE.temp" "$EPOLL_FILE"
  288.     else
  289.         echo "Using direct sed method..."
  290.         cp "$EPOLL_FILE.backup" "$EPOLL_FILE"
  291.        
  292.         # Simple direct replacements
  293.         sed -i 's/#if (NGX_READ_EVENT != EPOLLIN|EPOLLRDHUP)/        events = EPOLLIN|EPOLLRDHUP;  \/* PATCHED1 *\//' "$EPOLL_FILE"
  294.         sed -i 's/#if (NGX_WRITE_EVENT != EPOLLOUT)/        events = EPOLLOUT;  \/* PATCHED2 *\//' "$EPOLL_FILE"
  295.        
  296.         # Remove any standalone #endif that might follow
  297.         sed -i '/^[[:space:]]*#[[:space:]]*endif[[:space:]]*$/d' "$EPOLL_FILE"
  298.     fi
  299.    
  300.     # Apply MAXNS fix
  301.     echo "Applying MAXNS fix to ngx_resolver.c..."
  302.     if [ -f "src/core/ngx_resolver.c" ]; then
  303.         cp src/core/ngx_resolver.c src/core/ngx_resolver.c.backup
  304.         awk '
  305.        BEGIN { added = 0 }
  306.        /^#include/ && !added {
  307.            print "#ifndef MAXNS"
  308.            print "#define MAXNS 3"
  309.            print "#endif"
  310.            added = 1
  311.        }
  312.        { print }
  313.        ' src/core/ngx_resolver.c.backup > src/core/ngx_resolver.c
  314.         echo "✓ MAXNS fix applied"
  315.     fi
  316.    
  317.     # Go back to OpenResty directory
  318.     cd ../..
  319. else
  320.     echo "ERROR: Could not find or access nginx source!"
  321.     exit 1
  322. fi
  323. echo "Configuring OpenResty for Termux..."
  324. export CC="clang -std=gnu90"
  325. export CFLAGS="-std=gnu90 -fPIC -I$PREFIX/include -Wno-language-extension-token -Wno-inline -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -DMAXNS=3 -DLUAJIT_ENABLE_LUA52COMPAT"
  326. export LDFLAGS="-L$PREFIX/lib -Wl,-rpath,$PREFIX/lib -Wl,--export-dynamic"
  327. # Clean any previous build
  328. if [ -f "Makefile" ]; then
  329.     make clean 2>/dev/null || true
  330. fi
  331. ./configure \
  332.     --prefix=$PREFIX/openresty \
  333.     --sbin-path=$PREFIX/bin/openresty \
  334.     --conf-path=$PREFIX/etc/openresty/nginx.conf \
  335.     --pid-path=$PREFIX/openresty/logs/nginx.pid \
  336.     --lock-path=$PREFIX/openresty/logs/nginx.lock \
  337.     --user=$(whoami) \
  338.     --group=$(whoami) \
  339.     --with-threads \
  340.     --with-file-aio \
  341.     --with-http_ssl_module \
  342.     --with-http_v2_module \
  343.     --with-http_realip_module \
  344.     --with-http_stub_status_module \
  345.     --with-http_gzip_static_module \
  346.     --with-stream \
  347.     --with-stream_ssl_module \
  348.     --with-luajit \
  349.     --with-cc="$CC" \
  350.     --with-cc-opt="$CFLAGS" \
  351.     --with-ld-opt="$LDFLAGS" \
  352.     --with-pcre-jit \
  353.     --without-http_upstream_zone_module
  354. echo "Building OpenResty..."
  355. if ! make -j$(nproc) 2>&1 | tee build.log; then
  356.     echo "Parallel build failed, checking for errors..."
  357.    
  358.     # Check for LuaJIT exdata errors
  359.     if grep -iq "lua_getexdata\|lua_setexdata" build.log; then
  360.         echo "LuaJIT exdata functions missing, applying emergency patch..."
  361.        
  362.         # Emergency patch: Download and use pre-patched LuaJIT
  363.         echo "Downloading pre-patched LuaJIT for Android..."
  364.         cd "$BUILD_DIR"
  365.        
  366.         # Try to download a pre-built LuaJIT with exdata support
  367.         if [ ! -f "luajit-patched.tar.gz" ]; then
  368.             echo "Downloading patched LuaJIT..."
  369.             # Try multiple sources for patched LuaJIT
  370.             wget -q --tries=3 --timeout=30 https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20250117.tar.gz -O luajit2.tar.gz || \
  371.             wget -q https://github.com/LuaJIT/LuaJIT/archive/refs/tags/v2.1.1699231517.tar.gz -O luajit2.tar.gz || \
  372.             echo "Could not download LuaJIT, building from source with patches..."
  373.            
  374.             if [ -f "luajit2.tar.gz" ]; then
  375.                 tar xzf luajit2.tar.gz
  376.                 LUAJIT_DIR=$(find . -type d -name "LuaJIT-*" -o -name "luajit2-*" | head -1)
  377.                 if [ -n "$LUAJIT_DIR" ]; then
  378.                     echo "Using LuaJIT from: $LUAJIT_DIR"
  379.                     # Copy it to OpenResty build directory
  380.                     rm -rf openresty-1.27.1.2/build/LuaJIT-2.1-20250117
  381.                     cp -r "$LUAJIT_DIR" openresty-1.27.1.2/build/LuaJIT-2.1-20250117
  382.                 fi
  383.             fi
  384.         fi
  385.        
  386.         cd openresty-1.27.1.2
  387.        
  388.         echo "Trying build again with single core..."
  389.         make
  390.     else
  391.         echo "Trying build again with single core..."
  392.         make
  393.     fi
  394. fi
  395. echo "Installing OpenResty..."
  396. make install
  397. echo "Creating symlinks and directories..."
  398. ln -sf $PREFIX/bin/openresty $PREFIX/bin/nginx-lua 2>/dev/null || true
  399. mkdir -p $PREFIX/etc/openresty/conf.d
  400. mkdir -p $PREFIX/openresty/{logs,html,lualib,lua}
  401. # Fix LuaJIT installation
  402. echo "Setting up LuaJIT..."
  403. if [ -d "$PREFIX/openresty/luajit" ]; then
  404.     echo "Found LuaJIT installation..."
  405.    
  406.     # Create necessary symlinks
  407.     if [ -f "$PREFIX/openresty/luajit/lib/libluajit-5.1.so" ]; then
  408.         ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so $PREFIX/lib/libluajit-5.1.so 2>/dev/null || true
  409.         ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so $PREFIX/lib/libluajit.so 2>/dev/null || true
  410.     fi
  411.    
  412.     if [ -f "$PREFIX/openresty/luajit/lib/libluajit-5.1.so.2" ]; then
  413.         ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so.2 $PREFIX/lib/libluajit-5.1.so.2 2>/dev/null || true
  414.         ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so.2 $PREFIX/lib/libluajit.so.2 2>/dev/null || true
  415.     fi
  416.    
  417.     if [ -f "$PREFIX/openresty/luajit/bin/luajit" ]; then
  418.         ln -sf $PREFIX/openresty/luajit/bin/luajit $PREFIX/bin/luajit 2>/dev/null || true
  419.     fi
  420.    
  421.     # Check if we need to manually add exdata symbols
  422.     echo "Checking LuaJIT library for exdata symbols..."
  423.     if ! nm $PREFIX/openresty/luajit/lib/libluajit-5.1.so 2>/dev/null | grep -q "lua_getexdata"; then
  424.         echo "WARNING: LuaJIT library missing exdata symbols"
  425.         echo "Creating wrapper library with exdata support..."
  426.        
  427.         # Create a wrapper library with the missing symbols
  428.         cat > /tmp/luajit_wrapper.c << 'WRAPPER'
  429. #include <lua.h>
  430. #include <lauxlib.h>
  431. #include <lualib.h>
  432. static void *exdata = NULL;
  433. LUA_API void *lua_getexdata(lua_State *L) {
  434.     (void)L;  // Unused parameter
  435.     return exdata;
  436. }
  437. LUA_API void lua_setexdata(lua_State *L, void *ed) {
  438.     (void)L;  // Unused parameter
  439.     exdata = ed;
  440. }
  441. WRAPPER
  442.        
  443.         # Compile the wrapper
  444.         clang -std=gnu90 -fPIC -shared -o $PREFIX/lib/libluajit_exdata.so /tmp/luajit_wrapper.c
  445.        
  446.         # Create a shell script wrapper for openresty
  447.         cat > $PREFIX/bin/openresty_wrapper << 'WRAPPER_SCRIPT'
  448. #!/bin/bash
  449. export LD_PRELOAD="$PREFIX/lib/libluajit_exdata.so:$PREFIX/openresty/luajit/lib/libluajit-5.1.so"
  450. exec $PREFIX/openresty/nginx/sbin/nginx "$@"
  451. WRAPPER_SCRIPT
  452.        
  453.         chmod +x $PREFIX/bin/openresty_wrapper
  454.         mv $PREFIX/bin/openresty $PREFIX/bin/openresty.orig
  455.         ln -sf $PREFIX/bin/openresty_wrapper $PREFIX/bin/openresty
  456.        
  457.         echo "✓ Created wrapper with exdata support"
  458.     else
  459.         echo "✓ LuaJIT library has exdata symbols"
  460.     fi
  461. fi
  462. echo "Creating nginx configuration..."
  463. # Create a proper nginx.conf with hardcoded paths
  464. cat > "$PREFIX/etc/openresty/nginx.conf" << NGINXCONF
  465. worker_processes  1;
  466. error_log  $PREFIX/openresty/logs/error.log;
  467. pid        $PREFIX/openresty/logs/nginx.pid;
  468. events {
  469.     worker_connections  1024;
  470. }
  471. http {
  472.     include       mime.types;
  473.     default_type  application/octet-stream;
  474.     sendfile        on;
  475.     keepalive_timeout  65;
  476.     lua_package_path "$PREFIX/openresty/lua/?.lua;$PREFIX/openresty/lualib/?.lua;;";
  477.     lua_package_cpath "$PREFIX/openresty/lib/lua/?.so;;";
  478.    
  479.     server {
  480.         listen       8081;
  481.         server_name  localhost;
  482.        
  483.         location / {
  484.             root   $PREFIX/openresty/html;
  485.             index  index.html index.htm;
  486.         }
  487.        
  488.         location /test_lua {
  489.             default_type 'text/plain';
  490.             content_by_lua_block {
  491.                 ngx.say("OpenResty on Termux: FINALLY WORKING!");
  492.                 if jit then
  493.                     ngx.say("LuaJIT: " .. jit.version);
  494.                 end
  495.                 ngx.say("Time: " .. os.date("%Y-%m-%d %H:%M:%S"));
  496.             }
  497.         }
  498.        
  499.         location /nginx_status {
  500.             stub_status;
  501.             access_log off;
  502.         }
  503.        
  504.         error_page   500 502 503 504  /50x.html;
  505.         location = /50x.html {
  506.             root   $PREFIX/openresty/html;
  507.         }
  508.     }
  509. }
  510. NGINXCONF
  511. # Create mime.types file if it doesn't exist
  512. if [ ! -f "$PREFIX/etc/openresty/mime.types" ]; then
  513.     echo "Creating mime.types file..."
  514.     cat > "$PREFIX/etc/openresty/mime.types" << 'MIMETYPES'
  515. types {
  516.     text/html                             html htm shtml;
  517.     text/css                              css;
  518.     text/xml                              xml;
  519.     image/gif                             gif;
  520.     image/jpeg                            jpeg jpg;
  521.     application/javascript                js;
  522.     application/atom+xml                  atom;
  523.     application/rss+xml                   rss;
  524.     text/mathml                           mml;
  525.     text/plain                            txt;
  526.     text/vnd.sun.j2me.app-descriptor      jad;
  527.     text/vnd.wap.wml                      wml;
  528.     text/x-component                      htc;
  529.     image/png                             png;
  530.     image/tiff                            tif tiff;
  531.     image/vnd.wap.wbmp                    wbmp;
  532.     image/x-icon                          ico;
  533.     image/x-jng                           jng;
  534.     image/x-ms-bmp                        bmp;
  535.     image/svg+xml                         svg svgz;
  536.     image/webp                            webp;
  537.     application/font-woff                 woff;
  538.     application/java-archive              jar war ear;
  539.     application/json                      json;
  540.     application/mac-binhex40              hqx;
  541.     application/msword                    doc;
  542.     application/pdf                       pdf;
  543.     application/postscript                ps eps ai;
  544.     application/rtf                       rtf;
  545.     application/vnd.apple.mpegurl         m3u8;
  546.     application/vnd.ms-excel              xls;
  547.     application/vnd.ms-fontobject         eot;
  548.     application/vnd.ms-powerpoint         ppt;
  549.     application/vnd.wap.wmlc              wmlc;
  550.     application/vnd.google-earth.kml+xml  kml;
  551.     application/vnd.google-earth.kmz      kmz;
  552.     application/x-7z-compressed           7z;
  553.     application/x-cocoa                   cco;
  554.     application/x-java-archive-diff       jardiff;
  555.     application/x-java-jnlp-file          jnlp;
  556.     application/x-makeself                run;
  557.     application/x-perl                    pl pm;
  558.     application/x-pilot                   prc pdb;
  559.     application/x-rar-compressed          rar;
  560.     application/x-redhat-package-manager  rpm;
  561.     application/x-sea                     sea;
  562.     application/x-shockwave-flash         swf;
  563.     application/x-stuffit                 sit;
  564.     application/x-tcl                     tcl tk;
  565.     application/x-x509-ca-cert            der pem crt;
  566.     application/x-xpinstall               xpi;
  567.     application/xhtml+xml                 xhtml;
  568.     application/xspf+xml                  xspf;
  569.     application/zip                       zip;
  570.     application/octet-stream              bin exe dll;
  571.     application/octet-stream              deb;
  572.     application/octet-stream              dmg;
  573.     application/octet-stream              iso img;
  574.     application/octet-stream              msi msp msm;
  575.     audio/midi                            mid midi kar;
  576.     audio/mpeg                            mp3;
  577.     audio/ogg                             ogg;
  578.     audio/x-m4a                           m4a;
  579.     audio/x-realaudio                     ra;
  580.     video/3gpp                            3gpp 3gp;
  581.     video/mp2t                            ts;
  582.     video/mp4                             mp4;
  583.     video/mpeg                            mpeg mpg;
  584.     video/quicktime                       mov;
  585.     video/webm                            webm;
  586.     video/x-flv                           flv;
  587.     video/x-m4v                           m4v;
  588.     video/x-mng                           mng;
  589.     video/x-ms-asf                        asx asf;
  590.     video/x-ms-wmv                        wmv;
  591.     video/x-msvideo                       avi;
  592. }
  593. MIMETYPES
  594. fi
  595. cat > "$PREFIX/openresty/html/index.html" << 'HTML'
  596. <!DOCTYPE html>
  597. <html>
  598. <head>
  599.     <title>OpenResty Termux</title>
  600. </head>
  601. <body>
  602.     <h1>OpenResty on Termux</h1>
  603.     <p>Server is running.</p>
  604.     <p><a href="/test_lua">Test Lua</a></p>
  605.     <p><a href="/nginx_status">Nginx Status</a></p>
  606. </body>
  607. </html>
  608. HTML
  609. # Create a test script to verify installation - FIXED PATH
  610. mkdir -p "$PREFIX/openresty/lua"
  611. cat > "$PREFIX/openresty/lua/test.lua" << 'LUA_TEST'
  612. ngx.header["Content-Type"] = "text/plain"
  613. ngx.say("OpenResty Lua test successful!")
  614. ngx.say("LuaJIT version: " .. (jit and jit.version or "unknown"))
  615. ngx.say("Current time: " .. os.date("%Y-%m-%d %H:%M:%S"))
  616. LUA_TEST
  617. # Test if openresty works
  618. echo "Testing OpenResty installation..."
  619. if $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf 2>&1 | grep -q "successful"; then
  620.     echo "✅ OpenResty configuration test successful!"
  621.     echo ""
  622.     echo "To start OpenResty:"
  623.     echo "  openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf"
  624.     echo ""
  625.     echo "Or use the created launcher scripts:"
  626.    
  627.     # Create launcher scripts
  628.     cat > $PREFIX/bin/start-openresty << 'LAUNCHER'
  629. #!/bin/bash
  630. $PREFIX/bin/openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
  631. LAUNCHER
  632.     chmod +x $PREFIX/bin/start-openresty
  633.    
  634.     cat > $PREFIX/bin/stop-openresty << 'STOPPER'
  635. #!/bin/bash
  636. $PREFIX/bin/openresty -s stop -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
  637. STOPPER
  638.     chmod +x $PREFIX/bin/stop-openresty
  639.    
  640.     cat > $PREFIX/bin/restart-openresty << 'RESTARTER'
  641. #!/bin/bash
  642. $PREFIX/bin/openresty -s stop -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf 2>/dev/null || true
  643. sleep 1
  644. $PREFIX/bin/openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
  645. RESTARTER
  646.     chmod +x $PREFIX/bin/restart-openresty
  647.    
  648.     echo "Start: start-openresty"
  649.     echo "Test: curl http://localhost:8081/test_lua"
  650.     echo "Stop: stop-openresty"
  651.     echo "Restart: restart-openresty"
  652. else
  653.     echo "⚠️  OpenResty configuration test failed"
  654.     echo "Trying alternative approach..."
  655.    
  656.     # Create a launcher script with explicit paths
  657.     cat > $PREFIX/bin/openresty_launcher << 'LAUNCHER'
  658. #!/bin/bash
  659. # OpenResty launcher for Termux
  660. export PATH=$PREFIX/openresty/luajit/bin:$PATH
  661. export LD_LIBRARY_PATH=$PREFIX/openresty/luajit/lib:$PREFIX/lib:$LD_LIBRARY_PATH
  662. exec $PREFIX/openresty/nginx/sbin/nginx -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf "\$@"
  663. LAUNCHER
  664.    
  665.     chmod +x $PREFIX/bin/openresty_launcher
  666.    
  667.     # Test the launcher
  668.     if $PREFIX/bin/openresty_launcher -t 2>&1 | grep -q "successful"; then
  669.         echo "✅ Launcher script works!"
  670.        
  671.         # Create aliases for the launcher
  672.         cat > $PREFIX/bin/start-openresty << 'LAUNCHER2'
  673. #!/bin/bash
  674. $PREFIX/bin/openresty_launcher
  675. LAUNCHER2
  676.         chmod +x $PREFIX/bin/start-openresty
  677.        
  678.         cat > $PREFIX/bin/stop-openresty << 'STOPPER2'
  679. #!/bin/bash
  680. $PREFIX/bin/openresty_launcher -s stop
  681. STOPPER2
  682.         chmod +x $PREFIX/bin/stop-openresty
  683.        
  684.         echo ""
  685.         echo "Start: start-openresty"
  686.         echo "Test: curl http://localhost:8081/test_lua"
  687.         echo "Stop: stop-openresty"
  688.     else
  689.         echo "❌ Configuration issues detected"
  690.         echo "Try manually checking the config:"
  691.         echo "  $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf"
  692.         echo ""
  693.         echo "Common issues and fixes:"
  694.         echo "1. Make sure all directories exist:"
  695.         echo "   mkdir -p $PREFIX/openresty/{logs,html,lualib,lua}"
  696.         echo "2. Check permissions:"
  697.         echo "   chmod 755 $PREFIX/openresty"
  698.         echo "3. Try running nginx directly:"
  699.         echo "   $PREFIX/openresty/nginx/sbin/nginx -t -c $PREFIX/etc/openresty/nginx.conf"
  700.     fi
  701. fi
  702. # Create a simple test script
  703. cat > $HOME/test_openresty.sh << 'TEST_SCRIPT'
  704. #!/bin/bash
  705. echo "Testing OpenResty installation..."
  706. echo "1. Checking if openresty binary exists..."
  707. if [ -f "$PREFIX/bin/openresty" ]; then
  708.     echo "   ✓ openresty binary found"
  709. else
  710.     echo "   ✗ openresty binary not found"
  711.     exit 1
  712. fi
  713. echo "2. Testing nginx configuration..."
  714. if $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf 2>&1 | grep -q "successful"; then
  715.     echo "   ✓ Configuration test passed"
  716. else
  717.     echo "   ✗ Configuration test failed"
  718.     $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf
  719.     exit 1
  720. fi
  721. echo "3. Starting OpenResty..."
  722. $PREFIX/bin/openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
  723. sleep 2
  724. echo "4. Testing HTTP endpoint..."
  725. if curl -s http://localhost:8081/test_lua | grep -q "OpenResty on Termux"; then
  726.     echo "   ✓ HTTP test passed"
  727. else
  728.     echo "   ✗ HTTP test failed"
  729.     echo "Response was:"
  730.     curl -s http://localhost:8081/test_lua
  731. fi
  732. echo "5. Stopping OpenResty..."
  733. $PREFIX/bin/openresty -s stop -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
  734. echo ""
  735. echo "✅ OpenResty installation verified!"
  736. TEST_SCRIPT
  737. chmod +x $HOME/test_openresty.sh
  738. echo ""
  739. echo "Installation complete!"
  740. echo "You can test the installation with: ./test_openresty.sh"
  741. echo ""
  742. echo "Quick reference:"
  743. echo "  Start: start-openresty"
  744. echo "  Stop: stop-openresty"
  745. echo "  Restart: restart-openresty"
  746. echo "  Test: curl http://localhost:8081/test_lua"
  747. echo ""
  748. echo "Configuration files:"
  749. echo "  Main config: $PREFIX/etc/openresty/nginx.conf"
  750. echo "  Logs: $PREFIX/openresty/logs/"
  751. echo "  HTML: $PREFIX/openresty/html/"
  752. echo "  Lua scripts: $PREFIX/openresty/lua/"
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment