# Releasing the site on production

## Setting Boorunaut on Heroku with PostgreSQL

{% hint style="info" %}
Boorunaut is only compatible with **Python 3**. [Check which version](https://stackoverflow.com/a/8917907) is instaled with the command _python_. You may be required to use the commands _**python3**_ and _**pip3**_ instead of _python_ and _pip_.
{% endhint %}

To install Boorunaut on a Heroku server, you will need to make some changes on the default settings to make it work with PostgreSQL.

First, create a project for your site:

```bash
pip install Boorunaut
boorunaut startproject mysite
```

Once it's done, you need to add Heroku-related files. Create a `requirements.txt` file on the root of the project with:

```
Boorunaut==0.1
psycopg2==2.7.6.1
gunicorn==19.9.0
```

{% hint style="info" %}
You will probably want to create a virtual environment here and install the `requirements.txt` with pip to test your project. DigitalOcean has a tutorial for testing on Ubuntu ([link](https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04)).
{% endhint %}

Create a `Procfile` for Heroku:

```
web: gunicorn <mysite>.wsgi --log-file -
```

### Configuring the settings.py file

You will need to make some adjustments to the default settings.py generated to use in production. These involve configurating a new database and transforming sensitive data into environment variables (an security measure).

#### Automatic configuration

Heroku has an library for automating the configuration of Django apps with Heroku sites, [django-heroku](https://devcenter.heroku.com/articles/django-app-configuration). You need to add to your `requirements.txt`:

{% code title="requirements.txt" %}
```
django-heroku==0.3.1
```
{% endcode %}

Then, add to the end of your `settings.py`:

{% code title="settings.py" %}
```python
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())
```
{% endcode %}

#### Manual configuration

Firstly, you will need to turn your SECRET\_KEY into an enviroment variable.

{% code title="settings.py" %}
```python
...
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', "DEFAULT_VALUE")
...
```
{% endcode %}

Then you can change the default SQLite database with a backend better suited for production.

For PostgreSQL (better compatibility with Heroku), replace the `DATABASES` variable content with:

{% tabs %}
{% tab title="Automatic Heroku parsing" %}
{% code title="settings.py" %}
```python
from urllib.parse import urlparse

db_credentials = urlparse(os.environ.get('DATABASE_URL'))
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': db_credentials.path[1:],
        'USER': db_credentials.username,
        'PASSWORD': db_credentials.password,
        'HOST': db_credentials.hostname,
        'PORT': db_credentials.port,
    }
}
```
{% endcode %}
{% endtab %}

{% tab title="Manual separate credentials" %}
{% code title="settings.py" %}
```python
...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_DB'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': os.environ.get('POSTGRES_HOST'),
        'PORT': '5432',
    }
}
...
```
{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
Remember to set environment variables on Heroku with the details of your PostgreSQL database and secret key.
{% endhint %}

Add your website on `ALLOWED_HOSTS`:

{% code title="settings.py" %}
```python
...
ALLOWED_HOSTS = ['<mysite>.herokuapp.com']
...
```
{% endcode %}

### Publishing

Once the configuration is done, you can initialize git on your project (if haven't already), commit the changes and be ready to push it to Heroku.&#x20;

{% hint style="warning" %}
Remember to set `DEBUG` to `False` on `settings.py` before sending to production!
{% endhint %}

