You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu环境下使用服务账号调用Google Translate PHP库失败求助

Troubleshooting Google Translate API Failure in Laravel (Apache/AWS Environment)

Hey there, let's break down how to fix this issue—since your code works in CLI but not in your Laravel/Apache setup, the problem almost always stems from differences between the command-line and web server environments. Here are actionable steps to diagnose and resolve it:

1. Verify Credentials Access for Apache User

  • The CLI runs under your personal user account, which has access to your Google Cloud credentials file. Apache, however, runs as the www-data user on Ubuntu. Make sure:
    • Your service account key file has read permissions for www-data (run sudo chown www-data:www-data /path/to/key.json and sudo chmod 644 /path/to/key.json).
    • If you're using environment variables to point to the credentials, Apache doesn't inherit your user's env vars. Avoid relying on GOOGLE_APPLICATION_CREDENTIALS set in your .bashrc—instead, define it explicitly in your Laravel code or Apache config.
    • Double-check that your service account has the Cloud Translation API Editor IAM role (sometimes CLI uses your personal Google account with broader permissions, but the service account might lack the necessary access).

2. Fix Environment Variables for Apache

  • To set GOOGLE_APPLICATION_CREDENTIALS for Apache, edit /etc/apache2/envvars and add:
    export GOOGLE_APPLICATION_CREDENTIALS="/absolute/path/to/your-service-account-key.json"
    
    Then restart Apache with sudo service apache2 restart.
  • Alternatively, define the credentials path directly in Laravel's config/services.php using Laravel's helper functions to avoid path issues:
    'google_translate' => [
        'credentials' => base_path('storage/app/google-credentials.json'),
    ]
    
    Ensure the file in storage/app is accessible to www-data.

3. Check PHP Configuration Discrepancies

  • CLI and Apache often use separate php.ini files. Key settings to compare:
    • allow_url_fopen: Must be enabled for API calls to work.
    • Extensions like curl: Ensure they're installed and enabled for Apache's PHP.
    • To find CLI's php.ini: Run php -i | grep php.ini. For Apache, create a temporary phpinfo.php file in your Laravel public folder with <?php phpinfo(); ?> and access it via your browser to check the config path.
  • Also, test if SELinux is blocking access—run sudo setenforce 0 temporarily. If this fixes the issue, adjust SELinux policies to allow Apache to access your credentials file instead of leaving it disabled.

4. Debug Laravel-Specific Issues

  • Always check Laravel's log files (storage/logs/laravel.log) for exact error messages—this will tell you if it's a permission issue, missing credentials, or an API-related error.
  • Ensure you're initializing the Translate client correctly using Laravel's path helpers instead of hardcoding paths:
    use Google\Cloud\Translate\V2\TranslateClient;
    
    $translate = new TranslateClient([
        'credentials' => base_path('path/to/your-key.json'),
    ]);
    

5. Rule Out AWS Network/Load Balancer Issues

  • Confirm your EC2 instances have outbound internet access to reach translate.googleapis.com (port 443). Check your security groups and network ACLs to ensure outbound HTTPS traffic is allowed.
  • If you're using a proxy, make sure Apache is configured to use it (CLI might be using your user's proxy settings, but Apache doesn't inherit them by default).

内容的提问来源于stack exchange,提问作者I. Degollado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:21:46