Tomcat stuff

How to connect apache and tomcat

mod_proxy_ajp is an Apache module which can be used to forward a client HTTP request to an internal Tomcat application server using the AJP protocol. Make sure this line is in the httpd.conf:

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

Add this info into a VirtualHost:

Listen 1989
NameVirtualHost *:1989

ServerName localhost
ErrorLog /var/log/apache2/ajp.error.log
CustomLog /var/log/apache2/ajp.log combined

AddDefaultCharset Off
Order deny,allow
Allow from all

ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

Make sure that Tomcat is listening on port 8009 (will show as java listening):

netstat -ntlp

should show a line like:

tcp        0      0 :::8009                     :::*                        LISTEN      1329/java

A web client will now be able to connect through HTTP to http://IPADDDR:1989/, the mod_proxy_ajp will forward your request transparently using the AJP protocol to the tomcat application server on port 8009. Remember to open 1989 on the firewall if appropriate.

At this point, the servlet container is ready to start processing the request. Tomcat can send the following messages back to the web server:

SEND_HEADERS
Send a set of headers back to the browser.
SEND_BODY_CHUNK
Send a chunk of body data back to the browser.
GET_BODY_CHUNK
Get further data from the request if it hasn’t all been transferred yet. This is necessary because the packets have a fixed maximum size and arbitrary amounts of data can be included the body of a request (for uploaded files, for example). (Note: this is unrelated to HTTP chunked transfer).
END_RESPONSE
Finish the request-handling cycle.

How to deploy a war file

Detemine webapps location
Drop sample.war in that location (sample war file is available at http://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/sample.war )
Check that it deploys by checking for a directory call sample in your webapps directory
Browse to http://ServerIP:8080/sample
Should see ‘Sample “Hello, World” Application’

Note: Any web application archive file within the application base (appBase) directory that does not have a corresponding directory of the same name (without the “.war” extension) will be automatically expanded, unless the unpackWARs property is set to false. If you redeploy an updated WAR file, be sure to delete the expanded directory when restarting Tomcat, so that the updated WAR file will be re-expanded (note that the auto deployer, if enabled, will automatically expand the updated WAR file once the previously expanded directory is removed).

Troubleshooting

Tomcat logs to $TOMCAT_HOME/logs/catalina.out

How to increase java heap size

If you are experiencing performance issues with Tomcat, a common cause is the lack of JVM (Java Virtual Machine) memory allocation. The default setting of the maximum heap size is 64MB or 128MB. You can increase the maximum heap size of applications by setting the -Xmx JVM parameter.

For example -Xmx512m allows maximum 512MB heap to be allocated for the JVM.
Check both CATALINA_OPTS and JAVA_OPTS environment variables. Set the environment variables to a higher value. For example if the JAVA_OPTS variable is containing a minimum of 64MB and 128MB maximum heap size, increase it to 128MB and 256MB values.

OLD: JAVA_OPTS=”-Xms64m -Xmx128m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000″

OLD: JAVA_OPTS=”-Xms128m -Xmx256m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000″

Make sure not to use so high a value that exceeds your physical RAM’s size – otherwise it will be paged to the harddisk which could cause more performance issues.

How to rotate Tomcat’s catalina.out log

Create:
/etc/logrotate.d/tomcat

/var/log/tomcat/catalina.out {
copytruncate
daily
rotate 7
compress
missingok
size 5M
}

Run the following command to run the cron job manually

/usr/sbin/logrotate /etc/logrotate.conf

Tomcat uses Log4j for everything else, but catalina.out gets a copy of everything printed to stdout and stderr.

Posted in Linux | Comments Off on Tomcat stuff

Making files only executable locally

Add the following to your .htaccess file to prevent files from being accessible from external sites:


Order deny,allow
Deny from all
Allow from localhost 127.0.0.0/8 ::1 externalIP/32

Posted in Linux | Comments Off on Making files only executable locally

ReSync MySQL Master Slave

At the master:

RESET MASTER;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

And copy the values of the result of the last command somewhere.

Wihtout closing the connection to the client (because it would release the read lock) issue the command to get a dump of the master:

mysqldump -uroot -p –all-database > /a/path/mysqldump.sql

Now you can release the lock, even if the dump hasn’t end. To do it perform the following command in the mysql client:

UNLOCK TABLES;

Now copy the dump file to the slave using scp or your preferred tool.

At the slave:

Open a connection to mysql and type:

STOP SLAVE;

Load master’s data dump with this console command:

mysql -uroot -p < mysqldump.sql

Sync slave and master logs:

RESET SLAVE;
CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000001′, MASTER_LOG_POS=98;

Where the values of the above fields are the ones you got from the folllowing:

zcat dumpfile.sql.gz | head −30 | grep -i change

Finally type

START SLAVE;

And to check that everything is working again, if you type

SHOW SLAVE STATUS;

Posted in Linux, MySQL | Comments Off on ReSync MySQL Master Slave

Unique hits to apache site

cat access.log | awk ‘{print $1}’ | sort | uniq -c | sort -g

Posted in Linux | Comments Off on Unique hits to apache site

mysqlbinlog command

The “mysqlbinlog” command reads the binary logs and outputs them in SQL format. As a result, you can use it to create a database dump and then modify that file by hand. For example, let’s say that everything since April 10th, 10AM server time needs to be restored (leaving out one statement). We would first use this command:

mysqlbinlog –start-datetime=”2013-04-10 10:0:00″ /var/log/mysql/bin.123456 > output.sql

to create the database dump. After this file is created, we can delete the offending statement that we would not want re-run. Alternately, if you know the specific binary log position, you can use the –start-position flag. Here are some articles on this process:

http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html
http://dev.mysql.com/doc/refman/5.5/en/point-in-time-recovery-times.html
http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery-positions.html

Posted in Linux | Comments Off on mysqlbinlog command

To prevent an apt package from auto updating

Using apt

you can hold a package using

sudo apt-mark hold package_name

and remove the hold with

sudo apt-mark unhold package_name

Posted in Linux | Comments Off on To prevent an apt package from auto updating

Preventing access to a website from specific IP addresses with Deny/Allow

Configure the Virtual Host as follows:


Order Deny,Allow
Deny from all
Allow from 11.211.0.0/15
Allow from 12.212.0.0/15
Require valid-user
Satisfy all
AuthName “Restricted Area”
AuthType Basic
AuthUserFile /home/web/.htpasswd
Require valid-user

Create file with the following command:

htpaddwd /home/web/.htpasswd

Another option:

1. Type the following text into your VirtualHost file:

Order Allow,Deny
Allow from all
Deny from [Enter IP address here]

2. Save the file.

Alternatively, you may specify the directory by using:

Order Allow,Deny
Allow from all
Deny from [Enter IP address here]

Posted in Linux | Comments Off on Preventing access to a website from specific IP addresses with Deny/Allow

Password Protecting a web site

Add the following to .htaccess:

CentOS:

AuthType Basic
AuthName “Restricted”
AuthUserFile /etc/httpd/htaccess-pass
Require valid-user
#Order deny,allow
#Deny from All
#Satisfy any

Create the file as follows:

htpasswd /etc/httpd/htaccess-pass username
You will then be prompted for a password

Ubuntu:

AuthType Basic
AuthName “Restricted”
AuthUserFile /etc/apache2/htaccess-pass
Require valid-user
#Order deny,allow
#Deny from All
#Satisfy any

Create the file as follows:

htpasswd /etc/apache2/htaccess-pass username
You will then be prompted for a password

 

Posted in Linux | Comments Off on Password Protecting a web site

Useful netstat commands

To show which IP addresses are currently connected to your server:
netstat -nt

Posted in Linux | Comments Off on Useful netstat commands

Connecting to an external MySQL server through an SSH tunnel

Scenario: You’re at home, and you want to connect to a mysql server on the other side of a firewall. There is a machine with ssh open on it that you can use as a gateway.

  1. On your home machine:
    ssh -L 3307:domain.name.of.mysqlserver:3306 username@domain.name.of.gatewayserver

     

    This will open a tunnel, listening on localhost:3307 and forwarding everything to mysqlserver:3306, and doing it all via the ssh service on the gateway machine.

    This example shows us specifying port 3307 on the local end of the tunnel; I did this because I run a MySQL server on my home machine, so I can’t re-use the default MySQL port.

    You’ll now have a terminal open on the gateway machine, but you don’t need it for this procedure, so set it aside.

     

  2. Now, on your local machine, execute a mysql connection like so:
    mysql -u username -p -h 127.0.0.1 -P 3307 databasename

    In other words, mysql thinks it’s connecting to localhost, but on a different port. In fact, the connection is being made securely to the remote mysql server, via the gateway machine and the local “mouth” of the ssh tunnel on your own machine. 

  3. When you’re finished with your mysql session, log out of the session on the gateway machine. That will properly close the tunnel.
Posted in Linux, SSH | Comments Off on Connecting to an external MySQL server through an SSH tunnel