UTF-8 all the way through
Setting up a new Linux server with Apache 2, PHP 5, and MySQL 5 to support UTF-8 fully requires more than just flipping a switch in one configuration file. The most common failure mode is treating encoding as a single setting rather than a pipeline. You need consistency from the moment a character enters your application until it leaves it. If any link in that chain defaults back to ISO-8859-1 (Latin-1), you will see mojibakeâthose garbled characters like é instead of éâimmediately.
The goal is strict UTF-8 propagation. This means ensuring the browser sends data as UTF-8, PHP processes it as UTF-8, MySQL stores it as UTF-8, and Apache serves it with the correct headers. Here is how to verify and configure each layer on a fresh installation.
The HTTP Header: The First Line of Defense
Before your application even renders a page, the web server must tell the browser that the response is UTF-8 encoded. Without this, modern browsers often default to Latin-1 or Windows-1252. In Apache 2, you should explicitly set the Content-Type header to include charset=utf-8.
You can do this globally in your virtual host configuration or specifically for a directory:
<VirtualHost *:80>
DirectoryIndex index.php
<Directory /var/www/html>
Options -MultiViews
AddDefaultCharset utf-8
# Or more explicitly for specific content types if needed
Header set Content-Type "text/html; charset=utf-8"
</Directory>
</VirtualHost>
Note the AddDefaultCharset directive. This ensures that even if your PHP script doesn't specify a charset, Apache will inject it. However, relying on defaults is risky. It is best practice for your application to explicitly set this header in the response. If you are using PHP 5, add this at the very top of every page or globally via php.ini:
default_charset = "UTF-8"
PHP Session and Input Handling
PHP 5 handles input streams based on the request content-type headers and internal settings. By default, PHP assumes POST data is UTF-8 if the header says so, but it can be finicky about sessions.
If your application stores user-specific data in sessions, ensure the session encoding matches your database. While PHP sessions are usually stored as binary strings, some legacy drivers or serialization issues can cause corruption if mixed encodings exist. For a new setup, stick to standard php://file or memcached storage and rely on the database for character consistency.
Crucially, you must ensure that your HTML files themselves declare the encoding. An HTML5 document should start with:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
<!-- Content -->
</body>
</html>
Without the <meta charset="UTF-8"> tag, and without the Apache header, browsers will guess the encoding based on the first character. This guesswork is where mojibake usually originates.
Database Configuration: The Storage Layer
MySQL 5 is notorious for falling back to Latin-1 if not explicitly configured. Even if you create a table with utf8mb4 (which is superior to utf8 as it supports emojis and rare characters), the connection character set must match.
In your MySQL configuration file (my.cnf), add these settings under the [client], [mysqld], and [mysql] sections:
[client]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
[mysql]
default-character-set = utf8mb4
When creating a database and user, you must enforce this connection setting programmatically:
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON myapp.* TO 'user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
Finally, within your application code (or a bootstrap script), ensure the connection explicitly sets the charset:
$conn = new PDO('mysql:host=localhost;dbname=myapp;charset=utf8mb4', $user, $pass);
// Or for mysqli
mysqli_set_charset($conn, 'utf8mb4');
Troubleshooting Mismatches
If you still encounter issues where text looks correct in the browser but breaks when saved to the database, or vice versa, you have a mismatch between layers. A common pitfall is copying files from Windows systems that may have been saved with different line endings or hidden BOMs, though UTF-8 usually handles this gracefully.
The most frequent error is forgetting to update the default_charset in php.ini while keeping an old HTML file without the meta tag. Always check the actual HTTP response headers using tools like curl -I to see what charset Apache is actually sending before your PHP logic even runs.
By enforcing UTF-8 at every boundaryâthe HTTP header, the HTML document declaration, the PHP session context, and the MySQL storage engineâyou eliminate the ambiguity that forces systems to revert to ISO-8859-1. This disciplined approach prevents data corruption and ensures international characters render correctly across all modern browsers.
