PyBlosxom |
/Coding/php:
Geolocation with PHP
You will probably want:
apt-get install php5-geoip geoip-database
Then a command-line script is this easy:
And you might want to do something like this for a bit of web code:
'; $country = geoip_country_code_by_name($clientIP); echo $country . ' price = '; switch ($country) { case 'DE': echo "500"; break; case 'IL': echo "400"; break; case 'US': echo "100"; break; case 'CN': echo "200"; break; case 'HK': echo "300"; break; } ?>
If the geoip database provided by the geoip-database package is too old (three years already in Ubuntu Lucid, for example!) then remove it and download the database manually:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
Unzip it, and then add this line to /etc/php5/conf.d/geoip.ini:
geoip.custom_directory = /path/to/directory/containing/GeoIP.dat/
posted at: 09:55 | path: /Coding/php | permanent link to this entry
/Admin/Apache/performance:
Apache/PHP Modules to Increase Performance
Note that one can monitor progress with these changes using Google's analysis page at https://developers.google.com/speed/pagespeed/insights
One of the cheapest things you can do server-side is leverage browser caching with mod_expires[1]. Apparently[2], even if a file is already in the browser cache, the browser will still query the server on every hit to see if the file has changed. No, the file is not transferred if it has not changed, but yes, there is still a demand made of the server. Unless you turn on mod_expires, which tells the browser in advance how long the file is good for. And thereafter, until the file expires, the browser will not bother the server to check if it has changed. Other then turning it on, there is little to configure with mod_expires except possibly to specify the expiry times in your VirtualHost or in global config file in the conf.d directory, for example:
ExpiresActive on ExpiresDefault "access plus 1 hour" ExpiresByType image/jpg "access 1 week" ExpiresByType image/jpeg "access 1 week" ExpiresByType image/gif "access 1 week" ExpiresByType image/png "access 1 week" ExpiresByType text/css "access 1 week" ExpiresByType application/pdf "access 1 week" ExpiresByType text/x-javascript "access 1 week"
CacheEnable disk /
There are three commonly mentioned possibilities for PHP opcode caching: eAccelerator, APC[6], and XCache. I do not see much to choose from between them performance-wise, but APC seems to be a very native PHP project, so I will go with that:
apt-get install php-apc
The first thing you will want to do is copy apc.php into some web root and have a look. There, apparently the most important thing to check[7] is "Cache full count" which you want to keep pretty close to zero. To get there, I increased my cache size in /etc/php5/conf.d/apc.ini to:
apc.shm_size=50
Get and install your preferred version of the module from [8], ie.
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
dpkg -i mod-pagespeed-stable_current_amd64.deb
/etc/init.d/apache2 restart
[1] https://httpd.apache.org/docs/2.0/mod/mod_expires.html
[2] http://bytes.com/topic/apache/insights/740981-advanced-caching-mod_expires
[3] https://httpd.apache.org/docs/2.2/mod/mod_cache.html
[4] https://httpd.apache.org/docs/2.2/caching.html
[5] https://httpd.apache.org/docs/2.2/programs/htcacheclean.html
[6] http://php.net/apc
[7] http://php.net/manual/en/apc.configuration.php
[8] https://developers.google.com/speed/docs/mod_pagespeed/download
posted at: 05:28 | path: /Admin/Apache/performance | permanent link to this entry