Posted on 5 mins read

I always wanted to learn some backend development seriously and finally I have the opportunity to do so. I have played around with server setup in the past but most of the information has been forgotten.

This time round a development server will be setup in virtualbox using ubuntu server 14.04 LTS (support expires 2019). For 16.04 LTS, I think the apt-get command is replace with apt and there are many new changes… To save some time and focus on learning backend development sticking with 14.04 LTS is the better choice.

Technologies used are:

  • Apache2: Web layer, sits infront of tomcat
  • MySQL: Relational database
  • Tomcat: Java app server
  • Adminer: Web tool to manage MySQL

Also,

  • VirtualBox

VirtualBox

I installed an image of ubuntu server 14.04 LTS. Then I had to configure the network setting so that I would be able to access the web server hosted on the VM.

The steps were:

  1. Go to preferences for VirtualBox.
  2. Networks > Host-only networks.
  3. Ensure there is an entry vboxnet0 (with whatever ip range you want).
  4. Add a new adapter under network settings for VM.
  5. Set it to host-only.
  6. Start up VM and configure /etc/network/interfaces. (VM and Vbox settings have no relation hence you have to configure the new adapter here too)

How to configure /etc/network/interfaces:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet dhcp

Originally, there was only eth0. You have to add eth1 too. (Special thanks: Vitor)

After changing the settings, you can reboot the system. Although… It might be possible to do sudo ifdown eth1 and sudo ifup eth1.

Apache2

This was easy to setup. It gets complicated if you want to configure it for a production environment though. (Further reading to be done)

sudo apt-get install apache2

MySQL

You could do sudo apt-get install mysql-server and it will work fine. However, I wanted a newer version mysql 5.7 so I had to download a newer APT package from the MySQL APT repository and then run the commands below.

sudo dpkg -i <mysql-apt-file>.deb
sudo apt-get update
sudo apt-get install mysql-server

Adminer

phpMyAdmin is a popular tool people use to view MySQL database instances and it is known to have many security flaws. Adminer is an alternative. I had to try a few times to set this up. For some reason I kept getting 404 errors. Eventually, it worked after I tried retarting from a clean slate 2-3 times. There might have been some miscofigured files lying around (not clean slate). Unfortunately, apt-get doesn’t give you the latest version.

sudo apt-get install adminer
sudo ln -s /etc/adminer/apache.conf /etc/apache2/conf-available/adminer.conf
sudo a2enconf adminer.conf
sudo service apache2 reload

Link for manual installation

Tomcat

Tomcat requires java. Hence, install java before installing tomcat. Next, it is good practice to run the server as another user (security) but this can be annoying to get right. If you are not too familiar with the linux system and do not have a large repertoire of commandline commands as well as not a deep/intermediate understanding of the commands it will be a challenge.

sudo apt-get update
sudo apt-get install default-jdk

sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

wget <link to tomcat binary>
sudo mkdir /opt/tomcat
sudo tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
cd /opt/tomcat

# We put the tomcat user as the owner of the folder of tomcat:
sudo chown -R tomcat:tomcat /opt/tomcat
# Users can not modify the configuration of tomcat:
sudo chmod -R g+r /opt/tomcat/conf
# Users can modify the other folders:
sudo chmod -R g+w /opt/tomcat/conf
sudo chmod -R g+w /opt/tomcat/logs
sudo chmod -R g+w /opt/tomcat/temp
sudo chmod -R g+w /opt/tomcat/webapps
sudo chmod -R g+w /opt/tomcat/work
# Activate the sticky-bit for new files keep permissions defined:
sudo chmod -R g+s /opt/tomcat/conf
sudo chmod -R g+s /opt/tomcat/logs
sudo chmod -R g+s /opt/tomcat/temp
sudo chmod -R g+s /opt/tomcat/webapps
sudo chmod -R g+s /opt/tomcat/work

You might get a permission denied message when trying to cd into logs or one of the folders…

sudo usermod -a -G tomcat <your username>
exit

exit till you are logged out then relog for permission changes to take place. You need to be in the tomcat group to have access.

More on permissions

/opt is for third-party applications that don’t rely on any dependencies outside the scope of said package. /usr/local is for packages installed on this machine outside the scope of the distribution package manager. link

After all that you need to make a script that will run on startup. The script will be placed in /etc/init.d/<name>. update-rc.d will be of help. (So may commands to know)

tomcat8 script:

#!/bin/bash

### References:
### http://askubuntu.com/questions/223944/how-to-automatically-restart-tomcat7-on-system-reboots/224402
### http://superuser.com/questions/632618/best-practice-for-access-permission-to-users-for-apache-tomcat
###

### BEGIN INIT INFO
# Provides:        tomcat8
# Required-Start:  $network
# Required-Stop:   $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO

JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
PATH=/sbin:/bin:/usr/sbin:/usr/bin

start() {
 sh /opt/tomcat/bin/startup.sh
}

stop() {
 sh /opt/tomcat/bin/shutdown.sh
}

case $1 in
  start|stop) $1;;
  restart) stop; start;;
  *) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac

Give the script permissions:

sudo chmod 755 /etc/init.d/tomcat8
sudo update-rc.d tomcat8 defaults
sudo reboot

Use like service tomcat8 <stop|start|restart>.

If you wish may use a fancier script.

Testing setup

Default port for tomcat is 8080. Test curl 127.0.0.1:8080. You should see the default tomcat webpage.

ipconfig | grep addr

Now get the ip for you virtual machine and see if you can access apache, adminer and tomcat from your host machine’s web browser.

<ip address>
<ip address>/adminer
<ip address>:8080

Phew, that is done.