Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # install-openresty-termux.sh
- # Install OpenResty on Termux with all patches
- set -e
- echo "=== OpenResty Installation for Termux ==="
- echo "Setting up directories..."
- BUILD_DIR=~/openresty-build
- BACKUP_DIR=~/openresty-backup
- echo ""
- echo "Do you want to delete everything and start fresh? (y/N)"
- read -r DELETE_ALL
- DELETE_ALL=${DELETE_ALL:-N}
- if [[ "$DELETE_ALL" =~ ^[Yy]$ ]]; then
- echo "Deleting all build directories..."
- rm -rf "$BUILD_DIR" "$BACKUP_DIR"
- mkdir -p "$BUILD_DIR" "$BACKUP_DIR"
- FRESH_START=true
- else
- echo "Using existing files if available..."
- if [ ! -d "$BUILD_DIR" ]; then
- mkdir -p "$BUILD_DIR"
- FRESH_START=true
- else
- FRESH_START=false
- fi
- if [ ! -d "$BACKUP_DIR" ]; then
- mkdir -p "$BACKUP_DIR"
- fi
- fi
- # Function to backup source
- backup_source() {
- local src_dir="$1"
- local backup_name="$2"
- if [ ! -d "$BACKUP_DIR/$backup_name" ] || [ "$FRESH_START" = true ]; then
- echo "Backing up $backup_name..."
- rm -rf "$BACKUP_DIR/$backup_name"
- cp -r "$src_dir" "$BACKUP_DIR/$backup_name"
- fi
- }
- # Function to restore from backup
- restore_source() {
- local src_dir="$1"
- local backup_name="$2"
- if [ -d "$BACKUP_DIR/$backup_name" ]; then
- echo "Restoring $backup_name from backup..."
- rm -rf "$src_dir"
- cp -r "$BACKUP_DIR/$backup_name" "$src_dir"
- fi
- }
- echo "Checking for existing installations..."
- if [ -f "$PREFIX/bin/nginx-lua" ]; then
- echo "Removing existing nginx-lua..."
- pkill -f nginx-lua 2>/dev/null || true
- rm -f $PREFIX/bin/nginx-lua
- rm -rf $PREFIX/etc/nginx-lua 2>/dev/null || true
- echo "✓ nginx-lua removed"
- fi
- if pkg list-installed | grep -q nginx; then
- echo "Removing system nginx package..."
- pkg uninstall -y nginx
- echo "✓ System nginx removed"
- fi
- echo "Updating Termux packages..."
- yes | pkg update -y
- echo "Installing build dependencies..."
- pkg install -y binutils build-essential clang make git wget tar patch openssl pcre2 zlib perl
- cd "$BUILD_DIR"
- # Download or restore OpenResty
- if [ ! -d "openresty-1.27.1.2" ] || [ "$FRESH_START" = true ]; then
- echo "Downloading OpenResty source (1.27.1.2)..."
- rm -f openresty-1.27.1.2.tar.gz 2>/dev/null || true
- wget -q https://openresty.org/download/openresty-1.27.1.2.tar.gz
- tar xzf openresty-1.27.1.2.tar.gz
- backup_source "openresty-1.27.1.2" "openresty-1.27.1.2-orig"
- else
- echo "OpenResty source already exists, restoring from backup..."
- restore_source "openresty-1.27.1.2" "openresty-1.27.1.2-orig"
- fi
- cd openresty-1.27.1.2
- # First, let's patch LuaJIT before anything else
- echo "=== PATCHING LUAJIT ==="
- # Find LuaJIT source directory
- LUAJIT_SRC=""
- if [ -d "build/LuaJIT-2.1-20250117" ]; then
- LUAJIT_SRC="build/LuaJIT-2.1-20250117"
- elif [ -d "bundle/LuaJIT-2.1-20250117" ]; then
- LUAJIT_SRC="bundle/LuaJIT-2.1-20250117"
- else
- LUAJIT_SRC=$(find build/ -type d -name "LuaJIT-*" | head -1)
- fi
- if [ -n "$LUAJIT_SRC" ] && [ -d "$LUAJIT_SRC" ]; then
- echo "Found LuaJIT source at: $LUAJIT_SRC"
- # Backup original LuaJIT
- backup_source "$LUAJIT_SRC" "luajit-orig"
- cd "$LUAJIT_SRC"
- echo "Now in LuaJIT directory: $(pwd)"
- # Apply LuaJIT patches for Android/Termux
- echo "Applying LuaJIT patches for Termux..."
- # Patch 1: Add lua_getexdata and lua_setexdata symbols
- # Create a patch file for src/lj_api.c
- if [ -f "src/lj_api.c" ]; then
- echo "Patching src/lj_api.c for exdata functions..."
- cp src/lj_api.c src/lj_api.c.backup
- # Add exdata functions after lua_getextraspace
- awk '
- /LUA_API void \*lua_getextraspace\(lua_State \*L\)/ {
- print $0
- print ""
- print "LUA_API void *lua_getexdata(lua_State *L)"
- print "{"
- print " global_State *g = G(L);"
- print " return g->exdata;"
- print "}"
- print ""
- print "LUA_API void lua_setexdata(lua_State *L, void *exdata)"
- print "{"
- print " global_State *g = G(L);"
- print " g->exdata = exdata;"
- print "}"
- next
- }
- { print }
- ' src/lj_api.c.backup > src/lj_api.c
- # Also need to declare these functions in lua.h
- if [ -f "src/lua.h" ]; then
- echo "Patching src/lua.h for exdata declarations..."
- cp src/lua.h src/lua.h.backup
- # Add declarations after lua_getextraspace
- awk '
- /LUA_API void \*lua_getextraspace \(lua_State \*L\);/ {
- print $0
- print ""
- print "#define LUA_EXDATA_IDX\t(-1001000)"
- print ""
- print "LUA_API void *lua_getexdata(lua_State *L);"
- print "LUA_API void lua_setexdata(lua_State *L, void *exdata);"
- next
- }
- { print }
- ' src/lua.h.backup > src/lua.h
- fi
- # Patch lj_state.c to initialize exdata
- if [ -f "src/lj_state.c" ]; then
- echo "Patching src/lj_state.c for exdata initialization..."
- cp src/lj_state.c src/lj_state.c.backup
- # Find the lj_state_newstate function and add exdata initialization
- awk '
- /lj_state_newstate/ {
- in_function = 1
- }
- in_function && /g->gc\.state = GCSpause/ {
- print $0
- print " g->exdata = NULL;"
- next
- }
- { print }
- ' src/lj_state.c.backup > src/lj_state.c
- fi
- # Patch lj_obj.h to add exdata to global_State struct
- if [ -f "src/lj_obj.h" ]; then
- echo "Patching src/lj_obj.h to add exdata field..."
- cp src/lj_obj.h src/lj_obj.h.backup
- # Find global_State struct and add exdata field
- awk '
- /typedef struct global_State/ {
- in_struct = 1
- print $0
- getline
- print $0
- print " void *exdata; /* for lua_getexdata/lua_setexdata */"
- next
- }
- in_struct && /^\}/ {
- in_struct = 0
- }
- { print }
- ' src/lj_obj.h.backup > src/lj_obj.h
- fi
- fi
- # Patch 2: Fix for Android/Termux build
- echo "Applying Android/Termux build fixes..."
- # Patch Makefile to use correct flags for Termux
- if [ -f "Makefile" ]; then
- cp Makefile Makefile.backup
- sed -i 's/^BUILDMODE= mixed$/BUILDMODE= static/' Makefile
- sed -i 's/^CC= gcc$/CC= clang/' Makefile
- sed -i 's/^HOST_CC= gcc$/HOST_CC= clang/' Makefile
- sed -i 's/^CFLAGS= -O2 -fomit-frame-pointer$/CFLAGS= -O2 -fomit-frame-pointer -fPIC/' Makefile
- fi
- # Patch src/Makefile
- if [ -f "src/Makefile" ]; then
- cp src/Makefile src/Makefile.backup
- sed -i 's/^CC= gcc$/CC= clang/' src/Makefile
- sed -i 's/^CCOPT= -O2 -fomit-frame-pointer$/CCOPT= -O2 -fomit-frame-pointer -fPIC -DLUAJIT_ENABLE_LUA52COMPAT/' src/Makefile
- fi
- cd ../..
- echo "✓ LuaJIT patches applied"
- else
- echo "WARNING: Could not find LuaJIT source directory"
- fi
- echo "=== FINDING AND PATCHING NGINX SOURCE ==="
- echo "Current directory: $(pwd)"
- # Find the nginx source directory
- NGINX_SRC=""
- if [ -d "build/nginx-1.27.1" ]; then
- NGINX_SRC="build/nginx-1.27.1"
- echo "Found nginx source at: $NGINX_SRC"
- elif [ -d "bundle/nginx-1.27.1" ]; then
- NGINX_SRC="bundle/nginx-1.27.1"
- echo "Found nginx source at: $NGINX_SRC"
- else
- echo "Searching for nginx source..."
- NGINX_SRC=$(find build/ -type d -name "nginx-*" | head -1)
- if [ -n "$NGINX_SRC" ]; then
- echo "Found nginx source at: $NGINX_SRC"
- else
- echo "ERROR: Could not find nginx source!"
- echo "Contents of build directory:"
- ls -la build/ || true
- exit 1
- fi
- fi
- if [ -n "$NGINX_SRC" ] && [ -d "$NGINX_SRC" ]; then
- echo "Entering nginx source: $NGINX_SRC"
- cd "$NGINX_SRC"
- echo "Now in: $(pwd)"
- EPOLL_FILE="src/event/modules/ngx_epoll_module.c"
- echo "Patching file: $EPOLL_FILE"
- # Check if file exists
- if [ ! -f "$EPOLL_FILE" ]; then
- echo "ERROR: File not found!"
- exit 1
- fi
- # Create backup
- cp "$EPOLL_FILE" "$EPOLL_FILE.backup"
- echo "Created backup: $EPOLL_FILE.backup"
- echo "Applying epoll patch..."
- # Create a properly patched version
- awk '
- BEGIN { skip_next_endif = 0 }
- /#if \(NGX_READ_EVENT != EPOLLIN\|EPOLLRDHUP\)/ {
- print " events = EPOLLIN|EPOLLRDHUP; /* PATCHED1 */"
- skip_next_endif = 1
- next
- }
- /#if \(NGX_WRITE_EVENT != EPOLLOUT\)/ {
- print " events = EPOLLOUT; /* PATCHED2 */"
- skip_next_endif = 1
- next
- }
- /#endif/ && skip_next_endif {
- skip_next_endif = 0
- next
- }
- { print }
- ' "$EPOLL_FILE.backup" > "$EPOLL_FILE.temp"
- # Check if patch was applied
- if grep -q "PATCHED" "$EPOLL_FILE.temp"; then
- echo "✓ Epoll patch applied successfully"
- mv "$EPOLL_FILE.temp" "$EPOLL_FILE"
- else
- echo "Using direct sed method..."
- cp "$EPOLL_FILE.backup" "$EPOLL_FILE"
- # Simple direct replacements
- sed -i 's/#if (NGX_READ_EVENT != EPOLLIN|EPOLLRDHUP)/ events = EPOLLIN|EPOLLRDHUP; \/* PATCHED1 *\//' "$EPOLL_FILE"
- sed -i 's/#if (NGX_WRITE_EVENT != EPOLLOUT)/ events = EPOLLOUT; \/* PATCHED2 *\//' "$EPOLL_FILE"
- # Remove any standalone #endif that might follow
- sed -i '/^[[:space:]]*#[[:space:]]*endif[[:space:]]*$/d' "$EPOLL_FILE"
- fi
- # Apply MAXNS fix
- echo "Applying MAXNS fix to ngx_resolver.c..."
- if [ -f "src/core/ngx_resolver.c" ]; then
- cp src/core/ngx_resolver.c src/core/ngx_resolver.c.backup
- awk '
- BEGIN { added = 0 }
- /^#include/ && !added {
- print "#ifndef MAXNS"
- print "#define MAXNS 3"
- print "#endif"
- added = 1
- }
- { print }
- ' src/core/ngx_resolver.c.backup > src/core/ngx_resolver.c
- echo "✓ MAXNS fix applied"
- fi
- # Go back to OpenResty directory
- cd ../..
- else
- echo "ERROR: Could not find or access nginx source!"
- exit 1
- fi
- echo "Configuring OpenResty for Termux..."
- export CC="clang -std=gnu90"
- 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"
- export LDFLAGS="-L$PREFIX/lib -Wl,-rpath,$PREFIX/lib -Wl,--export-dynamic"
- # Clean any previous build
- if [ -f "Makefile" ]; then
- make clean 2>/dev/null || true
- fi
- ./configure \
- --prefix=$PREFIX/openresty \
- --sbin-path=$PREFIX/bin/openresty \
- --conf-path=$PREFIX/etc/openresty/nginx.conf \
- --pid-path=$PREFIX/openresty/logs/nginx.pid \
- --lock-path=$PREFIX/openresty/logs/nginx.lock \
- --user=$(whoami) \
- --group=$(whoami) \
- --with-threads \
- --with-file-aio \
- --with-http_ssl_module \
- --with-http_v2_module \
- --with-http_realip_module \
- --with-http_stub_status_module \
- --with-http_gzip_static_module \
- --with-stream \
- --with-stream_ssl_module \
- --with-luajit \
- --with-cc="$CC" \
- --with-cc-opt="$CFLAGS" \
- --with-ld-opt="$LDFLAGS" \
- --with-pcre-jit \
- --without-http_upstream_zone_module
- echo "Building OpenResty..."
- if ! make -j$(nproc) 2>&1 | tee build.log; then
- echo "Parallel build failed, checking for errors..."
- # Check for LuaJIT exdata errors
- if grep -iq "lua_getexdata\|lua_setexdata" build.log; then
- echo "LuaJIT exdata functions missing, applying emergency patch..."
- # Emergency patch: Download and use pre-patched LuaJIT
- echo "Downloading pre-patched LuaJIT for Android..."
- cd "$BUILD_DIR"
- # Try to download a pre-built LuaJIT with exdata support
- if [ ! -f "luajit-patched.tar.gz" ]; then
- echo "Downloading patched LuaJIT..."
- # Try multiple sources for patched LuaJIT
- wget -q --tries=3 --timeout=30 https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20250117.tar.gz -O luajit2.tar.gz || \
- wget -q https://github.com/LuaJIT/LuaJIT/archive/refs/tags/v2.1.1699231517.tar.gz -O luajit2.tar.gz || \
- echo "Could not download LuaJIT, building from source with patches..."
- if [ -f "luajit2.tar.gz" ]; then
- tar xzf luajit2.tar.gz
- LUAJIT_DIR=$(find . -type d -name "LuaJIT-*" -o -name "luajit2-*" | head -1)
- if [ -n "$LUAJIT_DIR" ]; then
- echo "Using LuaJIT from: $LUAJIT_DIR"
- # Copy it to OpenResty build directory
- rm -rf openresty-1.27.1.2/build/LuaJIT-2.1-20250117
- cp -r "$LUAJIT_DIR" openresty-1.27.1.2/build/LuaJIT-2.1-20250117
- fi
- fi
- fi
- cd openresty-1.27.1.2
- echo "Trying build again with single core..."
- make
- else
- echo "Trying build again with single core..."
- make
- fi
- fi
- echo "Installing OpenResty..."
- make install
- echo "Creating symlinks and directories..."
- ln -sf $PREFIX/bin/openresty $PREFIX/bin/nginx-lua 2>/dev/null || true
- mkdir -p $PREFIX/etc/openresty/conf.d
- mkdir -p $PREFIX/openresty/{logs,html,lualib,lua}
- # Fix LuaJIT installation
- echo "Setting up LuaJIT..."
- if [ -d "$PREFIX/openresty/luajit" ]; then
- echo "Found LuaJIT installation..."
- # Create necessary symlinks
- if [ -f "$PREFIX/openresty/luajit/lib/libluajit-5.1.so" ]; then
- ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so $PREFIX/lib/libluajit-5.1.so 2>/dev/null || true
- ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so $PREFIX/lib/libluajit.so 2>/dev/null || true
- fi
- if [ -f "$PREFIX/openresty/luajit/lib/libluajit-5.1.so.2" ]; then
- ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so.2 $PREFIX/lib/libluajit-5.1.so.2 2>/dev/null || true
- ln -sf $PREFIX/openresty/luajit/lib/libluajit-5.1.so.2 $PREFIX/lib/libluajit.so.2 2>/dev/null || true
- fi
- if [ -f "$PREFIX/openresty/luajit/bin/luajit" ]; then
- ln -sf $PREFIX/openresty/luajit/bin/luajit $PREFIX/bin/luajit 2>/dev/null || true
- fi
- # Check if we need to manually add exdata symbols
- echo "Checking LuaJIT library for exdata symbols..."
- if ! nm $PREFIX/openresty/luajit/lib/libluajit-5.1.so 2>/dev/null | grep -q "lua_getexdata"; then
- echo "WARNING: LuaJIT library missing exdata symbols"
- echo "Creating wrapper library with exdata support..."
- # Create a wrapper library with the missing symbols
- cat > /tmp/luajit_wrapper.c << 'WRAPPER'
- #include <lua.h>
- #include <lauxlib.h>
- #include <lualib.h>
- static void *exdata = NULL;
- LUA_API void *lua_getexdata(lua_State *L) {
- (void)L; // Unused parameter
- return exdata;
- }
- LUA_API void lua_setexdata(lua_State *L, void *ed) {
- (void)L; // Unused parameter
- exdata = ed;
- }
- WRAPPER
- # Compile the wrapper
- clang -std=gnu90 -fPIC -shared -o $PREFIX/lib/libluajit_exdata.so /tmp/luajit_wrapper.c
- # Create a shell script wrapper for openresty
- cat > $PREFIX/bin/openresty_wrapper << 'WRAPPER_SCRIPT'
- #!/bin/bash
- export LD_PRELOAD="$PREFIX/lib/libluajit_exdata.so:$PREFIX/openresty/luajit/lib/libluajit-5.1.so"
- exec $PREFIX/openresty/nginx/sbin/nginx "$@"
- WRAPPER_SCRIPT
- chmod +x $PREFIX/bin/openresty_wrapper
- mv $PREFIX/bin/openresty $PREFIX/bin/openresty.orig
- ln -sf $PREFIX/bin/openresty_wrapper $PREFIX/bin/openresty
- echo "✓ Created wrapper with exdata support"
- else
- echo "✓ LuaJIT library has exdata symbols"
- fi
- fi
- echo "Creating nginx configuration..."
- # Create a proper nginx.conf with hardcoded paths
- cat > "$PREFIX/etc/openresty/nginx.conf" << NGINXCONF
- worker_processes 1;
- error_log $PREFIX/openresty/logs/error.log;
- pid $PREFIX/openresty/logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- lua_package_path "$PREFIX/openresty/lua/?.lua;$PREFIX/openresty/lualib/?.lua;;";
- lua_package_cpath "$PREFIX/openresty/lib/lua/?.so;;";
- server {
- listen 8081;
- server_name localhost;
- location / {
- root $PREFIX/openresty/html;
- index index.html index.htm;
- }
- location /test_lua {
- default_type 'text/plain';
- content_by_lua_block {
- ngx.say("OpenResty on Termux: FINALLY WORKING!");
- if jit then
- ngx.say("LuaJIT: " .. jit.version);
- end
- ngx.say("Time: " .. os.date("%Y-%m-%d %H:%M:%S"));
- }
- }
- location /nginx_status {
- stub_status;
- access_log off;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root $PREFIX/openresty/html;
- }
- }
- }
- NGINXCONF
- # Create mime.types file if it doesn't exist
- if [ ! -f "$PREFIX/etc/openresty/mime.types" ]; then
- echo "Creating mime.types file..."
- cat > "$PREFIX/etc/openresty/mime.types" << 'MIMETYPES'
- types {
- text/html html htm shtml;
- text/css css;
- text/xml xml;
- image/gif gif;
- image/jpeg jpeg jpg;
- application/javascript js;
- application/atom+xml atom;
- application/rss+xml rss;
- text/mathml mml;
- text/plain txt;
- text/vnd.sun.j2me.app-descriptor jad;
- text/vnd.wap.wml wml;
- text/x-component htc;
- image/png png;
- image/tiff tif tiff;
- image/vnd.wap.wbmp wbmp;
- image/x-icon ico;
- image/x-jng jng;
- image/x-ms-bmp bmp;
- image/svg+xml svg svgz;
- image/webp webp;
- application/font-woff woff;
- application/java-archive jar war ear;
- application/json json;
- application/mac-binhex40 hqx;
- application/msword doc;
- application/pdf pdf;
- application/postscript ps eps ai;
- application/rtf rtf;
- application/vnd.apple.mpegurl m3u8;
- application/vnd.ms-excel xls;
- application/vnd.ms-fontobject eot;
- application/vnd.ms-powerpoint ppt;
- application/vnd.wap.wmlc wmlc;
- application/vnd.google-earth.kml+xml kml;
- application/vnd.google-earth.kmz kmz;
- application/x-7z-compressed 7z;
- application/x-cocoa cco;
- application/x-java-archive-diff jardiff;
- application/x-java-jnlp-file jnlp;
- application/x-makeself run;
- application/x-perl pl pm;
- application/x-pilot prc pdb;
- application/x-rar-compressed rar;
- application/x-redhat-package-manager rpm;
- application/x-sea sea;
- application/x-shockwave-flash swf;
- application/x-stuffit sit;
- application/x-tcl tcl tk;
- application/x-x509-ca-cert der pem crt;
- application/x-xpinstall xpi;
- application/xhtml+xml xhtml;
- application/xspf+xml xspf;
- application/zip zip;
- application/octet-stream bin exe dll;
- application/octet-stream deb;
- application/octet-stream dmg;
- application/octet-stream iso img;
- application/octet-stream msi msp msm;
- audio/midi mid midi kar;
- audio/mpeg mp3;
- audio/ogg ogg;
- audio/x-m4a m4a;
- audio/x-realaudio ra;
- video/3gpp 3gpp 3gp;
- video/mp2t ts;
- video/mp4 mp4;
- video/mpeg mpeg mpg;
- video/quicktime mov;
- video/webm webm;
- video/x-flv flv;
- video/x-m4v m4v;
- video/x-mng mng;
- video/x-ms-asf asx asf;
- video/x-ms-wmv wmv;
- video/x-msvideo avi;
- }
- MIMETYPES
- fi
- cat > "$PREFIX/openresty/html/index.html" << 'HTML'
- <!DOCTYPE html>
- <html>
- <head>
- <title>OpenResty Termux</title>
- </head>
- <body>
- <h1>OpenResty on Termux</h1>
- <p>Server is running.</p>
- <p><a href="/test_lua">Test Lua</a></p>
- <p><a href="/nginx_status">Nginx Status</a></p>
- </body>
- </html>
- HTML
- # Create a test script to verify installation - FIXED PATH
- mkdir -p "$PREFIX/openresty/lua"
- cat > "$PREFIX/openresty/lua/test.lua" << 'LUA_TEST'
- ngx.header["Content-Type"] = "text/plain"
- ngx.say("OpenResty Lua test successful!")
- ngx.say("LuaJIT version: " .. (jit and jit.version or "unknown"))
- ngx.say("Current time: " .. os.date("%Y-%m-%d %H:%M:%S"))
- LUA_TEST
- # Test if openresty works
- echo "Testing OpenResty installation..."
- if $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf 2>&1 | grep -q "successful"; then
- echo "✅ OpenResty configuration test successful!"
- echo ""
- echo "To start OpenResty:"
- echo " openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf"
- echo ""
- echo "Or use the created launcher scripts:"
- # Create launcher scripts
- cat > $PREFIX/bin/start-openresty << 'LAUNCHER'
- #!/bin/bash
- $PREFIX/bin/openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
- LAUNCHER
- chmod +x $PREFIX/bin/start-openresty
- cat > $PREFIX/bin/stop-openresty << 'STOPPER'
- #!/bin/bash
- $PREFIX/bin/openresty -s stop -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
- STOPPER
- chmod +x $PREFIX/bin/stop-openresty
- cat > $PREFIX/bin/restart-openresty << 'RESTARTER'
- #!/bin/bash
- $PREFIX/bin/openresty -s stop -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf 2>/dev/null || true
- sleep 1
- $PREFIX/bin/openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
- RESTARTER
- chmod +x $PREFIX/bin/restart-openresty
- echo "Start: start-openresty"
- echo "Test: curl http://localhost:8081/test_lua"
- echo "Stop: stop-openresty"
- echo "Restart: restart-openresty"
- else
- echo "⚠️ OpenResty configuration test failed"
- echo "Trying alternative approach..."
- # Create a launcher script with explicit paths
- cat > $PREFIX/bin/openresty_launcher << 'LAUNCHER'
- #!/bin/bash
- # OpenResty launcher for Termux
- export PATH=$PREFIX/openresty/luajit/bin:$PATH
- export LD_LIBRARY_PATH=$PREFIX/openresty/luajit/lib:$PREFIX/lib:$LD_LIBRARY_PATH
- exec $PREFIX/openresty/nginx/sbin/nginx -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf "\$@"
- LAUNCHER
- chmod +x $PREFIX/bin/openresty_launcher
- # Test the launcher
- if $PREFIX/bin/openresty_launcher -t 2>&1 | grep -q "successful"; then
- echo "✅ Launcher script works!"
- # Create aliases for the launcher
- cat > $PREFIX/bin/start-openresty << 'LAUNCHER2'
- #!/bin/bash
- $PREFIX/bin/openresty_launcher
- LAUNCHER2
- chmod +x $PREFIX/bin/start-openresty
- cat > $PREFIX/bin/stop-openresty << 'STOPPER2'
- #!/bin/bash
- $PREFIX/bin/openresty_launcher -s stop
- STOPPER2
- chmod +x $PREFIX/bin/stop-openresty
- echo ""
- echo "Start: start-openresty"
- echo "Test: curl http://localhost:8081/test_lua"
- echo "Stop: stop-openresty"
- else
- echo "❌ Configuration issues detected"
- echo "Try manually checking the config:"
- echo " $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf"
- echo ""
- echo "Common issues and fixes:"
- echo "1. Make sure all directories exist:"
- echo " mkdir -p $PREFIX/openresty/{logs,html,lualib,lua}"
- echo "2. Check permissions:"
- echo " chmod 755 $PREFIX/openresty"
- echo "3. Try running nginx directly:"
- echo " $PREFIX/openresty/nginx/sbin/nginx -t -c $PREFIX/etc/openresty/nginx.conf"
- fi
- fi
- # Create a simple test script
- cat > $HOME/test_openresty.sh << 'TEST_SCRIPT'
- #!/bin/bash
- echo "Testing OpenResty installation..."
- echo "1. Checking if openresty binary exists..."
- if [ -f "$PREFIX/bin/openresty" ]; then
- echo " ✓ openresty binary found"
- else
- echo " ✗ openresty binary not found"
- exit 1
- fi
- echo "2. Testing nginx configuration..."
- if $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf 2>&1 | grep -q "successful"; then
- echo " ✓ Configuration test passed"
- else
- echo " ✗ Configuration test failed"
- $PREFIX/bin/openresty -t -c $PREFIX/etc/openresty/nginx.conf
- exit 1
- fi
- echo "3. Starting OpenResty..."
- $PREFIX/bin/openresty -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
- sleep 2
- echo "4. Testing HTTP endpoint..."
- if curl -s http://localhost:8081/test_lua | grep -q "OpenResty on Termux"; then
- echo " ✓ HTTP test passed"
- else
- echo " ✗ HTTP test failed"
- echo "Response was:"
- curl -s http://localhost:8081/test_lua
- fi
- echo "5. Stopping OpenResty..."
- $PREFIX/bin/openresty -s stop -p $PREFIX/openresty -c $PREFIX/etc/openresty/nginx.conf
- echo ""
- echo "✅ OpenResty installation verified!"
- TEST_SCRIPT
- chmod +x $HOME/test_openresty.sh
- echo ""
- echo "Installation complete!"
- echo "You can test the installation with: ./test_openresty.sh"
- echo ""
- echo "Quick reference:"
- echo " Start: start-openresty"
- echo " Stop: stop-openresty"
- echo " Restart: restart-openresty"
- echo " Test: curl http://localhost:8081/test_lua"
- echo ""
- echo "Configuration files:"
- echo " Main config: $PREFIX/etc/openresty/nginx.conf"
- echo " Logs: $PREFIX/openresty/logs/"
- echo " HTML: $PREFIX/openresty/html/"
- echo " Lua scripts: $PREFIX/openresty/lua/"
Advertisement