Running a very popular and high-bandwidth website or blog? The load is killing the server? The website content is generated in real-time using PHP, .NET or CGI scripts?
You can easily increase your website performance by generating static files and serving such files much faster rather than generating files in real-time. Static files can be kept on the hard drive or in memory for blazing fast access, for example, using memcached.
Generating PHP static content
You can generate PHP output in multiple ways - from PHP code directly writing output to files or by fetching specific PHP output via HTTP protocol, or using PERL code. If you are using specific web server based values and features - you will have to fetch file via HTTP and write output to local file. This is very easy to do and doesn’t require much programming skill.
Fetching file via HTTP using WGET
wget http://www.your-domain-is-here.com/page1.php -O /var/www/page1.html
Where /var/www/page1.html is the output file.
Fetching file using CURL
curl -o /var/www/page1.html http://www.your-domain-is-here.com/page1.php
fetch -o /var/www/page1.html http://www.your-domain-is-here.com/page1.php
lynx -source http://www.your-domain-is-here.com/page1.php > /var/www/page1.html
Generating php output using a PHP script:
<?php
$files = file(”filelist.txt”);
for($i = 0; $i < count($files); $i++) {
ob_start();
include($files[$i]);
$page = ob_get_contents();
ob_end_clean();
if(strpos($files[$i], “.php”) > -1) {
$file = fopen(substr($files[$i], -4) . “.html”);
fputs($file, $page);
fclose($file);
}
}
?>
where filelist.txt is the list of the files with every file listed in new line you want it to be generated.
Perl code for fetching a remote website page. Please save the following code into the filename fetch.pl:
#!/usr/bin/perl
use LWP::Simple;
getprint “http://www.your-domain-is-here.com/page1.php“;
$content = get( $url );
print $content;
And then run it with the following Linux command line:
perl fetch.pl > outputfile.html
In the next article we will review a solution where website content is saved to memcached and content served from fast memory-based cache without generating content on the fly.