How To Install Supervisor Amazon Linux 2023?

Setting Up and Configuring Supervisor for Process Management on AWS EC2 Ubuntu

Last Updated On - August 2nd, 2024 Published On - Aug 16, 2023

What is a Supervisor?

Supervisor is a convenient process control system that allows you to manage and monitor long-running processes on your server. In this article, I will take you through the steps to install, configure, and utilize Supervisor on an AWS EC2 instance running on Linux 2023. By the end of this tutorial, you’ll have a fully functional Supervisor installation managing your processes efficiently.

Use case of Supervisor with an example

Suppose you have an Excel sheet of 200k records and you want to insert all those records in your database. If you create a module on your website to upload 200k users file, then the script will execute to insert users. This process will take a lot of time, and resources, and block the other processes on the same page until this upload process gets completed. Also, there are very high chances to get timeout errors.
You can do the same thing in the background and simultaneously can perform other tasks too asynchronously. You’ve to write a script and mention it in the supervisor’s process configuration. Supervisor executes that script on the server in the background without consuming many resources, timeout errors, and blocking other processes.



Installation and Configuration Steps

Install Python 3 and pip

Before installing Supervisor, ensure that Python 3 and pip are installed on your system. You can install them using the following command:

sudo apt update
sudo apt install python3-pip

Install Supervisor

With pip installed, you can now proceed to install Supervisor:

pip install supervisor
sudo apt install supervisor

Generate Supervisor Configuration

After Supervisor is installed, you can generate a sample configuration file to work with:

echo_supervisord_conf

Copy the Configuration File

To use this configuration file, we’ll copy it to the appropriate location:\

sudo su
echo_supervisord_conf > /etc/supervisord.conf

Edit the Configuration File

Open the configuration file in a text editor to make the necessary changes:

nano /etc/supervisord.conf
Supervisor configuration file

Also Read: How to Integrate Google Calendar API with Laravel 10 to Send Bulk Invites?


Add Process Worker

At the end of the configuration file, add the following lines:

[program:worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/panel/public_dev/artisan queue:work --sleep=3 --tries=3
stdout_logfile=/var/www/html/panel/public_dev/storage/logs/worker.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/www/html/panel/public_dev/storage/logs/worker-error.log
stderr_logfile_maxbytes=0
autostart=true
autorestart=true
startsecs=1
numprocs=4

Make sure to modify the command line to match your specific use case.

Supervisor configuration file with added process workers

Definition of Process Worker Terms

  • [program:worker] – Name of worker. When you add multiple process workers make sure to uniquely define this name. e.g. [program:worker1], [program:worker2], [program:insertWorker]
  • command=php /var/www/html/panel/public_dev/artisan queue:work --sleep=3 --tries=3 – Command/method that need to be executed in background
  • stdout_logfile=/var/www/html/panel/public_dev/storage/logs/worker.log – Log file for this process worker
  • stderr_logfile=/var/www/html/panel/public_dev/storage/logs/worker-error.log – Error log file for this process worker. Logs the error of the process worker.
  • numprocs=4 – Total no. of processes/threads associated with this process worker(No. of parallel process allocated to this worker to execute the task).

Also Read: Transform Your Php Workflow: Visual Studio Code Takes the Lead over Phpstorm


Save and Exit

To save the changes in the text editor, press Ctrl + X, then press Y to confirm the changes, and finally press Enter.

Start Supervisor

To enable and start the Supervisor service, use the following command:

sudo supervisord -c /etc/supervisord.conf

Check Process Status

To verify that Supervisor is managing the process correctly, you can check the status of all processes:

sudo supervisorctl status all
Process workers status

Also Read: Resolved: How to resolve common docker errors on Mac and Linux?


Update existing process workers in Supervisor

Open the supervisor configuration file

nano /etc/supervisord.conf

Do the changes in your existing worker or you can also add a new process worker. In the below example, I’m doing the changes in the existing process worker. Just removing sleep and timeout options.

Change in existing process worker – supervisor

Re-read the updated configuration file

Using this command supervisor reads your changes done in supervisord.conf file.

sudo supervisorctl reread

Update the changes in the running process

To make the changes live and applicable, you have to update the changes in the running program by using this command.

sudo supervisorctl update

Note: Without re-read and updating the command your changes will not be effective.


Also Read: How To Validate Location From IP & Zipcode Using Laravel 10?


FAQs

What is Supervisor and why is it useful on AWS EC2 instances?

Supervisor is a process control system that helps you manage and monitor long-running processes on your server. On AWS EC2, Supervisor ensures your critical applications automatically restart in case of crashes or server reboots, promoting stability and uptime.

Is Supervisor difficult to set up on an AWS EC2 instance?

No! This guide provides a step-by-step walkthrough, making Supervisor installation and configuration on your EC2 instance a breeze.

What are the benefits of using Supervisor on AWS EC2 compared to manual process management?

Supervisor automates process management, saving you time and effort. It also offers features like automatic restarts, logging, and process grouping, improving overall server reliability.

Can Supervisor monitor multiple processes on my AWS EC2 instance?

Absolutely! Supervisor excels at managing a variety of processes. You can easily configure it to oversee all your critical applications on your EC2 instance.

What happens if a process managed by Supervisor crashes on my EC2 instance?

Supervisor can be configured to automatically restart crashed processes, ensuring your applications stay up and running without manual intervention.

Does Supervisor offer any logging capabilities on AWS EC2?

Yes! Supervisor logs process activity, including startup, shutdown, and errors. These logs can be invaluable for troubleshooting and monitoring the health of your applications.

Can I configure Supervisor to start processes only at specific times on my EC2 instance?

Yes! Supervisor allows you to define schedules for process startup and shutdown, offering more granular control over your application environment.

Is Supervisor compatible with different Linux distributions on AWS EC2?

Supervisor works well with popular Linux distributions like Amazon Linux 2, Ubuntu, and CentOS, making it a versatile choice for your EC2 instances.

Are there any security risks associated with using Supervisor on AWS EC2?

While Supervisor itself is generally secure, it’s crucial to follow best practices for user permissions and configuration files to maintain overall server security.

What if I encounter any issues while setting up Supervisor on my AWS EC2 instance?

This guide provides troubleshooting tips, but you can connect with me to troubleshoot the issues(one on one session is chargable).


Conclusion

You’ve successfully installed, configured, and utilized Supervisor on your AWS EC2 instance running Ubuntu. Supervisor provides an efficient way to manage and monitor long-running processes, ensuring that your applications run smoothly and reliably. With the easy-to-follow steps provided in this guide, you can now take advantage of Supervisor’s capabilities to enhance your server’s performance and stability.