Sentry Installation via Python on Ubuntu Server
Aug 25, 2017
6 minute read

Installing and Configuring Sentry

Sentry is a great tool for error-tracking and monitoring your applications. It is an open source project written in Django (Python). It tracks crashes, exceptions, errors in real time which saves a lot of debugging time especially in production environment. There are other alternatives to Sentry, namely Aibrake.io, Raygun etc.

Sentry being free & open source project, it gives freedom to be set your own way. It also has wide variety of language specific SDK integrations available which makes it a potential choice than its competitors.

Installing and Configuring Sentry is a complicated process in itself. I have tried my best to document these changes which might not be clear to a beginner.

Things to pay attention in this tutorial

  • your@system$: refers to local system
  • root@vultr#: root account login of remote server; check #
  • deploy@vultr$: a new user account on server, denoted by $
  • postgres@vultr$: postgres user gets created when we install postgres
  • ssh-copy-id: command available when we install ssh.
  • wget, cURL etc commands are packages/libraries which should be installed beforehand.

Setup a new server

I strongly recommend using a Vultr VPS server. As you will be given a root account with username and password, try SSH-ing on the server (xxx.xxx.xxx.xxx).

your@system$ ssh [email protected]

Once you are logged in the root account, its time to create a user named deploy. As it is possible to do any administrative task using root account, it is advisable to never use root and always use root privileges by creating user account and adding the user to sudo (superuser do!) group.

root@vultr# adduser deploy

In order to be able to install dependencies on server, created user deploy has to be included in sudo group.

root@vultr# usermod -aG sudo deploy

To be able to login into new server without the need to enter password everytime from your local system, copy the ssh key by running

your@system$ ssh-copy-id -i ~/.ssh/id_rsa [email protected]

This will copy your ssh key to server without any hassles. Now you can log again to your newly created user account.

your@system$ ssh [email protected]

Creating swap memory

Swap memory is a space in hard drive which gets used for operations when the RAM gets full. It is a very important step, as going ahead there will be few tasks which will be memory intensive. It is always a good idea to have swap memory allocated if you don’t want your tasks get aborted in mid way when RAM gets filled up.

# To create 4GB swap:
# Source: https://help.ubuntu.com/community/SwapFaq#How_do_I_add_a_swap_file.3F

sudo fallocate -l 4g /mnt/4GiB.swap
sudo chmod 600 /mnt/4GiB.swap
sudo mkswap /mnt/4GiB.swap
sudo swapon /mnt/4GiB.swap

free -m

# To make swap permanent across reboots:
echo '/mnt/4GiB.swap swap swap defaults 0 0' | sudo tee -a /etc/fstab

Now to the real work

Setting pre-environment

Install python and build-essential package.

  • python is needed for sentry setup
  • build-essential contains required libraries for compiling debian package

Most problems we get are due to some xyz library not installed.

deploy@vultr$ sudo apt-get update
deploy@vultr$ sudo apt-get install -y python build-essential

Setting environment for project

By looking at the official documentation of Sentry, dependencies are:

  • PostgresSQL DB
  • Redis DB
  • Dedicated Domain (in our case its our server [xxx.xxx.xxx.xxx])

Documentation lists two ways of installation

  • via Docker
  • via Python

Let’s now look on how can we install Sentry with the deprecated method using Python:

For PostgreSQL Db:
deploy@vultr$ sudo touch /etc/apt/sources.list.d/pgdg.list

Add the string provided in the link for your ubuntu version(https://www.postgresql.org/download/linux/ubuntu/)

deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main

OR You can alternatively run the following command.

echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" | sudo tee -a /etc/apt/sources.list.d/pgdg.list

# Import the signing key and update package lists
deploy@vultr$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
deploy@vultr$ sudo apt-get update
deploy@vultr$ sudo apt-get install postgresql-9.5

Redis installation:

There are two ways for redis to get installed

  • first
  $ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
    $ tar xzf redis-4.0.1.tar.gz
    $ cd redis-4.0.1
    $ sudo make install
    $ cd utils
    $ sudo ./install_server.sh
  • second
  $ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
  $ tar xzf redis-4.0.1.tar.gz
  $ cd redis-4.0.1
  $ make
  $ src/redis-server

to run in in background :

  $ src/redis-server --daemonize yes

Check if its running via :

  $ ps aux | grep redis-server

Libraries mentioned in dependencies section

deploy@vultr$ sudo apt-get install python-setuptools python-dev libxslt1-dev gcc libffi-dev libjpeg-dev libxml2-dev libxslt-dev libyaml-dev libpq-dev
Virtual python environment

To set up an environment we would need python virtual environment. With virtual environment, we can run applications on multiple versions of python.

There are two ways you can install it. I prefer to get the latest one, via pip.

  1. via python package manager (pip)
Get pip installed via `cURL`:

`curl -o get-pip.py https://bootstrap.pypa.io/get-pip.py` &

`sudo python get-pip.py`
  1. the other via ubuntu package manager (apt).
$ sudo pip install -U virtualenv

OR

$ sudo apt install python-virtualenv
  • Choose the location of virtual environment for sentry:
Documentation says we can install on /www/sentry folder, I have installed it on  ~/sentry path to avoid using `sudo pip` 
$ virtualenv ~/sentry/
  • To activate your virtualenv
$source ~/sentry/bin/activate

Installing Sentry:

$ pip install -U sentry

Check if the installation is properly done by typing sentry

Configuring Sentry:

We need a default configuration which gets initialized using

# path if not provided automatically takes ~/.sentry
$ sentry init

init provides 2 types of files for configuration sentry.conf.py and config.yml.

For default installation, we don’t need to change the settings

Running Migrations:

Before running migrations, create a DB by createdb command by logging into postgres user

deploy@vultr$ sudo su - postgres
postgres@vultr$ createdb -E utf-8 sentry

Once done, initial schema can be created by upgrade command. Run the command after coming back to deploy user. Our swap memory will be very helpful here.

$ SENTRY_CONF=~/.sentry sentry upgrade

You may get the following error.

  AttributeError: 'NoneType' object has no attribute 'connection_pool' 

Official link to this error is https://github.com/getsentry/sentry/issues/5908 pip install redis==2.10.5. It was caused by an upgrade in Redis client driver that broke the way Redis scripts get loaded in sentry.

You may also get following postgres error.

  OperationalError: FATAL:  Peer authentication failed for user "postgres"

Go to the file pg_hba.conf file (/etc/postgresql/9.5/main/pg_hba.conf) As of now I am changed it to trust , anyone who can connect to the server will be authorized to access the database with whatever database user name they specify (even superuser names). You should change it to md5 when deploying to production

  local                   all                postgres                    trust

Official Documentation

Don’t forget to restart the postgres service

  sudo service postgresql restart

Next is creation of the first user which will act as superuser to the DB

$ SENTRY_CONF=~/.sentry sentry createuser

Starting Sentry as web service:

$ SENTRY_CONF=~/.sentry sentry run web

You should be able to see the prompt on http://xxx.xxx.xxx.xxx:9000

Starting background workers:

$ SENTRY_CONF=~/.sentry sentry run worker


comments powered by Disqus