Edit the php.ini via SSH on cPanel

  • This article explains how you can edit the php.ini configuration file of a cPanel account via SSH.

    1 Server requirements

    The PHP handler must be suPHP.


    2 Locate the php.ini

    Connect to the cPanel account via SSH. Once you are connected via SSH, type the following command to know where the original php.ini configuration file is located:


    php -i | grep php.ini


    2.1 Example Output

    The output should be something like this:

    Shell-Script
    1. awesomeuser@openvservers[~]# php -i | grep php.ini
    2. Configuration File (php.ini) Path => /usr/local/lib
    3. Loaded Configuration File => /usr/local/lib/php.ini


    The important part from the previous output is the following:


    /usr/local/lib/php.ini


    Now that you know what is the full path to the php.ini configuration file, you can proceed to the next step.


    3 Copy the php.ini

    Copy the php.ini configuration to the home folder of the account. You can copy the php.ini configuration file using your preferred method (bash commands or SFTP). Once you have copied the file to the home folder, you will be able to edit it.


    3.1 Example

    The following command will copy the php.ini configuration file to the home folder:


    cp /usr/local/lib/php.ini ~/


    4 Edit the php.ini

    Now that you have a copy of the original php.ini configuration file, you may edit it using your preferred method (nano, vi, cPanel File Manager, SFTP or FTP).


    5 Enable the php.ini

    This is the final step. You have two options:

    1. You can enable the customized php.ini configuration file for selected folders only within the account. This is the recommended option if you are using untrusted third party PHP scripts.
    2. Or, you can enable your customized php.ini configuration file for all folders within the account. In other words, it will be enabled by default everywhere within the account.


    6 Selected Folders

    This is the recommended option if you are using untrusted third party PHP scripts. Any PHP scripts inside the folder with the edited php.ini configuration file will be able to use the customized options.


    You simply have to copy the php.ini configuration file (edited at step 4) into any folders you would like to customize. You can do this with any available method (cPanel File Manager, SSH, SFTP or FTP).


    6.1 Example

    If the PHP script you would like to execute with the customized options is located within the public_html folder, you can copy the php.ini configuration file via SSH with the following command:


    cp ~/php.ini ~/public_html/


    7 All Folders

    Choose this option to enable the customized options for all folders within the account.


    Create a file named .htaccess within the home folder of the account. You can do this with any available method (cPanel File Manager, SSH, SFTP or FTP). Within the .htaccess file you have created, insert the following code (replacing awesomeuser for the username of the account):


    Shell-Script
    1. suPHP_ConfigPath /home/awesomeuser
    2. <Files php.ini>
    3. order allow,deny
    4. deny from all
    5. </Files>

    If you have followed the example at step 3.1, you should have a php.ini configuration file inside the home folder. If not, copy the php.ini configuration file (edited at step 4.) to the home folder. That's it.


    7.1 Example

    The following command will create the necessary .htaccess file:


    touch ~/.htaccess



    Make sure you replace awesomeuser for the username of the account in the following command:

    Shell-Script
    1. echo "suPHP_ConfigPath /home/awesomeuser
    2. <Files php.ini>
    3. order allow,deny
    4. deny from all
    5. </Files>
    6. " >> ~/.htaccess


    That's it. All executed php scripts will be able to use the customized options.


    8 Conclusion

    Just in case you are having issues or trouble following the article, please create a new thread in the forums to ask for help. In case you are a client, open a support ticket instead. Thank you very much for reading the article.

Share