Skip to main content

Separate Frontend and Backend Deployment

Front-end and back-end separation deployment is suitable for environments where you already have experience maintaining Linux servers and Nginx. The front-end static files are served by Nginx, the back-end JAR file only listens on the server's local port, and browsers uniformly access the Nginx server.

Use Cases

  • You want users to access the system through ports 80 or 443.
  • You already have Nginx and want to manage front-end static files and back-end APIs separately.
  • You can connect to the server via SSH and maintain Java, Nginx, and firewall configurations.
warning

If you do not have server maintenance experience, use Docker Compose Deployment or aaPanel One-Click Deployment instead.

Deployment Structure

Browser
|
| http://your_domain/
v
Nginx
|-- / -> /opt/surveyking/html front-end static files
|-- /api -> http://127.0.0.1:1991 back-end API

The back-end port 1991 is only for Nginx proxy use and does not need to be directly exposed to the public network.

Prepare Files

FileDownload AddressPurpose
html.ziphttps://download.surveyking.cn/files/html.zipFront-end static files
surveyking.jarhttps://download.surveyking.cn/files/surveyking.jarBack-end application
tip

The back-end JAR file uses a fixed download address and filename. When a new version is released, you only need to update surveyking.jar on the download station without changing the version number in the document commands.

Operation Steps

1. Install Java, Nginx, and Unzip Tools

Ubuntu / Debian:

sudo apt update
sudo apt install -y openjdk-8-jdk nginx unzip curl
sudo systemctl enable --now nginx

CentOS / RHEL:

sudo yum install -y java-1.8.0-openjdk nginx unzip curl
sudo systemctl enable --now nginx

Check Java and Nginx:

java -version
nginx -v

2. Create Deployment Directory

sudo mkdir -p /opt/surveyking/html /opt/surveyking/files /opt/surveyking/logs
sudo chown -R $USER:$USER /opt/surveyking
cd /opt/surveyking

3. Download and Unzip Front-End

curl -L -o html.zip https://download.surveyking.cn/files/html.zip
rm -rf /opt/surveyking/html/*
unzip -q html.zip -d /opt/surveyking/html

Confirm that index.html has been unzipped:

ls -lh /opt/surveyking/html/index.html

4. Download Back-End JAR

curl -L -o surveyking.jar https://download.surveyking.cn/files/surveyking.jar

Confirm the file exists:

ls -lh /opt/surveyking/surveyking.jar

5. Start Back-End

Start it once with the command line to confirm that the back-end can run normally:

nohup java -Xms512m -Xmx1024m -jar /opt/surveyking/surveyking.jar \
--spring.profiles.active=h2 \
--server.port=1991 \
--file-storage.local.root-path=/opt/surveyking/files \
> /opt/surveyking/logs/surveyking.log 2>&1 &

View the log:

tail -f /opt/surveyking/logs/surveyking.log

When you see Started SurveyServerApplication, the back-end has started successfully.

6. Configure Nginx Proxy

Create a configuration file:

sudo vim /etc/nginx/conf.d/surveyking.conf

Enter the following content. If you do not have a domain name, you can temporarily leave server_name as _ and access it using the server's public IP address.

server {
listen 80;
server_name _;

root /opt/surveyking/html;
index index.html;

client_max_body_size 100m;

location /api {
proxy_pass http://127.0.0.1:1991;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}

location / {
try_files $uri $uri/ /index.html;
}
}

If Ubuntu uses the default site to occupy port 80, you can first delete the default site link:

sudo rm -f /etc/nginx/sites-enabled/default

Check and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

7. Open and Initialize System

Access via browser:

http://server_public_ip

If you have already bound a domain name, access:

http://your_domain

The first time you open it, you will enter the /setup page.

  • Trial: Select H2 embedded database.
  • Formal use: It is recommended to select MySQL database. If you need to manually install MySQL, refer to the MySQL creation steps in Linux Manual Deployment.

Subsequent system information, default account, and password change steps are described in: Initialization, Backup, and Upgrade.

Set Back-End Auto Startup

Create a systemd service:

sudo vim /etc/systemd/system/surveyking.service

Enter the following content:

[Unit]
Description=SurveyKing
After=network.target mysql.service mysqld.service

[Service]
Type=simple
WorkingDirectory=/opt/surveyking
ExecStart=/usr/bin/java -Xms512m -Xmx1024m -jar /opt/surveyking/surveyking.jar --spring.profiles.active=h2 --server.port=1991 --file-storage.local.root-path=/opt/surveyking/files
Restart=always
RestartSec=10
StandardOutput=append:/opt/surveyking/logs/surveyking.log
StandardError=append:/opt/surveyking/logs/surveyking.log

[Install]
WantedBy=multi-user.target

Enable the service:

pkill -f /opt/surveyking/surveyking.jar || true
sudo systemctl daemon-reload
sudo systemctl enable surveyking
sudo systemctl start surveyking
sudo systemctl status surveyking

Common commands:

What do you want to doCommand
Start back-endsudo systemctl start surveyking
Stop back-endsudo systemctl stop surveyking
Restart back-endsudo systemctl restart surveyking
View back-end statussudo systemctl status surveyking
View back-end logtail -f /opt/surveyking/logs/surveyking.log
Reload Nginxsudo systemctl reload nginx

Allow Ports

After front-end and back-end separation, you usually only need to allow ports 80 and 443 for the public network. The back-end port 1991 is recommended to be listened on locally only and not opened in the cloud server security group.

Ubuntu / Debian:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw status

CentOS / RHEL:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
warning

You also need to allow security groups in the cloud service provider's control panel. Allowing ports on the system firewall alone does not necessarily mean that the public network can access them.

Backup and Upgrade

Daily backups, import/restore, and database upgrade steps are unified in: Initialization, Backup, and Upgrade.

Update front-end:

cd /opt/surveyking
cp -r html html.bak
curl -L -o html.zip https://download.surveyking.cn/files/html.zip
rm -rf html/*
unzip -q html.zip -d html
sudo systemctl reload nginx

Update back-end:

cd /opt/surveyking
sudo systemctl stop surveyking
cp surveyking.jar surveyking.jar.bak
curl -L -o surveyking.jar.new https://download.surveyking.cn/files/surveyking.jar
mv surveyking.jar.new surveyking.jar
sudo systemctl start surveyking

About Download Filenames

The document uses a stable download address:

https://download.surveyking.cn/files/surveyking.jar

This way, when a new version is released, you only need to update surveyking.jar on the download station. The server local, systemd service, and document commands continue to use /opt/surveyking/surveyking.jar without changing the version number.

Common Questions

Why do I get a normal page but login or initialization fails?

This is usually because Nginx is not proxying /api to the back-end. Check if there is a location /api in /etc/nginx/conf.d/surveyking.conf, then execute:

sudo nginx -t
sudo systemctl reload nginx

Also confirm that the back-end is running:

sudo systemctl status surveyking
Why does refreshing the page turn into a 404?

The front-end is a single-page application, and Nginx needs to redirect non-file paths to index.html. Confirm that there is this section in the configuration:

location / {
try_files $uri $uri/ /index.html;
}
Why is it not recommended to open port 1991?

When deploying front-end and back-end separation, the public entrance should be uniformly handed over to Nginx. Nginx can be responsible for domain names, HTTPS, upload size limits, and logs; the back-end 1991 remains on the local machine for proxying only, reducing the attack surface.