How to Setup Sentry with Python on Ubuntu 18.04

Sentry is a free and open source Python application for error tracking. It can be used to monitor and fix crashes in real time. Sentry alerts you via email and SMS when errors occur or resurface. It can be integrated with a lot of applications including, Bitbucket, GitHub, GitLab, Jira, Trello, Redmine and more.

In this tutorial, we will learn how to setup Sentry with Python on Ubuntu 18.04.

Requirements

  • A server running Ubuntu 18.04.
  • A root password setup on your server.

Update Your Server

Before starting, it is recommended to update your packages with the latest version. You can update all your packages with the following command:

# apt-get update -y
# apt-get upgrade -y

Once the system is updated, restart your server to apply all the configuration changes.

Install Required Packages

Next, you will need to install some packages required by Sentry. You can install all of them with the following command:

# apt-get install python build-essential python-setuptools python-dev libxslt1-dev gcc libffi-dev libjpeg-dev libxml2-dev libxslt-dev libyaml-dev libpq-dev python-pip -y

Once all the packages are installed, Download the latest version of Redis with the following command:

# wget http://download.redis.io/releases/redis-4.0.1.tar.gz

After downloading, unzip the downloaded file with the following command:

# tar -xvf redis-4.0.1.tar.gz

Next, change the directory to redis-4.0.1 and install it with the following command:

# cd redis-4.0.1
# make

Next, run the Redis in the background with the following command:

# src/redis-server --daemonize yes

Next, install the Python virtual environment with the following command:

# pip install -U virtualenv

Install and Configure PostgreSQL

By default, the latest version of PostgreSQL is not available in the Ubuntu 18.04 default repository. So, you will need to add the repository for that.

You can do it with the following command:

# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

Next, update the repository and install PostgreSQL with the following command:

# apt-get update -y
# apt-get install postgresql-9.5 -y

Next, log in with Postgres user and enable the citext extension with the following command:

# su - postgres
# psql -d template1 -U postgres
# template1=# create extension citext;

Next, create the database for sentry with the following command:

# createdb sentrydb
# createuser sentry --pwprompt
# psql -d template1 -U postgres

Next, grant all the privileges to Sentry database with the following command:

# template1=# GRANT ALL PRIVILEGES ON DATABASE sentrydb to sentry;
# template1=# ALTER USER sentry WITH SUPERUSER;

Finally, exit from the Postgres with the following command:

# template1=# \q

Install Sentry

First, create a user for Sentry with the following command:

# adduser sentry

Next, login with Sentry user and create a virtual environment for Sentry with the following command:

# su - sentry
# virtualenv ~/sentry_app/

Next, activate the virtual environment with the following command:

# source ~/sentry_app/bin/activate

Next, install Sentry with the following command:

# pip install -U sentry

Next, initialize Sentry with the following command:

# sentry init

The above command will create a Sentry configuration file at ~/.sentry/sentry.conf.py.

Now, open the configuration file as shown below:

 nano ~/.sentry/sentry.conf.py 

Update the following lines as per your database:

DATABASES = {

   'default': {

    'ENGINE': 'sentry.db.postgres',

    'NAME': 'sentrydb',

    'USER': 'sentry',

    'PASSWORD': 'password',

    'HOST': 'localhost',

    'PORT': '5432',

    'AUTOCOMMIT': True,

    'ATOMIC_REQUESTS': False,

    }

  }

Save the file. Then, initialize the database with the following command:

# sentry upgrade

Next, exit from the Sentry user with the following command:

# exit

Configure Sentry to Run as a Service

Next, you will need to install and configure the Supervisor to start Sentry automatically after system reboot.

First, install the Supervisor with the following command:

# apt-get install supervisor -y

Next, create the Sentry configuration file with the following command:

# nano /etc/supervisor/conf.d/sentry.conf

Add the following lines:

[program:sentry-web]

directory=/home/sentry/sentry_app/

environment=SENTRY_CONF="/home/sentry/.sentry"

command=/home/sentry/sentry_app/bin/sentry run web

autostart=true

autorestart=true

redirect_stderr=true

user=sentry

stdout_logfile=syslog

stderr_logfile=syslog



[program:sentry-worker]

directory=/home/sentry/sentry_app/

environment=SENTRY_CONF="/home/sentry/.sentry"

command=/home/sentry/sentry_app/bin/sentry run worker

autostart=true

autorestart=true

redirect_stderr=true

user=sentry

stdout_logfile=syslog

stderr_logfile=syslog



[program:sentry-cron]

directory=/home/sentry/sentry_app/

environment=SENTRY_CONF="/home/sentry/.sentry"

command=/home/sentry/sentry_app/bin/sentry run cron

autostart=true

autorestart=true

redirect_stderr=true

stdout_logfile=syslog

stderr_logfile=syslog

Save and close the file. Then, reload Supervisor with the following command:

# supervisorctl reread
# supervisorctl update
# supervisorctl start all

Sentry is now installed and running on port 9000!

You only have to open your web browser and type the URL http://your-server-ip:9000 and complete the setup.

To write a comment on this article, fill out the form below. Fields marked with an asterisk (*) are required.