-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
144 lines (125 loc) · 5.79 KB
/
Copy pathsetup.sh
File metadata and controls
144 lines (125 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
# ============================================================================
# PSP Router — OpenResty Setup Script
# ============================================================================
# Installs OpenResty alongside existing Nginx on a Debian/Ubuntu server.
# OpenResty listens on port 8081. Existing Nginx config is NOT modified.
#
# Usage:
# sudo bash setup.sh
#
# What this script does:
# - Installs OpenResty from official repo
# - Installs lua-resty-http
# - Creates directory structure for Lua code
#
# What this script does NOT do:
# - Does NOT modify any existing Nginx config files
# - Does NOT touch /etc/nginx/
# - Does NOT affect the running Nginx process
#
# To enable the proxy, run deploy.sh which will:
# - Copy the snippet to /etc/nginx/snippets/
# - You must manually add the include line to your App config
#
# To fully remove: run uninstall.sh
# ============================================================================
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() { echo -e "${GREEN}[SETUP]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
err() { echo -e "${RED}[ERROR]${NC} $1"; }
OPENRESTY_DIR="/usr/local/openresty"
NGINX_DIR="${OPENRESTY_DIR}/nginx"
LUA_DIR="${NGINX_DIR}/lua/psp_handlers"
CONF_DIR="${NGINX_DIR}/conf"
# ── Step 1: Install dependencies ────────────────────────────────────────────
log "Installing dependencies..."
apt-get update -qq
apt-get install -y -qq \
wget \
gnupg2 \
ca-certificates \
lsb-release \
curl \
rsync \
procps \
> /dev/null 2>&1
# ── Step 2: Install OpenResty ───────────────────────────────────────────────
if [ -d "${OPENRESTY_DIR}" ]; then
log "OpenResty already installed at ${OPENRESTY_DIR}"
else
log "Adding OpenResty repository..."
wget -qO - https://openresty.org/package/pubkey.gpg | gpg --dearmor -o /usr/share/keyrings/openresty.gpg 2>/dev/null
CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}")
# Detect distro family: Ubuntu uses /package/ubuntu, Debian uses /package/debian
if [ -f /etc/os-release ]; then
DISTRO_ID=$(. /etc/os-release && echo "${ID}")
else
DISTRO_ID="debian"
fi
# The APT component differs per distro in OpenResty's repo layout:
# Ubuntu publishes under "main", Debian under "openresty". Using "main" on
# Debian yields a 404 on the Release file and the package can never install.
case "${DISTRO_ID}" in
ubuntu) REPO_DISTRO="ubuntu"; REPO_COMPONENT="main" ;;
*) REPO_DISTRO="debian"; REPO_COMPONENT="openresty" ;;
esac
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/openresty.gpg] http://openresty.org/package/${REPO_DISTRO} ${CODENAME} ${REPO_COMPONENT}" \
> /etc/apt/sources.list.d/openresty.list
apt-get update -qq
log "Installing OpenResty..."
apt-get install -y -qq openresty > /dev/null 2>&1
fi
if [ ! -x "${OPENRESTY_DIR}/bin/openresty" ]; then
err "OpenResty binary not found at ${OPENRESTY_DIR}/bin/openresty"
exit 1
fi
log "OpenResty version: $(${OPENRESTY_DIR}/bin/openresty -v 2>&1)"
# ── Step 3: Install lua-resty-http ──────────────────────────────────────────
if [ ! -f "${OPENRESTY_DIR}/lualib/resty/http.lua" ]; then
log "Installing lua-resty-http..."
apt-get install -y -qq openresty-opm > /dev/null 2>&1 || true
${OPENRESTY_DIR}/bin/opm get ledgetech/lua-resty-http 2>/dev/null || warn "opm install failed — can be installed manually later"
fi
# ── Step 4: Create directory structure ──────────────────────────────────────
log "Creating directory structure..."
mkdir -p "${LUA_DIR}/core"
mkdir -p "${CONF_DIR}/conf.d"
mkdir -p "${NGINX_DIR}/logs"
# ── Step 5: Set permissions ─────────────────────────────────────────────────
log "Setting permissions..."
chmod -R 755 "${NGINX_DIR}/lua"
# ── Step 6: Stop default OpenResty (package may auto-start) ─────────────────
# PID file, not `pgrep -f openresty` — that pattern matches any command line
# containing the word, including this script when run by absolute path.
SETUP_PID_FILE="${OPENRESTY_DIR}/nginx/logs/nginx.pid"
if [ -f "${SETUP_PID_FILE}" ] && kill -0 "$(cat "${SETUP_PID_FILE}" 2>/dev/null)" 2>/dev/null; then
log "Stopping auto-started OpenResty..."
${OPENRESTY_DIR}/bin/openresty -s stop 2>/dev/null || true
fi
if command -v systemctl > /dev/null 2>&1; then
systemctl disable openresty 2>/dev/null || true
systemctl stop openresty 2>/dev/null || true
fi
# ── Done ────────────────────────────────────────────────────────────────────
log ""
log "============================================"
log " OpenResty setup complete!"
log "============================================"
log " Binary: ${OPENRESTY_DIR}/bin/openresty"
log " Config: ${CONF_DIR}/nginx.conf"
log " Lua code: ${LUA_DIR}/"
log " Logs: ${NGINX_DIR}/logs/"
log ""
log " Existing Nginx was NOT modified."
log ""
log " Next steps:"
log " 1. Run ./deploy.sh to deploy code + config"
log " 2. Add this line to your App nginx config:"
log " include /etc/nginx/snippets/psp-router.conf;"
log " 3. Reload nginx: nginx -s reload"
log "============================================"