Tag Archives: linux

How to automate GeoIP updates on Linux

This works for Ubuntu, Debian and CentOS distributions. You need a valid license key from MaxMind. This is available as a subscription at a nominal fee.

If you have installed GeoIP successfully, you should have the geoipupdate program. If you don’t, you need the run the following.

Install over Ubuntu or Debian:

sudo apt-get install geoip-bin geoip-database libgeoip-dev

Install over CentOS:

sudo yum install re2c geoip geoip-data geoip-devel

Both libgeoip-dev and geoip-devel are required for compiling PHP’s GeoIP.

Once you run geoipupdate the first time, it tells you about a missing GeoIP file.

root@gladlycode:~# geoipupdate 
Error opening GeoIP Configuration file /etc/GeoIP.conf

Now you copy over from the default file:

mv /etc/GeoIP.conf.default /etc/GeoIP.conf

If you now run geoipupdate, you get the following:

MD5 Digest of installed database is f5c2f7eed9331dcaa99b7a6b4633b684
Invalid UserID

Continue reading “How to automate GeoIP updates on Linux” »

How to install PHP GeoIP in CentOS

I assume you already have PEAR/PECL available. I am using the CentOS’s stock PHP. First you need to update your channels:

kahwee:~ kahwee$ sudo pear update-channels
Updating channel "doc.php.net"
Channel "doc.php.net" is up to date
Updating channel "pear.php.net"
Channel "pear.php.net" is up to date
Updating channel "pecl.php.net"
Channel "pecl.php.net" is up to date

Continue reading “How to install PHP GeoIP in CentOS” »

How to let PHP send emails

PHP not sending emails? You need a mail transfer agent (MTA). If you’re in the Ubuntu platform, you can use postfix for this:

sudo apt-get install postfix

You can read on more information here.

Previously my Contact Form 7 WordPress plugin didn’t work, it works fine after I install and configured postfix. I can’t go through the specifics of the configuration with you since yours would probably differ. I selected “Internet Site” and press the return key (Enter) all the way selecting the default options. On doing so, Contact Form 7 starts to work already.

Should you need to configure postfix again, you can reconfigure too:

sudo dpkg-reconfigure postfix

Contact Form 7 uses WP_Mail which uses PHP’s mail() function. If you do need to troubleshoot, try sending emails with mail().

If you need additional configuration, you may need to update the following:

  • Settings SMTP and smtp_port need to be set in your php.ini
  • Also, either set the sendmail_from setting in php.ini, or pass it as an additional header.

I did not have to configure anything more than postfix. I am using Ubuntu server, on nginx web server.

How to install PHP APC in CentOS

I assume you already have PEAR/PECL available. I am using the CentOS’s stock PHP. First you need to update your channels:

kahwee:~ kahwee$ sudo pear update-channels
Updating channel "doc.php.net"
Channel "doc.php.net" is up to date
Updating channel "pear.php.net"
Channel "pear.php.net" is up to date
Updating channel "pecl.php.net"
Channel "pecl.php.net" is up to date

Continue reading “How to install PHP APC in CentOS” »

How to know which processes uses the most resources

There are times where some processes use too much resources. You can use “ps aux” to get a list of all the processes that are currently running. But what about having them sorted? Here is one way. This works in Ubuntu Linux.

Top 10 % CPU usage

ps aux|sort -nr -k3|grep -v USER|head -10

Top 10 % Memory usage

ps aux|sort -nr -k4|grep -v USER|head -10

The following is the sample output for Top 10 % memory usage:

briecheese ~: ps aux|sort -nr -k4|grep -v USER|head -10
www-data  3883  2.9 14.1 107052 37176 ?        S    11:44   1:46 /usr/bin/php-cgi
www-data  3882  2.9 13.8 107664 36264 ?        S    11:44   1:46 /usr/bin/php-cgi
www-data  3881  1.2 13.2 106768 34688 ?        S    11:44   0:44 /usr/bin/php-cgi
www-data  3884  3.1 13.1 107244 34592 ?        S    11:44   1:54 /usr/bin/php-cgi
www-data  3879  1.1 11.8 106888 31160 ?        S    11:44   0:40 /usr/bin/php-cgi
www-data  3880  0.9  9.5  94232 25092 ?        S    11:44   0:34 /usr/bin/php-cgi
mysql     3659  3.5  6.9 277256 18200 ?        Sl   09:45   6:25 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
root      4103  0.2  1.0  53468  2704 ?        Ss   12:44   0:00 sshd: me [priv]
me    4106  0.0  0.8  18128  2116 pts/0    Ss   12:44   0:00 -bash
www-data  3875  0.0  0.7  57440  1972 ?        S    11:44   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf

Percentage memory usage is the 4th column. This is useful when you need a quick overview of what’s consuming the most memory. [Thanks U-Zyn]

How to repair MySQL tables

There are generally 2 ways of repairing MySQL tables using the command line. First is to use “mysqlcheck“, the other is to use “myisamchk” (only for MyISAM). This is more for my own information.

This has been tested on Ubuntu 9.10 with MySQL 5

mysqlcheck

This is how you can check if your database tables are fine:

mysqlcheck -uUsername -pSecret --all-databases

Change “Username” to your username and “Secret” to your password.

To repair them, use:

mysqlcheck -uUsername -pSecret --all-databases --auto-repair

This requires of your MySQL daemon to be running. If it isn’t running use this to start:

sudo /etc/init.d/mysql start

myisamchk

If you have trouble running ‘mysqlcheck’, you can consider using ‘myisamchk’ instead if your tables are MyISAM. This command can be performed without MySQL daemon running. This fixes the data in your file system directly and this has saved me once.

sudo myisamchk --max-record-length=1048576 -o -f /var/lib/mysql/db_name/table_name.MYI

Change “db_name” to your database name and “table_name” to the name of the table you wish to repair.