Reputation: 309
I have set up a LAMP Web Server and I am looking to run an application on the server side when the client clicks a button on the servers web interface. This application will look for a certain USB Device, by Serial Number, open it up and send a packet of bytes to the device.
I have an index.html, which only has a button with an action to call my test.php file which uses shell_exec() to call my application.
When the application is invoked through the web interface, the application writes out an error indicating that it couldn't open the USB Device (this a built in error for this application, so the application works, it just can not locate the usb device).
But when I invoke the application via the Terminal, the application finds the usb device and writes to it no problem.
I am looking for some advice! Simply is what I'm doing feasible? If so, how can I get the application to find the usb device when invoked via the web interface? I have a feeling it has something to do with permissions, you never know.
test.php:
<?php
echo shell_exec("/home/pi/FDTI_test/FDTI_test_application");
?>
NOTE: The usb device is connected, works great with its driver, and is connected to the server via usb.
The application works when invoked via the terminal on server side, but not when invoked via web interface.
Upvotes: 1
Views: 367
Reputation: 2495
I think your on the right track with this being a permissions issue.
In a typical LAMP stack, the php process runs as a module in the apache process, unless you've configured it differently. In my server OS of choice, the php process runs as the user 'www-data' by default.
Probably the easiest solution would be to give sudo permission to your web user account, and set the sudoers file to NOPASSWD. This is very insecure, so only do this in rare cases.
<?php echo shell_exec("sudo /home/pi/FDTI_test/FDTI_test_application"); ?>
The next easiest option is to give the web user account permission to write to the USB device directly. Depending on your distribution, you may only need to add the user to the 'adm' group.
sudo usermod -a -G adm www-data
Again, this may not be the most secure method, but more secure than the first option.
Lastly, you could look into the hardest solution which would be to install a patched version of apache which allows suexec. This is about as equally as insecure as the second option, but much more difficult to implement. (I would have included a link to a tutorial, but I'm limited to 2 links as this is my first answer.)
Hope This Helps!
Upvotes: 1