How to Fix PHP 8.2 Fileinfo Installation Failure on Baota (BT) Panel
1. Problem Background
Server Environment
| Item | Configuration |
|---|---|
| Operating System | Ubuntu 22.04 |
| CPU | 2 cores |
| Memory | Approximately 2 GiB |
| PHP | PHP 8.2.x |
| PHP Build Type | NTS |
| Management Panel | Baota (BT) Panel |
| PHP Installation Directory | /www/server/php/82 |
| Fileinfo Source Directory | /www/server/php/82/src/ext/fileinfo |
The server has limited memory. When installing the PHP Fileinfo extension directly through the Baota panel, the compilation process fails.
2. Error Messages
During the Fileinfo installation process in Baota, compilation gets stuck at:
libmagic/apprentice.c
The following error may then appear:
cc: fatal error: Killed signal terminated program cc1
compilation terminated.
Eventually, the following error occurs:
make: *** [Makefile:xxx: libmagic/apprentice.lo] Error 1
Alternatively, Baota may simply report that the Fileinfo installation failed.
3. Root Cause
Although this error appears to be a C compilation error, the actual problem is not necessarily an issue with the Fileinfo source code itself.
The real cause is:
Insufficient available memory during compilation, causing the Linux OOM Killer to terminate the cc1 process.
Run the following command to verify:
dmesg | grep -i -E 'killed process|out of memory|oom'
If you see output similar to:
Out of memory
Killed process xxxx (cc1)you can confirm that the failure was caused by insufficient memory.
4. Why Compilation Can Fail Even with 2 GB of RAM
Although the server is advertised as having 2 GB of RAM, the amount of memory actually available for compilation may be much smaller.
The server may simultaneously run:
Baota control panel
Nginx
PHP-FPM
MySQL
Redis
System services
PHP compilation processes
As a result, only a few hundred megabytes of memory may be available for compilation.
The Fileinfo extension internally uses libmagic.
The following source file:
libmagic/apprentice.c
can consume a considerable amount of memory during compilation.
The default Makefile contains:
CFLAGS = -std=c99 -g -O2
The -O2 option enables compiler optimizations that can increase memory consumption. When compiling apprentice.c, the peak memory usage may become too high for a low-memory server.
This can result in the following chain of events:
2G RAM
+
Swap
+
-O2 compilation optimization
=
Excessive peak memory usage by cc1
=
OOM Killer
=
cc1 terminated by Killed
5. Solution
The core solution is to reduce memory consumption during compilation and use single-threaded compilation.
Two main changes are required:
Remove
-O2from the compilation flags.Use
make -j1.
6. Step 1: Confirm the PHP 8.2 Fileinfo Source Directory
After a failed installation through Baota, the source code is usually already downloaded to:
/www/server/php/82/src/ext/fileinfo
Check the directory:
cd /www/server/php/82/src/ext/fileinfo
ls
You should normally see files and directories such as:
Makefile
Makefile.fragments
Makefile.objects
libmagic
magic.c
fileinfo.c
php_fileinfo.h
modules
...
If the source code is missing, download or prepare the Fileinfo source code through the PHP 8.2 extension installation feature in Baota.
7. Step 2: Back Up the Makefile
Before making any changes, create a backup:
cp /www/server/php/82/src/ext/fileinfo/Makefile \
/www/server/php/82/src/ext/fileinfo/Makefile.bak
This allows you to restore the original file later if necessary.
8. Step 3: Check CFLAGS
Run the following command:
grep -n "CFLAGS" /www/server/php/82/src/ext/fileinfo/Makefile
If you see:
CFLAGS = -std=c99 -g -O2
the current compilation configuration includes -O2.
9. Step 4: Remove -O2
Run the following command:
sed -i 's/-O2//g' /www/server/php/82/src/ext/fileinfo/Makefile
Check the configuration again:
grep -n "CFLAGS" /www/server/php/82/src/ext/fileinfo/Makefile
It should now look like this:
CFLAGS = -std=c99 -g
In other words, the original configuration:
CFLAGS = -std=c99 -g -O2
has been changed to:
CFLAGS = -std=c99 -g
This does not modify the PHP source code itself. It only reduces the optimization level used when compiling Fileinfo.
10. Step 5: Clean Up Previous Compilation Results
Run the following commands:
cd /www/server/php/82/src/ext/fileinfo
make clean >/dev/null 2>&1 || true
rm -rf .libs
rm -rf libmagic/.libs
rm -rf modules/*
This removes previous compilation artifacts, including .o and .lo files, that might interfere with the next compilation attempt.
Important: Only run these cleanup commands in the confirmed Fileinfo source directory. The rm -rf commands permanently remove the specified files and directories.
11. Step 6: Compile Using a Single Thread
Do not use:
make -j2
Avoid using:
make -j4
The server has only two CPU cores and approximately 2 GB of RAM.
Instead, run:
make -j1
Here, -j1 means that only one compilation job runs at a time.
Although compilation will take longer, this can significantly reduce peak memory consumption.
After successful compilation, you should see output similar to:
Build complete.
Don't forget to run 'make test'.
The following file should also be generated:
modules/fileinfo.so
12. Step 7: Install Fileinfo
After compilation succeeds, run:
make install
The output should normally contain a message similar to:
Installing shared extensions:
The extension will be installed in a directory similar to:
/www/server/php/82/lib/php/extensions/no-debug-non-zts-20220829/
Check the generated extension file:
ls -lh /www/server/php/82/lib/php/extensions/no-debug-non-zts-20220829/fileinfo.so
If fileinfo.so exists, the extension file has been generated and installed successfully.
13. Step 8: Configure PHP CLI
To check which configuration file is used by PHP 8.2 CLI, run:
/www/server/php/82/bin/php --ini
For example, the output may contain:
Loaded Configuration File:
/www/server/php/82/etc/php-cli.ini
Back up the configuration file first:
cp /www/server/php/82/etc/php-cli.ini \
/www/server/php/82/etc/php-cli.ini.bak
Then add the extension configuration:
echo 'extension=/www/server/php/82/lib/php/extensions/no-debug-non-zts-20220829/fileinfo.so' \
>> /www/server/php/82/etc/php-cli.ini
14. Verify PHP CLI
Run:
/www/server/php/82/bin/php -m | grep -i fileinfo
If the output contains:
fileinfothe extension has been loaded successfully by PHP CLI.
For further verification, run:
/www/server/php/82/bin/php -i | grep -i "fileinfo support"
The expected output is:
fileinfo support => enabled
15. Important: PHP-FPM and CLI Use Different Configurations
This is one of the most commonly overlooked details in the entire process.
PHP CLI uses:
/www/server/php/82/etc/php-cli.ini
Websites running through PHP-FPM typically use:
/www/server/php/82/etc/php.ini
Therefore, seeing fileinfo in the output of php -m does not necessarily mean that Fileinfo is enabled for your website.
16. Configure PHP-FPM
First, check whether Fileinfo is already configured:
grep -nEi '^\s*extension\s*=.*fileinfo' \
/www/server/php/82/etc/php.ini
If the command returns no results, add the following configuration:
echo 'extension=/www/server/php/82/lib/php/extensions/no-debug-non-zts-20220829/fileinfo.so' \
>> /www/server/php/82/etc/php.ini
It is recommended to back up the configuration file before modifying it:
cp /www/server/php/82/etc/php.ini \
/www/server/php/82/etc/php.ini.bak_fileinfo
17. Check for Duplicate Zip Extension Loading
During extension installation, you may encounter a warning similar to:
PHP Warning: Module "zip" is already loaded
For example, run:
grep -nEi '^\s*extension\s*=.*(fileinfo|zip)' \
/www/server/php/82/etc/php.ini
If the output shows:
1975:extension = zip.so
1978:extension = zip.so
it means zip.so is configured to load twice.
Keep only one entry.
For example, to remove the second entry:
sed -i '1978d' /www/server/php/82/etc/php.ini
Then check the configuration again:
grep -nEi '^\s*extension\s*=.*(fileinfo|zip)' \
/www/server/php/82/etc/php.ini
The result should look similar to:
1975:extension = zip.so
xxxx:extension=/www/server/php/82/lib/php/extensions/no-debug-non-zts-20220829/fileinfo.so
Note: The line number 1978 is only an example. Before deleting a line, verify the actual line numbers in your configuration file to avoid removing the wrong setting.
18. Restart PHP-FPM
After modifying the PHP-FPM configuration, you must restart PHP 8.2 for the changes to take effect.
The recommended method is through Baota:
**Baota Panel → App Store → PHP-8.2 → Service →
Comments
Post a Comment