phplinuxscrapyampps

php shell_exec() - ampps - permission denied for scrapy command


I have a scrapy spider configured to fetch data from some sites. and i have developed a ui in php and html to show scraped data.this data is being fetched from json file being generated upon running scrapy command. iam using ampps for running php. initally my php code look like following

$output= shell_exec('cd /home/testuser/Desktop/scrapy_tutorial/ && scrapy crawl example -o example.json 2>&1');
print_r($output);

and i got result as 'scrapy command not found'.so i changed my code to set full path of scrapy bin

$output= shell_exec('cd /home/testuser/Desktop/scrapy_tutorial/ && /usr/local/bin/scrapy crawl example -o example.json 2>&1');
print_r($output);

now iam getting output which contains message

PermissionError: [Errno 13] Permission denied: 'example.json'

looks like crawler works fine but has no permissions for writing to file.

i checked user which executes php script using

exec('whoami');

and it outputs 'ampps'

Any help will be appreciated.


Solution

  • The root of the problem is in that PHP runs as a user that has no write privilege to the location where the output is being attempted to store.

    There are two simple solutions to that:

    1. Make the target location writable by the user PHP acts as.

      • like this everyone gets write access to the directory:

        chmod a+w /path/to/location

      • like this only group members get write access to the directory:

        usermod -aG <group-name-of-the-location-owner> ampps

        chmod g+w /path/to/location

    2. Save the output elsewhere. The /tmp directory is a location useful for things like this. (Be sure to delete the file after processing in case the data inside are sensitive)