TOCICI

Minecraft on a BuildYourVPS system

Prerequisites

In order to successfully run a Minecraft server:

  • Java does not run well with a single CPU thread, please sure that you've ordered an enviroment with access to at LEAST two threads.
  • Ensure that you have sufficient RAM to run your Minecraft server. The minimum recommended RAM is 1GB.

Installing just Minecraft

The following will ensure that you have a functional default minecraft installation.

Prerequisite: Default CentOS 5 VPS build

Run each command, in the order shown. Wait until each command completes before proceeding to the next:

Update your VPS:

yum -y update

Install SUN's Java:

yum -y install java-1.6.0-openjdk

Create a Minecraft specific user:

adduser -d /opt/minecraft minecraft

Install Minecraft

cd /opt/minecraft
wget http://minecraft.net/download/minecraft_server.jar
chown -R minecraft:minecraft /opt/minecraft/

Configure Minecraft to autostart:

cd /etc/init.d/

Create the file:

mineserv
#!/usr/bin/perl

# Mineserv by Christoper "BlackCow" Handwerker
# www.homebrewtechnology.org
# email: blackcow99@gmail.com or chandwer@student.fitchburgstate.edu

# INSTALLING THE SCRIPT
# Install "screen" if you don't already have it
# Edit the SETTINGS variables (down below) to fit your environment.
# Place this file in /etc/init.d/
# Make it executable: 			 sudo chmod a+x /etc/init.d/mineserv
# Add it to the init list: 	 sudo update-rc.d mineserv defaults


# UNINSTALLING THE SCRIPT
# Simply delete the file: 		 sudo rm /etc/init.d/mineserv
# Remove from the init list:   update-rc.d -f mineserv defaults

# USING THE SCRIPT
# Usage: /etc/init.d/mineserv {start|stop|status|backup|cleanup [days]}
# (Backup makes a .tar.gz archive of the world folder in the backup directory
# (Cleanup deletes backups older than [days] given in the backup directory)
#
# To send a command to the server console: /etc/init.d/mineserv command "your command"
# To bring up the server console run: screen -r mineserv (Make sure run as the correct user)

### BEGIN INIT INFO
# Provides:   mineserv
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $network
# Should-Stop:    $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:    Minecraft server
# Description:    Starts the minecraft server
### END INIT INFO

# SETTINGS

$SERVERJAR = "minecraft_server.jar";	# Name of the server jar file
$SERVICENAME = "mineserv";				# Name of the service
$SERVERPATH = "/opt/minecraft";			# Directory where the server lives
$BACKUPDIR = "backups";					# Name of backup directory
$USERNAME = "minecraft";				# User that the server should run as


chomp($CURRENTUSER = `whoami`);	# Determines what user is running the command to see if password is required

sub start
{
	if(&status == 1)
	{
		print "Service is already running (pid $pid)\n";
	}
	else
	{
		if($CURRENTUSER eq $USERNAME)
		{
			`cd $SERVERPATH && screen -dmS $SERVICENAME java -Xmx512M -Xms32M -jar $SERVERJAR nogui`;
		}
		else
		{
			print "Enter password for user $USERNAME\n";
			`cd $SERVERPATH && su - $USERNAME -c "screen -dmS $SERVICENAME java -Xmx512M -Xms32M -jar $SERVERJAR nogui"`;
		}
	}
}

sub stop
{
	if(&status == 0)
	{
		print "Service is already stopped.\n";
	}
	else
	{
		#Send stop command to server
		&command("stop");
	}
}

sub status
{
	#Returns 1 if service is running and 0 if it is not running.
	my $process = `ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVERJAR`;	

	if($process eq "")
	{
		return 0;
	}
	else
	{
		if ($process =~ m/(\d+)/)		#Strips out process PID number
		{		
			$pid = $1;	
		}
		return 1;
	}
}

sub command
{
	if($CURRENTUSER eq $USERNAME)
	{
		`screen -p 0 -S $SERVICENAME -X eval 'stuff \"$_[0]\"\r'`
	}
	else
	{
		my $CMD = "`screen -p 0 -S $SERVICENAME -X eval 'stuff \"$_[0]\"\r'`";
		print "Enter password for user $USERNAME\n";
		`su - $USERNAME -c "$CMD"`;
	}
}

sub backup
{
	my $filename = "world_`date +%y-%m-%d_%H-%M-%S`.tar.gz";

	&command("save-off");
	&command("save-all");
	sleep(2);
	&command("say Mineserv is making a world backup.");
	`cd $SERVERPATH && tar -czf $BACKUPDIR/$filename world`;
	&command("save-on");
}

sub cleanup
{
	print("Deleted backups older that $_[0] days.\n");
	`cd $SERVERPATH && find $BACKUPDIR -mmin +$_[0] -type f -exec rm -rf {} \\;`;
}

#Command tree
$command = $ARGV[0];

if($command eq "start")
{
	&start;
}
elsif($command eq "stop")
{
	&stop;
}
elsif($command eq "status")
{
	if(&status == 1)
	{
		print "Minecraft server is running (pid $pid).\n";
	}
	else
	{
		print "Minecraft server is not running.\n";
	}
}
elsif($command eq "command")
{
	&command($ARGV[1]);
}
elsif($command eq "backup")
{
	&backup;
}
elsif($command eq "cleanup")
{
	&cleanup($ARGV[1]);
}
else
{
	print "\nUsage: /etc/init.d/mineserv {start|stop|status|backup|cleanup [days]}\n";
	print "(Backup makes a .tar.gz archive of the world folder in \"$BACKUPDIR\")\n";
	print "(Cleanup deletes backups older than [days] given in \"$BACKUPDIR\")\n\n";
	print "To send command to console: /etc/init.d/mineserv command \"your command\"\n\n";
	print "To bring up the server console run: screen -r $SERVICENAME\n(Make sure run as user: $USERNAME)\n\n";
}
chmod a+x /etc/init.d/mineserv 
chkconfig --add mineserv
chkconfig --list mineserv

Start Minecraft

/etc/init.d/mineserv start

Congratulations, you now have a running Minecraft server.

Additional Performance Enhancements

OptiFine

We've seen many customers installing OptiFine, and obtaining up to a 20FPS improvement in performance as a result.