Skip to content

Instantly share code, notes, and snippets.

@jacky9813
Last active July 20, 2024 07:20
Show Gist options
  • Save jacky9813/63ee24f7bff753a551321d94e03e0b4a to your computer and use it in GitHub Desktop.
Save jacky9813/63ee24f7bff753a551321d94e03e0b4a to your computer and use it in GitHub Desktop.
Minecraft Server
[Unit]
Description=Minecraft Server in /opt/minecraft-server/%i
After=network-online.target
Requires=network-online.target
[Service]
Type=simple
KillSignal=SIGINT
ExecStart=/opt/minecraft-server/%i/run.sh --nogui
WorkingDirectory=/opt/minecraft-server/%i
Restart=always
User=minecraft-server
Group=minecraft-server
[Install]
WantedBy=multi-user.target
#!/bin/bash
# This script installs Forge Minecraft Server.
# This script should work well with RHEL and its derivatives (Rocky, Alma, etc) without any changes.
# For Minecraft 1.x.0, reference it as 1.x (removes the trailing .0)
# For Minecraft 1.x.y, when y is not zero. reference it as is (1.x.y)
MINECRAFT_VERSION=1.21
FORGE_VERSION=51.0.26
INSTALLED_VERSION=forge-${MINECRAFT_VERSION}-${FORGE_VERSION}
BASEDIR=/opt/minecraft-server
INSTALLDIR=$BASEDIR/$INSTALLED_VERSION
# Change this to apt, zypper, or whatever you system online package manager is.
sudo dnf install -y java-17-openjdk shadow-utils curl
sudo useradd -d $BASEDIR -m -r -s /sbin/nologin minecraft-server
sudo -u minecraft-server mkdir -p $INSTALLDIR
sudo -u minecraft-server curl \
-O $INSTALLDIR/ \
https://maven.minecraftforge.net/net/minecraftforge/forge/${MINECRAFT_VERSION}-${FORGE_VERSION}/forge-${MINECRAFT_VERSION}-${FORGE_VERSION}-installer.jar
sudo -u minecraft-server -D $INSTALLDIR forge-${MINECRAFT_VERSION}-${FORGE_VERSION}-installer.jar --installServer
cat << EOF | sudo tee /lib/systemd/system/[email protected]
[Unit]
Description=Minecraft Server in /opt/minecraft-server/%i
After=network-online.target
Requires=network-online.target
[Service]
Type=simple
KillSignal=SIGINT
ExecStart=/opt/minecraft-server/%i/run.sh --nogui
WorkingDirectory=/opt/minecraft-server/%i
Restart=always
User=minecraft-server
Group=minecraft-server
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now minecraft-server@${INSTALLED_VERSION}.service
# Ports for Minecraft Server in firewalld.
sudo firewall-cmd --new-service minecraft-server --permanent
sudo firewall-cmd --add-port 25565/tcp --service minecraft-server --permanent
sudo firewall-cmd --add-port 25565/udp --service minecraft-server --permanent
sudo firewall-cmd --add-service minecraft-server --permanent
sudo firewall-cmd --reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment