Skip to content

Instantly share code, notes, and snippets.

@tjtanjin
Last active June 18, 2022 16:41
Show Gist options
  • Save tjtanjin/d3f9c5e33e830a4b71f9c84e7c807f0a to your computer and use it in GitHub Desktop.
Save tjtanjin/d3f9c5e33e830a4b71f9c84e7c807f0a to your computer and use it in GitHub Desktop.
Setup Minecraft Server on Ubuntu 18.04/20.04

How to create a minecraft server on Ubuntu (18.04/20.04)

Introduction

This is a generic minecraft server setup guide that has been tested on Ubuntu 18.04 and 20.04. By the end of this guide, you will have your very own multiplayer minecraft server that you and your friends (or anyone for that matter) can join and have fun on!

Server Setup

To begin, you will need to provision a VPS from cloud providers such as digitalocean or upcloud. Other popular services like AWS and google cloud would work as well with their EC2 and compute instances but the nature of those services are such that they are slightly more complicated to work with so they will not be included in this guide.

Once you have your VPS provisioned, SSH into your server with the following command (replacing 11.11.11.11 with your server's IP address):

ssh [email protected]

Within your server, run the update command below:

apt-get update

As minecraft runs on java, there is also a need to install default-jdk:

apt-get install default-jdk

Next, to ensure that our minecraft server will be running 24/7, we will be using screen to keep the process alive even when we are no longer maintaining an SSH connection to the server:

apt-get install screen

Finally, as minecraft runs on the default port of 25565 (and that is what we will be using), we will need to update the firewall to allow connections on that port:

ufw allow 25565/tcp

And that sums up pretty much all the basics needed for our server setup!

User Setup

With the above installations completed, we are not too far off from getting a simple minecraft server up and running. First, let us create a new minecraft user that we will be handling this minecraft server with:

adduser minecraft

Give the user superuser permissions:

usermod -aG sudo minecraft

For the remaining part of the guide, exit from your root SSH session and login as minecraft instead (again replacing 11.11.11.11 with the IP address of your VPS):

ssh [email protected]

Launching Minecraft Server

Let us now create a directory for this project and keep the relevant server files within it:

mkdir server
cd server

We will be downloading the minecraft server files into this directory. For those who are interested, the official download link can be found at this website but the one we will be working with is paper spigot, a variation of the official minecraft server which is optimised for performance.

You may choose to download the files locally and then upload them to your VPS using another client like WinSCP or Filezilla, but the simplest way would be to use wget with the download link as below:

wget -O minecraft_server.jar https://papermc.io/api/v1/paper/1.16.1/79/download

Once the download is complete, you will see a minecraft_server.jar file residing within the directory. This is the executable that you will run to get your minecraft server up and running. Give the following command a try:

java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui

The above command executes your minecraft_server.jar file with the first 2 flags specifying the amount of memory to run the server with (1gb in this case) while the nogui is included to save server resources (and hence boost performance) since there is no need for any graphical interface here. Feel free to adjust the memory but note that it is capped by the ram you provisioned your VPS with and that your VPS itself would need some memory for its system processes too!

On the first try, you will get an error telling you accept the EULA. You may do so by editing the following file and changing the last line from false to true:

vim eula.txt

You will also notice several new files, including server.properties which we will edit to put online-mode to false:

vim server.properties

Try running minecraft_server.jar again and your command line would now be showing the console for the minecraft server instead. You may now start your minecraft launcher on your computer and join your very own multiplayer server with the IP address of your VPS.

This is the bare minimum required to get a minecraft server up and running, but try exiting from your SSH session. Did your minecraft server abruptly shutdown as well? Read on to find out why!

Making Server Available 24/7

As you may have realised from trying the above, the continued running of your minecraft server depended upon you maintaining an SSH connection to your VPS server. But this is not practical, which is why we have installed screen earlier to help us circumvent this problem! Start a new screen session with the following command:

screen

Hit enter and at first glance, everything would feel the same. However, you will notice that you are now attached to a screen session if you run the following command:

screen -ls

A screen session can essentially be thought of as its own window that continues to run in the background. Within your current screen session, run the exact same command again to start up your minecraft server:

java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui

Then exit the screen session (think of it as minimising the window) by pressing CTRL, A and D on your keyboard together at the same time. Make sure all 3 keys are pressed together lest you make the nasty mistake of pressing only CTRL and D which would terminate the entire screen session instead.

Try exiting from your SSH session and connecting to your minecraft server in the launcher. You will notice that this time, your minecraft server continues to run just fine!

To resume your screen session, you may SSH back into the server anytime and run the following command:

screen -r

On a side note, screen (or tmux) has a wide variety of uses beyond just the one shown in this guide. They may be used to run time consuming processes such as training machine learning models or even something as simple as telegram bots.

Minecraft as a Service

Minecraft, being the old game that it is does not handle resources well and often runs into server performance issues without good optimisation and regular server restarts.

We will now create a service for our minecraft server so that even in the event of a major crash, the server is able to (or will at least try to) bring itself back up online.

Create your own service file with the following command:

sudo vim /etc/systemd/system/[email protected]

Within it, copy and paste the following content:

[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/home/minecraft/%i

User=minecraft
Group=minecraft

Restart=always

ExecStart=/usr/bin/screen -DmSL mc-%i /usr/bin/java -Xms1024M -Xmx1024M -jar minecraft_server.jar nogui

ExecStop=/usr/bin/screen -p 0 -S mc-%i -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-%i -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\015'

[Install]
WantedBy=multi-user.target

Enable and start your service with the following command (terminate your existing screen session if you have not!):

sudo systemctl enable [email protected]
sudo systemctl start [email protected]

You will now notice again that there is an active screen session running if you run:

screen -ls

Your minecraft server will now restart itself in the unfortunate event that it crashes/terminates, as well as when you do a full reboot of your VPS.

Shellscripting/Crontab Daily Restart

As mentioned above, minecraft servers require a daily restart to maintain its performance. We will now create a shellscript within the base directory of our setup. If you are not already there, go to the base of our setup with:

cd ~/server

Next, create a simple shellscript:

vim dailyrestart.sh

Within the shellscript, copy and paste the following:

#!/bin/sh
screen -S "mc-server" -X stuff 'say Daily server restart in 60 seconds\r' 
sleep 30
screen -S "mc-server" -X stuff 'say Daily server restart in 30 seconds\r'
sleep 20
screen -S "mc-server" -X stuff 'say Daily server restart in 10 seconds\r'
sleep 5
screen -S "mc-server" -X stuff 'say Server restarting in 5 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 4 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 3 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 2 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 1 second\r'
sleep 1
screen -S "mc-server" -X stuff 'restart\r'

The above script runs commands directly into the console of your minecraft server, sending a 1 minute countdown reminder to whoever is in your server before actually restarting the server.

To have this script run automatically on a daily basis, we will use crontab:

crontab -e

At the top of your crontab file, add the following line and save:

0 1 * * * /bin/sh /home/minecraft/server/dailyrestart.sh

The script will now run at 9am daily (GMT +8) for our server restart!

Minecraft Server Domain Name (Optional)

With the majority of the setup done, the one last thing you need for your minecraft server to look as professional as it is is a domain name for people to remember and go by. Freenom provides free domain names so for those trying out but do not wish to spend the money to do so, do give it a try! Note that this section will only cover DNS configuration and will not go through how one can obtain a domain name.

Assuming you have obtained your own domain name, you will now want to configure your DNS (I am using digitalocean's networking but the provider does not really matter).

Within your DNS panel, you will want to create 2 new records - a service record as well as an A record.

In your service record, you will need to fill up the following fields (replace play.mcdemo.tk with the name you want your players to join with):

Hostname: _minecraft._tcp.play.mcdemo.tk
Will direct to: join
TTL: 43200
Port: 25565
Priority: 0
Weight: 5

In your A record, you will need to fill up the following fields (again switch out the domain name and direct the IP address to that of your VPS):

Hostname: join.mcdemo.tk
Will direct to: 11.11.11.11
TTL: 3600

Save both records and give them a while to propagate.

Conclusion

I hope this guide has been comprehensive enough in explaining how to properly setup a minecraft server. A lot of the details covered here are generic and there are many specifics that one can explore on their own so feel free to tinker around with your own setup! Have fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment