Applications developed using GoFrame
can be independently deployed on servers to run as background daemon processes. This mode is commonly used in simple API service projects.
We recommend using the *nix
server series (including: Linux
, MacOS
, *BSD
). Below, we use the Ubuntu
system as an example to introduce how to deploy projects developed using the GoFrame
framework.
*nix
1. nohup
We can use the simple nohup
command to run the application as a background daemon process so that even if a remote SSH connection is disconnected, the program’s execution will not be affected. The nohup
command tool is often pre-installed in popular Linux distributions. The command is as follows:
nohup ./gf-app &
2. tmux
tmux
is a terminal multiplexer tool under Linux
that can open different terminal windows to run applications as background daemon processes. Even if the remote SSH
connection is disconnected, the program’s execution will not be affected. Install it directly on the ubuntu
system using sudo apt-get install tmux
. Use the following steps to run the application in the background.
tmux new -s gf-app
;- Execute
./gf-app
in the new terminal window; - Use the shortcut
ctrl
+B & D
to exit the current terminal window; - Use
tmux attach -t gf-app
to enter the previous terminal window;
3. supervisor
supervisor
is a universal process control program developed in Python
, capable of transforming a normal command-line process into a background daemon
and monitoring the process status, automatically restarting it if it exits abnormally. Official website: http://supervisord.org/ Common configuration is as follows:
[program:gf-app]
user = root
directory = /var/www
command = /var/www/main
stdout_logfile = /var/log/gf-app-stdout.log
stderr_logfile = /var/log/gf-app-stderr.log
autostart = true
autorestart = true
The steps are as follows:
- Use
sudo service supervisor start
to start thesupervisor
service; - Create an application configuration file
/etc/supervisor/conf.d/gf-app.conf
, with the content as above; - Use
sudo supervisorctl
to enter thesupervisor
management terminal; - Use
reload
to reread the configuration file and restart all processes managed bysupervisor
; - You can also use
update
to reload the configuration (default without restarting) and then usestart gf-app
to start the specified application; - Then use the
status
command to view the status of the processes managed bysupervisor
;
Sharing pitfalls:
- After changing the
conf
configuration file, executereload
insupervisorctl
to update and use the latest configuration. - The
directory
configuration usually cannot be omitted, specifying the current working directory path, and must be configured beforecommand
. - The
command
needs to use an absolute path; otherwise, it will not find the executable file.
4. systemctl
Systemd
is a Linux
system tool used to start daemon processes, and it has become the standard configuration for most distributions.
systemctl
is the main command of Systemd
used to manage the system. You can refer to Ruan Yifeng’s interpretation of Systemd, particularly sections four and five.
In fact, most of our services are managed with systemctl
, such as MySQL, Nginx
, etc.
Common configuration is as follows:
[Unit]
# Unit description
Description=GF APP
# Execute this program after what service starts
After=mysql.service
[Service]
Type=simple
# Directory of program execution
WorkingDirectory=/data/server/gfapp/
# Startup script command
ExecStart=/data/server/gfapp/gfapp
# Restart conditions
Restart=always
# Seconds to restart
RestartSec=5
[Install]
WantedBy=multi-user.target
Usage:
- Create an application configuration file
/etc/systemd/system/gfapp.service
, with the content as above; - Use
systemctl daemon-reload
to reload services; - Execute
systemctl start gfapp
to start the service; - Finally, execute
systemctl status gfapp
to view the service running status information; - Execute
systemctl enable gfapp
to add the service to the boot startup items; - Note: The executed
gfapp
uses the file name as the service name; - Common commands are:
start(start), stop(stop), restart(restart), status(view running status), enable(add to boot startup), disable(remove program from boot startup)
5. screen
Screen
is a free software developed under the GNU
project for command-line terminal switching. Users can connect to multiple local or remote command-line sessions simultaneously and switch between them freely. GNU Screen
can be considered a command-line interface version of a window manager. It provides a unified interface and corresponding functionality for managing multiple sessions.
Installation:
sudo apt install -y screen
( debian
series)
sudo yum install -y screen
( centos
)
Common parameters:
screen -S yourname
-> Create a session called yournamescreen -ls
-> List all current sessionsscreen -r yourname
-> Return to yourname sessionscreen -d yourname
-> Remotely detach a sessionscreen -d -r yourname
-> End the current session and return to yourname session
Usage:
- Use the command
screen -S gfapp
to create a session; - Execute
./gf-app
in the new terminal window; - Execute
ctrl-a, ctrl-d
to temporarily leave the current session; - Execute
screen -r gfapp
to return to the command window; if not successful, the window might be occupied (Attached
), tryscreen -Dr gfapp
; - Execute
screen -X -S gfapp quit
to end the program;
windows
1. NSSM
Small and Practical NSSM Packaging Windows Service Tool Introduction - Zhihu (zhihu.com)