Oracle Cloud VPS free tier Minecraft server

Minecraft is a 3D sandbox game where players interact with a fully modifiable three-dimensional environment made of blocks and entities. Its diverse game-play lets players choose the way they play, allowing for countless possibilities.

Minecraft allow users to setup their own private/offline multiplayer server free of charge. Let install minecraft server on Oracle Cloud Ubuntu VPS free tier.

1. Setup Ubuntu 20.04 minimal instance and install below package requirement.

sudo apt-get install wget cron rsyslog logrotate lsof nano screen openjdk-17-jre-headless

2. Setup 2GB (or more) swap file for Oracle Cloud Ubuntu instance.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak # optional
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

3. Optional to disable extra services and gain more server resource for minecraft service.

sudo systemctl disable iscsid
sudo systemctl stop iscsid
sudo snap disable oracle-cloud-agent
sudo snap disable core18
sudo snap disable snapd

4. Create minecraft user and directory to store the minecraft data.

sudo useradd -r -s /usr/sbin/nologin minecraft
sudo mkdir -p /opt/minecraft

5. Download latest minecraft server from the official website.

sudo wget https://launcher.mojang.com/v1/objects/c8f83c5655308435b3dcf03c06d9fe8740a77469/server.jar -P /opt/minecraft

6. Accept EULA of minecraft service.

sudo bash -c "echo eula=true > /opt/minecraft/eula.txt"

7. Test run server.jar with 1G of memory (Oracle Cloud VPS free tier limit 😢). It may take 20 minute to run due to the low memory.

sudo java -Xms512M -Xmx1024M -jar server.jar --nogui

8. Create bash script to start minecraft server service (/opt/minecraft/start_mc.sh).

#!/bin/bash
xms=512M #initial memory size
xmx=1024M #how much memory it's allowed to use

cd /opt/minecraft/ && /usr/bin/java -Xms$xms -Xmx$xmx -jar server.jar --nogui

9. Set the script executable.

sudo chmod +x /opt/minecraft/start_mc.sh

10. If everything run successfully, create systemd service file /etc/systemd/system/minecraft.service to manage minecraft service.

[Unit]
Description=Minecraft Server
After=network.target

[Service]
WorkingDirectory=/opt/minecraft

User=minecraft
Group=minecraft

Restart=always

#ExecStart=/usr/bin/screen -DmS mc /usr/bin/java -Xms512M -Xmx1024M -jar server.jar --nogui
ExecStart=/usr/bin/screen -DmS mc /opt/minecraft/start_mc.sh
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "say SERVER SHUTTING DOWN IN 5 SECONDS. SAVING ALL MAPS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "stop"\015'

[Install]
WantedBy=multi-user.target

11. Set minecraft directory permission.

sudo chown -R minecraft:minecraft /opt/minecraft  

12. Edit /opt/minecraft/server.properties to customize your minecraft server. Refer to link on Minecraft's server.properties setting.

13. If you prefer to run an offline Minecraft server, change online-mode setting to false.

online-mode=false

14. You can add your online/offline minecraft player name/UUID as operator. Convert your online/offline username to UUID via link. Edit /opt/minecraft/ops.json with below format, replace the username and UUID with your own.

[
  {
    "uuid": "fb4cdad9-642b-358f-8f6f-717981c9f42b",
    "name": "user1",
    "level": 4

  }
]

15. To protect your Minecraft server, enable and enforce whitelist to prevent anonymous access.

white-list=true
enforce-whitelist=true

16. Follow step 14 to get the offline username and UUID and add/edit the offline username and UUID to /opt/minecraft/whitelist.json.

[
  {
    "uuid": "6e7d9aa0-0da2-390c-ab6a-377df9d77518",
    "name": "user3"
  },
  {
    "uuid": "8df6eb30-663b-3d04-97cb-b992f01beb0f",
    "name": "user2"
  }
]

17. Enable and start minecraft service with the new server settings.

sudo systemctl enable minecraft.service
sudo systemctl start minecraft.service

18. Configure iptables and Oracle Cloud VCN firewall to allow port TCP 25565.

-A INPUT -p tcp --dport 25565 -J ACCEPT

 

 

 

 

19. Create script (/opt/minecraft/mc_backup.sh) to backup minecraft's world and data.

#!/bin/bash

# What to backup.  Name of minecraft folder in /opt

bk_source="/opt"
bk_dir="minecraft"

# Specify which directory to backup to.
# Make sure you have enough space to the backups.
# Warning: minecraft worlds can get fairly large so
# choose your backup destination accordingly.
bk_dest="/home/ubuntu/backup"

# Backup retention for housekeeping
bk_retention="1"

# Today's date
bk_date=$(date "+%Y-%m-%dT%H_%M_%S")

# keep how many of backup copy
# example +1 will keep latest 2 copies of backup
# uncomment below to enable backup housekeeping
#find ${bk_dest} -name "*.tar.gz" -type f -mtime +${bk_retention}
#find ${bk_dest} -name "*.tar.gz" -type f -mtime +${bk_retention} -delete

# Stop minecraft service
systemctl stop minecraft && sleep 120

# Backup the files using tar.
cd ${bk_source} && tar -zcvf ${bk_dest}/mc_backup_$bk_date.tar.gz ${bk_dir}

# Start minecraft service or opt to restart the host
systemctl start minecraft
#/usr/sbin/reboot

20. Create cron job (/etc/cron.d/mc_backup) to auto backup at 1AM.

MAILTO=""
0 1 * * * root /opt/minecraft/mc_backup.sh > /dev/null 2>&1

 

Comments