> For the complete documentation index, see [llms.txt](https://docs.alistairtweed.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.alistairtweed.com/rails/manually-deploying-a-rails-6-application-on-ubuntu-18.04-digitalocean/application-configuration.md).

# Application Configuration

{% hint style="info" %}
The commands in the **Application Configuration** section should be run by the non-root user.
{% endhint %}

## Git

Download your Rails application to your home directory:

```bash
git clone <URL>
```

In the future, simply download the new commits from the remote repository:

```bash
git pull
```

## Rails

{% hint style="danger" %}
Make sure you create `config/master.key` and copy the file contents from your local machine.
{% endhint %}

{% hint style="info" %}
If your CSS is compiled using Webpacker, make sure to use the `stylesheet_pack_tag`.
{% endhint %}

Prepare the application for running in production using the following commands:

```bash
RAILS_ENV=production bundle install
```

```bash
RAILS_ENV=production rails assets:precompile
```

```bash
RAILS_ENV=production rails db:create
```

```bash
RAILS_ENV=production rails db:migrate
```

```bash
RAILS_ENV=production rails db:seed
```

{% hint style="danger" %}
In the future, you should skip the database creation and seed steps.
{% endhint %}

## Nginx

Using the template below, create a new Nginx configuration file:

```bash
sudo vim /etc/nginx/sites-enabled/<APPLICATION_NAME>
```

```bash
server {
  server_name <DOMAIN_NAME>;
  listen 80;
  root /home/<USERNAME>/<APPLICATION_FOLDER>/public;
  try_files /maintenance.html $uri $uri/index.html $uri.html @app;

  location ~ /.well-known {
    allow all;
  }

  location @app {
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;
    proxy_pass http://localhost:3000;
  }
}
```

Restart Nginx:

```bash
sudo service nginx restart
```

## Systemd

Using the template below, create a new Systemd initialisation file:

```bash
sudo vim /etc/systemd/system/<APPLICATION_NAME>.service
```

```bash
[Unit]
Description=<APPLICATION_NAME>
Requires=network.target

[Service]
Type=simple
User=<USERNAME>
Group=<USERNAME>
WorkingDirectory=/home/<USERNAME>/<APPLICATION_FOLDER>/
Environment=PATH=/home/<USERNAME>/.rbenv/plugins/ruby-build/bin:/home/<USERNAME>/.rbenv/shims:/home/<USERNAME>/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ExecStart=/bin/bash -lc 'bundle exec puma -e production -b 0.0.0.0 -p <PORT>'
TimeoutSec=30s
RestartSec=30s
Restart=always

[Install]
WantedBy=multi-user.target
```

Enable the service:

```bash
sudo systemctl daemon-reload
```

```bash
sudo systemctl enable <APPLICATION_NAME>.service
```

```bash
sudo service nginx restart
```

You can restart and check the status of the service using the following commands:

```bash
sudo systemctl restart <APPLICATION_NAME>.service
```

```bash
sudo systemctl status <APPLICATION_NAME>.service
```

```bash
sudo journalctl -xe
```
