# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.alistairtweed.com/rails/manually-deploying-a-rails-6-application-on-ubuntu-18.04-digitalocean/application-configuration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
