On ruachost.com, PHP scripts run under different environments depending on how they are executed. This means that scripts run from the command line or via cPanel cron jobs may not use the same php.ini configuration as scripts run through the web server. To ensure consistency, you can specify a custom php.ini file for cron jobs.

 

Why Use a Custom php.ini File?

  • Apply specific PHP settings (e.g., memory limits, execution time).

  • Enable directives required for cron‑based scripts (e.g., allow_url_fopen).

  • Ensure cron jobs behave the same way as web‑executed scripts.

  • Avoid conflicts between global server settings and application requirements.

 

Steps to Use a Custom php.ini File

Step 1: Create or Edit a php.ini File

  1. Log in to your hosting account via FTP, SFTP, or File Manager.

  2. Navigate to your home directory (e.g., /home/username/config/).

  3. Create or edit a php.ini file with the required directives. Example:

    memory_limit = 256M
    max_execution_time = 120
    allow_url_fopen = On
    
 

Step 2: Reference php.ini in the Cron Job

When setting up a cron job in cPanel → Cron Jobs, specify the path to your custom php.ini file using the -c option:

Bash

/usr/local/bin/php -c ${HOME}/config/php.ini ${HOME}/script.php
 
  • -c → tells PHP to use the specified configuration file.

  • ${HOME}/config/php.ini → path to your custom file.

  • ${HOME}/script.php → the script you want to run.

 

Step 3: Save and Test

  1. Save the cron job in cPanel.

  2. Wait for the scheduled run or trigger it manually.

  3. Check logs or script output to confirm the custom settings are applied.

 

Example Use Case

If your script needs to access remote URLs, you must enable the allow_url_fopen directive in your custom php.ini:

allow_url_fopen = On

Without this, the cron job may fail even if the directive is enabled for web‑based PHP scripts.

Important Notes

  • Different environments (web vs. cron) may use different php.ini files.

  • Always back up your configuration before making changes.

  • Use absolute paths when referencing files in cron jobs.

  • Test thoroughly to ensure your script runs as expected.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution