Create a Minecraft Server on DigitalOcean

This post was edited on 31 May 2015 to switch to using screen for spawning an instance of the Minecraft Server. Previously it created a startup script but that meant it wasn't possible to easily switch back into the running console.

Today we'll go through some steps to create your own MineCraft Server. We'll install Minecraft Server 1.8 onto a very basic 512Mb DigitalOcean Droplet.

Setup Pre-requisites

Start by creating a new 512Mb Droplet with Ubuntu 14.04. We won't need to use any of the Application droplets so make sure you use the Linux-only image.

Wait a shockingly fast 55 seconds and then SSH into your brand new Ubuntu Droplet.

First lets update the package list on our fresh Ubuntu installation:

apt-get update  

And then upgrade to the latest version of all installed packages.

apt-get upgrade  

Our next step on the box is to install Java. We won't be running a Java application with a full GUI so we can get away with installing the default-jre-headless package. JRE stands for Java Runtime Environment used to run the Java application, while headless refers to the lack of any GUI libraries.

apt-get install default-jre-headless  

Our last pre-requisite is to install screen which will be used when starting the Minecraft server.

apt-get install screen  

Note: You might need to run each command with sudo at the start of the command due to permissions, but on a new droplet you should have the right privileges.

Create a minecraft system user

Next up we create a Linux user and group to own the Java process when running the Minecraft Server.

Start off by creating a directory for Minecraft itself.

mkdir /srv/minecraft  

Then create the Linux group.

addgroup --system minecraft  

Now to actually create the user run the following on the command line (note that it's all one line):

adduser --system --no-create-home --home /srv/minecraft --ingroup minecraft minecraft  

The user's home is set to /srv/minecraft as srv is the prescribed location for site-specific data which are served by the system. Another popular location may be /opt/minecraft.

The --ingroup minecraft flag will automatically add the new user to the minecraft group created the line above.

Install Minecraft Server

Change to the minecraft directory.

cd /srv/minecraft  

And download the MineCraft Server package (note that the command is all one line).

wget https://s3.amazonaws.com/Minecraft.Download/versions/1.8/minecraft_server.1.8.jar -O minecraft_server.jar  

Configure the Minecraft Server

Start our MineCraft server.

java -Xms256M -Xmx450M -jar minecraft_server.jar nogui  

We only have 512Mb RAM so we have to be a bit conservative. The first parameter starts the process with 256Mb of RAM and sets an upper limit of 450Mb through the second parameter.

MineCraft will try to start and since it's the first launch it will generate a server.properties file with some sane defaults. It will, however, fail as we haven't yet agreed to the EULA (End-User License Agreement). We need to edit the newly generated eula.txt file and set the value to TRUE (You may need to type /stop to shutdown the Minecraft Server console).

So let us edit that file now:

vi eula.txt  

Once you've changed false to true, save the file and exit the editor.

Now that you've accepted the EULA, start MineCraft Server up again:

java -Xms256M -Xmx450M -jar minecraft_server.jar nogui  

This time it will create the banned-players.json, banned-ips.json, ops.json, and whitelist.json files.

These files allow you to add users to the different roles. The banned files allow you to ban users or IPs from accessing your instance. The ops file allows you to set "administrators" while whitelist is used if you only want to allow a specified list of users to access the instance at all.

We'll add our own Minecraft user to the ops and whitelists within Minecraft Server. I particularly like to keep a strict whitelist to ensure random users don't discover and use the Minecraft Server (especially because it's on such a modest setup).

To add yourself to the ops and whitelist files run the following while Minecraft is running (but obviously replacing YourMojangUsernameHere with your own username):

/op YourMojangUsernameHere
/whitelist add YourUserNameHere
/whitelist on

In future, you may add friends to the whitelist by using the /whitelist add YourFriendsUserName command from within Minecraft client itself so you won't always need server SSH access. Ahh, the power of being an Admin!

Quit from the Minecraft console by typing stop to return to the command prompt.

Next up we edit server.properties that was generated and change the value for max-players from 20 to a more reasonable 5 for our modest server specs.

vi server.properties  

Feel free to explore the properties set in this file to get an understanding for what all can be tweaked. I like to change the motd (Message of the Day) of the server from the default "A Minecraft Server" to something more personal but that's purely optional.

Once you've changed at least the maximum number of players to a sensible 5, save the file and exit the editor.

At this point a lot of files have been created in /srv/minecraft under your logged in user, but we'll want them to be owned by the minecraft user and group.

To do this we have to run:

chown -R minecraft:minecraft /srv/minecraft  

Some theory on the screen utility

Screen is a handy command-line utility that allows you to create detachable instances of your shell called screens.

Think of a screen on the command-line in a similar fashion to tabs in your browser. You can load a website and then switch to another tab to continue doing something else. Screens in Linux allow you to do the same thing.

To start a new screen you would type:

screen -S screen_name [command]  

Replace screen_name with a friendly name to identify it if you run multiple screen instances (not something we will do though).

Likewise, replace [command] with any command you wish to run when creating a new screen. The command is optional but it allows us to immediately spawn a Minecraft Server instance without having to manually type the java command.

To see a list of all running screen instances, run:

screen -list  

To switch back to a running screen (called reattaching), run:

screen -r screen_name  

This will "tab" you back into the relevant shell instance.

If you're inside a screen instance and want to return to the main commandline, press Ctrl-A followed by d. You won't see anything on screen when you first press Ctrl-A.

If you want to kill the screen instance, press Ctrl-d from within it. But since we'll be running a Minecraft instance, we'll need merely run stop in the Minecraft console instead.

Create a Minecraft startup script

Now that the initial setup is complete, we won't be starting Minecraft Server using the manual java line anymore. Instead we'll create a Linux shell script to start our Minecraft Server using screen.

This way you can always SSH into your Minecraft Server and switch to the Minecraft screen to run some handy Admin commands within the console.

Start off by ensuring we're in the right directory:

cd /srv/minecraft  

Create the shell script by running:

vi start.sh  

And save the contents as:

#!/bin/bash

# Some reminder text for the user...
echo "Welcome to the simple Minecraft screen script."  
echo "----------------------------------------------"  
echo " If you run this and no screen instance exists,"  
echo " one will be created and you will be transferred"  
echo " into that screen instance."  
echo " "  
echo " To leave and return to the terminal prompt (i.e. detach)"  
echo " Then press:  Ctrl-A, followed by D"  
echo "----------------------------------------------"  
echo " To shutdown your instance of Minecraft Server,"  
echo " switch to the Minecraft screen instance"  
echo " by typing: screen -r mcscreen"  
echo " from anywhere and the type stop"  
echo "----------------------------------------------"

# Run a check for an already running minecraft screen session
RUNNING=$(pgrep -f "mcscreen" | wc -l)

# DEBUG. UNCOMMENT TO SEE VALUE
# echo $RUNNING

# If there is no detached screen running, create one and switch
# Else default to switching to the Minecraft screen instance.
if [ $RUNNING -eq 0 ]  
then  
    echo "Minecraft screen not yet running - starting one now..."
    echo " "
    read -p "Press any key to initiate..." -n 1

    screen -S mcscreen java -Xms256M -Xmx450M -jar minecraft_server.jar nogui
else  
    echo "An instance of Minecraft screen is already running."
    echo " "
    echo "To detach from inside screen:  Press Ctrl-A followed by D"
    read -p "Press any key to switch now. " -n 1

    screen -r mcscreen
fi  

(Warning: The line starting with screen -S... and ending with nogui should all be one line)

Save the file and return to the command prompt. If you installed the server to a different location other than /srv/minecraft then be sure to change the above file to match your install location.

Set the newly created script to be executable by running:

chmod a+x start.sh  

To start the Minecraft instance for the first time, run:

./start.sh

In future when you SSH into your server, you can either:
1. cd into /srv/minecraft/ and re-run this script using ./start.sh command (it will intelligently check for a running screen instance and switch to it instead of creating a new one)
2. run screen -r mcscreen to reattach to the already running Minecraft screen

Connecting to our new Minecraft Server

If you ran our start.sh script already then you should have a Minecraft Server up and ready to connect to. If not, now is the time to run that script.

At this point logout of your SSH session as we are done with the server setup phase (remember to hit Ctrl-A followed by d to disconnect from the Minecraft screen to get back to the terminal to logout).

You should now be able to load up Minecraft Client and attempt to connect to a Multiplayer game. Add your newly created server to the list by using 1.2.3.4:25565 where 1.2.3.4 is your DigitalOcean droplet IP.

Now go build that house before nightfall and escape those Creepers!

Bonus Paragraph: Performance

If you find the performance of your Minecraft Server is a bit poor you might want to consider upgrading your Droplet to a 1Gb instance. To do this you don't have to start from scratch, instead power down your droplet and increase the resources accordingly.

Don't forget, once you've booted up the new powerhouse to edit the /srv/minecraft/start.sh script. If you upgrade to 1Gb total RAM you should change the 450M to roughly 920Mb. End this off by re-running the start.sh script to initiate a new screen instance with the new upper limit in effect. Jeepers Creepers!