Managing PHP Scripts with Supervisord: A Guide
PHP is a popular programming language that is widely used for web development. However, running PHP scripts on a server can be challenging, especially when it comes to managing processes and ensuring they are always running. This is where Supervisord comes in handy. Supervisord is a process control system that allows you to manage and monitor processes on Unix-like systems. In this article, we’ll explore how to use Supervisord with PHP to manage and run simple PHP scripts.
Installing Supervisord
Before we start, we need to install Supervisord. Supervisord can be installed using the package manager of your operating system. For example, on Ubuntu, you can use the following command to install Supervisord:
sudo apt-get install supervisor
Once installed, you can check the status of the Supervisord service using the following command:
sudo systemctl status supervisor
If the service is not running, you can start it using the following command:
sudo systemctl start supervisor
Creating a PHP script
Let’s create a simple PHP script that we can use to test Supervisord. This script will print “Hello, world!” to the console every second indefinitely. Create a file called test.php
with the following contents:
<?php
// Log a message to a file every 5 seconds
$logfile = "/var/log/test.log";
while (true) {
$message = "Hello, world!";
error_log($message . PHP_EOL, 3, $logfile);
sleep(5);
}
?>
To manage this PHP script with Supervisord, you can create a configuration file for Supervisord that defines the process. The configuration file tells Supervisord which processes to run and how to manage them. Create a file called test.conf
with the following contents:
[program:test]
command=/usr/bin/php /path/to/test.php
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/log/test.log
In this configuration file, we define a process named test
that runs the test.php
script using the PHP binary located at /usr/bin/php
. The autostart
and autorestart
options ensure that the process is started automatically and restarted if it crashes. The user
option specifies the user under which the process should run, and the redirect_stderr
option sends any error messages to the standard output stream. Finally, the stdout_logfile
option specifies the log file where the output of the process should be written.
Starting the PHP script with Supervisord
To start the PHP script using Supervisord, we need to tell Supervisord to read our configuration file. We can do this using the following command:
sudo supervisorctl reread
This command tells Supervisord to read the configuration files in /etc/supervisor/conf.d/
. If everything is set up correctly, you should see a message like this:
test: available
This means that the test
process is available to be started.
To start the test
process, use the following command:
sudo supervisorctl start test
This command tells Supervisord to start the test
process defined in the test.conf
configuration file. If everything is set up correctly, you should see a message like this:
test: started
This means that the test
process has started successfully.
Managing the PHP script with Supervisord
To show the running processes managed by Supervisord, you can use the supervisorctl
command with the status
option. Here's how to do it:
- Open a terminal window on your server.
- Run the following command to connect to the Supervisord daemon:
sudo supervisorctl
- Once connected, you will see the
supervisor>
prompt. - Run the following command to show the status of all processes managed by Supervisord:
status
This will display a list of all processes managed by Supervisord, along with their status. The status can be one of the following:
RUNNING
: The process is running normally.STOPPED
: The process has been stopped.EXITED
: The process has exited.FATAL
: The process has exited due to a fatal error.UNKNOWN
: The status of the process is unknown.
Here’s an example output of the status
command:
test RUNNING pid 12345, uptime 1 day, 2:34:56
othertest RUNNING pid 67890, uptime 0:00:10
In this example, there are two processes managed by Supervisord named test
and othertest
. Both processes are currently running, and their process IDs and uptime are displayed.
You can use the supervisorctl
command to perform various actions on these processes, such as starting, stopping, or restarting them. For example, to stop the test
process, you can run the following command:
stop test
This will stop the test
process and update its status to STOPPED
when you run the status
command again.
Monitoring the PHP script with Supervisord (optional)
Supervisord provides a web-based interface that allows you to monitor the processes that it manages. You can follow this tutorial https://morioh.com/p/fb55bc63da0c. The web interface shows the status of all the processes that Supervisord is managing.
Conclusion
In conclusion, Supervisord is a powerful tool that can be used to manage PHP scripts running on Unix-like systems. By using Supervisord to manage your PHP scripts, you can ensure that they are always running efficiently and avoid any potential issues that may arise from unmanaged processes. With the help of this guide, you should now have a better understanding of how to install and configure Supervisord to manage your PHP scripts, as well as some tips and best practices for using Supervisord effectively. By incorporating Supervisord into your PHP development workflow, you can improve your overall application performance and uptime, and have greater control over your server processes.