Tomcat created directory have different owner and group on server - java

I have a simple JSP page that creates a directory on centOS server root
<% File f= new File("/test/testdir");
if(f.mkdir()){
%>
generated a .WAR file and deployed on server. when i run this code. the created directory
testdir have following attributes.
it should have tomcat as a owner. tomcat is a user on my server and tomcat is the member of tgroup group.
i need that the directory should have owner as tomcat and group as a tgroup

Maybe you are running tomcat with the user root. Change the user you are using to run tomcat process.

Your Tomcat process is running as root (which is not a good thing). Since you already have a separate tomcat user setup, complete the process of always running Tomcat as that user:
Assuming:
Install directory is /opt/tomcat (replace with your value)
Startup script is /etc/init.d/tomcat (modify to reflect your startup
script)
sudo /etc/init.d/tomcat stop
sudo chown -R tomcat:tgroup /opt/tomcat
sudo -u tomcat /etc/init.d/tomcat start
You may choose to hard code the user to run as in your startup script, which would be beneficial if you are setting Tomcat up to run on boot.

Related

Spring boot App not working after ssh logout

So I'm deploying my Spring Boot application on an Ubuntu LTS Server. It is built with maven and running with embedded Tomcat.
I'm still new to the deployment process, what I did was:
Log into server via ssh
use scp to upload my_application.zip
unzip it in ssh
java -jar my_application.jar
Now all of that works perfectly fine and I've been using it like that for quiet some time. Now I have to make the Application to stay online and available after logging out of the shell.
I have read some documentation about running processes in background on Linux and I've tried it with nohup java -jar myApplication.jar &, with the screen command and with bg. All of them worked fine while I'm logged into the ssh.
Here comes my problem:
As soon as I end the ssh session the Web App is still available (so the process clearly didn't stop) but it just looks & behaves really weird.
CSS is not applied, JS does not work etc.
My guess would be that some paths or file system accesses are messed up, but I have no idea at all how that could origin from the ssh session.
(When I log back into ssh everything is working fine again)
Would be great if someone has a clue here
If your server has encrypted home directory, it will get re-encrypted once you log out and therefore your script will stop working. It does not have a lot of sense to have encrypted homes on servers so you can disable it.
Or just run the script from different directory and avoid working with files under home directory.
I think you should use systemd for this case.
Also You can add new system user for your app.
You can find more information here:
Spring Boot: 59.2.2 Installation as a systemd service
Ubuntu Wiki: Systemd For UpstartUsers
For example:
Create file myunit.service
[Unit]
Description=MySpringService
After=syslog.target
After=network.target
After=mysql.service
[Service]
Type=forking
PIDFile=/work/www/myunit/shared/tmp/pids/service.pid
WorkingDirectory=/work/www/myunit/current
User=myunit
Group=myunit
Environment=RACK_ENV=production
OOMScoreAdjust=-1000
ExecStart=/usr/local/bin/bundle exec service -C /work/www/myunit/shared/config/service.rb --daemon
ExecStop=/usr/local/bin/bundle exec service -S /work/www/myunit/shared/tmp/pids/service.state stop
ExecReload=/usr/local/bin/bundle exec service -S /work/www/myunit/shared/tmp/pids/service.state restart
TimeoutSec=300
[Install]
WantedBy=multi-user.target
Copy file to /etc/systemd/system/
Run:
systemctl enable myunit
systemctl start myunit

how to start and stop application in apache-tomcat manually if there are more than one application in webapps directory of tomcat?

Actually i have two application in webapps directory of tomcat
webapps/hello_web1 and
webapps/hello_web2
I want to start and stop these to apps separably and by using command line (or any other way except using tomcat manager gui so that i can implement start and stop function manually in my program).
Please suggest me if it is possible, and if it is not possible in tomcat than suggest me web server in which it is possible.
Best way is using CURL targetting your desired command from Tomcat /manager
curl --user user:pass http://localhost:8080/manager/text/<your command>?path=/<your web appl>
here you can find list of commands
https://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html
in your case
Start Application
http://localhost:8080/manager/text/start?path=/yourWebAppl
Stop Application
http://localhost:8080/manager/text/stop?path=/yourWebAppl
Restart Application
http://localhost:8080/manager/text/reload?path=/yourWebAppl
There are several ways of deploying web applications in Apache Tomcat. One is the XML deployment descriptor file in the directory <TOMCATDIR>/conf/Catalina/localhost.
The content of such a file is a <Context...></Context> configuration that may look like this (eg. webapp1.xml):
<?xml version='1.0' encoding='utf-8'?>
<Context displayName="webapp1"
override="true"
path="/webapp1"
docBase="${catalina.base}/webapps/webapp1">
... some context configuration ...
</Context>
If Tomcat finds a XML file in this directory, it will try to deploy the context defined in that file. If you remove that file from this directory, Tomcat will undeploy the web application context.
All of that can be done by command line or shell script.
Regarding to your question: You may put two context files in this directory. One for each of your web apps. To deploy one just move the file in and to undeploy one just move it out of the directory.
# deployment
&> cd "<TOMCATPATH>/conf/Catalina/"
&> mv webapp1.xml localhost/
# undeploy
&> cd "<TOMCATPATH>/conf/Catalina/localhost"
&> mv webapp1.xml ../
For more detailed information about the <Context...></Context> element refer to: The Context Container Doc Page

Log File not Creating after tomcat restart

I have an ec2 instance from which i am remotely logging in to a remote EC2 server (as ec2-user) on which my application is hosted in its tomcat server.
The tomcat server generates a log file for this particular application.
If i delete the log file (test_app.log) and restart the server manually using startup.sh command a new log file with same name gets created but when i try to do this by remote login it is not working. The tomcat server is restarting (i have verified it on my browser) but the log file do not get genrated.
As i want a separate log for all my application runs , i am first stopping the tomcat by killing it, then i move the test_app.log to other name with time stamp, and then restart the server.
when i use automated script to ssh for the first time everything happens as planned above but from the next iteration nothing happens.Even after using touch no new file gets created.
Please note if i do entire process manually ,all the commands run properly and everything works fine.
ssh -i $Public_Key_Loc -tt $Remote_user#$PrivateIpSPL <<-ENDSSH
sudo su - spluser
kill -9 $(ps -elf | grep tomcat|cut -d ' ' -f6 | head -n 1)
mv /opt/tomcat/current/logs/spl/spl-test-tool/test_app.log /opt/tomcat/current/logs/spl/spl-test-tool/$(date "+%Y.%m.%d-%H.%M.%S").test_app.log
touch test_app.log
chown spluser:splgrp test_app.log
chmod 644 test_app.log
/opt/tomcat/current/bin/startup.sh
exit
exit
ENDSSH
i think the culprit is touch command ,the file will get created at the current folder , i think you should provide absolute path for that too.

What is the minimal set of files for a jetty 9.x base directory for a war file?

According to http://www.eclipse.org/jetty/documentation/current/quickstart-running-jetty.html it is possible to manage web applications in base directories in jetty 9.x. The guide explains what can be put inside those and gives an example by pointing to the demo-base directory in the binary distribution. However it would have been useful to point out what actually needs to be in such a jetty base in order to make deployment successful, e.g. so that
cd /path/to/my-base/
java -jar ~/jetty-distribution-9.2.3.v20140905/start.jar jetty.home=~/jetty-distribution-9.2.3.v20140905/ jetty.base=.
succeeds. Putting a minimal valid war file (with only one jsf file) into /path/to/my-base or /path/to/my-base/webapps/ fails with WARNING: Nothing to start, exiting ..., although it would make sense to deploy a minimal application or display a helpful warning what needs to be added.
What needs to be added to be able to deploy an application from a separate base directory?
Jetty can make this for you through flags to the start.jar
There's an exmample in the docs, here: http://www.eclipse.org/jetty/documentation/9.3.0.v20150612/quickstart-running-jetty.html
"The following commands: create a new base directory; enables a HTTP connector and the web application deployer; copies a demo webapp to be deployed.
Simplified:
mkdir /home/me/mybase
cd /home/me/mybase
java -jar $JETTY_HOME/start.jar --add-to-startd=http,deploy
Then copy your war, if you use ROOT.war, it will map to /, and start jetty:
cp my.war webapps/ROOT.war
java -jar $JETTY_HOME/start.jar
Alternatively, if you have docker installed, you can also get the official setup by copying it, like so:
First, have docker download and run jetty, map a directory on the host to the docker container. I was already mapping webapps, so I just continued to use that mapping. This removes the container when done (-rm) so it's clean and it starts an interactive bash shell, logging you right into an official barebones jetty container that is ready to deploy wars plopped into the webapps directory (just what we want!)
sudo docker run --rm -it -v /home/myuser/jetty/webapps:/var/lib/jetty/webapps jetty:latest /bin/bash
If you run and env on the container, you'll see:
JETTY_BASE=/var/lib/jetty
Just tar this base up, copy the tar to the webapps directory, which is mapped back to the localhost, and exit. (feel free to map
root#f99cc00c9c77:/var/lib# tar -czvf ../jetty-base.tar.gz .
root#f99cc00c9c77:/var/lib# cp ../jetty-base.tar.gz jetty/webapps/
root#f99cc00c9c77:/var/lib# exit
Back on the localhost, you have a tar of the official jetty base! The docker container should have stopped on exit, you can test this with sudo docker ps, which should show an empty list (just headers).
Just to finish this off, back on the host, create a base directory (as myuser, not root, of course):
mkdir ~/jetty/localbase
cp ~/jetty/jetty-base.tar.gz ~/jetty/localbase/
cd ~/jetty/localbase/
tar xvzf jetty-base.tar.gz
Then start it up like before:
java -jar $JETTY_HOME/start.jar

Using Tomcat 7 on Ubuntu 12.10 --- How To?

I am a newbie to Ubuntu 12.10 and moved to it from Windows.
In Windows I have configured the environment variable to include servlet-api.jar in the CLASSPATH variable so I do not have to type -cp <path to servlet-api.jar> every time I compile.
For normal Java programs, I have set the JAVA_HOME using:
export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386" >> ~/.bashrc
With that solved, I need to set the CLASSPATH but do not know how to.
Please help on that!
Also I read on some forums that I should change the CLASSPATH manually each time I compile because changing the CLASSPATH may upset other applications.
Next, I tried this command to start Tomcat
service tomcat7 start but I got an error :
You need root privileges to run this script I do not know why. Help me solve that!
And if within the scope, please tell me how the directory structure of tomcat in Windows differs from that in Ubuntu.
Misc
I used sudo apt-get install tomcat7 to get Tomcat 7.
You don't say whether you're using an Ubuntu Tomcat package or a standalone Tomcat installation.
For a standalone Tomcat, you will be starting and stopping it using the scripts in its bin directory: startup.sh and shutdown.sh. In that case, Tomcat will use the environment variable values set at the time of invocation.
When doing this, you can set the environment variables prior to running these commands.
The same is true for the Tomcat 6 or Tomcat 7 that Ubuntu distributes as packages.
These neatly separate the $CATALINA_HOME directory (where the Tomcat 7 distribution is supposed to be) from the $CATALINA_BASE directory (where all of the changes and additions for a particular Tomcat instance are supposed to be. For the tomcat7 package, the first directory is /usr/share/tomcat7, while the second is /var/lib/tomcat7.
You can find startup.sh and shutdown.sh in /usr/share/tomcat7/bin and of course you can use them.
However, if you use the Tomcat provided with Ubuntu, you're probably going to want to run it just like any other standard system service, and the packages support this: e.g. in the case of tomcat7, you can just use
sudo service tomcat7 status
sudo service tomcat7 start
sudo service tomcat7 stop
sudo service tomcat7 restart
just like you can for any system service, and it uses the same mechanism as other system services do:
the shell script /etc/init.d/tomcat7 is used to start and stop Tomcat 7
/etc/default/tomcat7 is its configuration file, allowing a few parameters to be set
Using this method, you cannot specify additional environment variables for Tomcat to use by setting them in your shell or in /etc/default/tomcat7; they will not be passed to Tomcat.
However, there is a second method to set environment variables: if you set them in the shell script /var/lib/tomcat7/bin/setenv.sh (or, if you must, /usr/share/tomcat7/bin/setenv.sh), they will be picked up and passed to Tomcat. This method always works.
Finally, Tomcat offers finer control than just using a $CLASSPATH when it comes to specifying additional classes or JARs to be loaded on startup: in its conf/catalina.properties configuration file, you can set the common.loader, server.loader and shared.loader to a list of directories and/or JAR files to be loaded in additional to the standard ones.
All it means is that you should do that as a superuser - which translates to administrator in windows so you should try something like sudo service tomcat7 start
Try export CLASSPATH=/usr/share/tomcat7/lib/servlet-api.jar:/usr/share/tomcat7/lib/jsp-api.jar - This will depend on where your tomcat installation in located.
You can get more here
Cheers

Categories

Resources