How to build a Minecraft server on Ubuntu

So, I decided to reboot my Minecraft server mainly because it hadn’t been updated in forever, and there are several new things available in Minecraft with the recent(ish) updates, and I decided that to start nice and super clean, I would just start with a fresh server rebuild.  I am going to be doing this on a virtualized server with 1.5GB of ram (java is a little memory hungry).

If you are looking for a way to set up a nice Minecraft server with some fancy schmancy web functionality, this is the article for you, more info after the break.

 

So, the planned stack for this Minecraft server will end up with us having MySQL, nginx, php-fpm, Minecraft and the Multicraft web control panel.  Multicraft uses MySQL as the back-end and serves itself through the web server.  We will also be serving a semi-real-time web map of the server through the web server as well.  And because Minecraft is a memory hungry beast, we will be scrapping the standard Apache web server and instead doing it with nginx.

Also, This blog post assumes that you will be installing everything all on the same server. If you need something more advanced with multiple servers, then much of this post will help you, but there will be some bits that need to be adjusted. It will still work quite well as a “rough guide” for multi-server installations.

The end goal of this post will be that you can get it all done by almost copy+paste, but there will be enough information here to where you should know what you are doing any how it all ties together.  To start, we need our server, I am going to be using a server with 1.5GB of ram, running Ubuntu 11.10, just a baseline server install, we will be adding on all the bits and pieces that we need with the following command:

apt-get install openjdk-7-jre nginx php5-fpm php5-mysql php5-gd mysql-server zip

Next we will setup Multicraft. Because the installation instructions for it may change in the future, rather than telling you how to do it here, I will give you a link to the official documentation on it over at [2345].

Run through the installer for Multicraft. If you want to be able to copy+paste the nginx config that I have later, make sure that you specify ‘/srv/minecraft/multicraft’ for the location to install the PHP frontend to.

Next we are going to set up php-fpm and nginx. First we are going to make a small tweak to php-fpm to have it listen on a UNIX/filesystem socket rather than a TCP socket. Edit the file at /etc/php5/fpm/pool.d/www.conf and find the line like

listen = 127.0.0.1:9000

And change it to…

;listen = 127.0.0.1:9000
 listen = /var/run/php5-fpm.sock

Then restart php-fpm

service php5-fpm restart

Then we are going to set up a file in nginx so that it will know about php-fpm. Create a file at /etc/nginx/conf.d/php5-fpm.conf with the following contents:

upstream php5-fpm-sock {
         server unix:/var/run/php5-fpm.sock;
 }

now we are going to put an nginx configuration in place to serve multicraft. Create a file at /etc/nginx/sites-available/minecraft with the following contents:

server {
     listen       80;
     server_name  minecraft.example.com;
     root         /srv/minecraft/multicraft/;
 
     index index.php;
 
     access_log /var/log/nginx/minecraft.example.com-access_log;
     error_log /var/log/nginx/minecraft.example.com-error_log;
 
     location / {
         try_files $  uri $  uri/ =404;
     }
 
     location ~ ^/protected {
         deny all;
     }
 
     location ~ ^/(.*.php)$   {
         fastcgi_pass php5-fpm-sock;
         fastcgi_param SCRIPT_FILENAME $  request_filename;
         include /etc/nginx/fastcgi_params;
     }
 
     location ~ .php$   {
         try_files $  uri =404;
         fastcgi_index index.php;
         fastcgi_pass php5-fpm-sock;
         fastcgi_param SCRIPT_FILENAME $  document_root$  fastcgi_script_name;
         include /etc/nginx/fastcgi_params;
     }
 }

Make sure to change every occasion of minecraft.example.com with the URL that your minecraft server will be available at. Now we will put it in place with

ln -s ../sites-available/minecraft /etc/nginx/sites-enabled/minecraft
 service nginx restart

Now we will create a database and a user account that is needed in just a moment by Multicraft. Log into your MySQL server with the following command

mysql -u root -p

The password is the one that you gave when you installed mysql-server earlier (you did set a password didn’t you?). Once logged in, you will want to run the following MySQL queries

CREATE DATABASE multicraft_panel;
 CREATE DATABASE multicraft_daemon;
 GRANT ALL PRIVILEGES ON multicraft_panel.* TO 'USERNAME'@'localhost' identified by 'PASSWORD';
 GRANT ALL PRIVILEGES ON multicraft_daemon.* TO 'USERNAME'@'localhost';
 FLUSH PRIVILEGES;

Make sure that instead of using USERNAME in that third and fourth query, that you instead use the Database username that you specified during the Multicraft installation earlier. Similar for where you see PASSWORD, use the password that you gave during the Multicraft installation.
At this point you need to go to that URL that you configured in nginx and complete the Multicraft installation. Please do not move on further with this post until you have Multicraft up and running.

Once you have finished the Multicraft installation and you know that everything is working there, we are going to set up an upstart configuration for Multicraft so that it will start at server bootup, and you can use those nice friendly service commands to interact with it.

Create a file at /etc/init/multicraft.conf with the following contents

UPDATE: As Tux2 pointed out in the comments, there was a “more correct” way to write the upstart configuration. This block now reflects that change. Thanks Tux2!

description "Multicraft Minecraft Server Control Panel"
 
 start on (net-device-up
 and local-filesystems
 and runlevel [2345])
 stop on runlevel [016]
 
 script
 /opt/multicraft/bin/multicraft start
 end script
 
 pre-stop script
 /opt/multicraft/bin/multicraft stop
 end script

If you installed multicraft to somewhere other than /opt/multicraft then adjust the two lines accordingly. Now run the following

ln -s /lib/init/upstart-job /etc/init.d/multicraft

Now your multicraft will start at boot up, stop at shutdown, and you will be able to use `service multicraft start` and `service multicraft stop`

Leave a Reply