Donnerstag, 17. Juni 2010

Change your MySQL, Apache2 and PHP character-encoding to UTF-8

Character Encoding Problems are annoying for Webdevelopers. To get rid you can use UTF-8 as your webservers character encoding. This example was testet on a Debian based system

MySQL

Edit /etc/mysql/my.cnf and find skip-external-locking and append the following lines
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init_connect = 'SET collation_connection = utf8_general_ci'
init_connect = 'SET NAMES utf8'
Now find [mysqldump] and [mysql] append this line to each of them
default-character-set=utf8

Apache2

Append to /etc/apache2/apache2.conf
AddDefaultCharset UTF-8

PHP5

Edit /etc/php5/apache2/php.ini and replace/append default_charset
default_charset = "UTF-8"
Alternatively you can add this line to your PHP root file
ini_set('default_charset', 'UTF-8');

Now restart Apache2 and Mysql
/etc/init.d/apache2 restart
/etc/init.d/mysql restart