Optimize a Website for Speed, Security, and Easy ManagementOctober 18th, 2008
« SEO Secrets of AskApache Part 2.htaccess trick to show Alternate CSS file based on IP »
Over the past 10 or so years I’ve been directly or indirectly involved in configuring/administrating/hacking thousands of websites, and I realized today that I’ve actually learned quite a bit about how to really make them work hard for me, instead of the other way around. It came as a mild shock to think of where I was back then vs. now because the improvements and optimizations are hundreds of smaller improvements, but taken together, the optimization hacks I’ve found through trial and error and much reading are as Donald would say, YOOUUGE compared to a basic website setup.
I use this awesome skeleton setup for all my high-paying clients sorry poor people! and also of course on this blog, which I use as a bleeding-edge dev server for my crazy testing. So realize that I’m already past this setup and using it to do cooler stuff. In order for you to use these more advanced ideas, you first need to get up to speed on what I’m doing so you know what I’m talking about. This article tries to help you accomplish that… remains to be seen.
This first article is to give you some ideas and get you thinking and reading before the first article in this series comes out. This series details how to setup, configure, secure, optimize, and manage a website the best possible way I can come up with. It pieces together all the AskApache hacks and tricks and uses methods and ideas discussed all over this blog and all over the net and glues them all together to show you how to have the most optimized, fastest, best website setup I can think of.
Knowing the why and how behind the operation of a Web Server allows us to optimize that operation. For this example we will be creating the website www.askapache.com, which will be running WordPress and php. We will also set up static.askapache.com to serve all of our sites uploads, images, css and javascript files, flash files, etc. with advanced caching and security using Apache Server .htaccess files. So lets get started and take a look at this site structure for a moment.
/home/askapache.com |-- /home/askapache.com/backups/ |-- /home/askapache.com/htdocs/ |-- /home/askapache.com/inc/ |-- /home/askapache.com/logs/ |-- /home/askapache.com/static/ |-- /home/askapache.com/tmp/ |-- /home/askapache.com/.htpasswd-basic `-- /home/askapache.com/.htpasswd-digest
/backups/ – For encrypted backups of WordPress database and site files. And any other backups./htdocs/ – The document root for www.askapache.com/inc/ – Folder to keep your php include files for extra security and easy management./logs/ – Save your php, apache, and other logs here or create symlinks to them./static/ – The document root for static.askapache.com/tmp/ – Only need this if your host doesn’t already have a /tmp folder
Simply by implementing correct access permissions, file permissions, password protection and segmenting various folders and services we are already ahead of the game. I’ve always taken security extremely seriously, so you can benefit from alot of the simple solutions I’m recommending for a really locked down site.
Indeed, security is a major part of every step of this setup process, as security concerns are what drives a lot of the motivations I have for coming up with this setup in the first place. We will be doing very simple but very effective site security like the following items, which is a short list compared to everything we will be doing.
My past work for an Internet Service Provider, followed by 4 years of auditing the security of organizations external/internal networks has given me a fresh perspective on website security, and I think it allows me to see what would really be effective at preventing and killing attacks. In fact just last night I was once again doing some research into some off-the-wall security topics, and I discovered a new defense method that I will be writing about very soon. I believe that this new method, could be quickly adopted and implemented by hosting providers and software developers, which would result in us finally taking the Internet back from all those zombies and robots. This method will be discussed in great detail soon, and will be a core part of this site setups security and optimization.
Serve’s files as fast and efficiently as possible using advanced caching, HTTP Protocols, php/server configurations.
Many of the articles and research on this blog is about improving the speed and efficiency of your website. In fact that is why I am helping develop open-source software to block spammers from WordPress blogs… not because I’m bothered by the spam, but because they make the net slow! So lets look at some of the ideas we’ll be implementing.
Many techniques I’ve been using and tweaking for several years, and recently many of them were included in the high-performance websites list. Of course we will be taking a look at this list in practical terms, meaning almost all of it, the caching, compression, etc., will be automated in keeping with our “comfort” goal, which is to say we want to make the Web Developer and Server Admin’s lives as easy and comfy as possible. After all, we do the work right?
Ok it might not be the #1, but surely the top 10.. ;)
I’m very proud of the performance I am able to achieve on this site. Very proud. I started looking for ways to improve the wp-cache and wp-super-cache WordPress plugins, and came up with hacks for both of them.. but they still didn’t do what I wanted so I started from scratch and wrote my own caching plugin.
With much more advanced caching options and unquestionably higher performance and lower time usage on the machine. I’m hesitant to release it to the public until I get faded on it.. I just really love it.. it has been running my site for several months now and I keep finding ways to improve it.. Stay tuned.
One feature it has is the ability to allow negotiation of a resource between apache and the client. Think almost transparent mutli-lingual blogs, mutliple formats per document (look at the rdf for this page for an example*). But that plugin is the future and this is the present.. so back to it we go.
This section alone would make this setup appealing. I have developed all types of techniques and methods to make my life as easy as possible. I could literally DIE right now and this blog would continue to run and operate for years on its own. The general philosophy that I have used to get to where I can goto the beach with my laptop and do all this crazy stuff is the idea of perfection. That may sound a little put-offish, but it basically means I will focus in on one very specific area for improvement or research and just get sick with it. Most of this blogs articles are enlightening examples of this in action. I will take a relatively unknown or unused piece of code or software and experiment with it until I feel I have it down, then I move on to the next item of never-ending research. Mostly I think this is just plain habit from when I was studying security. I’m much better at this then that :)
The 57 HTTP Status Codes and Apache ErrorDocuments article is a prime example. I was simply searching for an authoritative list of HTTP status codes, an issue not many web people find worth their time, and that search led to some of the most useful stuff I’ve found about the Web
This “Comfort” article will include multi-language, intelligent, and optimized error documents for handling any type of HTTP error with class and allow us to stop spammers, save bandwidth, redirect correctly, etc.. You will probably be surprised at all the uses an Apache ErrorDocument can have.. It IS one of the foundations of the HTTP-based Net after all.
The whole setup is geared to make hassle-free WordPress/PHP/application upgrades possible by keeping different types of files in separate places, keeping backups, other misc tricks and since all of these files are in /home/askapache.com, your FTP connection can still access every file easily. Sometimes security and optimizing your server can lead to it being more of a pain to do updates, backups, and general maintenance. This article tries to overwhelm the balance with a trifecta of goals.
Comfort to me also means being able to pack up the whole website and database and move to another web host in under an hour. I can move the whole AskApache site to one of several other hosting providers accounts I have in about 30minutes. If this was a clients site or I was getting paid more, I’d also be focused on round-robin DNS technology, balance-load setups, and just go crazy making it fast.
Ever since I started sharing information and software to stop all these resource hogging zombies attacking everything I’ve been attacked several times. Normally I get over 10K exploit attempts or requests per day, which I pretty much block 100%. But a few times they’ve actually tried to DDOS me off the net in a distributed attack. I have implemented several “poor mans” techniques to put up your best effort at surviving, which I did. Basically you want to configure your server to KILL connections just as fast as possible and prevent your server resources from skyrocketing and surpassing your quotas. A skilled attacker could easily shut you down even without the use of a widespread botnet if they are clever, which could be devastating to your small blog or site if it goes down at a crucial instant.
I used to work with a guy who did alot of the coldfusion programming for us, and I used to cringe every time I was called in to upgrade a site or do a re-design. Files and folders EVERYWHERE! Literally images in every folder, multiple index.html, index1.html, index-old.html, and on and on it went.. It would take me hours just to reverse-engineer the site enough so I could modify files on it without having some unkown consequence happen.
Everyone has this problem, what I do all the time is just grab everything on my desktop and put it in a folder named with the date. Then the process repeats itself and invariably a few months later I’m looking at a cluttered screen again.
This absolutely is the worst thing that can happen to a website, worst for security, comfort for webmaster, and speed. So this setup addresses that issue completely heads on. With all the different pages, tools, and resources available on this blog, I can almost promise you that my site has less files than yours. No small feat to be sure, but worth every second I spent researching how to do it now that its on and popping.
All hosts are different, but any host worth their salt is running some kind of BSD/Linux operating system, and that is good news because those operating systems all use very similarly excellent file/folder structures with huge organization systems. If your web hosting provider is running on a Windows based operating system or other locked/proprietary software than this article is not for you and I would recommend switching hosts to a BSD/Linux open-source operating system.
First you set your website up so it can start serving.
This is a very cool method I’ve been using more and more frequently because it makes updates, upgrades, and changes so much easier to manage. And segmenting various parts of the site is smart security, and even smarter in the way of speeding up a website and keeping your servers running mean and lean.
/home/askapache.com |-- /home/askapache.com/backups/ |-- /home/askapache.com/htdocs/ | |-- /home/askapache.com/htdocs/about/ | |-- /home/askapache.com/htdocs/admin/ | |-- /home/askapache.com/htdocs/cgi-bin/ | |-- /home/askapache.com/htdocs/.htaccess | |-- /home/askapache.com/htdocs/index.php | `-- /home/askapache.com/htdocs/robots.txt |-- /home/askapache.com/inc/ | |-- /home/askapache.com/inc/config.inc.php | `-- /home/askapache.com/inc/settings.inc.php |-- /home/askapache.com/logs/ | |-- /home/askapache.com/logs/access.log | |-- /home/askapache.com/logs/error.log | |-- /home/askapache.com/logs/logins.log | |-- /home/askapache.com/logs/modsec_audit.log | |-- /home/askapache.com/logs/modsec_debug.log | `-- /home/askapache.com/logs/php_error.log |-- /home/askapache.com/static/ | |-- /home/askapache.com/static/css/ | |-- /home/askapache.com/static/flv/ | |-- /home/askapache.com/static/img/ | |-- /home/askapache.com/static/js/ | |-- /home/askapache.com/static/mp3/ | |-- /home/askapache.com/static/pdf/ | |-- /home/askapache.com/static/swf/ | |-- /home/askapache.com/static/.htaccess | |-- /home/askapache.com/static/index.html | `-- /home/askapache.com/static/robots.txt |-- /home/askapache.com/tmp/ |-- /home/askapache.com/.htpasswd-basic `-- /home/askapache.com/.htpasswd-digest
/home/askapache.com |-- /home/askapache.com/backups/ |-- /home/askapache.com/htdocs/ | |-- /home/askapache.com/htdocs/about/ | | `-- /home/askapache.com/htdocs/about/index.html | |-- /home/askapache.com/htdocs/admin/ | | |-- /home/askapache.com/htdocs/admin/.htaccess | | `-- /home/askapache.com/htdocs/admin/index.html | |-- /home/askapache.com/htdocs/cgi-bin/ | | |-- /home/askapache.com/htdocs/cgi-bin/bin/ | | | |-- /home/askapache.com/htdocs/cgi-bin/bin/.htaccess | | | |-- /home/askapache.com/htdocs/cgi-bin/bin/php.cgi* | | | |-- /home/askapache.com/htdocs/cgi-bin/bin/php.ini | | | |-- /home/askapache.com/htdocs/cgi-bin/bin/php4.cgi* | | | `-- /home/askapache.com/htdocs/cgi-bin/bin/php5.cgi* | | |-- /home/askapache.com/htdocs/cgi-bin/private/ | | | |-- /home/askapache.com/htdocs/cgi-bin/private/.htaccess | | | |-- /home/askapache.com/htdocs/cgi-bin/private/debug.php | | | `-- /home/askapache.com/htdocs/cgi-bin/private/stats.php | | |-- /home/askapache.com/htdocs/cgi-bin/.htaccess | | |-- /home/askapache.com/htdocs/cgi-bin/login.php | | |-- /home/askapache.com/htdocs/cgi-bin/printenv.cgi* | | `-- /home/askapache.com/htdocs/cgi-bin/redir.cgi* | |-- /home/askapache.com/htdocs/.htaccess | |-- /home/askapache.com/htdocs/index.php | `-- /home/askapache.com/htdocs/robots.txt |-- /home/askapache.com/inc/ | |-- /home/askapache.com/inc/config.php | `-- /home/askapache.com/inc/functions.php |-- /home/askapache.com/logs/ | |-- /home/askapache.com/logs/access.log | |-- /home/askapache.com/logs/error.log | |-- /home/askapache.com/logs/logins.log | |-- /home/askapache.com/logs/modsec_audit.log | |-- /home/askapache.com/logs/modsec_debug.log | `-- /home/askapache.com/logs/php_error.log |-- /home/askapache.com/static/ | |-- /home/askapache.com/static/css/ | | `-- /home/askapache.com/static/css/apache.css | |-- /home/askapache.com/static/flv/ | | `-- /home/askapache.com/static/flv/apache.flv | |-- /home/askapache.com/static/img/ | | |-- /home/askapache.com/static/img/apache.gif | | |-- /home/askapache.com/static/img/apache.jpg | | `-- /home/askapache.com/static/img/apache.png | |-- /home/askapache.com/static/js/ | | `-- /home/askapache.com/static/js/apache.js | |-- /home/askapache.com/static/mp3/ | | `-- /home/askapache.com/static/mp3/apache.mp3 | |-- /home/askapache.com/static/pdf/ | | `-- /home/askapache.com/static/pdf/apache.pdf | |-- /home/askapache.com/static/swf/ | | `-- /home/askapache.com/static/swf/apache.swf | |-- /home/askapache.com/static/.htaccess | |-- /home/askapache.com/static/index.html | `-- /home/askapache.com/static/robots.txt |-- /home/askapache.com/tmp/ |-- /home/askapache.com/.htpasswd-basic `-- /home/askapache.com/.htpasswd-digest
The buzz about apache and open-source is very real, apache is becoming more of a discussed topic as people realize the power and importance of Doing it and Doing it and Doing it well.
– Movable Type Apache Installs made easy, Checking out Apache Web logs, Securing Wordpress with .htaccess, WordPress Permalinks and .htaccess, New search and replace module for apache!, creating an .htaccess template, .htaccess allow directive
Check back in a week for the first article, or better yet subscribe to my rss feed or use the comment form below to get notified.
« SEO Secrets of AskApache Part 2.htaccess trick to show Alternate CSS file based on IP »
Tags: Apache, Cache, compression, hosting, htaccess, optimization, Security, server
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
It's very simple -
you read the protocol
and write the code.
-Bill Joy
HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | WAI | DISA | ICSI | GIAC | SANS RR | GHOST
Authority: 110 ↑ TOPExcept where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed according to these terms. "Apache" is a trademark of The ASF.
Thank you for sharing some of your knowledge and experience.
This is the 1st page I came across when searching for a tutorial on how to re-use downloaded files in the client browser cache.
Seems you have a lot of reading gems to chew through…please keep ‘em coming.
Great enlightening post..thanks a ton
Hi, first of all great articles and super information! Thank you for all the hard work ;)
Thanks again
Pawel
Good information.. thanks!