2

I need to run the PHP WebSocket scripted in PHP 24/7/365 as a service automatically whenever, the server restarts/stops/starts the process. The process must invoke automatically

Suppose if we access the test.php from the browser i.e., www.example.com/libraries/server/test.php it starts and continues to run with port 8080

However, suppose if we try to terminate the process by killing with the below commands

# kill $(lsof -t -i:8080)
# lsof -i:8080

It must be invoked automatically

If we plan to run as a scheduler with a cron job for every second or particular day. How do we compromise with the compensation beared by the server uptime as well as server downtime? or if none of the options are left suppose if we try to run the cron job for every second, how do I script the code, whether the WebSocket is running with port 8080, if not execute this script by shell_exec() or via exec() commands

Here is the code for test.php

<?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/somedomain.com/httpdocs/websocket.log");

echo "Script start at: " . date('h:i:s') . "\n"; 
exec('bash -c "exec nohup php websocket_server.php >> /var/www/vhosts/somedomain.com/httpdocs/websocket.log 2>&1 &"');
shell_exec('nohup php /var/www/vhosts/somedomain.com/httpdocs/proj_ci/application/libraries/server/websocket_server.php 2>&1 >> /var/www/vhosts/somedomain.com/httpdocs/websocket.log &');
echo "Script end at: " . date('h:i:s'); 
?>

So How do I need to run the process as a service automated when HTTP server starts?

With the help of supervisor or systemd which launches it in the background and keeps it running. What should be the Linux commands that need to be executed via SSH?

2

Using systemd to run PHP as Daemon/Service

php_socketme.sh

#!/bin/bash
nohup /usr/bin/php /var/www/vhosts/somedomain.com/httpdocs/libraries/server/test.php >> /var/www/vhosts/somedomain.com/httpdocs/php_socketme.log 2>&1

php_socketme.service

[Unit]
Description=RunningPHPWebSockets(socketme.io)

[Service]
User=root
ExecStart=/var/www/vhosts/somedomain.com/httpdocs/libraries/server/php_socketme.sh

[Install]
WantedBy=multi-user.target

From the terminal

root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# ./php_socketme.sh
-bash: ./php_socketme.sh: Permission denied
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# chmod a+x socketme.sh
chmod: cannot access 'socketme.sh': No such file or directory
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# chmod a+x php_socketme.sh
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# ./php_socketme.sh
-bash: ./php_socketme.sh: /bin/bash^M: bad interpreter: No such file or directory
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# dos2unix php_socketme.sh
dos2unix: Datei php_socketme.sh wird ins Unix-Format umgewandelt ...
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# chmod 755 test.php
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# cp php_socketme.service /etc/systemd/system/php_socketme.service
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# systemctl daemon-reload
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# systemctl start php_socketme.service
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# systemctl status  php_socketme.service
● php_socketme.service - RunningPHPWebSockets(socketme.io)
   Loaded: loaded (/etc/systemd/system/php_socketme.service; disabled; vendor preset: enabled)
   Active: active (running) since Fr 2018-11-16 16:06:18 IST; 4min 9s ago
 Main PID: 29799 (myphp.sh)
   CGroup: /system.slice/php_socketme.service
           ├─29799 /bin/bash /var/www/vhosts/somedomain.com/httpdocs/libraries/server/myphp.sh
           └─29800 /usr/bin/php /var/www/vhosts/somedomain.com/httpdocs/libraries/server/myservice.php
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# systemctl stop php_socketme.service
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# systemctl status  php_socketme.service
● php_socketme.service - RunningPHPWebSockets(socketme.io)
   Loaded: loaded (/etc/systemd/system/php_socketme.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# systemctl enable php_socketme.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php_socketme.service to /etc/systemd/system/php_socketme.service.
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server# systemctl is-enabled php_socketme.service
enabled
root@user:/var/www/vhosts/somedomain.com/httpdocs/libraries/server#


Updated:

webs.sh

#!/bin/bash
### BEGIN INIT INFO
# Provides:          webSocket
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: php webSocket
# Description:       php webSocket
### END INIT INFO
nohup /usr/bin/php /var/www/vhosts/somedomain.com/httpdocs/libraries/server/websocket_server.php >> /var/www/vhosts/somedomain.com/httpdocs/custom_logs/webssh.log 2>&1

websocket.service

[Unit]
Description=php webSocket
After=syslog.target network.target

[Service]
User=root

Type=simple
ExecStart=/var/www/vhosts/somedomain.com/httpdocs/libraries/server/webs.sh >> /var/www/vhosts/somedomain.com/httpdocs/custom_logs/AllStandardOutput.log>> /var/www/vhosts/somedomain.com/httpdocs/custom_logs/AllStandardError.log
TimeoutStopSec=20
KillMode=process
Restart=always
RestartSec=2
StandardOutput=file:/var/www/vhosts/somedomain.com/httpdocs/custom_logs/StandardOutput.log
StandardError=file:/var/www/vhosts/somedomain.com/httpdocs/custom_logs/StandardError.log

[Install]
WantedBy=multi-user.target
Alias=websocket.service

Commands are as follows

cp websocket.service /etc/systemd/system/websocket.service 
systemctl daemon-reload 

systemctl start websocket.service
systemctl status websocket.service 
systemctl stop websocket.service
systemctl restart websocket.service

systemctl enable websocket.service 
systemctl is-enabled websocket.service 
systemctl disable websocket.service

lsof -i:8080

ExecStart=/bin/sh -c '/usr/local/bin/binary1 agent -config-dir /etc/sample.d/server 2>&1 > /var/log.log' 
ExecStart=/usr/bin/sh -c 'exec /usr/bin/my_binary [arguments] >>/var/log/flume-ng/log1.log 2>>/var/log/flume-ng/log2.log'

If you want to maintain file log between service restarts and just append new logged lines to it, use instead:

ExecStart=/usr/bin/sh -c 'exec /usr/bin/my_binary [arguments] >>/var/log/flume-ng/log1.log 2>>/var/log/flume-ng/log2.log'

exec means that shell program will be substituted with the my_binary program after setting up redirections without forking. So there will be no different from running my_binary directly after ExecStart=.

In the firewall

System policy for incoming traffic Allow all other incoming traffic

OR

create a custom rule:

WebSockets Allow incoming from all on ports 8080/tcp, 8080/udp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.