First, we need a simple PHP socket service script that listens on a certain port, accepts connections, and responds simply.
<?php
// Create a TCP socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");
}
<p>// Bind address and port<br>
$address = "0.0.0.0";<br>
$port = 12345;<br>
if (socket_bind($socket, $address, $port) === false) {<br>
die("socket_bind() failed: " . socket_strerror(socket_last_error($socket)) . "\n");<br>
}</p>
<p>// Listen for connections<br>
if (socket_listen($socket, 5) === false) {<br>
die("socket_listen() failed: " . socket_strerror(socket_last_error($socket)) . "\n");<br>
}</p>
<p>echo "Service started, listening on port $port\n";</p>
<p>// Loop to accept client connections<br>
while (true) {<br>
$clientSocket = socket_accept($socket);<br>
if ($clientSocket === false) {<br>
echo "socket_accept() failed: " . socket_strerror(socket_last_error($socket)) . "\n";<br>
continue;<br>
}</p>
socket_write($clientSocket, $message, strlen($message));
socket_close($clientSocket);
}
socket_close($socket);
?>
This script listens on port 12345 of the local machine, accepts client connections, sends a welcome message, and then closes the connection.
Assuming the above PHP script is saved as /usr/local/bin/php-socket-server.php, we need to write a systemd service file to manage it.
Create a new service file at /etc/systemd/system/php-socket-server.service, with the following content:
[Unit]
Description=PHP Socket Service
After=network.target
<p>[Service]<br>
Type=simple<br>
ExecStart=/usr/bin/php /usr/local/bin/php-socket-server.php<br>
Restart=always<br>
RestartSec=5<br>
StandardOutput=syslog<br>
StandardError=syslog<br>
SyslogIdentifier=php-socket-server</p>
<p>[Install]<br>
WantedBy=multi-user.target<br>
Explanation:
ExecStart specifies the command to start the service, running the script with the system's PHP interpreter.
Restart=always ensures the service will automatically restart if it crashes.
RestartSec=5 specifies a 5-second restart interval.
StandardOutput and StandardError both output to the system log (syslog), making troubleshooting easier.
SyslogIdentifier sets the log identifier.
Execute the following commands to apply the service and start it:
sudo systemctl daemon-reload
sudo systemctl enable php-socket-server.service
sudo systemctl start php-socket-server.service
To check the service status:
sudo systemctl status php-socket-server.service
If the service is running correctly, you can test the connection using telnet or netcat:
telnet 127.0.0.1 12345
You should see the welcome message:
Welcome to m66.net PHP Socket Service!
Log Management: It is recommended to add logging functionality in the PHP script, or use rsyslog to filter logs with the php-socket-server identifier for easier log viewing.
Security: Set firewall rules based on actual needs to only allow trusted IPs to access the port.
Service Permissions: Configure the User and Group fields in the systemd service file to avoid running as root and improve security.
Code Robustness: Add signal handling (e.g., pcntl_signal) in the service script to implement graceful shutdown.