stig1 Posted February 24, 2010 Share Posted February 24, 2010 I am trying to build a php page, that will send a shell command to print a pdf. I am using a centos 5.4 with cups installed. I can print fine when I enter the shell command manually into the terminal screen and hit enter, but I would like a user to be able to go to load a page via the browser and it sends the print request. The following is the code I am using does not seem to work: <?php $cmd = 'lpr -o sides=two-side-long-edge order2.pdf'; echo "<pre>".shell_exec($cmd)."</pre>"; ?> Help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/ Share on other sites More sharing options...
roopurt18 Posted February 24, 2010 Share Posted February 24, 2010 I'm assuming you have full access to this machine, which you'll likely need to fix the issue. The issue is probably permissions related. You need to figure out which user Apache is running as, probably one of: nobody, httpd, apache, apache2, or httpd2 Next you need to make sure that user can actually log into the machine. For example, as root you might run: usermod -s /bin/bash apache2 To allow user apache2 to log in with the bash shell. Then as root, switch to that user: su apache2 Now change directories into your application's directory where you're calling shell_exec() from. Try and execute the command as the user from the console: apache2@/var/www/html/yourapp/$ lpr -o sides=two-side-long-edge order2.pdf Does it work? Do you receive any errors? When you're done, log out from the web user: exit You should be back at root. Disallow shells for the user (pick the appropriate command) usermod -s /bin/false usermod -s /bin/nologin I'm not Linux guru, so my advice may not be the best advice. But it should tell you pretty clearly if your user that apache is running as can run the command properly. Another feature that might stop this from working is some sort of PHP safe_mode setting or the php_suhosin (or whatever it's called) patch / module. Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/#findComment-1017765 Share on other sites More sharing options...
stig1 Posted February 24, 2010 Author Share Posted February 24, 2010 I am alittle confused, do you type that all in the php script? or the terminal screen? cause i run my server as root, no one else has access. Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/#findComment-1017767 Share on other sites More sharing options...
roopurt18 Posted February 24, 2010 Share Posted February 24, 2010 You type all that in the terminal screen from the root account. If httpd is running as user root on your machine, then you have some serious installation and configuration issues to fix. httpd should start as root and then fork to a different user account on the machine. The user account it forks to is typically one of: nobody, httpd, httpd2, apache, apache2 That user account will not have the full access to the machine that root has. How did you go about installing apache / httpd? Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/#findComment-1017772 Share on other sites More sharing options...
stig1 Posted February 24, 2010 Author Share Posted February 24, 2010 I installed it via the gui when installing centos 5.4. So it probably is default. Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/#findComment-1017775 Share on other sites More sharing options...
stig1 Posted February 24, 2010 Author Share Posted February 24, 2010 In my terminal screen, I got permission denied for the users nobody and apache when typing in the following: nobody@/var/www/html/trail/$ lpr -o sides=two-sides-long-edge order2.pdf or apache@/var/www/html/trail/$ lpr -o sides=two-sides-long-edge order2.pdf Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/#findComment-1017778 Share on other sites More sharing options...
stig1 Posted February 25, 2010 Author Share Posted February 25, 2010 Solved! Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/#findComment-1017784 Share on other sites More sharing options...
roopurt18 Posted February 25, 2010 Share Posted February 25, 2010 What exactly did you do to solve this? Just curious. Quote Link to comment https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/#findComment-1017787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.