Serve Shlink using openswoole
Shlink can be served using openswoole.
It provides a non-blocking I/O server, similar to the one used for node.js applications, which lets web apps to be run once and kept in memory while serving requests.
openswoole is quite fast, and has a few extra benefits (like running some heavy tasks immediately after a request has been served).
Note
Shlink v3.0.0 officially supports only openswoole, and no longer regular swoole. However, the functionality does not currently deviate too much, so regular swoole will continue to be detected as openswoole for the time being.
If for any reason regular swoole stops working, the only officially supported solution will be to migrate to openswoole.
In order to use openswoole, you have to install the openswoole PHP extension using Pecl: pecl install openswoole-22.0.0
.
After that, you can serve Shlink with php vendor/bin/laminas mezzio:swoole:start
.
Shlink as a system service
Since you probably don’t want to manually start Shlink every time, this explains how to set it up as a Daemon in Linux.
First you need to create a daemon script, in /etc/init.d/shlink_swoole
.
You can use this sample, replacing /path/to/shlink
by the path to your shlink installation:
#!/bin/bash
### BEGIN INIT INFO
# Provides: shlink_openswoole
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Shlink non-blocking server with openswoole
### END INIT INFO
SCRIPT=/path/to/shlink/vendor/bin/laminas\ mezzio:swoole:start
RUNAS=root
PIDFILE=/var/run/shlink_openswoole.pid
LOGDIR=/var/log/shlink
LOGFILE=${LOGDIR}/shlink_openswoole.log
start() {
if [[ -f "$PIDFILE" ]] && kill -0 $(cat "$PIDFILE"); then
echo 'Shlink with openswoole already running' >&2
return 1
fi
echo 'Starting shlink with openswoole' >&2
mkdir -p "$LOGDIR"
touch "$LOGFILE"
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Shlink started' >&2
}
stop() {
if [[ ! -f "$PIDFILE" ]] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Shlink with openswoole not running' >&2
return 1
fi
echo 'Stopping shlink with openswoole' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Shlink stopped' >&2
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac
Then run these commands to enable the service and start it:
sudo chmod +x /etc/init.d/shlink_swoole
sudo update-rc.d shlink_swoole defaults
sudo update-rc.d shlink_swoole enable
/etc/init.d/shlink_swoole start
After that, you should be able to access shlink on port 8080. The service will be automatically run at system start-up, and all access logs will be written in /var/log/shlink/shlink_swoole.log
.
Exposing Shlink through a reverse proxy
Event though previous steps are enough to make Shlink usable, there are still a couple of missing things to make it production-ready.
Follow the instructions to configure a reverse proxy in front of Shlink.