Edit application path when using AWS Elastic Beanstalk - java

With local tomcat my application url is: http://ip:port/myapp
but when deploying it to AWS Elastic Beanstalk the url becomes http://some_domain.com. I want the url to be http://some_domain.com/myapp.
How do I do that? I found something about adding some config file to .ebextensions but I wasn't sure what exactly I should do and whether it helps at all.
UPDATE:
I created an .ebextensions folder and put the the following files:
server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JasperListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Context docBase="EizeRest" path="/EizeRest" reloadable="true" source="org.eclipse.jst.jee.server:EizeRest"/>
</Host>
</Engine>
</Service>
</Server>
server-update.config
container_commands:
replace-config:
command: cp .ebextensions/server.xml /etc/tomcat7/server.xml
And when deploying I'm getting the following error:
"aws Unable to detect application deployment"
Here is a screenshot:
Another update:
I see in the events tab of the server the following error: cannot stat '.ebextensions/server.xml': No such file or directory

I believe you will need to take the following steps:
create a Tomcat server.xml file that sets path=myapp
copy this file to beanstalk during its deployment phase
Beanstalk documentation provides a relevant guide for that here:
http://aws.typepad.com/aws/2012/10/customize-elastic-beanstalk-using-configuration-files.html
Basically the guide says you need to copy the server.xml to beanstalk by creating the following file in your application root: .ebextenstions/tomcat.config:
container_commands:
replace-config:
command: cp .ebextensions/server.xml /etc/tomcat7/server.xml
don't forget to commit both the server.xml and the tomcat.config files in the .ebextensions folder.

You must place your ".ebextensions" folder at the same level as your WEB-INF folder. Doing that will remove the ".ebextensions/server.xml: No such file or directory" error.

Related

Can I have multiple folders like webapps and specify what domains to access which ROOT.war file

I am using apache apache-tomcat-8.5.6 for deploying my web applications. I have a webapps folder and a ROOT.war file under webapps and all my domains by default access this ROOT.war file.
Now I am planning to add two more folders similar to webapps, let's say folder1 and folder2 and planning to deploy ROOT.war under both these folders.
Now I want a certain domain to access ROOT.war under folder1 and another to access folder2. All other domains by default should access ROOT.war under webapps.
I believe that this is possible and might need some changes under the server.xml file. I am new to server configurations. Is there any good documentations or su
This is absolutely possible.
The key of this solution is that the Engine-Tag attribute "defaultHost" names the application-Host which is used by default and the Host-Tag attribute "name" contains the domain name which uses this application if it matches.
Example server.xml:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource
auth="Container"
description="Database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
name="UserDatabase" pathname="conf/tomcat-users.xml"
type="org.apache.catalina.UserDatabase" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" />
<Engine defaultHost="defaultHost" name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
<Host
name="defaultHost"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToDefaultApplication>" path="" />
</Host>
<Host
name="<SpecialDomain1>"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToApplication1>" path="" />
</Host>
<Host
name="<SpecialDomain2>"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToApplication2>" path="" />
</Host>
</Engine>
</Service>
</Server>

Duplication server name in URL

I have a trouble with configure of Apache and Tomcat. My purpose is to get ability to adress to tui.org/servlet, when tui.org is my virtual host configured on local machine and /servlet/ is url-pattern which is defined at web.xml. But instead the servelet I get 404 Not Found by Tomcat but I can to adress to my server by tui.org/tui.org/servlet - it works. Structure of files on my server is the following:
Root: /var/www/tui.org
-------->/WEB-INF/classes/a
-------->/WEB-INF/classes/a/MainServlet.class
-------->/WEB-INF/web.xml
-------->/index.html
How can I achieve this behaviour?
web.xml:
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
My server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="/var/www/"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
Configuration of tui.org (tui.org.conf at /etc/apache2/sites-available:
<VirtualHost *:80>
ServerName tui.org
DocumentRoot /var/www/tui.org
<Directory /var/www/tui.org>
AllowOverride All
Require all granted
</Directory>
JkMount /servlet* ajp13_worker
</VirtualHost>
I use Apache2, tomcat9 and Ubuntu 18.04.
Thank you in advance!
P.S. Sorry for my english.
There are several issues here:
You must not serve the web application directory with Apache - e.g. you're declaring DocumentRoot /var/www/tui.org for Apache and <Host appBase="/var/www/"> for Tomcat.
e.g but not limited to: The servlet spec specifically disallows to serve the directory WEB-INF to ever be served to a client, and you're explicitly bypassing this limitation.
An application deployed in a Host's appBase is always deployed under that name (unless specified differently in a context.xml file). The only other exception to that rule is if a directory is called ROOT - in that case, it's deployed without a name, thus omitting the tui.org that follows your server name.
but this is secondary to the issue of serving the whole webapp through Apache httpd. Fix that one first.
One way to fix your biggest problem is to omit the DocumentRoot directive in Apache and rather serve everything from Tomcat: JkMount * ajp13_worker.
For solving problem with adressing by tui.org/servlet I had to add <Context> directive to server.xml:
<Context path="" docBase="/var/www/tui.org/"/>
and change appBase from /var/www/ to /var/www/tui.org/.
And for fix problem with access to WEB-INF (thanks to #OlafKock) I had to add next to tui.org.conf:
<Directory /var/www/tui.org>
AllowOverride All
Deny from all
</Directory>

How to request contents from an external directory in Tomcat?

To be precise I need to get pictures from an external directory say: C:/MWDB.
I found this link: How to config Tomcat to serve images from an external folder outside webapps? which I wanna follow up with the part where you need to add a directory in your server.xml context tag.
So Basically Based on the way it says I came with that it should be like:
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/> (request: www.example.com/images/image.png) with this way while restaring the server it pops me an error saying
The server at localhost has encountered a problem
Which then I did this: <Context docBase="C:/MWDB" path="/images"/> And This I can't even access my index.html file
FULL server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="20011" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="20012" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="20013" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/></Host>
</Engine>
</Service>
</Server>
I'm using Java 8, Apache Tomcat v9.0
Okay I figured out my problem.
Basically What I didn't know is that you could have multiple <context> at the same time.
So When I had something like:
<Context docBase="C:/MWDB" path="/images"/>
I could Server from only MWDB directory and not my internal Project and that is why I couldn't access my index.html file. So For to server from both directory Simply you can do:
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/>
<Context docBase="C:/MWDB" path="/images"/>
Works Perfectly so far :)

Tomcat: status code 404 on requests of every application

Every application deployed on my Tomcat returns status code 404 on every request I make. I've tried several projects, helloworlds or skeletons and every project behaves the same as others:
Some of the projects I've used:
https://github.com/mwarman/skeleton-ws-spring-boot
https://spring.io/guides/gs/rest-service/ (I can run it with mvn spring-boot:run but it's not working as deployed to Tomcat)
https://github.com/shagstrom/spring-mvc-hibernate-skeleton
I'm using the newest Tomcat8 (8.0.27), Oracle JDK 8. I've also tried Tomcat7 with OpenJDK 7.
Used Tomcats are running on MAC and Debian.
I've build apps via mvn and via Intellij Idea.
Tomcats are clean, no configs are changed (except adding manager-gui user).
Since I've tried many different projects, I don't believe the problem is in the code. Is it in Tomcat's config? How can I get it to work?
EDIT:
server.xml code:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
If you installed tomcat successfully, you may have seen tomcat main page from browser http://localhost:8080/ URL. (Otherwise, you need to check tomcat installation first.)
I guess you may have built jar by fallowing above sites that you mentioned.
I am wondering whether you applied the jar into your tomcat or not because you mentioned same result from any other projects.
You need to apply jar or war into your tomcat in order to apply java program.
Here are links that I wish they might be helpful to you.
Packaging war : How to make war file in Eclipse
Tomcat - war file deployment : https://www.youtube.com/watch?v=9X9DA8oVodk
If tomcat returns 404 response, you have to check those points:
check if your deploy was correct
check if war bootstrap and its ServletContext initialization has no errors
check if the resource your invoking was successfully exposed.
To test these issues you have to expose:
static resource test
web service resource test
in this way you can exclude the pitfalls you can meet during application deploy

Getting mod_jk running [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am trying to get Apache to forward requests to the examples that come with Tomcat. I did it once on another machine a few months ago, but on this new server I am stump.
Tomcat is running fine, localhost:8080 loads the default homepage and localhost:8080/examples/ displays the example page. But when I drop the 8080 and use port 80 on the apache server, it simply gives me a 404.
What is really bugging me is the lack of any log info on why. I am using the stock httpd-jk.conf with this added:
JkMount /examples/* worker1
Then this is my workers.properties:
worker.list=jk-status
worker.jk-status.type=status
worker.jk-status.read_only=true
worker.list=jk-manager
worker.jk-manager.type=status
worker.list=worker1
worker.worker1.reference=worker.template
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.activation=A
worker.template.type=ajp13
worker.template.socket_connect_timeout=5000
worker.template.socket_keepalive=true
worker.template.ping_mode=A
worker.template.ping_timeout=10000
worker.template.connection_pool_minsize=0
worker.template.connection_pool_timeout=600
worker.template.reply_timeout=300000
worker.template.recovery_options=3
And the Tomcat server.xml:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
Answering with a checklist;
Are you certain port 80 is served by the Apache you expect - are your "404" responses showing in the access logs of that Apache instance
are you certain your mod_jk module is being loaded (see Apache logs, or use one of the Apache status page views to verify); if the module was not loaded, the JK configuration section would be silently ignored
are you certain your workers.properties is being found (Still, see Apache logs; you might also wish to raise the JkLogLevel in httpd.conf to trace for the time being; also, make sure you know where your JkLogFile is located)
Then about the SSL issue; mod_jk and mod_proxy_ajp should both work. If you just want basic "server-side SSL", and are not working with client certificates, then everything will be handled by your Apache httpd. If, on the other hand, you'll be doing client authentication using SSL client certificates, then I think there's another SO posting rather relevant to that use case.

Categories

Resources