Next, we launch a small tutorial on how to install Ruby on Rails and deploy Capistrano. Starting with a newly installed Ubuntu (14.04 or 16.04) machine, steps to install several Ruby on Rails projects with nginx and unicorn.
We access by ssh with the initial credentials that they gave us with the server.
Package repository update
sudo apt-get update
sudo apt-get upgrade
Language and time zone package
sudo apt-get install language-pack-es
sudo cp /usr/share/zoneinfo/Europe/Madrid /etc/localtime
We are with the user << root >> by default, we must create a user with privileges. For this tutorial, we will use the << web >> user. We execute:
sudo mkdir /home/web
sudo groupadd web
sudo useradd -d /home/web -s /bin/bash -g web web
sudo chown -R web:web /home/web
sudo passwd web
You will be prompted to enter and confirm a password for this user.
Then we give privileges to install the dependencies of Ruby on Rails.
sudo adduser web sudo
Now we force the web user to be able to scale to root without entering the root password
At the end of the file we add this line:
web ALL=(ALL) NOPASSWD: ALL
Since this time the editor is nano, we save with Ctrl + X and we accept
You can use any text editor. For this tutorial we will use vi. We execute:
sudo vi /etc/ssh/sshd_config
We look for the commented line “AuthorizedKeysFile %h/.ssh/authorized_keys” and uncomment it. If it does not exist, we create it.
We keep changes and close the editor. Restart SSH, running:
sudo /etc/init.d/ssh restart
We connect as the new web user.
To facilitate the process, we will make the server ‘know’ each time we connect to it and do not ask for the password. It is a safe operation since it will only affect the equipment from which we are connecting. For this we must know the IP of the server and the password that we assigned to the user ‘web’ a few steps back in this tutorial.
We will ask for the web user’s password. We write it.
If we are using MAC OS, we must first add the following command on our computer: brew install ssh-copy-id
Now we can access our server without a password:
Once inside the server again, we generate key pair the SSH of the user ‘web’ to add to our repository, executing:
ssh-keygen -t rsa
Choose a destination or leave it by default “/home/web/.ssh/id_rsa“. Do not assign any password, simply press Enter
We follow the rest of the tutorial with this web user.
Install required packages
sudo apt-get install mysql-server libmysqlclient-dev libmagickwand-dev imagemagick nginx nodejs git-core curl build-essential openssl libreadline6 libreadline6-dev curl zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt-dev autoconf libc6-dev nmon
We will ask for a password for the mysql root user, and we will confirm it. We must remember it.
Programming mySQL automatic backups
We should probably use mysql as a database on the system. It is good practice for daily back up existing databases to prevent or mitigate disasters that may occur. For this we will use automysqlbackup. This script is automatically installed on the system and maintains daily, weekly and monthly copies of all our databases.
mkdir -p /home/web/apps/bbdd
sudo apt-get install automysqlbackup
The installation of this package requires the installation of a postfix server to send emails with the results of the backup. For now we can leave it as ‘No configuration’.
Since we use the folder /home/ web to store all our projects, it seems a good idea to also leave backups of the databases. That way, if our project manages images, uploads or any other type of file that the users of our project contribute, all ‘important’ will be inside ‘home/web’. Taking a backup of that directory, we will have our projects safe and complete.
sudo vi /etc/default/automysqlbackup
There we will locate the line: BACKUPDIR=”…….” And we will replace it with this: BACKUPDIR=”/home/web/apps/bbdd” y agregamos POSTBACKUP=”chmod -R 644 /home/web/apps/bbdd/”
Let’s try and verify that the daily backups are going to be made
There will be some folders (daily / weekly / monthly) that will store the databases. At least one copy of the database that we created earlier should exist in the daily folder.
Also added a task in cron.daily so that daily backups are made without us having to do anything.
Now every day will run a backup of all MySQL databases (current and future) and will be stored in our deploys folder.
Creating MySQL BD
We access the MySQL console by running:
mysql -u root -p
Enter the password we generated earlier.
For this tutorial, we will use “database_proof” as a sample database, and the MySQL root password will be “pass_proof”.
We create the database and give privileges executing:
CREATE DATABASE database_proof;
GRANT ALL PRIVILEGES ON database_proof.* TO root@localhost IDENTIFIED BY ‘pass_proof’;
Important: If you are copying and pasting the code from this tutorial, the single quotation marks must be put manually.
We leave with “exit”
Connection with repository
Add the key SSH in the repository git for deploy.
We copy the content of the file on /home/web/.ssh/id_rsa.pub in the section ‘Deployment keys’ in our code repository. We can visualize it with this command:
Once the deploy key is added, we make sure that the repository recognizes us.
ssh -T firstname.lastname@example.org
Since it is probable that in the same server several different projects exist and these projects may have different versions of ruby, we will install rvm to be able to manage the different versions of ruby and the different gemsets of each project more easily.
We install rvm.
curl -L https://get.rvm.io | bash -s stable
The first time informs us that the download can only be done if we have added the GPG key. It will ask us to execute a command similar to this: (Execute the one that appears in the screen, not this example)
gpg –keyserver hkp://keys.gnupg.net –recv-keys 409B6B1796C275462xxxxxxxxxxxxxxx
curl -L https://get.rvm.io | bash -s stable
The rvm installer tells us at the end that we should run this line in order to use rvm immediately:
If we know the version of ruby that our first project uses we can leave it already downloaded.
rvm install ruby-2.3.1
Since the gem bundler needs the system to be able to deploy, we install it:
gem install bundler
The system is now ready to receive the project and deploy.
In this repository you can find the appropriate Capistrano recipe and the instructions to add it to your project.