ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer How-Tos & Tutorials

Install supervisor on CentOS 7 and setup multiple processes for web apps

Updated on May 24, 2017

First of all, update your server.

$ sudo yum update

To install supervisor, you first need to install easy_install and then supervisor.

# If any error occurs during installation, you can tail system logs with
$ sudo tail -f /var/log/messages

# To install easy_install 
$ sudo yum install python-setuptools

# Install supervisor
$ sudo easy_install supervisor

# Check if it's correctly installed
$ supervisord --version
3.3.1

# Load the configuration files
$ sudo echo_supervisord_conf > /etc/supervisord.conf

# (If you are not root and get an error for the command above, use below command to change to root account if you are in sudoers list. 
$ sudo su -

# If you are in root account and run the echo_supervisord_conf command you can exit from root account by typing
$ exit

After loading the configuration files, we need to create a service in operating system so that we can control supervisor with systemctl. For that create a new file named supervisord.service in /usr/lib/systemd/system folder and you can copy all the codes from https://github.com/Supervisor/initscripts/blob/master/centos-systemd-etcs. You can also find init scripts for other operating systems in the repository.

If you created the service for supervisor, start it with

$ sudo systemctl start supervisord

look for supervisor.conf file in /etc folder. If it's there you successfully installed supervisor. Now we will add a few new processes and to keep our configuration files clean, I suggest that you keep them in a path like /etc/supervisord/conf.d

# Open /etc/supervisord.conf file and add below lines at the end of file

[include]
files = supervisord/conf.d/*.conf

Save and exit.

# Restart supervisor
$ sudo systemctl restart supervisord

Now, create supervisord/conf.d folder that will contain your processes and we can start to write one program for supervisor

# Create a folder named supervisord in etc folder
$ cd /etc
$ sudo mkdir supervisord
$ cd supervisord
$ sudo mkdir conf.d

# Go to conf.d folder and create a file named demo.conf
$ cd conf.d
$ sudo touch demo.conf

# Edit the demo.conf file

In supervisor, a seperate program should be defined for each process. For example if you need to run a webserver, a redis instance and a sidekiq instance, you should define 3 programs.

Once you define your programs, you need to list them in a group, then you can start your processes.

Now consider a small Ruby on Rails app running with two processes, webserver and sidekiq. For this app, a simple configuration would be something like below. To see all available options and descriptions, check supervisor.conf file in /etc folder

[program:demo_app-web-1]
command=/usr/local/rvm/gems/ruby-2.3.1/wrappers/bundle exec unicorn -c config/unicorn.rb
autostart=true
autorestart=true
stopsignal=QUIT
stdout_logfile=/var/www/apps/demoapp/shared/log/web-1.log
stderr_logfile=/var/www/apps/demoapp/shared/log/web-1.error.log
user=appuser
directory=/var/www/apps/demoapp/current
environment=RAILS_ENV="production",PORT="1234",DEMO_APP_DATABASE_PASSWORD="anypassword"



[program:demo_app-sidekiq-1]
command=/usr/local/rvm/gems/ruby-2.3.1/wrappers/bundle exec sidekiq -e production -C config/sidekiq.yml
autostart=true
autorestart=true
stopsignal=QUIT
stdout_logfile=/var/www/apps/demoapp/shared/log/sidekiq-1.log
stderr_logfile=/var/www/apps/demoapp/shared/log/sidekiq-1.error.log
user=appuser
directory=/var/www/apps/demoapp/current
environment=RAILS_ENV="production",PORT="1234",DEMO_APP_DATABASE_PASSWORD="anypassword"

[group:demo_app]
programs=demo_app-web-1,demo_app-sidekiq-1

After having a set of defined programs and a group for you app, save demo.conf file and exit.

Now since we added a new program configurations, we need to run supervisor update command. If we would only change some configurations instead of having new ones, we could use supervisor reread command

To relaod the configuration files

# make sure supervisor is running
$ ps aux | grep supervisord

# Reload config and add/remove as necessary
$ sudo supervisorctl update

# Reload configuration files for the running deamon's
$ sudo supervisorctl reread

# See https://github.com/Supervisor/supervisor/issues/714
# for any confusion

# Enter supervisor control
$ sudo supervisorctl

# Now in supervisor
supervisor> status
demo_app:demo_app-web-1       RUNNING pid 47089, uptime 1 day, 14:37:34
demo_app:demo_app-sidekiq-1   RUNNING pid 47085, uptime 1 day, 14:37:34

# If your app is not running, you can check the log file that you have defined in demo.conf

# To see the status of one specific app you can
supervisor> status demo_app:*

# or
supervisor> status demo_app:demo_app-web-1

# You can restart/start/stop your apps same way like
supervisor> restart demo_app:*
supervisor> stop demo_app:*
supervisor> start demo_app:*

# reread/update
supervisor> reread
supervisor> update

© 2017 vacancier

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.