Skip to content

Commit

Permalink
Update procedure, adding remote repository
Browse files Browse the repository at this point in the history
  • Loading branch information
lnfel committed Dec 9, 2019
1 parent 5e2e983 commit 6705447
Showing 1 changed file with 163 additions and 1 deletion.
164 changes: 163 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,169 @@ One of the best and cheapest VPS hosting you can find out there.
3. **Create a Droplet on Digitalocean**
You can create a droplet (virtual private server) with Dokku pre-installed! When creating a droplet, select the Marketplace and look for Dokku, [add your SSH keys](https://timleland.com/copy-ssh-key-to-clipboard/) and for testing you can select the lowest and cheapest plan.

4. **Finish Dokku setup**
4. **Go to your server's IP and follow the web installer**
Navigate to your droplet's IP address which will be listed in digitalocean. You will need to paste in your public ssh key, then make sure to check "Virtual host naming" for your apps. It means that if you create an app called _myapp_, it will be accessible at _myapp.mydomain.com_

## Creating a Dokku app

5. **SSH onto your server**

```console
ssh [email protected]
```

6. **Update everything** (Digital Ocean's droplets will not be completely up to date, and Dokku can easily be a few versions behind)

```console
apt update && apt upgrade
```

7. **Create the app**

```console
dokku apps:create yourappname
```

8. **Install Postgres, create and link database**

```console
dokku plugin:install https://github.com/dokku/dokku-postgres.git
dokku postgres:create yourdbname
dokku postgres:link yourdbname yourappname
```

9. **Create a swap file** to help out on the ram front. You will only see output after the 3rd line.

```console
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
```

10. **Open fstab with Vim**

```console
vi /etc/fstab
```

11. **Add this line at the bottom**

```console
/swapfile none swap sw 0 0
```

* Press "i" to enter edit mode.
* Press "esc" after finish adding the line to exit edit mode
* Press "shift+:" then type "wq" to write file and quit editor

12. **Configure Digitalocean DNS**
Click Add domain on digitalocean then add the domain you have got freely on freenom or bought on namecheap.
Create 'A' record for your domain:

Type | Hostname | Value
---- | -------- | -----
A | yourdomain.com | (select your droplet)

## Rails app

1. **Creating the app**

```ruby
rails new yourrailsapp --database=postgresql
```

2. **Navigate to your app directory and generate a controller**

```ruby
cd awesomeapp
rails generate controller Static index
```

* Here we got a controller name Static with index method
* Add something to that index page in app/views
* then add the route to config/routes.rb

```ruby
root 'static#index'
```

3. **Automatic Migrations**
This one just runs `rails db:migrate` automatically.
Create `app.json` in the root directory of your app

```json
{
"name": "awesomeapp",
"description": "My awesome Rails app, running on Dokku!",
"keywords": [
"dokku",
"rails"
],
"scripts": {
"dokku": {
"postdeploy": "bundle exec rails db:migrate"
}
}
}
```

4. **Add checks**
This feature is rather nice, it makes Dokku check to make sure that your freshly uploaded code actually starts up before switching over to it!
Create a file called `CHECKS` in the root of your project directory

```ruby
# CHECKS

WAIT=10
ATTEMPTS=6
/check.txt it_works
```

* Add the following route to config/routes.rb

```ruby
get '/check.txt', to: proc {[200, {}, ['it_works']]}
```

* And it will make a call to that route when it starts up your new code, thereby ensuring that the new server actually started.

5. **Add your secrets file**

```ruby
# config/secrets.yml
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
```
* on Rails 6 we now also have encryption for the secret key base file in the form of the master.key file which decrypts the file called credentials.yml.enc that holds your secret_key_base
* the 2 file will be generated upon executing `rails new` command at the start of creating your rails app.
```ruby
# config/database.yml
production:
adapter: postgresql
secret_key_base: <%= Rails.application.credentials.dig(:secret_key_base) %>
rails_master_key: <%= ENV['RAILS_MASTER_KEY'] %>
encoding: unicode
pool: 5
```
* It is recommended not to include your master.key when pushing to repo. By default it is included on .gitignorefile
* Without the master.key, the application won't run
* Locate ythe master key on same directory as credentials.yml.enc `config/master.key` then copy the hash string
* Since it will not be included when pushing to our dokku vps, we will save it on ENV variable instead and access it from the variable. ssh to your droplet then run the following dokku command:
```console
dokku config:set yourappname RAILS_MASTER_KEY=thehashstringfromyourmasterkeyfile
```
6. **Set up Puma correctly**
If you are using Rails 6, [take a look at this post](https://www.alanvardy.com/posts/38) to save yourself some headache in getting Puma up and running.
7. **Add remote repository**
Navigate to your yourrailsapp project directory and add the repository
```git
git remote add dokku [email protected]:yourappname
```

0 comments on commit 6705447

Please sign in to comment.