Current Location: Home> Latest Articles> How to register the socket_accept() service as a Linux background daemon

How to register the socket_accept() service as a Linux background daemon

M66 2025-05-28

socket_accept() is a common way to handle socket connections when programming networks using PHP. However, it itself does not automatically run as a daemon, and must be configured to be stable in the form of background processes in Linux. This article will introduce how to register a PHP script containing socket_accept() as a Linux background daemon to ensure that it runs continuously, restarts automatically, and has good log management capabilities.

1. Prepare your PHP socket service script

First, you need a basic socket server-side script. Here is a simple PHP example for the TCP server:

 <?php

$host = '0.0.0.0';
$port = 12345;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $host, $port);
socket_listen($socket);

echo "Server started on {$host}:{$port}\n";

while (true) {
    $client = @socket_accept($socket);
    if ($client === false) {
        usleep(100000); // avoidCPUToo high occupancy
        continue;
    }

    $message = "Hello from server at m66.net\n";
    socket_write($client, $message, strlen($message));
    socket_close($client);
}

socket_close($socket);

2. Create a daemon starter

To turn it into a daemon running, you need to manually "get out of terminal". A common way is to use PHP to implement daemon mode, or more commonly, systemd with Linux.

Use systemd to register daemons

  1. Save PHP scripts <br> Assuming your script is named /usr/local/bin/php_socket_server.php , make sure the file is executable:

 chmod +x /usr/local/bin/php_socket_server.php
  1. Create systemd service unit file <br> Add the following content to /etc/systemd/system/php-socket.service :

 [Unit]
Description=PHP Socket Server
After=network.target

[Service]
ExecStart=/usr/bin/php /usr/local/bin/php_socket_server.php
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=php-socket-server
User=www-data
Group=www-data

[Install]
WantedBy=multi-user.target
  1. Enable and start the service

 sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable php-socket.service
sudo systemctl start php-socket.service

This way, your socket service will automatically run when the system starts and will automatically restart when the abnormal exit is eliminated.

3. Verify the running status of the daemon

You can check the service status using the following command:

 sudo systemctl status php-socket.service

View log information:

 journalctl -u php-socket.service -f

4. Prevent zombie processes and resources from leaking

In long-running background socket services, reasonable exception handling is very critical. It is recommended to make appropriate delays for socket_accept to return false , and monitor the log for frequent errors.

5. Enable domain name listening and reverse proxy (optional)

If you want to provide services to the outside world through domain names such as m66.net , you can map the port to the corresponding service in combination with Nginx or other reverse proxy software. For example: