How to fix Ubuntu PHP Call to undefined function mb_internal_encoding()

Problem:

When you see the following error message in your PHP log:

2022/12/22 12:57:20 [error] 1908680#1908680: *9092980 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function mb_internal_encoding() in /var/www/snappymail/v/0.0.0/include.php:90
Stack trace:
#0 /var/www/index.php(11): include()
#1 {main}
  thrown in /var/www/snappymail/v/0.0.0/include.php on line 90" while reading response header from upstream, client: ::ffff:77.7.108.195, server: mydomain.com, request: "GET /?admin HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php8.2-fpm.sock:", host: "mydomain.com"

Solution

The missing function mb_internal_encoding() is from the PHP mbstring module.

In order to install it, first you need to identify the PHP version running the given script. This can be done either by looking at the webserver config files for the given domain, or by looking in the error log. In this case, we can identify that the PHP version is 8.2 from upstream: "fastcgi://unix:/var/run/php/php8.2-fpm.sock:"

In case you can’t identify the correct version, I recommend installing mbstring for every installed PHP version, which you can see by using sudo dpkg --get-selections | grep php.

Given the version number, we can install the mbstring extension from the Ubuntu/Debian package sources:

sudo apt -y install php8.2-mbstring

Depending on your configuration you might also need to restart the webserver and/or PHP-FPM service. I recommend to try it out without restart, then restart the webserver and (if you are using PHP-FPM) restart the PHP-FPM for your PHP version. In case that doesn’t help, you can always reboot.

After that, the error message should have disappeared - sometimes you need to install additional PHP modules and it’s best to just watch the error log for more missing functions or other errors related to missing extension.