How to make your python service live for 24*7 using supervisor?
Lots of people use python for their online service and have to make the service live for 24*7. I will show you how can you make your server live for forever.
The supervisor is the service that we use for python service to run for endless time.
first of all, you need to install the supervisor in your machine. For Linux please follow the below command
apt-get install supervisor
Now restart the supervisor
service supervisor restart
To run your server 24*7 we need Gunicorn library which basically helps us to make our server live. So install the Gunicorn library into your virtual environment. To install use the following command.
pip install gunicorn
Now your supervisor is installed and it’s working now we have to set up. the supervisor for our project. Go to the following location in your machine.
/etc/supervisor/conf.d
Now create the new file here like your_project.conf and add following lines into the file and save the file.
[program:your_project_name]
command=/home/your_virualenv/bin/gunicorn --log-level debug
run_apiengine:main_app --bind 0.0.0.0:5007 --workers 2 --worker-class gevent
directory=your_project_directory
stdout_logfile= your_log_folder_path/supervisor_stdout.log
stderr_logfile= your_log_folder_path/supervisor_stderr.log
user=your_user
autostart=true
PYTHONPATH="$PYTHONPATH:your_python_path";OAUTHLIB_INSECURE_TRANSPORT='1';
Here we have set your project to the supervisor. save this file now we have upgraded the supervisor so that supervisor can identify that we have new service added. Use the following command to upgrade.
sudo service supervisor update
sudo service supervisor restart
Bingo your server is live for forever.
No comments: