1
+ #! /usr/bin/env bash
2
+ #
3
+ # Setup script for a brand new Ubuntu 24 VPS to run question.sh on port 22
4
+ # while keeping the main system SSH daemon on port 2345.
5
+ #
6
+ # USAGE:
7
+ # 1) Upload this script to your VPS (e.g., scp setup-question-sh.sh user@vps:/tmp/)
8
+ # 2) Run it as root: sudo bash /tmp/setup-question-sh.sh
9
+ # 3) Adjust firewall settings as needed.
10
+ #
11
+ # NOTES:
12
+ # - This script modifies the default OpenSSH config to listen on port 2345 (for admin).
13
+ # - question.sh will replace port 22 for the public "ssh question.sh".
14
+ # - Installs PostgreSQL and creates a DB + user for question.sh.
15
+ # - Please adjust DB credentials (username/password) as you prefer.
16
+
17
+ set -e # Exit immediately on error
18
+
19
+ # ######################################
20
+ # 1. System Updates & Install Packages
21
+ # ######################################
22
+ echo " Updating system packages..."
23
+ apt-get update -y
24
+ apt-get upgrade -y
25
+
26
+ echo " Installing required packages (git, cron, openssh-server, postgresql)..."
27
+ apt-get install -y git cron openssh-server unzip postgresql postgresql-contrib
28
+
29
+ # ######################################
30
+ # 2. Configure Main SSH Server on Port 2345
31
+ # ######################################
32
+ echo " Configuring existing OpenSSH to run on port 2345 for admin use..."
33
+ sed -i ' s/^#*Port .*/Port 2345/' /etc/ssh/sshd_config
34
+ systemctl restart ssh
35
+ echo " Main SSH server is now on port 2345. You can connect via: ssh -p 2345 your-admin-user@your-server"
36
+
37
+ # ######################################
38
+ # 3. Install Bun (for running question.sh)
39
+ # ######################################
40
+ echo " Installing Bun..."
41
+ curl -fsSL https://bun.sh/install | bash
42
+ echo ' export BUN_INSTALL="$HOME/.bun"' >> ~ /.bashrc
43
+ echo ' export PATH="$BUN_INSTALL/bin:$PATH"' >> ~ /.bashrc
44
+ source /root/.bashrc
45
+
46
+ # ######################################
47
+ # 4. Setup PostgreSQL (user + database)
48
+ # ######################################
49
+ DB_USER=" questionsh_user"
50
+ DB_PASSWORD=" questionsh_password"
51
+ DB_NAME=" questionsh_db"
52
+
53
+ echo " Creating PostgreSQL user and database..."
54
+ sudo -u postgres psql << EOF
55
+ CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD ';
56
+ CREATE DATABASE $DB_NAME ;
57
+ GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER ;
58
+ EOF
59
+
60
+ # ######################################
61
+ # 5. Clone question.sh Repository
62
+ # ######################################
63
+ REPO_URL=" https://github.com/vincelwt/questionsh.git"
64
+ APP_DIR=" /opt/question-sh"
65
+
66
+ echo " Fetching question.sh from $REPO_URL into $APP_DIR ..."
67
+ if [ -d " $APP_DIR " ]; then
68
+ echo " Directory $APP_DIR already exists, pulling latest..."
69
+ cd " $APP_DIR "
70
+ git pull
71
+ else
72
+ git clone " $REPO_URL " " $APP_DIR "
73
+ cd " $APP_DIR "
74
+ fi
75
+
76
+ # ######################################
77
+ # 6. Create .env File for DB Access
78
+ # ######################################
79
+ echo " Creating .env file with DATABASE_URL..."
80
+ cat << EOF > .env
81
+ DATABASE_URL=postgres://$DB_USER :$DB_PASSWORD @localhost:5432/$DB_NAME
82
+ NODE_ENV=production
83
+ PORT=22
84
+ EOF
85
+
86
+ # ######################################
87
+ # 7. Generate SSH Host Keys
88
+ # ######################################
89
+ echo " Generating SSH host keys..."
90
+ mkdir -p $APP_DIR /storage
91
+ ssh-keygen -t rsa -f $APP_DIR /storage/host.key -N " "
92
+ chmod 600 $APP_DIR /storage/host.key
93
+
94
+ # ######################################
95
+ # 8. Install Dependencies
96
+ # ######################################
97
+ echo " Installing dependencies with Bun..."
98
+ bun install
99
+
100
+ # ######################################
101
+ # 9. Create a Systemd Service for question.sh on Port 22
102
+ # ######################################
103
+ SERVICE_FILE=" /etc/systemd/system/question-sh.service"
104
+ echo " Creating systemd service file at $SERVICE_FILE ..."
105
+ cat << EOF > "$SERVICE_FILE "
106
+ [Unit]
107
+ Description=Question.sh SSH server on port 22
108
+ After=network.target
109
+
110
+ [Service]
111
+ Type=simple
112
+ User=root
113
+ WorkingDirectory=$APP_DIR
114
+ EnvironmentFile=$APP_DIR /.env
115
+ ExecStart=/root/.bun/bin/bun run index.ts
116
+ Restart=on-failure
117
+
118
+ [Install]
119
+ WantedBy=multi-user.target
120
+ EOF
121
+
122
+ echo " Reloading systemd, enabling, and starting question-sh service..."
123
+ systemctl daemon-reload
124
+ systemctl enable question-sh
125
+ systemctl start question-sh
126
+
127
+ # ######################################
128
+ # 10. Open Firewall (UFW) for ports 22 & 2345
129
+ # ######################################
130
+ if command -v ufw > /dev/null 2>&1 ; then
131
+ echo " Configuring UFW..."
132
+ ufw allow 22
133
+ ufw allow 2345
134
+ ufw --force enable
135
+ else
136
+ echo " UFW not found or not installed. Skipping firewall setup..."
137
+ fi
138
+
139
+ # ######################################
140
+ # 11. Final Status
141
+ # ######################################
142
+ echo "
143
+ Setup complete!
144
+ Main SSH for admin is listening on port 2345.
145
+ question.sh is now running on port 22.
146
+
147
+ You should be able to connect publicly with:
148
+ ssh question.sh
149
+
150
+ Admin access:
151
+ ssh -p 2345 root@your-server-domain
152
+
153
+ To see question.sh logs:
154
+ journalctl -u question-sh -f
155
+
156
+ To stop:
157
+ systemctl stop question-sh
158
+ To restart:
159
+ systemctl restart question-sh
160
+ "
0 commit comments