Glassfish 4.1 start script

GlassFish logo Ubuntu logo Installing GlassFish 4.1 on Ubuntu 14.10 (last updated 2015-02-05)
This HOWTO explains one way of setting up GlassFish 4.1 under Ubuntu 14.10. Questions or suggestions for improvement can be addressed to shadowm at lyonlabs dot org.

Installing GlassFish
Fronting GlassFish with Apache 2.4
Setting up Database Connection Pools
Installing GlassFish

Download the ZIP file from Oracle. Make sure you have a current JDK installed on your machine (Glassfish 4.1 supports Java 8).
As root, enter the directory you want to install to (I install it in /usr/share) and extract the ZIP file there.
Add the binaries directory of your new installation to the PATH. One way of doing this is to use a profile script in /etc/profile.d. Create a file there whose name ends in .sh, make it executable, and add this line:
export PATH=/usr/share/glassfish4/bin/glassfish/bin:$PATH
Log out and back in to make the change take effect.
Create a group and a user for GlassFish:
addgroup –system glassfish
adduser –system –shell /bin/bash –ingroup glassfish glassfish
Add the following lines to /etc/security/limits.conf to increase the maximum number of open files for the user that Glassfish will run as:
glassfish soft nofile 32768
glassfish hard nofile 65536
You will also need to add this line in /etc/pam.d/su:
session required pam_limits.so
As root, in /usr/share, change the permissions of the newly-created GlassFish directory:
chown -R glassfish:glassfish glassfish4
If you want to use your own domain name instead of the default domain1, become the glassfish user (as root, su – glassfish), then delete the default domain and create a new one:
asadmin delete-domain domain1
asadmin create-domain –savemasterpassword
You will be prompted for new admin and master passwords. If you will be installing on several machines, I suggest that you give your domain the same name as the hostname of the machine you are installing on; that way, you can use the same startup scripts on all machines, using $HOSTNAME for the domain.
Start the domain:
asadmin start-domain
Verify that GlassFish is running by opening a browser on localhost:8080. You should also be able to log in to the admin console on localhost:4848
In order for GlassFish to be able to start from a script, you will also need to create a file containing the domain and master passwords. The startup script given below expects to find a file named passwd.gf in the glassfish user’s home directory; make sure it has glassfish:glassfish ownership and 600 permissions (i.e. readable and writable only by the glassfish user). This is the format of that file:
AS_ADMIN_PASSWORD= AS_ADMIN_MASTERPASSWORD= This is a sysvinit script to make GlassFish start at boot time. To use it, copy it to /etc/init.d, then enable it with update-rc.d:
update-rc.d glassfish-4.1 defaults
update-rc.d glassfish-4.1 enable
This script expects the domain name to be same as the hostname (see above); if that is not the case, edit accordingly. It also expects to find Java in /usr/lib; if you have it installed somewhere else, just change the JPS variable. The script writes to a log file that prints any startup errors, so create that first (as root):
touch /var/log/glassfish-4.1
chown glassfish.glassfish /var/log/glassfish-4.1
Verify that the script works by running it as root:
/etc/init.d/glassfish-4.1 start
You’ll want to do some tuning before using GlassFish in production (these suggestions come from Jean-François Arcand’s excellent blog entry). In Configuration / JVM Settings / JVM Options, replace these two options:
-client
-Xmx512m
with these:
-server
-XX:+AggressiveHeap -Xmx3500m -Xms3500m -Xss128k -XX:+DisableExplicitGC -XX:-UseLoopPredicate -XX:+UseCompressedOops
back to top

Fronting GlassFish with Apache 2.4

Many people use Apache to handle redirects, SSL, &c., while leaving the heavy lifting to GlassFish. It also avoids the issue of running GlassFish on port 80 (which is privileged under Linux), since Apache is set up that way to begin with. Here is one way to do it, using the Apache AJP connector (here are links to the reference guides for the jk.conf and workers.properties files given below).
Set MaxRequestWorkers to 400 in /etc/apache2/mods-available/mpm_event.conf. This is the highest value that can be used with the default settings (ServerLimit default of 16, multiplied by ThreadsPerChild, which defaults to 25). For reference, see the Apache documentation. You can verify that the event MPM is the one you’re using with this command:
apache2ctl -t -D DUMP_MODULES | grep event
Back up the original jk.conf file in /etc/apache2/mods-available and create a new one. It should start like this (mind the domain name):

JkWorkersFile /usr/share/glassfish4/glassfish/domains//config/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel error
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat “%w %V %T”
JkMountCopy all

You’ll also need to tell Apache to forward any traffic for your GlassFish-hosted web app; it’s also a good idea to disallow access to the WEB-INF directory. If your app’s context is /myapp, add lines like this to the file:
JkMount /myapp/* ajp13

require all denied

Create a workers.properties file in your GlassFish domain’s config directory (referenced in the above file) that looks like this:
worker.list=ajp13
worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009
# load balancing only: worker.ajp13.lbfactor=50
connection_pool_size=10
connection_pool_timeout=600
worker.ajp13.socket_keepalive=False
# make same as GlassFish:
worker.ajp13.socket_timeout=30
Create the JK listener in GlassFish using these commands:
asadmin create-http-listener –listenerport 8009 –listeneraddress 0.0.0.0 –defaultvs server jk-listener
asadmin set server-config.network-config.network-listeners.network-listener.jk-listener.jk-enabled=true
After doing this, edit server-config and create a new thread pool called jk-thread-pool with minimum and maximum size of 400, then assign this pool to the jk-listener you just created (in Network Config/Network Listeners). The number 400 is to match the values in /etc/apache2/mods-available/mpm_event.conf (see above). For reference, see the GlassFish 4.0 documentation.
Restart Apache and GlassFish. If you could access your app at e.g. localhost:8080/myapp/index.html before fronting with Apache, you should be able to access it without the :8080 now.
back to top

Setting up Database Connection Pools

Here are the steps to set up a connection pool for a PostgreSQL database:
Put the latest Postgres JDBC driver to the lib/ subdirectory of the GlassFish install (/usr/share/glassfish4/glassfish/lib).
In the admin client, navigate to Resources / JDBC / Connection Pools, and create a new pool named after the database. Set the resource type to javax.sql.DataSource; leave the database vendor field blank.
On the next panel, set the datasource classname to org.postgresql.ds.PGSimpleDataSource. (You could also use javax.sql.XADataSource and org.postgresql.xa.PGXADataSource).
At the bottom of the page is an area to enter additional properties; you must enter values for serverName, databaseName, user, and password. If you want to use entity class generation or the JPQL browser in NetBeans, add a URL parameter that looks like jdbc:postgresql://hostname:5432/databasename.
After saving, select the entry for this pool and click the “Ping” button. You should see a message saying “Ping Succeeded.”
Create a new entry pointing to the pool under Resources / JDBC / JDBC Resources. The name must start with jdbc/.
If you are using EJBs with JPA, your persistence.xml should look something like this (edit to taste):
org.eclipse.persistence.jpa.PersistenceProvider jdbc/your-resource-here
false
For those using MySQL, the datasource class name is com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. The necessary properties are user, password, and URL; the format for URL is:
jdbc:mysql://:3306/
back to top


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *