Enable fopen only for a specific cPanel or linux user with suPHP

For security reasons most hosts will have fopen disabled. This is for security reasons but there are also times when it is legitimately needed. Below I will show you how to enable fopen for a single cPanel user instead of server wide.
This may be the error you get:
1 |
Warning: fopen(): URL file-access is disabled in the server configuration |
or
1 |
Warning: fopen() [function.fopen]: URL file-access is disabled in the server configuration |
Here’s how to fix it:
You will need shell access to the server and i’m assuming that you are the server administrator and not just a user on the server. If you are just a user on the server you can just create your own php.ini file in your public_html directory and change the setting explained below.
UPDATE 1/2/2014:
I recommend using the PHP.ini manager for cPanel/WHM instead of custom files, you can find more information here:
http://how2.be/en/community/phpinimgr/
There’s also great information on the cPanel forum about adding specific entries for specific directories:
First let’s find out where our PHP.INI file is located at, do so with this command:
1 |
php5 -i | grep php.ini |
You should get an output similar to this:
1 2 3 |
php5 -i | grep php.ini Configuration File (php.ini) Path => /usr/local/lib Loaded Configuration File => /usr/local/lib/php.ini |
That tells us that our current loaded PHP.INI file is located at /usr/local/lib/php.ini. We will need this file because we will be creating a copy of it in the users public_html directory.
Change directory into the users public_html folder:
1 |
cd /home/user/public_html |
Let’s verify that a PHP.INI does not already exist:
1 |
find -type f -name "php.ini" |
If you do not find a PHP.INI file with the above command, copy the PHP.INI file we just found using this command:
UPDATE: I do not recommend copying the entire file, this could cause issues with references in the php.ini file pointing at old modules, etc.
Recommendation is to just create a new php.ini file and put allow_url_fopen=On in it. Add the suPHP_ConfigPath below and it should work
1 |
cp /usr/local/lib/php.ini ./ |
Now we need to assign the user as the owner and group, do this with chown:
1 |
chown user.user php.ini |
Now we need to add the configpath to the .htaccess file, use this command to add it:
1 |
echo 'suPHP_ConfigPath /home/user/public_html' >> .htaccess |
You have now successfully setup a custom PHP.INI file for that specific user.
In order to enable fopen, use nano or your favorite editor and open the php.ini file.
Search for
1 |
allow_url_fopen |
and change the setting to ON.