Installing Java and Tomcat » History » Version 2

Aleksander Bešir, 07.12.2010 13:03

1 1 Aleksander Bešir
h1. Installing Java and Tomcat
2 1 Aleksander Bešir
3 1 Aleksander Bešir
Some of the following
4 1 Aleksander Bešir
5 1 Aleksander Bešir
h2. JDK installation
6 1 Aleksander Bešir
7 1 Aleksander Bešir
Before you install Tomcat you’ll want to make sure that you’ve installed Java. I would assume if you are trying to install Tomcat you’ve already installed java, but if you aren’t sure you can check with the dpkg command like so:
8 1 Aleksander Bešir
9 1 Aleksander Bešir
<pre>
10 1 Aleksander Bešir
    dpkg –get-selections | grep openjdk
11 1 Aleksander Bešir
</pre>
12 1 Aleksander Bešir
13 1 Aleksander Bešir
This should give you this output if you already installed java:
14 1 Aleksander Bešir
15 1 Aleksander Bešir
<pre>
16 1 Aleksander Bešir
    openjdk-6-jdk            install
17 1 Aleksander Bešir
    openjdk-6-jre            install
18 1 Aleksander Bešir
    ...
19 1 Aleksander Bešir
</pre>
20 1 Aleksander Bešir
21 1 Aleksander Bešir
If that command has no results, you’ll want to install the latest version with this command:
22 1 Aleksander Bešir
23 1 Aleksander Bešir
<pre>
24 1 Aleksander Bešir
    sudo apt-get install openjdk-6-jdk
25 1 Aleksander Bešir
</pre>
26 1 Aleksander Bešir
27 1 Aleksander Bešir
h2. Apache Tomcat installation
28 1 Aleksander Bešir
29 1 Aleksander Bešir
Now we’ll download and extract Tomcat from the apache site. You should check to make sure there’s not another version and adjust accordingly.
30 1 Aleksander Bešir
<pre>
31 1 Aleksander Bešir
32 1 Aleksander Bešir
    wget http://apache.hoxt.com/tomcat/tomcat-6/v6.0.29/bin/apache-tomcat-6.0.14.tar.gz
33 1 Aleksander Bešir
34 1 Aleksander Bešir
    tar xvzf apache-tomcat-6.0.29.tar.gz
35 1 Aleksander Bešir
</pre>
36 1 Aleksander Bešir
37 1 Aleksander Bešir
The best thing to do is move the tomcat folder to a permanent location. I chose /usr/local/tomcat, but you could move it somewhere else if you wanted to.
38 1 Aleksander Bešir
39 1 Aleksander Bešir
<pre>
40 1 Aleksander Bešir
    sudo mv apache-tomcat-6.0.29 /usr/local/tomcat
41 1 Aleksander Bešir
</pre>
42 1 Aleksander Bešir
43 1 Aleksander Bešir
Tomcat requires setting the JAVA_HOME variable. The best way to do this is to set it in your .bashrc file. You could also edit your startup.sh file if you so chose.
44 1 Aleksander Bešir
45 1 Aleksander Bešir
The better method is editing your .bashrc file and adding the bolded line there. You’ll have to logout of the shell for the change to take effect.
46 1 Aleksander Bešir
47 1 Aleksander Bešir
<pre>
48 1 Aleksander Bešir
    vi ~/.bashrc
49 1 Aleksander Bešir
</pre>
50 1 Aleksander Bešir
51 1 Aleksander Bešir
Add the following line:
52 1 Aleksander Bešir
53 1 Aleksander Bešir
<pre>
54 1 Aleksander Bešir
    export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
55 1 Aleksander Bešir
</pre>
56 1 Aleksander Bešir
57 1 Aleksander Bešir
At this point you can start tomcat by just executing the startup.sh script in the tomcat/bin folder.
58 1 Aleksander Bešir
59 1 Aleksander Bešir
h2. Automatic Starting
60 1 Aleksander Bešir
61 1 Aleksander Bešir
To make tomcat automatically start when we boot up the computer, you can add a script to make it auto-start and shutdown.
62 1 Aleksander Bešir
63 1 Aleksander Bešir
<pre>
64 1 Aleksander Bešir
    sudo vi /etc/init.d/tomcat
65 1 Aleksander Bešir
</pre>
66 1 Aleksander Bešir
67 1 Aleksander Bešir
Now paste in the following:
68 1 Aleksander Bešir
69 1 Aleksander Bešir
<pre>
70 1 Aleksander Bešir
    # Tomcat auto-start
71 1 Aleksander Bešir
    #
72 1 Aleksander Bešir
    # description: Auto-starts tomcat
73 1 Aleksander Bešir
    # processname: tomcat
74 1 Aleksander Bešir
    # pidfile: /var/run/tomcat.pid
75 1 Aleksander Bešir
76 1 Aleksander Bešir
    export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
77 1 Aleksander Bešir
78 1 Aleksander Bešir
    case $1 in
79 1 Aleksander Bešir
    start)
80 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/startup.sh
81 1 Aleksander Bešir
            ;;
82 1 Aleksander Bešir
    stop)  
83 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/shutdown.sh
84 1 Aleksander Bešir
            ;;
85 1 Aleksander Bešir
    restart)
86 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/shutdown.sh
87 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/startup.sh
88 1 Aleksander Bešir
            ;;
89 1 Aleksander Bešir
    esac   
90 1 Aleksander Bešir
    exit 0
91 1 Aleksander Bešir
</pre>
92 1 Aleksander Bešir
93 1 Aleksander Bešir
You’ll need to make the script executable by running the chmod command:
94 1 Aleksander Bešir
95 1 Aleksander Bešir
<pre>
96 1 Aleksander Bešir
    sudo chmod 755 /etc/init.d/tomcat
97 1 Aleksander Bešir
</pre>
98 1 Aleksander Bešir
99 1 Aleksander Bešir
The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.
100 1 Aleksander Bešir
101 1 Aleksander Bešir
<pre>
102 1 Aleksander Bešir
    sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
103 1 Aleksander Bešir
    sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat
104 1 Aleksander Bešir
</pre>
105 1 Aleksander Bešir
106 1 Aleksander Bešir
Tomcat should now be fully installed and operational.