Installing Java and Tomcat » History » Version 4

Aleksander Bešir, 07.12.2010 13:04

1 1 Aleksander Bešir
h1. Installing Java and Tomcat
2 1 Aleksander Bešir
3 4 Aleksander Bešir
{{toc}}
4 1 Aleksander Bešir
5 4 Aleksander Bešir
h2. 1 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
</pre>
19 1 Aleksander Bešir
20 1 Aleksander Bešir
If that command has no results, you’ll want to install the latest version with this command:
21 1 Aleksander Bešir
22 1 Aleksander Bešir
<pre>
23 1 Aleksander Bešir
    sudo apt-get install openjdk-6-jdk
24 1 Aleksander Bešir
</pre>
25 1 Aleksander Bešir
26 4 Aleksander Bešir
h2. 2 Apache Tomcat installation
27 1 Aleksander Bešir
28 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.
29 1 Aleksander Bešir
<pre>
30 1 Aleksander Bešir
31 1 Aleksander Bešir
    wget http://apache.hoxt.com/tomcat/tomcat-6/v6.0.29/bin/apache-tomcat-6.0.14.tar.gz
32 1 Aleksander Bešir
33 1 Aleksander Bešir
    tar xvzf apache-tomcat-6.0.29.tar.gz
34 1 Aleksander Bešir
</pre>
35 1 Aleksander Bešir
36 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.
37 1 Aleksander Bešir
38 1 Aleksander Bešir
<pre>
39 1 Aleksander Bešir
    sudo mv apache-tomcat-6.0.29 /usr/local/tomcat
40 1 Aleksander Bešir
</pre>
41 1 Aleksander Bešir
42 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.
43 1 Aleksander Bešir
44 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.
45 1 Aleksander Bešir
46 1 Aleksander Bešir
<pre>
47 1 Aleksander Bešir
    vi ~/.bashrc
48 1 Aleksander Bešir
</pre>
49 1 Aleksander Bešir
50 1 Aleksander Bešir
Add the following line:
51 1 Aleksander Bešir
52 1 Aleksander Bešir
<pre>
53 1 Aleksander Bešir
    export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
54 1 Aleksander Bešir
</pre>
55 1 Aleksander Bešir
56 1 Aleksander Bešir
At this point you can start tomcat by just executing the startup.sh script in the tomcat/bin folder.
57 1 Aleksander Bešir
58 4 Aleksander Bešir
h2. 3 Automatic Starting
59 1 Aleksander Bešir
60 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.
61 1 Aleksander Bešir
62 1 Aleksander Bešir
<pre>
63 1 Aleksander Bešir
    sudo vi /etc/init.d/tomcat
64 1 Aleksander Bešir
</pre>
65 1 Aleksander Bešir
66 1 Aleksander Bešir
Now paste in the following:
67 1 Aleksander Bešir
68 1 Aleksander Bešir
<pre>
69 1 Aleksander Bešir
    # Tomcat auto-start
70 1 Aleksander Bešir
    #
71 1 Aleksander Bešir
    # description: Auto-starts tomcat
72 1 Aleksander Bešir
    # processname: tomcat
73 1 Aleksander Bešir
    # pidfile: /var/run/tomcat.pid
74 1 Aleksander Bešir
75 1 Aleksander Bešir
    export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
76 1 Aleksander Bešir
77 1 Aleksander Bešir
    case $1 in
78 1 Aleksander Bešir
    start)
79 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/startup.sh
80 1 Aleksander Bešir
            ;;
81 1 Aleksander Bešir
    stop)  
82 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/shutdown.sh
83 1 Aleksander Bešir
            ;;
84 1 Aleksander Bešir
    restart)
85 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/shutdown.sh
86 1 Aleksander Bešir
            sh /usr/local/tomcat/bin/startup.sh
87 1 Aleksander Bešir
            ;;
88 1 Aleksander Bešir
    esac   
89 1 Aleksander Bešir
    exit 0
90 1 Aleksander Bešir
</pre>
91 1 Aleksander Bešir
92 1 Aleksander Bešir
You’ll need to make the script executable by running the chmod command:
93 1 Aleksander Bešir
94 1 Aleksander Bešir
<pre>
95 1 Aleksander Bešir
    sudo chmod 755 /etc/init.d/tomcat
96 1 Aleksander Bešir
</pre>
97 1 Aleksander Bešir
98 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.
99 1 Aleksander Bešir
100 1 Aleksander Bešir
<pre>
101 1 Aleksander Bešir
    sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
102 1 Aleksander Bešir
    sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat
103 1 Aleksander Bešir
</pre>
104 1 Aleksander Bešir
105 1 Aleksander Bešir
Tomcat should now be fully installed and operational.