The post Write a Simple Caching Proxy Server with PHP and Memcached appeared first on Justin Silver.
]]>I recently had an issue where a plugin was trying to download remote data and then cache it to a directory, but the problem was that the directory wasn’t writable and the 500kb file was downloaded over and over again. To globally fix the issue and make sure I don’t spam people I built a quick caching proxy server for externally requested files using PHP and Memcached. You will need to have a Memcached server running and the php-pecl-memcache
extension installed. It’s a hack, so you also have to have another server, but it works best for a largish environment anyway to throttle external requests. Faster internally and you won’t flood the remote host.
On the web server trick it into thinking the remote server is local by editting /etc/hosts
and using the hostname of the server you are requesting data from – say www.remote.com
– and the internal IP address of the Apache server you will use to proxy the requests. This way when you make an outbound request it will actually go to your proxy server instead of the final destination on the Internet.
Next on the Apache server you are using as a proxy, create a VirtualHost
that is listening on the hostname of the remote server – only your servers will route here since it overrides the public DNS. You can add additional hosts here by using ServerAlias blah.whatever.com
lines.
<VirtualHost *:80> ServerName www.remote.com DocumentRoot /var/www/cacheproxy <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /cacheproxy.php [L,QSA] </IfModule> </VirtualHost>
Finally create a file under /var/www/cacheproxy/cacheproxy.php
with the following. It assumes your memcache server is on localhost and port 11211, caching for 6 hours. Adjust as you wish.
<?php class CacheProxy { private static $memcached = null; private static $memcached_host = 'localhost'; private static $memcached_port = 11211; private static $cache_ttl = 2160; // 6 hours private function __construct(){} public static function proxy(){ $host = $_SERVER['HTTP_HOST']; // We think we *are* this host, neato $path = $_SERVER['REQUEST_URI']; // Grab the URI if ( empty( $path ) ) return false; // Don't request the homepage $url = "http://$host$path"; $data = self::get_memcached()->get( $url ); // check if the path is in the cache if ( false === $data ){ // Load and cache data $data = self::get_url( $url ); self::get_memcached()->set( $url, ( ( false !== $data )? $data : '' ), self::$cache_ttl ); } echo $data; } /* fetch remote data */ private static function get_url( $url ) { $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 ); curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] ); $file = curl_exec( $ch ); curl_close( $ch ); return $file; } /* get a memcached instance */ private static function get_memcached(){ if ( empty( self::$memcached ) ){ self::$memcached = new Memcached(); self::$memcached->addServer( self::$memcached_host, self::$memcached_port ); } return self::$memcached; } } // run the proxy for this request CacheProxy::proxy();
You should now be able to proxy HTTP requests to outbound servers through this script, caching the results for performance.
The post Write a Simple Caching Proxy Server with PHP and Memcached appeared first on Justin Silver.
]]>The post Install PHP Memcached() on CentOS 5 appeared first on Justin Silver.
]]>If you want to have direct access to the Memacache() object from PHP on CentOS, the easiest way is using yum and the Remi repository.
To install the repository for various platforms, see http://blog.famillecollet.com/pages/Config-en but basically type the following:
wget http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm
The repository is not enabled by default, but you can edit it in /etc/yum.repos.d/remi.repo setting enablerepo to 1 to have it always on. Otherwise use –enablerepo=remi in your yum commands.
yum --enablerepo=remi install memcached php-pecl-memcache php-pecl-memcached
The post Install PHP Memcached() on CentOS 5 appeared first on Justin Silver.
]]>The post Memcached with MAMP 2.0.5 and Brew appeared first on Justin Silver.
]]>Requires that you have brew installed which is pretty easy, info here, but run:
/usr/bin/ruby -e "$(/usr/bin/curl -fsSL \ https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
We ultimately want to install memcached-php, and since libmemcached and memcached are both dependencies for it we can get them all at once. First we need to access the proper repository with “brew tap josegonzalez/php”
Io:/ $ brew tap josegonzalez/php Cloning into /usr/local/Library/Taps/josegonzalez-php... remote: Counting objects: 757, done. remote: Compressing objects: 100% (386/386), done. remote: Total 757 (delta 308), reused 703 (delta 267) Receiving objects: 100% (757/757), 105.33 KiB, done. Resolving deltas: 100% (308/308), done. Tapped 26 formula
Now we can access the memcached-php with “brew install memcached-php”
Io:/ $ brew install memcached-php ==> Installing memcached-php dependency: memcached ==> Downloading http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz ==> ./configure --prefix=/usr/local/Cellar/memcached/1.4.13 ==> make install ==> Caveats You can enable memcached to automatically load on login with: mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/memcached/1.4.13/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist If this is an upgrade and you already have the homebrew.mxcl.memcached.plist loaded: launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist cp /usr/local/Cellar/memcached/1.4.13/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist Or start it manually: /usr/local/bin/memcached Add "-d" to start it as a daemon. ==> Summary /usr/local/Cellar/memcached/1.4.13: 9 files, 168K, built in 5 seconds ==> Installing memcached-php dependency: libmemcached ==> Downloading http://launchpad.net/libmemcached/1.0/1.0.4/+download/libmemcached-1.0.4.tar.gz ==> ./configure --prefix=/usr/local/Cellar/libmemcached/1.0.4 ==> make install /usr/local/Cellar/libmemcached/1.0.4: 224 files, 3.1M, built in 33 seconds ==> Installing memcached-php ==> Downloading http://pecl.php.net/get/memcached-2.0.1.tgz ######################################################################## 100.0% ==> phpize ==> ./configure --prefix=/usr/local/Cellar/memcached-php/2.0.1 --with-libmemcached-dir=/usr/local/Cellar/libmemcached/1.0.4 ==> make ==> Caveats To finish installing memcached-php: * Add the following line to /usr/local/etc/php.ini: extension="/usr/local/Cellar/memcached-php/2.0.1/memcached.so" * Restart your webserver. * Write a PHP page that calls "phpinfo();" * Load it in a browser and look for the info on the memcached module. * If you see it, you have been successful! ==> Summary /usr/local/Cellar/memcached-php/2.0.1: 4 files, 100K, built in 6 seconds
The rest of the process is actually spelled out in the brew output. You can memcached manually as a daemon by running the following.
memcached -d
Alternatively you can start memcached up every time you log in by running these commands.
mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/memcached/1.4.13/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
Next we need to install the extension in MAMP, so copy the following into the section labeled “; Extensions” in your php.ini file. Mine is located at /Applications/MAMP/bin/php/php5.3.6/conf/php.ini.
extension="/usr/local/Cellar/memcached-php/2.0.1/memcached.so
Restart the Apache service via the MAMP console and you should be good to go. You can check by making a file called phpinfo.php in your root that contains ““. Loading http://localhost:PORT/phpinfo.php will have a section titled “memcached” if the module loaded successfully.
PS – don’t forget to actually start the memcached server via “memcached -d” or you won’t actually be using it even if the module is enabled :/
The post Memcached with MAMP 2.0.5 and Brew appeared first on Justin Silver.
]]>