16

when deploying locally to tomcat, I make this change (below) to server.xml, is there a way I can supply this to Elastic Beanstalk?

<Connector connectionTimeout="20000" port="8080" 
       protocol="org.apache.coyote.http11.Http11NioProtocol" 
       redirectPort="8443"/>'

thanks '

Marcus Wolschon
  • 2,550
  • 2
  • 22
  • 28
MikeW
  • 4,749
  • 9
  • 42
  • 83

2 Answers2

33

You can do it now without providing custom AMI. Follow instructions in: http://aws.typepad.com/aws/2012/10/customize-elastic-beanstalk-using-configuration-files.html

In order to provide custom server xml create .ebextensions folder in webapp, put there custom server.xml file and add one more file: server-update.config with content:

container_commands:
  replace-config:
  command: cp .ebextensions/server.xml /etc/tomcat7/server.xml
Maciej Walkowiak
  • 12,372
  • 59
  • 63
  • Hello, i tried to do it this way and I got the following error message: "The configuration file .ebextensions/server-update.config in application version gd377807-dirty contains invalid YAML or JSON. YAML exception: while scanning for the next token found character '\t' that cannot start any token in "", line 2, column 3: replace-config: ^ , JSON exception: Unexpected character (c) at position 0.. Update the configuration file. – TeraTon Nov 14 '13 at 14:26
  • 7
    This is because YAML does not support TAB (\t) characters at the start of the line, you must use only spaces – Sébastien Stormacq Dec 17 '13 at 07:14
  • @sebsto, Wow. I wish I more than one "Plus One" to give. – Beachhouse May 28 '14 at 01:17
  • @Maciej Walkowiak I am getting error message that replace-config failed. I had created .ebextensions in web-inf folder and added two files server.xml and server-update.config. When I check logs i can find that cannot read file at .ebextensions/server.xml what might be the problem – Vineeth NG Feb 17 '15 at 04:36
  • @MaciejWalkowiak How would I add muliple commands to the server-update.config file? – Abu Sulaiman Apr 23 '16 at 15:27
  • The easiest way would be just combine them with &&. – Maciej Walkowiak Apr 23 '16 at 17:14
  • 1
    You could also call a bash script located in your .ebextensions folder – wholenewstrain Oct 24 '16 at 22:01
19

Another way to implement this without replacing the entire Tomcat server.xml file is using the following in your .ebextensions folder (e.g. tomcat.config)

files:
  "/tmp/update_tomcat_server_xml.sh":
    owner: root
    group: root
    mode: "000755"
    content: |
      #! /bin/bash
      CONFIGURED=`grep -c '<Connector port="8080" URIEncoding="UTF-8"' /etc/tomcat7/server.xml`
      if [ $CONFIGURED = 0 ]
        then
          sed -i 's/Connector port="8080"/Connector port="8080" URIEncoding="UTF-8"/' /etc/tomcat7/server.xml
          logger -t tomcat_conf "/etc/tomcat7/server.xml updated successfully"
          exit 0
        else
          logger -t tomcat_conf "/etc/tomcat7/server.xml already updated"
          exit 0
      fi

container_commands:
  00_update_tomcat_server_xml:
    command: sh /tmp/update_tomcat_server_xml.sh

This config creates a script (files) and then runs it (container_command). The script checks the server.xml for the UIREncoding="UTF8" string and if it doesn't find it, it then adds it in using the sed command.

The nice thing about this solution is that if you upgrade your version of Tomcat (e.g. from 7 to 8) then you don't have to worry about updating the server.xml in your various WAR files.

Also, this example is for adding the UIREncoding parameter but the script is very easily adapted to add <Connector ... />' property from the original question.

bobmarksie
  • 3,282
  • 1
  • 41
  • 54
  • It really is a smart way to config URIEncoding. – Ego Slayer Oct 08 '15 at 17:49
  • Can I do this without adding an .ebextensions to my code base? I didn't have one and I have already provisioned the tomcat instance in Beanstalk. I don't want to make any changes to the code itself, if possible. – Web User Jan 14 '17 at 01:08
  • Using .ebextensions is the recommended way if you're on ElasticBeanstalk. – bobmarksie Jan 14 '17 at 13:40