[Tutorial] CentOS Bukkit Installation

Discussion in 'Bukkit Help' started by Aetherspawn, Jan 24, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Aetherspawn

    Prerequisites:
    1. VPS Server with root:root access
    2. CentOS x86 or x86_64
    Step 1
    Install screen, if it's missing.
    Code:
    yum install screen
    Step 2
    Install FTP so we can upload files to the server
    Code:
    yum install vsftpd
    Code:
    chkconfig vsftpd on
    Code:
    service vsftpd start
    Step 3
    Setup the user that the server is going to use. Generally it's good practice to have services run under a different user than root, incase a security vulnerability were to arise. You should replace "mypassword" with a secure password.
    Code:
    useradd minecraft
    Code:
    passwd minecraft mypassword
    Step 4
    Install Sun Java. Locate your appropriate .rpm from the sun java website or use the following (32-bit as an example). Don't use openJDK to run your server on CentOS; it's unstable.
    Code:
    cd /tmp/
    Code:
    wget http://download.oracle.com/otn-pub/java/jdk/7u2-b13/jdk-7u2-linux-i586.rpm
    Code:
    rpm -Uvh jdk-7u2-linux-i586.rpm
    Step 5
    Create the directories that we're going to use for Bukkit.
    Code:
    mkdir /home/minecraft/bukkit
    Code:
    mkdir /home/minecraft/archive
    Step 6
    Create the file /etc/init.d/minecraft with a text editor (such as nano) and paste the following in;
    Code:
    #!/bin/bash
    # /etc/init.d/minecraft
    # version 0.3.2 2011-01-27 (YYYY-MM-DD)
     
    ### BEGIN INIT INFO
    # Provides:  minecraft
    # 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
    SERVICE='craftbukkit.jar'
    USERNAME="minecraft"
    MCPATH='/home/minecraft/bukkit'
    CPU_COUNT=1
    INVOCATION="java -Xmx1024M -Xms1024M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar craftbukkit.jar nogui"
    BACKUPPATH='/home/minecraft/archive'
     
    ME=`whoami`
    as_user() {
      if [ $ME == $USERNAME ] ; then
        bash -c "$1"
      else
        su - $USERNAME -c "$1"
      fi
    }
     
    mc_start() {
      if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
      then
        echo "Tried to start but $SERVICE was already running!"
      else
        echo "$SERVICE was not running... starting."
        cd $MCPATH
        as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
        sleep 7
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
          echo "$SERVICE is now running."
        else
          echo "Could not start $SERVICE."
        fi
      fi
    }
     
    mc_saveoff() {
            if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
            then
                    echo "$SERVICE is running... suspending saves"
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly...\"\015'"
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
                    sync
                    sleep 10
            else
                    echo "$SERVICE was not running. Not suspending saves."
            fi
    }
     
    mc_saveon() {
            if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
            then
                    echo "$SERVICE is running... re-enabling saves"
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
            else
                    echo "$SERVICE was not running. Not resuming saves."
            fi
    }
     
    mc_stop() {
            if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
            then
                    echo "$SERVICE is running... stopping."
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
                    sleep 10
                    as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
                    sleep 7
            else
                    echo "$SERVICE was not running."
            fi
            if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
            then
                    echo "$SERVICE could not be shut down... still running."
            else
                    echo "$SERVICE is shut down."
            fi
    }
     
     
    mc_update() {
      if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
      then
        echo "$SERVICE is running! Will not start update."
      else
        MC_SERVER_URL=http://minecraft.net/`wget -q -O - http://www.minecraft.net/download.jsp | grep minecraft_server.jar\ | cut -d \" -f 2`
        as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update $MC_SERVER_URL"
        if [ -f $MCPATH/minecraft_server.jar.update ]
        then
          if `diff $MCPATH/minecraft_server.jar $MCPATH/minecraft_server.jar.update >/dev/null`
            then
              echo "You are already running the latest version of $SERVICE."
            else
              as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/minecraft_server.jar"
              echo "Minecraft successfully updated."
          fi
        else
          echo "Minecraft update could not be downloaded."
        fi
      fi
    }
     
    mc_backup() {
      echo "Backing up minecraft world"
      if [ -d $BACKUPPATH/world_`date "+%Y.%m.%d"` ]
      then
        for i in 1 2 3 4 5 6
        do
          if [ -d $BACKUPPATH/world_`date "+%Y.%m.%d"`-$i ]
          then
            continue
          else
            as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%Y.%m.%d"`-$i"
            break
          fi
        done
      else
        as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%Y.%m.%d"`"
        echo "Backed up world"
      fi
      echo "Backing up the minecraft server executable"
      if [ -f "$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`.jar" ]
      then
        for i in 1 2 3 4 5 6
        do
          if [ -f "$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`-$i.jar" ]
          then
            continue
          else
            as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`-$i.jar\""
            break
          fi
        done
      else
        as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`.jar\""
      fi
      echo "Backup complete"
    }
     
     
    #Start-Stop here
    case "$1" in
      start)
        mc_start
        ;;
      stop)
        mc_stop
        ;;
      restart)
        mc_stop
        mc_start
        ;;
      update)
        mc_stop
        mc_backup
        mc_update
        mc_start
        ;;
      backup)
        mc_saveoff
        mc_backup
        mc_saveon
        ;;
      status)
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
          echo "$SERVICE is running."
        else
          echo "$SERVICE is not running."
        fi
        ;;
     
      *)
      echo "Usage: /etc/init.d/minecraft {start|stop|update|backup|status|restart}"
      exit 1
      ;;
    esac
     
    exit 0
    Code:
    chmod +x /etc/init.d/minecraft


    Step 7

    Upload the craftbukkit files to /home/minecraft/bukkit/ using an FTP program such as FileZilla. Connect to your server with the following details:
    Host - Your IP
    Username: minecraft
    Password: the password you set earlier

    Step 8

    Set the service we just created to start on boot.
    Code:
    chkconfig minecraft on


    Step 9

    Setup the crontab for backups. Type:
    Code:
    crontab -e


    Type in and save:
    Code:
    30 * * * * /etc/init.d/minecraft backup


    Step 10

    Start the service.
    Code:
    service minecraft start
     
  2. Offline

    Elias Nilsson

    You don't need to install FTP ! Use the ssh server , Just connect with port 22 on a regular Ftp Client , Login with the root and root password , :)
     
  3. Offline

    Weznezz

    you'll need to replace step 4
    Code:
    wget http://download.oracle.com/otn-pub/java/jdk/7u2-b13/jdk-7u2-linux-i586.rpm
    to:
    Code:
    wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" \
    "http://download.oracle.com/otn-pub/java/jdk/7u5-b06/jdk-7u5-linux-i586.rpm" 
    to overcome Oracles new agreement thingamie and allow you to download ;)
     
    MEC666 likes this.
  4. Offline

    corellon

    Question, if I wanted to access a screen session to do do CLI administration, how would I do about that?

    nevermind. I figured it out.
    If you want to use the CLI to admin the server you need to:
    1. log into the minecraft user, do not su (aka: su minecraft). You cannot access the screen session from someone elses terminal session it will give you an error.
    Code:
    Cannot open your terminal '/dev/pts/X' - please check.
    
    If you get this error, you are under the wrong terminal
    2. type
    Code:
    screen -list
    [code]
    This will present you with a list of screen sessions similar to this:
    [code]
     
    There is a screen on:
    xxxxx.minecraft(Detached)
    1 Socket in /var/run/screen/S-minecraft.
    
    the xxxxx is the process number. This is what we will use to attatch to the session
    3. next we attach to the session:
    Code:
    screen -r xxxxx
    
    Now you have a screen session with the CLI for your minecraft server. IMPORTANT: You can simply close the terminal window if you wish to detach the session. Closing the session without detaching will likely shut down the minecraft server or at least make CLI administration of it impossible.
    Sorry for the original post I made. but my obsession paid off. At least now people can google and find the specific answer :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  5. Offline

    MEC666


    i downloaded that. but how do you install it once you get it?

    actually i found it

    Code:
    rpm -ivh jdk-7u5-linux-i586.rpm?AuthParam=1359770421_db6eed994fa6ba57dc10a646622cf540
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  6. Offline

    watusimoto

  7. Offline

    OmgImAlexis

  8. Offline

    JohnathanGuti

    Could you help me with step 9? I have Ubuntu and when I use 'service minecraft start' the console says 'env: /etc/init.d/minecraft: No such file or directory'. I'm having trouble when using 'crontab -e'. I try typing but it doesn't write anything. Then I tried the print screen button and it allowed me to type, but the 'service minecraft start' button doesn't work still.
     
  9. Offline

    LHammonds

    CentOS is not the same as Ubuntu Server.

    Besides, you really need better management of the server than just telling it to startup when the computer starts.

    You should make a start script that checks to see if the server is not already running and if not, try to start it up. Then have that script run every minute of the day in crontab. I have one additional check that looks for a special temp file such as /tmp/do-not-start.txt in case I am preparing to shutdown all servers and need to reboot cleanly without any Minecraft servers running.

    Make a start.sh script to control the startup of Craftbukkit.
    Make a stop.sh script to control the shutdown of Craftbukkit.
    Make a reboot.sh script that calls stop.sh (and maybe sets a "do not start" flag to prevent "start.sh" from starting the server back up) and then reboots the server.
    Make a backup.sh script to prep the running server for copying, then copy to a backup folder, then archive the copy.

    LHammonds
     
  10. Offline

    JohnathanGuti

    Okay thanks, I will try rebuilding my server with Centos and try again. Thanks.
     
Thread Status:
Not open for further replies.

Share This Page