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
-
Log in to your hosting account via FTP, SFTP, or File Manager.
-
Navigate to your home directory (e.g.,
/home/username/config/). -
Create or edit a
php.inifile 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
-
Save the cron job in cPanel.
-
Wait for the scheduled run or trigger it manually.
-
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
|