I want to write terminal errors to a log file - LUMEN - php

I am using LUMEN framework.
I want to log or write all those errors which appears in console or terminal to a log file. So that I can see what errors are appearing in the process.
Currently I am running my LUMEN server using this command:
nohup php artisan queue:work --daemon & ?
So how can I get all those error in a log file?
Example errors I want to log:
[Mon Jul 6 13:39:12 2015] 127.0.0.1:36615 [500]: /v1/user/fetchUser/ -
Class 'App\Http\Controllers\V1\UserController' not found in /opt/lampp/htdocs/api/app/Http/Controllers/V1/ArticleController.php on line 504
[Tue Jul 7 11:11:59 2015] 127.0.0.1:37774 [500]: /v1/user/fetchUsers/

You can redirect the output to a file like this:
nohup php artisan queue:work --daemon > ./queue_worker.log &
To save the terminal output to one file and errors to another:
nohup php artisan queue:work --daemon > output.log 2> errors.log &

Example: nohup ruby post_receive.rb >> /root/logPostReceive.log 2>&1 &
2>&1 redirect stderr to stdout

Related

How to run WebSocket(socketo.me) server automatically when HTTP server start in Plesk server

I need to run the PHP WebSocket scripted in PHP 24/7/365
The path of the script named websocket_server.php lies in the below path
/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/websocket_server.php
In order to run the above script first, it needs to register as a service and invoke this command automatically.
I didn't have much knowledge about setting up the service in Linux (Ubuntu 16.04). So I had gone with setting up the crown job scheduler. But it didn't yield any results because of errors.
I had added three commands in order to run in task scheduler they are as follows:
#reboot root nohup php
/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php
2>&1 >/dev/null &
Task "#reboot root nohup php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/websocket_server.php 2>&1 >/dev/null &" successfully completed in 0 seconds, output:
-: #reboot: command not found
httpdocs/proj_ci/application/libraries/server/websocket_launch.sh
with arguments cron:run
Task "httpdocs/proj_ci/application/libraries/server/websocket_launch.sh" successfully completed in 0 seconds, output:
PID=ps -aef | grep "websocket_server.php" | grep -v grep | awk '{print $2}'
if [ -z $PID ]
then
#echo "Launching now" nohup php websocket_server.php > error_log & else # echo "Running as PID $PID" fi
/usr/bin/php
/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php
Task "/usr/bin/php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php" completed with error in 0 seconds, output:
Could not open input file: /usr/bin/php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php
This is from the PuTTY terminal
root#h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries# php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php
PHP Warning: require_once(../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php on line 10
PHP Fatal error: require_once(): Failed opening required '../vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php on line 10
root#h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries# cd /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server
root#h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server# php websocket_server.php ^C
root#h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server#
root#h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server#
Apart from adding crown jobs are there any better ways to do so?
Please help me with the solution as much as possible. Since from the PuTTY terminal,
I could able to run the command and everything works fine
<?php
# The maximum execution time, in seconds. If set to zero, no time limit is imposed.
set_time_limit(0);
# Make sure to keep alive the script when a client disconnect.
ignore_user_abort(true);
error_reporting(E_ALL);
ini_set('display_errors', 'On');
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
ini_set("error_log", "/var/www/vhosts/abc.xy/httpdocs/websocket.log");
echo "Script start at: " . date('h:i:s') . "\n";
exec('bash -c "exec nohup php websocket_server.php >> /var/www/vhosts/abc.xy/httpdocs/websocket.log 2>&1 &"');
shell_exec('nohup php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php 2>&1 >> /var/www/vhosts/abc.xy/httpdocs/websocket.log &');
echo "Script end at: " . date('h:i:s');
?>
This will keep all the logs in a specified path given /var/www/vhosts/abc.xy/httpdocs/websocket.log
The above script works fine if you access the specified script by name from the browser. In case if the server restarts you'll be able to schedule it in the Cron job
In case if the exec() method fails in some hosting server shell_exec() will work without affecting in case if it's already started in port 8080

Execute Openwrt UCI command through PHP

Im developing simple (dead simple) front end for openwrt using PHP. To do this I need to call many openwrt UCI (Unified conf. interface) commands through PHP shell_exec() or system() functions. All the UCI commands that I tried in terminal are working perfectly fine. But as soon as I run them through above functions they are simply not working.
As an example I run following two commands which worked well in terminal
uci set wireless.#wifi-iface[0].ssid=test
uci commit
But as soon as I run them through PHP nothing happens. They are simply not working. The I make .sh file and save above two lines and run that files using PHP but again!! results are the same. But when I execute .sh file through terminal it works!!
For testing I set the both file permission to 777. but that doesn't helps. Is there are any additional requirements to run shell commands through PHP like root access to the PHP or Apache ? I'm new to this and I would thankful if someone can help
my apache error_log
[Wed Aug 19 08:26:53 2015] [error] [client 192.168.2.117] uci
[Wed Aug 19 08:26:53 2015] [error] [client 192.168.2.117] :
[Wed Aug 19 08:26:53 2015] [error] [client 192.168.2.117] I/O error
[Wed Aug 19 08:26:53 2015] [error] [client 192.168.2.117]
Im using apache as a web server and openwrt Chaos Calmer 15.05-rc3 as my base firmware on top of Raspberry pi 2
I managed to solve my problem using uhttpd web server instead of Apache. Apache somehow doesn't have enough privileges to execute UCI commands directly. uhttpd the default web server in openwrt can execute this commands directly
I tried to figure out the same problem and my conclusion so far is run php with root permissions. I know this is not secure, but at least it works. Here is one line from /etc/init.d/php5-fpm to run php-fpm with root privileges:
service_start $PROG -R -y $CONFIG -g $SERVICE_PID_FILE
The key flag here:
-R, --allow-to-run-as-root Allow pool to run as root (disabled by default)
Both answers are right. What sameera mentions was the fact that uhttpd has special rights running on LEDE or OPENWRT (as default), but what Anton Glukhov wrote is also correct, it allowed me to run as root but limited to avoid errors. I was not able to run my scripts (.sh) as with uhttpd, but php runs ok and does not have any bugs while running as root. I guess its a file permissions issue by default on Nginx with Openwrt. My solution was leave uhttpd running on a different port and run my program with all the rights and permissions while running everything else as non root in Nginx. Security is no issue in my case, its offline server.
service_start $PROG -R -y $CONFIG -g $SERVICE_PID_FILE
Works, in my case editing adding the -R flag in /etc/init.d/php7-fpm

Shell_exec nohup with nohup.out

Executing this code:
shell_exec('nohup command&');
or this
shell_exec('nohup command > /path/to/nohup.out 2>&1&');
But there is no nohup.out in both cases. How can I run nohup with nohup.out via php?
shell_exec('nohup command > /path/to/nohup.out 2>&1&');
This line will work. Just make sure you have write permissions for the output folder. Consider your output folder is /usr/nohup-out
ls -l /usr/nohup-out
It should have write, read and execute permissions (rwx). If not, do this:
sudo chmod -R 777 /usr/nohup-out
Now, try to execute php file. It should create a nohup file in /usr/nohup-out folder.
Sample script and result:
1. date.php:
<?php
shell_exec('nohup date > /usr/nohup-out/nohup.out 2>&1&');
?>
2. Execute php from terminal:
php date.php
3. nohup.out content after execution
Thu Apr 23 11:30:28 IST 2015

PHPAGI: Exec format error

Encountering a problem when running phpagi:
-- Executing [123#DLPN_C:1] AGI("SIP/1000-00000001", "hello_world.php") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/hello_world.php
hello_world.php: Failed to execute '/var/lib/asterisk/agi-bin/hello_world.php': Exec format error
-- Auto fallthrough, channel 'SIP/1000-00000001' status is 'UNKNOWN' Scheduling destruction of SIP dialog '343930130' in 32000 ms (Method: INVITE)
From command line:
root#asterisk-test:/var/lib/asterisk/agi-bin# php5 -q hello_world.php
#!/usr/bin/php5 -q
Additional info:
-rwxr-xr-x 1 root root 757 Mar 29 19:32 hello_world.php
drwxrwxr-x 4 root root 4096 Mar 29 19:44 phpagi
-rwxr-xr-x 1 root root 25079 Sep 30 2010 phpagi-asmanager.php
-rwxr-xr-x 1 root root 2322 Sep 30 2010 phpagi-fastagi.php
-rwxr-xr-x 1 root root 67615 Sep 30 2010 phpagi.php
Source of hello world: http://www.eder.us/projects/phpagi/phpagi/api-docs/__examplesource/exsource_home_html_projects_phpagi_phpagi_examples_dtmf.php_acb7257145e4a5249182c8373cd8e848.html
The Exec Format Error is from /bin/bash, asterisk executes hello_world.php as a bash script.
shebang
If you add a correct shebang, the script get executed by the given PHP intepreter.
The first Line tells the System which program should run the script.
#!/usr/bin/env php
To test your shebang, execute the script itself, not by PHP:
root#asterisk-test:/var/lib/asterisk/agi-bin# ./hello_world.php
Make sure it is executable with:
root#asterisk-test:/var/lib/asterisk/agi-bin# chmod +x hello_world.php
alternative wrapper
Create a bash script that executes the PHP script.
example hello_world.sh:
/usr/bin/php hello_world.php
and call it in the Dialplan AGI("hello_world.sh").
Make sure the shellscript is executable chmod +x hello_world.sh.
I added following line on top script to get it working for me
#!/usr/bin/php -q
You issue is not asterisk issue,but general linux one.
Please try from your command line following:
su asterisk -c "/var/lib/asterisk/agi-bin/hello_world.php"
Most likly reasons: php path is incorrect or selinux enabled and not configured.
Could you check your extensions.conf or extensions_custom.conf, if the extension and priority are not continuous also this error will occur.
please check the below example:
[context]
exten => 1,1,Answer()
exten => 1,2,AGI(your-agi-script)
exten => 1,3,Hangup()

Run command from PHP script: sh: 1: screen: not found

I have problem with script that running screen session.
<?php exec("screen -m -d ffmpeg2theora -v 3 /path/to/video.mp4"); ?>
and see this on logs:
Wed Oct 17 16:17:39 2012] [error] [client 83.5.x.x] sh: 1: screen: not found, referer: http://xxxxxx.xx
It's stop working on some update (apache/php/suphp?), before that was working like charm
Any idea's what's happend, how configure suPhp/Apache?
If you want PHP to start a long-running command in the background, why not try the following:
<?php pclose(popen("/usr/bin/nohup /path/to/ffmpeg2theora -v 3 /path/to/video.mp4 >/tmp/result 2>&1", "r")); ?>
It's always wise to use explicit paths to commands in these sorts of situations.
Instead, simply do:
exec("ffmpeg2theora -v 3 /path/to/video.mp4 &");

Categories