PHP无法连接Google Cloud中的MongoDB,求排查解决方法
Hey there, let's work through why your MongoDB PHP extension isn't loading on your Google Cloud Bitnami setup. The issue likely stems from mismatched PHP environments—here's how to fix it step by step:
1. Verify You're Targeting Bitnami's PHP, Not System PHP
The sudo apt-get install mongodb command installs the extension for the system's default PHP, but Bitnami uses its own isolated PHP installation. Let's confirm where Bitnami's PHP lives and its extension directory:
- Run this command to get Bitnami PHP's extension directory:
Note down the path (it'll look something like/opt/bitnami/php/bin/php -i | grep extension_dir/opt/bitnami/php/lib/php/extensions/no-debug-non-zts-XXXXXX/).
2. Install the MongoDB Extension for Bitnami's PHP
Use Bitnami's built-in pecl tool to install the extension specifically for its PHP:
sudo /opt/bitnami/php/bin/pecl install mongodb
This will automatically place the extension file in the correct directory we found earlier.
3. Update the Correct php.ini File
The php.ini in your root directory (next to app.yaml) isn't the one Bitnami's PHP uses. Find the loaded configuration file with:
/opt/bitnami/php/bin/php --ini
Look for the line labeled Loaded Configuration File (e.g., /opt/bitnami/php/etc/php.ini). Open this file with a text editor:
sudo nano /opt/bitnami/php/etc/php.ini
Add the extension line at the end of the file:
extension=mongodb.so
Save and close the file.
4. Restart Your Web Server
For the changes to take effect, restart Bitnami's web server (Apache or Nginx, depending on your setup):
- For Apache:
sudo /opt/bitnami/ctlscript.sh restart apache - For Nginx:
sudo /opt/bitnami/ctlscript.sh restart nginx
5. Verify the Extension is Loaded
Check if the extension is now active with:
/opt/bitnami/php/bin/php -m | grep mongodb
If you see mongodb in the output, the extension is loaded. You can also check phpinfo() again—just make sure you're accessing the Bitnami PHP's info page (not the system PHP's).
Quick Note for GAE Flexible Users
If you're using Google App Engine Flexible Environment with Bitnami, double-check your app.yaml to ensure it specifies the correct PHP runtime and references the Bitnami PHP configuration if needed. For most standard Bitnami VM setups though, the steps above should resolve the issue.
内容的提问来源于stack exchange,提问作者Emirodgar

