References

Prerequisite

  1. Debian
  2. Java

Install Glassfish

Download and unzip Glassfish

apt-get install wget unzip
cd /opt
wget http://download.java.net/glassfish/4.1.2/release/glassfish-4.1.2.zip
unzip glassfish-4.1.2.zip -d /opt

You would want to ease the starting/stopping/restarting of your Glassfish server. Therefore, we will create an init script.

nano /etc/init.d/glassfish

Paste the following lines:

#!/bin/bash
#
# description: Startup script for Glassfish Application Server
# processname: glassfish

GLASSFISH_HOME=/opt/glassfish4/
export GLASSFISH_HOME
GLASSFISH_USER=root
export GLASSFISH_USER

start() {
echo -n "Starting Glassfish: "
su $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin start-domain domain1"
sleep 2
echo "done"
}

stop() {
echo -n "Stopping Glassfish: "
su $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin stop-domain domain1"
echo "done"
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: glassfish {start|stop|restart}"
exit
esac

Make the script executable:

chmod a+x /etc/init.d/glassfish

Make the Glassfish server to start on boot up:

update-rc.d glassfish defaults

You can now start Glassfish:

/etc/init.d/glassfish start

In order to visit admin page (your_server_IP:4848) remotely, you need to enable secure admin not to get “Secure Admin must be enabled to access the DAS remotely” error

cd /opt/glassfish4/bin
# Make the asadmin script executable
chmod +x asadmin
./asadmin --host your_server_IP --port 4848 enable-secure-admin

You will receive the below error:

remote failure: At least one admin user has an empty password, which secure admin does not permit. Use the change-admin-password command or the admin console to create non-empty passwords for admin accounts.
Command enable-secure-admin failed.

If it is failed as above, change admin password first (default: username admin, password empty)

./asadmin --port 15123 change-admin-password

Now run this command again:

./asadmin --host your_server_IP --port 4848 enable-secure-admin

You must restart all running servers for the change in secure admin to take effect.

/etc/init.d/glassfish restart

Useful Glassfish commands

Starting, Stopping, Restarting

./asadmin start-domain
./asadmin stop-domain
./asadmin restart-domain

Adding Resources

./asadmin add-resources "/path/to/glassfish-resources.xml"

Creating AuthRealm

./asadmin create-auth-realm --classname realm_class [--help] [--property(name=value)[:name=value]*][--target target_name] auth_realm_name

During the creation of AuthRealm, if you get “remote failure: Creation of Authrealm jdbcbid failed.”, it turns out that the problem was locale language of JVM. After adding the following properties to JVM Options in Glassfish, everything will begin to work properly.

./asadmin create-jvm-options -Duser.language=en
./asadmin create-jvm-options -Duser.region=US

Then restart server to apply changes.

Deploying, Undeploying

./asadmin deploy "/path/to/[application-name].war"
./asadmin undeploy [application-name]

To prevent password entry each time

In order to save typing “admin username” and “password” every time you deploy or undeploy an application, create a password file pwdfile with content:

AS_ADMIN_PASSWORD=your_admin_password

Add –passwordfile in command:

./asadmin --passwordfile pwdfile deploy /path/to/[application-name].war

Now the prompt for username/password won’t appear.

Recover Glassfish admin password

changeit

admin;{SSHA256}dvCEGFNHGtSyXIhJvwR5FnviH+u8fCadrUIqp6uJc1tP9Bv10CGT7A==;asadmin

empty

admin;{SSHA256}2GiTMM0n4ulpMfRaMPMImYvVF522XY6nW2cKSXoY1EoM2QlHrHUkDQ==;asadmin

Glassfish mail-resource for domain, gmail and yandex using no-ssl, ssl, tis

Domain settings with no-ssl

<mail-resource jndi-name="mail/myMailSession"
               host="mail.domain.com"
               user="user@domain.com"
               from="user@domain.com"
               object-type="user"
               store-protocol="imap"
               store-protocol-class="com.sun.mail.imap.IMAPStore"
               transport-protocol="smtp"
               transport-protocol-class="com.sun.mail.smtp.SMTPTransport"
               debug="true"
               enabled="true">
    <property name="mail.smtp.auth" value="true"/>
    <property name="mail.smtp.port" value="587"/>
    <property name="mail.smtp.password" value="PASSWORD"/>
    <property name="mail.smtp.starttls.enable" value="false"/>
</mail-resource>

Gmail settings with ssl

<mail-resource jndi-name="mail/myMailSession"
               host="smtp.gmail.com"
               user="user@gmail.com"
               from="user@gmail.com"
               object-type="user"
               store-protocol="imaps"
               store-protocol-class="com.sun.mail.imap.IMAPSSLStore"
               transport-protocol="smtps"
               transport-protocol-class="com.sun.mail.smtp.SMTPSSLTransport"
               debug="true"
               enabled="true">
    <property name="mail.smtps.auth" value="true"/>
    <property name="mail.smtps.password" value="PASSWORD"/>
</mail-resource>

Yandex settings with ssl

<mail-resource jndi-name="mail/myMailSession"
               host="smtp.yandex.com.tr"
               user="user@domain.com.tr"
               from="user@domain.com.tr"
               object-type="user"
               store-protocol="imaps"
               store-protocol-class="com.sun.mail.imap.IMAPSSLStore"
               transport-protocol="smtps"
               transport-protocol-class="com.sun.mail.smtp.SMTPSSLTransport"
               debug="true"
               enabled="true">
    <property name="mail.smtps.auth" value="true"/>
    <property name="mail.smtps.password" value="PASSWORD"/>
</mail-resource>

alternatedocroot example

glassfish-web.xml

<property description="Uploaded images" name="alternatedocroot_1" value="from=/images/* dir=/Users/[username]/Developments/backup"/>

xhtml file

<p:graphicImage value="/images/logo.png"/>

Note: Be sure that there is “/Users/[username]/Developments/backup/images” directory Note: use “value” attribute, not “name” attribute