Keepalives

Using Apache keepalives on your server can reduces CPU usage since it allows the same connection to be used for images, stylesheets, javascript, etc. If KeepAlive is disabled a separate connection must be made for each of those files. Creating and closing connections has an overhead and doing it for every single file wastes CPU time. The converse to this is memory usage. Keepalives can use a lot of memory.

Posted in Linux | Comments Off on Keepalives

Fixing 503 Service Unavailable error

Cause of 503 Service Unavailable Errors

The 503 Service Unavailable error is an HTTP status code that means the web site’s server is simply not available right now. Most of the time this is because the server is too busy or because there’s maintenance being performed on it. It can also mean that a backend process like Tomcat has become unavailable.

1) Retry the URL from the address bar again by clicking the reload/refresh button or pressing F5.

Note: If the 503 Service Unavailable error message appears while paying for an online purchase, be aware that multiple attempts to checkout may end up creating multiple orders – and multiple charges! Most payment systems, and some credit card companies, have protections from this kind of thing but it’s still something to be aware of.

2) Come back later. The 503 Service Unavailable error is a common error message on very popular websites when a huge increase in traffic by visitors (that’s you!) is overwhelming the servers. As more and more visitors leave the website, the chances of a successful page load for you increases.

3) Restart your router and then your computer, especially if you’re seeing the “Service Unavailable – DNS Failure” error. While the 503 error is still most likely the fault of the web site you’re visiting, it’s possible that there’s an issue with your computer’s or router’s DNS server configurations and a simple restart of both might fix the problem.

Posted in Linux | Comments Off on Fixing 503 Service Unavailable error

Troubleshooting a site that is slow

When you are trying to troubleshoot a site and see that, from a host perspective, nothing is overloaded or in trouble.

Run:
lsof -ni | grep httpd | wc -l

If that number is the same as (or about the same as) MaxClients, and nothing else looks bad, you probably need to employ some caching.

To test, increase maxCilents, and restart apache. If these newly available connections immediately get used, you have found your culprit.

Install Varnish and rejoice

Posted in Linux | Comments Off on Troubleshooting a site that is slow

chkconfig or update-rc.d

TO ensure that a service does not start at reboot, use the following tools:

chkconfig servicename off

or ubuntu:

update-rc.d servicename disable

Posted in Linux | Comments Off on chkconfig or update-rc.d

Changing a user’s default group

Pick the appropriate one for your operating system:

usermod -g www-data -G username username
usermod -g apache -G username username

Posted in Linux | Comments Off on Changing a user’s default group

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