Optimizing Server Performance, Part 3
April 3, 2008
Part 3 of 3: Part 1 | Part 2 | Part 3
This is part 3 in a series of posts covering server performance optimization. You should try to follow these tips in order; i.e. start at the top of post one and work you way through each post. If you’re coming here from anywhere other than Part 1 of this post, head there first before continuing on.
Most of the optimization tips presented herein will provide little incremental value. We’ve now reached the point of diminishing returns on our server tweaks.
Install Post Query Accelerator
Important: if you are using WordPress v2.1 or above, skip this step. This tweak is already contained in newer versions of WordPress.
The Post Query Accelerator plug-in improves your server’s perform-ance by ensuring that the MySQL query cache is able to cache query requests for posts.
- Download the Post Query Accelerator plug-in.
- Upload and extract it in your plug-ins folder.
- Activate it in your WordPress administrative panel.
Edit Your Theme or Plug-ins
- Move comments to a separate page from your posts.
- Paginate comments.
- Optimize your plug-ins. Some are poorly written unfortu-nately.
- Optimize your themes: many themes query for information that is static in nature. If it’s static, it should be treated as such, and be hard-coded. Note that this tip would have a huge im-pact if we hadn’t already taken care of caching from the start.
Compress JavaScript and CSS
Use a CSS compressor to remove white space and comments. Try this one at Arantius, or the one at CSS Drive.
Use a JavaScript obfuscator to remove white space and comments, and shorten method and variable names. Or minify your code with jsMin. The space saved is a bit less than with obfuscation, but it could lead to fewer debugging problems down the road.
Place JavaScript at the Bottom of the Page
While a script is downloading, it will block downloading of other page components, even if those components are on different servers. Moving scripts to the bottom of the page alleviates this problem, but obviously not every script can or should be moved to the page bottom.
Additional Security
This is not an optimization technique. In fact, using this technique will hurt performance on your server (not for everyone; just for you when you are using your administrative panel). However, if you are beginning to receive high traffic, it might be a wise idea to harden the security on your WordPress installation.
The Admin SSL plug-in for WordPress will secure admin and login pages via SSL. You will need access to your own or a shared SSL certificate. Installation and usage instructions can be found on the developer’s site.
Use eAccelerator
“eAccelerator is a free open-source PHP accelerator, optimizer, and dynamic content cache. It increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compil-ing is almost completely eliminated. It also optimizes scripts to speed up their execution. eAccelerator typically reduces server load and increases the speed of your PHP code by 1-10 times.”
Tune your MySQL Database
MySQL performance tuning is a bit of a mystical art, and conflicting advice is common. Use these settings at your own risk, and only use them on a capable machine:
- connect_timeout = 5
- join_buffer_size = 1M
- key_buffer_size=64M
- max_allowed_packet = 16M
- max_connect_errors = 20
- max_connections = 500
- max_heap_table_size = 128M
- myisam_sort_buffer_size = 64M
- read_buffer_size = 1M
- read_rnd_buffer_size = 2M
- query_cache_limit = 8M
- query_cache_size =128M
- query_cache_type = 1
- sort_buffer_size = 16M
- table_cache = 512
- thread_cache_size = 256
- tmp_table_size = 64M
- wait_timeout = 14400
Tune Apache
IBM’s developerWorks is always a great source of information, including this article on Apache tuning.
Conclusion
Preparing for an expected traffic spike or responding to a traffic spike is a great problem to have, and you’re in good company. I hope these tips get you our or keep you from getting into a jam. If you have any additional tips, please post in the comments. I’m all ears.
Part 3 of 3: Part 1 | Part 2 | Part 3
Apache and Tomcat via Cpanel - Servlet Display Problems
November 7, 2007
Problem
Apache Web Server was not passing servlet requests to Apache Tomcat. Instead it served 404 errors, even though the Apache Tomcat Connector (JK 1.2, mod_jk) was auto-configured by the WHM / Cpanel installation.
Other Possible Descriptions of the Problem
- Jsp’s work in Tomcat, but servlets do not
- Apache Http Server won’t pass servlet requests to Tomcat
- Tomcat problems using the Cpanel plugin
- Virtual host configuration problem with Cpanel Tomcat
- Apache not recognizing servlets
- Servlets can’t be accessed through Apache
In the latest Cpanel Release (11.15.0-RELEASE 17853), Tomcat support has been integrated. Prior to this (I’m not sure for how long), Tomcat was available via a beta plug-in. I experienced this problem with both the beta plug-in and the integrated support.
What’s Happening
Apache Http Server accepts all web requests and determines which are requests for static content, and which requests should be forwarded to Tomcat.
Apache correctly serves static content, and correctly passes all requests for .jsp pages to Tomcat. But when a servlet is requested, e.g. www.myserver.com/myapp/myservlet, Apache looks for the “/myapp/myservlet” directory, and finding none, spits out a 404 error.
How to Resolve the Problem
I tried several things that I thought should work but did not, though I don’t know if it was due to my specific configuration or because they were just the wrong things to do. What finally solved the problem was just adding an .htaccess file to the root of the web application with the following lines:
SetHandler jakarta-servlet
SetEnv JK_WORKER_NAME ajp13
This forces Apache to forward all requests to resources within this context to Tomcat for processing, specifically to worker ajp13. Ajp13 is one of the default workers set up, and is defined (on my system) in /usr/local/jakarta/tomcat/conf/workers.properties.
Other things that I thought should work but didn’t (your mileage may vary):
1) In /etc/httpd/conf/jk.conf (if your httpd.conf file includes jk.conf),
add/edit the switch “+ForwardDirectories.” Normally, if Apache runs
across a directory it doesn’t recognize, it will spit out a 404. This
switch says to forward those requests to Tomcat, and let Tomcat spit
out a 404 if it can’t fulfill the request.
2) In /etc/httpd/conf/jk.conf (if your httpd.conf file includes jk.conf), specifically mount each context, and unmount static content. Mounting tells Apache to pass requests to Tomcat, and unmounting tells Apache to serve the content itself. Newer versions of Tomcat are faster than Apache at serving static content, but apparently, using Apache to serve static content is safer from a security perspective.
JkMount /mywebapp/* ajp13
JkUnMount /*.gif ajp13
JkUnMount /*.jpg ajp13
A separate issue that is outside the scope of this post is whether you should use Apache Web Server to front Tomcat requests, or whether you should just have Tomcat accept requests over port 80. If you’re using a recent Tomcat version (5.5+), Tomcat can serve both static and dynamic content faster than Apache.
Use Tomcat if: 1) you’re only dealing with a single server; 2) and you’re not using any other software that requires Apache (e.g. forums or wikis written in PHP).
Use Apache to front Tomcat requests if: 1) you want to load balance across multiple servers; 2) or you want different web applications or virtual hosts to be served by different processes.
Disclaimer
I know embarrassingly little about hardware, networking, or server setup. This solution might be a hack or it might be obvious to some more familiar with the components mentioned, but it couldn’t be resolved through a dedicated server help desk or through the Cpanel help desk, so I assume there are others out there that this could help. Everything in this post is based on my limited experience with the aforementioned software and my own research. If you know of a better, cleaner way to do this, or if you know how better to describe this problem or solution, please forward to me and I’ll amend this post.
Rationale Behind this Post
I recently (as in yesterday) resolved a difficult to diagnose problem involving Cpanel, Apache Web Server, Apache Tomcat, and the Apache Tomcat Connector (JK 1.2, mod_jk2). My googling skills tend to be above average, but I could find no reference to this specific problem anywhere, and the sole purpose of this post is to hopefully save someone else the aggravation. So please disregard the keyword-heavy text — it’s altruistic in nature, I assure you.




Recent Comments