Wednesday, January 1, 2014

Placing Glassfish behind Apache HTTP to redirect domain:port to domain

Glassfish can be configured to run behind Apache HTTP server and configure apache to point external requests to Glassfish. Therefore HTTP requests are handled by Apache and all www.example.com requests are pointed to www.example.com:8080 internally. Below is a brief explanation of how to do this from scratch:

  • Install Glassfish 3+ (make sure your listener is created and activated on 8009 and your jk is enabled)
  • Get the mod_jk connector and place it into apache/modules.
  • Create a worker.properties file and place it into apache/conf. It should contain the following properties:
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

Open httpd.conf file in apache/conf folder and place the following commands:

#load jk modules from modules folder
LoadModule jk_module modules/mod_jk.so 
#location of the worker file
JkWorkersFile conf/worker.properties
#where to put jk logs
JkLogFile logs/mod_jk.log 
#log level [deug/error/info]
JkLogLevel debug
#Log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# Indicate to send SSL KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# Set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to Glassfish
JkMount /*.jsp worker1
# Send all webapp requests to Glassfish
JkMount /* worker1

You need also to add a VirtualHost section in your conf file. This maps your domain with the path in Glassfish so Apache will be able to see it. The following tells apache to map all /myapp/* links to glassfish (you can also use /* instead of /myapp/* to link all request to Glassfish explicitly, however this is not recommended as it gives full external access to Glassfish server)

<virtualhost 111.111.111.111:80>
  ServerAdmin admin@domain
  ServerName domain
  JkMount /myapp/* worker1
</virtualhost>


Note: if your Glassfish listener is not created you can created from shell command using glassfish asadmin with the following command (Path your command to asadmin file which is located in glassfish/glassfish/bin:


asadmin create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector

  


Restart Apache and Glassfish for the new configurations to be updated. Apache should now see your Glassfish on port 80. So your www.example.com:8080 will be served on www.example.com.



Tuesday, June 12, 2012

Connecting Netbeans 7.1 to MS SQL Server 2012

Usually I use Java DB/Derby database for my small projects but never used SQL Server in any of my past projects and found lots of difficulties and headaches in creating a JDBC connection. However after days of troubleshooting I finally could successfully connect to SQL Server. I will explain the steps in this blog.

Software used:
- Netbeans 7.1
- MS SQL Server 2012 Enterprise Edition
- Microsoft JDBC Driver

I assume you already have your own versions of netbeans and SQL Server, if not you can download them from their own website (you can get a developer version of SQL Server for free).

Microsoft JDBC Driver:
You can download this driver from official MSDN website. After you download, execute/unzip the file, copy the unzipped folder and paste it in the SQL Server installation folder so it will be easier to remember its path (or any other directory you want).

TCP Listening Port
Make sure your Server is listening on the correct port (Default port is 1433, but you can change that if needed). Follow these steps to make sure your server is listening on the right port:
- Open SQL Server Configuration Manager
- On the left column expand SQL Server Network Configuration
- Click on Protocols for <your server name>
- On the right column, right-click on TCP/IP and click properties
- In the protocol tab make sure that your TCP IP is enabled and listens to all
- In the IP Addresses navigate to IPAll section and make sure the Dynamic ports is set to 1433 (or the port you want), if not change it to that value.
- Click Ok and you will be prompted with a warning message indicating that the changes will not take effect untill you restart the service.
- Now in the left column click on SQL Server Services.
- Find your server on the right column (SQL Server (<your server name>).
- Right-click on the server and click Restart.

Make sure your TCP is listening to your port
 You need to make sure that the port you have defined for SQL Server is listening. To do that open the command prompt and write the following command: netstat -a
You can track all the active ports on this list. Try to find your port from this list

Create DB Server
On the installation of MS SQL Server 2012 it will prompt you to create a server. Make sure you choose the SQL Server Authentication (in opposed to Windows Authentication mode) and assign a username ("sa" by default) and a password. This is because netbeans cannot establish connection with SQL Server through JDBC if it is on Windows Authentication mode. However, if If you already have chosen your authentication mode to Windows Authentication you can change it by following the steps in the following section

Change Authentication Mode
- Open Microsoft SQL Server Management Studio.
- In the Object Explorer, Right-click your server and click Properties
- Select the security page
- Change the Server Authentication mode from Windows Authentication mode to SQL Server and Windows Authentication mode
- You will receive the warning message that changes will not take effect until you restart your server.
- Before you restart, you need to enable the login for the server.
- In the Object Explorer expand your server
- Expand the security folder.
- Expand logins folder
-  Right-click "sa" and click properties
- In the status page, set the login "enabled"
- In the general page, you might have to create and confirm a password for the login.

Configuring JDBC Driver with Netbeans:
- Open netbeans and open the services tab.
- Right-click the database and click new connection
- From the driver drop down menu, select "New Driver"
- Click Add to add a driver file, the driver file would be the JDBC driver you have downloaded from MSDN. Navigate to the folder you have pasted in SQL installation folder (or any directory you have pasted in), choose "sqljdbc4.jar" and click open.
- The name and class name will be filled once you select the JDBC Driver file. The class name will be: "com.microsoft.sqlserver.jdbc.SQLServerDriver" click ok to make the new driver.
- Now with selected driver, click next
- Fill the following information:
       *Host: localhost
       *Port: 1433 (or the port you have selected previously)
       *Database: the name of the database you created
       *User Name: sa
       *Password: <the password you have defined for the database authentication>
- Click next and make sure you receive no errors. If you received no errors that means netbeans can successfully connect to the db. Click next and choose the schema. 
- Click finish and Bingo!!! You are successfully connected to MS SQL Server through netbeans.

This is how it worked for me!, you might find other errors that I did not face, I had to do lots of troubleshooting to find out some errors. But if you followed the instructions carefully, you will probably get no errors. Please leave a comment and good luck :)