Monday, May 12, 2014

Netbeans + Tomcat7 integration


Hi devs,

This post explains how to setup a smooth and productive working environment with Netbeans 8.0 and Tomcat7. Where Netbeans 8.0 is able to run the auto deploys and everything is refreshed properly so that its easier to write code and test it on the browser, specially for UI implementations. 

The main issue in an Ubuntu machine is that running Netbeans with the logged in user account has permissions problems with the the Tomcat 7 server installed in the system as root user.

A quick and dirty solution is not to use the Tomcat instance installed in the system and instead download a binaries file from Apache Tomcat and extract it in the user home directory. While this may work is a bit messy and feels less linuxness and more windowness. 

All what its required to do is to set up the proper permissions for all Tomcat 7 folders in the system. 

/etc/tomcat7 --> server configuration
/var/lib/tomcat7 --> variable content of the server. Some of the folders are linked to the /etc/tomcat7 folders that contains the configuration files.
/usr/share/tomcat7 --> server base directory.

First we provide read / write and execute permissions to the folders using the following commands:

sudo chmod -r 774 /var/lib/tomcat7 sudo chmod -r 774 /etc/tomcat7 sudo chmod -r 774 /use/share/tomcat7 
Note: you can use 770 permissions if you like to be more restrictive.


Then we change the user and group owners of those folders:

chown -r tomcat7:tomcat7 /var/lib/tomcat7
chown -r tomcat7:tomcat7 /etc/tomcat7
chown -r tomcat7:tomcat7 /use/share/tomcat7 


Then we add the logged in user to the tomcat7 group, in order to be able to access to those folders.

usermod -a -G groupName userName
Note!!: if after this commands things are not working, you may need to do a reset because sometimes the user and group permissions are not properly refreshed. 


Then we set up the SGID (Set Group ID up on execution) flag to the following directories, in order to use the parent folder permissions for new files created at runtime by Netbeans and Tomcat.

sudo chmod g+s /var/lib/tomcat7/work/Catalina
sudo chmod g+s ~/.netbeans/8.0/conf/J2EE


Finally register a new server in Netbeans. Go to Services tab, right click in Servers and click to Add Server ... . Enter values as follows in the Connection tab.

Server Name: Apache Tomcat or TomEE
Server Type: Web server
Catalina Home: /usr/share/tomcat7
Catalina Base: /var/lib/tomcat7
Server Port: 8080 (if you use another one, check the tomcat config files)
Shutdown Port: 8005 (if you use another one, check the tomcat config files)


User / Password : Configure an admin user. Add the following lines into the tomcat-users.xml file in /etc/tomcat7 folder.

<role rolename="manager-script"/>
<role rolename="manager"/>
<user username="admin" password="admin" roles="manager-script,manager"/>


And I think that is all what I needed to do to marriage Netbeans and Tomcat for a good fast and productive working environment. 

Thanks for reading, 
Posted by Marc Andreu Fernàndez.

No comments: