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 their site.
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 started mysql) stop on runlevel [016] pre-start script /opt/multicraft/bin/multicraft set_permissions end script exec /opt/multicraft/bin/multicraft --nodaemon --verbose start
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`
Samuel says:
Good work crypt! Much appreciated
Cheers,
Notau
unknown3311 says:
sorry but i get scared when i do stuff like this because i never want to screw up my computer ever again so after you type in apt-get install openjdk-7-jre nginx php5-fpm php5-mysql php5-gd mysql-server zip it says New password fer the MySQL "root" user:
does that do anything with my computer if it does plz tell me
cryptk says:
That is the mysql installation asking you to set a password for the mysql administrative user. That users name is root, but it is not the same root account as on your operating system. So it won't change your root account in linux at all, it is just for MySQL.
So set a password and remember what it is as you will need it to log into the mysql administrative user account in the future.
dis-broke says:
well this looks awesome but for what ever reason nginx isnt serving up anything when i go to run the multicraft installer http://my-server/multicraft/installer.php. It servers up a 404 now that i removed the default site from sites-avalible and sites-enabled9used to just serve the Welcome to nginx! message). I dont know enough about nginx configs to tell why it isnt serving the php files needed. would be gald to post any logs or configs!
cryptk says:
looks like something in your nginx configuration isn't right. Unfortunately without being able to look at the server, I couldn't tell you where you went wrong.
John says:
Hello,
when i restart the nginx service, during the installation following the script. It give me an error "no port in upstream php5-fpm-sock". what have i missed?
cryptk says:
It looks like you missed the file at /etc/nginx/conf.d/php5-fpm.conf . It is in the code block just above the nginx configuration, it should have the following contents:
upstream php5-fpm-sock {
server unix:/var/run/php5-fpm.sock;
}
Tux2 says:
The multicraft.conf file, although it works, is grammatically wrong, the right way to do it would be:
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
cryptk says:
Indeed you are correct. I actually have the conf file on my personal minecraft server using script and pre-stop script now, I was still in the process of learning all of the in's and out's of upstart when I wrote this. And I will update the post accordingly!
cradixus says:
Thanks for the upstart config file, but it took a while for me to get it working properly. Turns out it's because I needed to wait for mysql to be started as well. Hence:
start on (net-device-up
and local-filesystems
and started mysql <——
and runlevel [2345])
stop on runlevel [016]
However, although this now gets Multicraft working automatically after startup, I've found I can't use Upstart's "start/stop" commands. "sudo start multicraft" seems to work, but "sudo stop multicraft" as well as "sudo service multicraft stop" outputs:
stop: Unknown instance:
So, to stop, I have to run the "pre-stop" line manually. I'm assuming this has something to do with the various states & goals upstart supports. Perhaps once Multicraft v1.8.2 is started, it's no longer in a state that upstart recognizes? Not sure.
Any advice anyone has on how to troubleshoot this would be appreciated. Thanks!
Paul says:
I don’t think the pre-stop stanza is necessary as Multicraft should shut down gracefully upon receiving SIGTERM. I believe what’s happening in your case is that Multicraft is shut down in the pre-stop stanza so when Upstart actually tries to send SIGTERM to Multicraft the process no longer exists, hence the “unknown instance” message.
Paul says:
I finally figured out a good Upstart config file for Multicraft. The key is to start Multicraft with the –nodaemon option:
description “Multicraft Minecraft Server Control Panel”
start on (net-device-up
and local-filesystems)
stop on runlevel [016]
exec /minecraft/mc/multicraft/bin/multicraft –nodaemon –verbose start
The file goes in /etc/init/multicraft.conf and allows Multicraft to be started and stopped as a service, i.e.
service multicraft start
service multicraft stop
You’ll see two multicraft processes in the process list when starting with the –nodaemon option but one is a child of the other. Upstart uses the parent PID to control Multicraft so when Multicraft is stopped both processes are terminated. Listing the running services, i.e.
initctl list
or
initctl status multicraft
shows only one instance of Multicraft is running. If Upstart is configured as shown in the blog post then initctl list will show Multicraft as stopped even though it’s not (this is because Multicraft forks multiple times if –nodaemon is not used causing Upstart to follow the wrong PID).
In the config file shown in the blog post, “and runlevel[2345]” is not necessary because it’s redundant, see http://upstart.ubuntu.com/cookbook/#normal-start
cryptk says:
Awesome work. I'm pretty late getting to this, but I just tested out your upstart config, made a couple of minor adjustments (make sure mysql is started, run multicrafts set_permissions) and updated the blog post.