You are on page 1of 9

Install GlassFish 3.

1 on CentOS or RHEL
This post will cover installing GlassFish 3.1 on CentOS or RHEL.
We'll also see how to run GlassFish as a service, how to access the Admin Console, and how to run GlassFish
under a minimally privileged user.
GlassFish 3.1 is the latest release and is available as GlassFish Server Open Source Edition 3.1 (free) and
Oracle GlassFish Server 3.1 (supported and requires paid subscription).
I installed both using the same process below.
The procedure is the same as with 3.0.1 with some minor changes.
If you do not already have the Java Development Kit (JDK) installed on your machine, you will need to download
and install the required JDK for your platform.
If you do have the JDK installed, you can skip to: Step 2: Download and Install the GlassFish 3.1 Server:

Step 1: Install the JDK


You can download the JDK here: http://www.oracle.com/technetwork/java/javase/downloads/index.html
I'm using the latest, which is JDK 6, update 24. The JDK is specific to 32 and 64 bit versions.
My CentOS box is 64 bit, so I'll need: jdk-6u24-linux-x64.bin.
If you are on 32 bit, you'll need: jdk-6u24-linux-i586.bin
Download the appropriate JDK and save it to a directory. I'm saving it to /root.
Move (mv) or copy (cp) the file to the /opt directory:
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# mv jdk-6u24-linux-x64.bin /opt/jdk-6u24-linux-x64.bin

Create the directory /usr/java.


view plaincopy to clipboardprint?

1.

[root@sv2 ~]# mkdir /usr/java

Change to the /usr/java directory we created and install the JDK using 'sh /opt/jdk-6u24-linux-x64.bin'
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# cd /usr/java

2.

[root@sv2 java]# sh /opt/jdk-6u24-linux-x64.bin

Set the JAVA_HOME path. This is where we installed the JDK above.
To do this for your current session, you can issue the following:

view plaincopy to clipboardprint?

1.

[root@sv2 java]# JAVA_HOME=/usr/java/jdk1.6.0_24

2.

[root@sv2 java]# export JAVA_HOME

3.

[root@sv2 java]# PATH=$JAVA_HOME/bin:$PATH

4.

[root@sv2 java]# export PATH

To set the JAVA_HOME for users, we add this to the user ~/.bashrc or ~/.bash_profile of the user. We can also
add it /etc/profile and then source it to give to all users.
view plaincopy to clipboardprint?

1.

JAVA_HOME=/usr/java/jdk1.6.0_24

2.

export JAVA_HOME

3.

PATH=$JAVA_HOME/bin:$PATH

4.

export PATH

Once you have added the above to ~/.bash_profile or ~/.bashrc, you should log out, then log back in and check
that the JAVA_HOME is set correctly.
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# echo $JAVA_HOME

2.

/usr/java/jdk1.6.0_24

Step 2: Download and Install the GlassFish 3.1 Server:


You can download both the GlassFish Server Open Source Edition 3.1 and Oracle GlassFish Server 3.1
at http://glassfish.java.net/
Once you have downloaded the desired file, move (mv) or copy (cp) the file to /usr/share/glassfish-3.1.zip (or
/usr/share/ogs-3.1.zip for Oracle GlassFish).
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# mv glassfish-3.1.zip /usr/share/glassfish-3.1.zip

Change to the /usr/share directory and unzip the file:


view plaincopy to clipboardprint?

1.

[root@sv2 ~]# cd /usr/share

2.

[root@sv2 share]# unzip -q glassfish-3.1.zip

The unzip will create the following directory: /usr/share/glassfish3


Note: Both GlassFish editions will create the same directory when unzipped: glassfish3

Step 3: Running GlassFish as a Service.


To run GlassFish as a service and enable start up at boot, we'll now create a Start/Stop/Restart script.
We'll create the script as /etc/init.d/glassfish, make the script executable, and then add our new glassfish service
to chkconfig.
Create our glassfish script:
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# cd /etc/init.d

2.

[root@sv2 init.d]# vi glassfish

view plaincopy to clipboardprint?

1.

#!/bin/bash

2.

# description: Glassfish Start Stop Restart

3.

# processname: glassfish

4.

# chkconfig: 234 20 80

5.

JAVA_HOME=/usr/java/jdk1.6.0_24

6.

export JAVA_HOME

7.

PATH=$JAVA_HOME/bin:$PATH

8.

export PATH

9.

GLASSFISH_HOME=/usr/share/glassfish3/glassfish

10.
11. case $1 in
12. start)
13. sh $GLASSFISH_HOME/bin/asadmin start-domain domain1
14. ;;
15. stop)
16. sh $GLASSFISH_HOME/bin/asadmin stop-domain domain1
17. ;;
18. restart)
19. sh $GLASSFISH_HOME/bin/asadmin stop-domain domain1
20. sh $GLASSFISH_HOME/bin/asadmin start-domain domain1
21. ;;

22. esac
23. exit 0

If you do not set the JAVA_HOME and PATH in the GlassFish script, when you attempt to start the GlassFish
server it will complain it cannot find Java with the following:
error: /usr/share/glassfish3/glassfish/bin/asadmin: line 19: exec: java: not found
Now, make the script executable and add it to our chkconfig so it starts at boot.
view plaincopy to clipboardprint?

1.

[root@sv2 init.d]# chmod 755 glassfish

2.

[root@sv2 init.d]# chkconfig --add glassfish

3.

[root@sv2 init.d]# chkconfig --level 234 glassfish on

We should now be able to Start, Stop, and Restart GlassFish as a service.


Start GlassFish:
view plaincopy to clipboardprint?

1.

[root@sv2 init.d]# service glassfish start

2.

Waiting for domain1 to start .........

3.

Successfully started the domain : domain1

4.

domain Location: /usr/share/glassfish3/glassfish/domains/domain1

5.

Log File: /usr/share/glassfish3/glassfish/domains/domain1/logs/server.log

6.

Admin Port: 4848

7.

Command start-domain executed successfully.

Stop GlassFish:
view plaincopy to clipboardprint?

1.

[root@sv2 init.d]# service glassfish stop

2.

Waiting for the domain to stop ....

3.

Command stop-domain executed successfully.

Step 4: Check Homepage.


You should now see the GlassFish default home page at http://youdomain.com:8080 or http://YourIP:8080

If you do not see the home page, check the logs and insure that port 8080 is open and available.

Step 5: Access GlassFish Admin Console.


You should now be able to access the GlassFish Admin Console at:
http://yourdomain.com:4848 or http://yourip:4848

On accessing the GlassFish Admin Console for the first time, you will find that no user name or password is
required.
Previous to 3.1, a default password 'adminadmin' was used.
You can set (or change) the admin password within the GlassFish Admin console.
1. Click "Domain" on the tree.
2. Click the Administrator Password tab.
3. Enter and confirm your password and click Save.
4. Note that the "Logout" button now appears at top right.

The first password save will create a file,.asadminpass, in the home directory of the user you are running the
service under.
Alternatively, you can set the admin password via the CLI using.
view plaincopy to clipboardprint?

1.

[root@sv2 bin]# $GLASSFISH_HOME/bin/asadmin change-admin-password

2.

Enter admin user name [default: admin]>

3.

Enter admin password>

4.

Enter new admin password>

5.

Enter new admin password again>

6.
7.

Command change-admin-password executed successfully.

8.

[root@sv2 bin]#

Step 6: Running GlassFish with Minimally Privileged (non-root) User.


Since I am installing this on my development machine, I am running GlassFish as root above.
In production, you will want to run GlassFish as a non-root user with minimal privileges.
To do this, we can need to the following.
1. Create the user, glassfish, who will own the files.
Create the new group, glassfish, and add the user glassfish to the group:
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# groupadd glassfish

2.

[root@sv2 ~]# useradd -s /bin/bash -g glassfish glassfish

2. Change ownership of the GlassFish files to the user glassfish we created.


We'll change ownership of the files under /usr/share/glassfish3 from root to the user glassfish we created above:
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# chown -Rf glassfish.glassfish /usr/share/glassfish3/

3. Update our glassfish script.


Finally, we update the glassfish start/stop/restart script we created above so we su to user glassfish:
view plaincopy to clipboardprint?

1.

#!/bin/bash

2.

# description: Glassfish Start Stop Restart

3.

# processname: glassfish

4.

# chkconfig: 234 20 80

5.

JAVA_HOME=/usr/java/jdk1.6.0_24

6.

export JAVA_HOME

7.

PATH=$JAVA_HOME/bin:$PATH

8.

export PATH

9.

GLASSFISH_HOME=/usr/share/glassfish3/glassfish

10. GLASSFISH_USER=glassfish
11.
12. case $1 in

13. start)
14. su $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin start-domain domain1"
15. ;;
16. stop)
17. su $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin stop-domain domain1"
18. ;;
19. restart)
20. su $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin stop-domain domain1"
21. su $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin start-domain domain1"
22. ;;
23. esac
24. exit 0

Step 7: Running GlassFish on Port 80 as Non-Root User.


To run services below port 1024 as user other than root, you will need to use port forwarding.
You can do this by adding the following to your IP tables:
view plaincopy to clipboardprint?

1.

[root@sv2 ~]# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080

2.

[root@sv2 ~]# iptables -t nat -A PREROUTING -p udp -m udp --dport 80 -j REDIRECT --to-ports 8080

PARA EL SUBSCRIBER CON VERSION 3.1.1 y JDK 6.1-25


export JAVA_HOME=/usr/local/src/jdk1.6.0_25
export JRE_HOME=//usr/local/src/jdk1.6.0_25jre
export PATH=$PATH:/usr/local/src/jdk1.6.0_25/bin:/usr/local/src/jdk1.6.0_25/jre/bin

alternatives --set jar /usr/local/src/jdk1.6.0_25/bin/jar


alternatives --set javac /usr/local/src/jdk1.6.0_25/bin/javac
export
export
export
export
export

JRE_HOME=/usr/local/src/jdk1.6.0_25
PATH=$PATH:$JRE_HOME/bin
JAVA_HOME=/usr/local/src/jdk1.6.0_25
JAVA_PATH=$JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin

You might also like