Rails in Production on Passenger

I have a project to measure temperature and humidity information and then do useful things with it. Right now I need to get the Rails part of my app "stood up" on a server so that it's not constantly in development mode. At the very least I want the web service to be offered on the standard TCP port 80 so I can use HTTP protocol without a port number specifier. Gee, it would be kind of nice too if the service came alive when the server machine boots.

I have been using a procedure at PhusionPassenger.com (this one) but I've tripped over a few things and it didn't work out of the box. If you're looking for a procedure to stand up your Rails app on Passenger you might like to try that one first. It is from Passenger's creators after all. If that doesn't work straight out of the box you might want to try mine.


Disclaimers:

  • All due respect to Phusion. This is not an attempt to republish their work. My own attempts to make their procedure work resulted in a complete re-write and so I thought it was deserving of a post.
  • I've chosen to host on an old netbook which is not exposed to the Internet. I don't recommend you use this procedure and then connect the server to the Internet without first performing some rigorous system hardening.
  • I've used a virtual machine (Parallels 11 on macOS Sierra) for the development of this procedure. This should not affect the outcome. Any machine physical or virtual should do as long as you can install Ubuntu 16.04.2 LTE Desktop or Server onto it.

Before we start, we have a laundry list of settings we'll use. You'll likely have your own values for each of these. I'm documenting mine up front so you can tell what I'm doing later. We'll set most of this up as we go along.

Operating System Ubuntu 16.04.2 LTS Desktop
IP address 10.211.55.15
Application Name Conditions
Application Path /var/www/Conditions/code/ConditionsServer
Administrator user account prawnhead
Administrator password 9Mond4y
Rails application user account conditionsuser
Rails application user password 8Tu3sday
MySQL administrator user account root
MySQL administrator password 7W3dnesday
MySQL application user account conditionsUser
MySQL application user password 6Thur5day
Secret Key Base Env Variable rvmSecretKeyBase
Database Password Env Variable rvmConditionsDatabaseUserPassword

I won't cover operating system installation or virtual host installation. It might be useful to you to connect to the machine remotely for this procedure. You can ignore this step if you're just going to use the console of a physical machine or the virtual screen of a virtual machine. If you have not already done so, install SSH.

 prawnhead@ubuntu:~$ sudo apt-get install openssh-server  
 [sudo] password for prawnhead:   
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following packages were automatically installed and are no longer required:  
  linux-headers-4.8.0-36 linux-headers-4.8.0-36-generic  
  linux-image-4.8.0-36-generic linux-image-extra-4.8.0-36-generic snap-confine  
 Use 'sudo apt autoremove' to remove them.  
 Suggested packages:  
  ssh-askpass rssh molly-guard monkeysphere  
 The following NEW packages will be installed:  
  openssh-server  
 0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.  
 Need to get 0 B/338 kB of archives.  
 After this operation, 912 kB of additional disk space will be used.  
 Preconfiguring packages ...  
 Selecting previously unselected package openssh-server.  
 (Reading database ... 244535 files and directories currently installed.)  
 Preparing to unpack .../openssh-server_1%3a7.2p2-4ubuntu2.2_amd64.deb ...  
 Unpacking openssh-server (1:7.2p2-4ubuntu2.2) ...  
 Processing triggers for ufw (0.35-0ubuntu2) ...  
 Processing triggers for systemd (229-4ubuntu17) ...  
 Processing triggers for ureadahead (0.100.0-19) ...  
 Processing triggers for man-db (2.7.5-1) ...  
 Setting up openssh-server (1:7.2p2-4ubuntu2.2) ...  
 prawnhead@ubuntu:~$

So that you know the IP address to connect to you can run ifconfig:

 prawnhead@ubuntu:~$ ifconfig  
 enp0s5  Link encap:Ethernet HWaddr 00:1c:42:87:b9:ef   
      inet addr:10.211.55.15 Bcast:10.211.55.255 Mask:255.255.255.0  
      inet6 addr: fe80::4517:3282:585b:a28a/64 Scope:Link  
      inet6 addr: fdb2:2c26:f4e4:0:dd94:a7a7:cd24:16d9/64 Scope:Global  
      inet6 addr: fdb2:2c26:f4e4:0:4641:1a4e:a395:b98b/64 Scope:Global  
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1  
      RX packets:72 errors:0 dropped:0 overruns:0 frame:0  
      TX packets:153 errors:0 dropped:0 overruns:0 carrier:0  
      collisions:0 txqueuelen:1000   
      RX bytes:8337 (8.3 KB) TX bytes:17237 (17.2 KB)  
   
 lo    Link encap:Local Loopback   
      inet addr:127.0.0.1 Mask:255.0.0.0  
      inet6 addr: ::1/128 Scope:Host  
      UP LOOPBACK RUNNING MTU:65536 Metric:1  
      RX packets:257 errors:0 dropped:0 overruns:0 frame:0  
      TX packets:257 errors:0 dropped:0 overruns:0 carrier:0  
      collisions:0 txqueuelen:1   
      RX bytes:19858 (19.8 KB) TX bytes:19858 (19.8 KB)  
   
 prawnhead@ubuntu:~$

Now on your development machine you can use SSH client from a bash prompt or use PuTTY on Windows. See: http://arduinoetcetera.blogspot.com.au/2017/04/the-raspberry-pi-basics.html Use certificate authentication to get rid of the need to log in.

 ~: ssh prawnhead@10.211.55.15  
 Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.8.0-49-generic x86_64)  
   
  * Documentation: https://help.ubuntu.com  
  * Management:   https://landscape.canonical.com  
  * Support:    https://ubuntu.com/advantage  
   
 17 packages can be updated.  
 7 updates are security updates.  
   
 Last login: Wed Jun 7 16:50:40 2017 from 10.211.55.2  
 prawnhead@ubuntu:~$

Update the operating system. Run:
  • sudo apt-get update
  • sudo apt-get upgrade -y
  • sudo apt autoremove -y
 prawnhead@ubuntu:~$ sudo apt-get update  
 [sudo] password for prawnhead:   
 Ign:1 http://dl.google.com/linux/chrome/deb stable InRelease      
 Hit:2 http://us.archive.ubuntu.com/ubuntu xenial InRelease       
 Get:3 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]         
 Get:4 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]    
 Get:5 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]        
 Get:6 http://security.ubuntu.com/ubuntu xenial-security/main amd64 DEP-11 Metadata [54.6 kB]  
 Hit:7 http://dl.google.com/linux/chrome/deb stable Release                 
 Get:9 http://security.ubuntu.com/ubuntu xenial-security/main DEP-11 64x64 Icons [45.7 kB]  
 Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 DEP-11 Metadata [35.2 kB]  
 Get:11 http://security.ubuntu.com/ubuntu xenial-security/universe DEP-11 64x64 Icons [46.7 kB]  
 Get:12 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 DEP-11 Metadata [299 kB]  
 Get:13 http://us.archive.ubuntu.com/ubuntu xenial-updates/main DEP-11 64x64 Icons [195 kB]  
 Get:14 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 DEP-11 Metadata [162 kB]  
 Get:15 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe DEP-11 64x64 Icons [203 kB]  
 Get:16 http://us.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 DEP-11 Metadata [2,520 B]  
 Get:17 http://us.archive.ubuntu.com/ubuntu xenial-backports/main amd64 DEP-11 Metadata [3,328 B]  
 Get:18 http://us.archive.ubuntu.com/ubuntu xenial-backports/universe amd64 DEP-11 Metadata [3,976 B]  
 Fetched 1,357 kB in 7s (182 kB/s)                              
 Reading package lists... Done  
 prawnhead@ubuntu:~$    

 prawnhead@ubuntu:~$ sudo apt-get upgrade -y  
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 Calculating upgrade... Done  
 The following packages were automatically installed and are no longer required:  
  linux-headers-4.8.0-36 linux-headers-4.8.0-36-generic linux-image-4.8.0-36-generic  
  linux-image-extra-4.8.0-36-generic snap-confine  
 Use 'sudo apt autoremove' to remove them.  
 The following packages have been kept back:  
  gnome-software gnome-software-common linux-generic-hwe-16.04 linux-headers-generic-hwe-16.04  
  linux-image-generic-hwe-16.04 python3-software-properties software-properties-common software-properties-gtk  
  ubuntu-software  
 0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.  
 prawnhead@ubuntu:~$

 prawnhead@ubuntu:~$ sudo apt autoremove -y  
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following packages will be REMOVED:  
  linux-headers-4.8.0-36 linux-headers-4.8.0-36-generic linux-image-4.8.0-36-generic linux-image-extra-4.8.0-36-generic snap-confine  
 0 upgraded, 0 newly installed, 5 to remove and 9 not upgraded.  
 After this operation, 315 MB disk space will be freed.  
 (Reading database ... 244547 files and directories currently installed.)  
 Removing linux-headers-4.8.0-36-generic (4.8.0-36.36~16.04.1) ...  
 Removing linux-headers-4.8.0-36 (4.8.0-36.36~16.04.1) ...  
 Removing linux-image-extra-4.8.0-36-generic (4.8.0-36.36~16.04.1) ...  
 run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 run-parts: executing /etc/kernel/postinst.d/dkms 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 update-initramfs: Generating /boot/initrd.img-4.8.0-36-generic  
 run-parts: executing /etc/kernel/postinst.d/pm-utils 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 run-parts: executing /etc/kernel/postinst.d/update-notifier 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 run-parts: executing /etc/kernel/postinst.d/zz-update-grub 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 Generating grub configuration file ...  
 Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.  
 Found linux image: /boot/vmlinuz-4.8.0-49-generic  
 Found initrd image: /boot/initrd.img-4.8.0-49-generic  
 Found linux image: /boot/vmlinuz-4.8.0-46-generic  
 Found initrd image: /boot/initrd.img-4.8.0-46-generic  
 Found linux image: /boot/vmlinuz-4.8.0-36-generic  
 Found initrd image: /boot/initrd.img-4.8.0-36-generic  
 Found memtest86+ image: /boot/memtest86+.elf  
 Found memtest86+ image: /boot/memtest86+.bin  
 done  
 Removing linux-image-4.8.0-36-generic (4.8.0-36.36~16.04.1) ...  
 Examining /etc/kernel/prerm.d.  
 run-parts: executing /etc/kernel/prerm.d/dkms 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 dkms: removing: parallels-tools 11.2.3.32663 (4.8.0-36-generic) (x86_64)  
   
 -------- Uninstall Beginning --------  
 Module: parallels-tools  
 Version: 11.2.3.32663  
 Kernel: 4.8.0-36-generic (x86_64)  
 -------------------------------------  
   
 Status: Before uninstall, this module version was ACTIVE on this kernel.  
   
 prl_eth.ko:  
  - Uninstallation  
   - Deleting from: /lib/modules/4.8.0-36-generic/updates/dkms/  
  - Original module  
   - No original module was found for this module on this kernel.  
   - Use the dkms install command to reinstall any previous module version.  
   
   
 prl_tg.ko:  
  - Uninstallation  
   - Deleting from: /lib/modules/4.8.0-36-generic/updates/dkms/  
  - Original module  
   - No original module was found for this module on this kernel.  
   - Use the dkms install command to reinstall any previous module version.  
   
   
 prl_fs.ko:  
  - Uninstallation  
   - Deleting from: /lib/modules/4.8.0-36-generic/updates/dkms/  
  - Original module  
   - No original module was found for this module on this kernel.  
   - Use the dkms install command to reinstall any previous module version.  
   
   
 prl_fs_freeze.ko:  
  - Uninstallation  
   - Deleting from: /lib/modules/4.8.0-36-generic/updates/dkms/  
  - Original module  
   - No original module was found for this module on this kernel.  
   - Use the dkms install command to reinstall any previous module version.  
   
 depmod....  
   
 DKMS: uninstall completed.  
 Examining /etc/kernel/postrm.d .  
 run-parts: executing /etc/kernel/postrm.d/initramfs-tools 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 update-initramfs: Deleting /boot/initrd.img-4.8.0-36-generic  
 run-parts: executing /etc/kernel/postrm.d/zz-update-grub 4.8.0-36-generic /boot/vmlinuz-4.8.0-36-generic  
 Generating grub configuration file ...  
 Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.  
 Found linux image: /boot/vmlinuz-4.8.0-49-generic  
 Found initrd image: /boot/initrd.img-4.8.0-49-generic  
 Found linux image: /boot/vmlinuz-4.8.0-46-generic  
 Found initrd image: /boot/initrd.img-4.8.0-46-generic  
 Found memtest86+ image: /boot/memtest86+.elf  
 Found memtest86+ image: /boot/memtest86+.bin  
 done  
 Removing snap-confine (2.25) ...  
 prawnhead@ubuntu:~$

MySQL

While Rails has sqlite built in you would not use it for a production server. MySQL is the logical choice.

Installation

We need the database server, client and the development files. Installation of MySQL server will require you to set the root password. We'll also need git later so we'll install that now.

 prawnhead@ubuntu:~$ sudo apt-get install mysql-server mysql-client libmysqlclient-dev git -y  
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following additional packages will be installed:  
  git-man libaio1 liberror-perl libevent-core-2.0-5 libhtml-template-perl libmysqlclient20 mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7  
  mysql-server-core-5.7 zlib1g-dev  
 Suggested packages:  
  git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch git-cvs git-mediawiki git-svn libipc-sharedcache-perl mailx tinyca  
 The following NEW packages will be installed:  
  git git-man libaio1 liberror-perl libevent-core-2.0-5 libhtml-template-perl libmysqlclient-dev libmysqlclient20 mysql-client mysql-client-5.7 mysql-client-core-5.7 mysql-common  
  mysql-server mysql-server-5.7 mysql-server-core-5.7 zlib1g-dev  
 0 upgraded, 16 newly installed, 0 to remove and 9 not upgraded.  
 Need to get 24.3 MB of archives.  
 After this operation, 198 MB of additional disk space will be used.  
 Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-common all 5.7.18-0ubuntu0.16.04.1 [15.9 kB]  
 Get:2 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libaio1 amd64 0.3.110-2 [6,356 B]  
 Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-client-core-5.7 amd64 5.7.18-0ubuntu0.16.04.1 [6,340 kB]  
 Get:4 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-client-5.7 amd64 5.7.18-0ubuntu0.16.04.1 [1,725 kB]                              
 Get:5 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-server-core-5.7 amd64 5.7.18-0ubuntu0.16.04.1 [7,566 kB]                           
 Get:6 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libevent-core-2.0-5 amd64 2.0.21-stable-2ubuntu0.16.04.1 [70.6 kB]                         
 Get:7 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-server-5.7 amd64 5.7.18-0ubuntu0.16.04.1 [2,554 kB]                              
 Get:8 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 liberror-perl all 0.17-1.2 [19.6 kB]                                            
 Get:9 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 git-man all 1:2.7.4-0ubuntu1.1 [735 kB]                                       
 Get:10 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 git amd64 1:2.7.4-0ubuntu1.1 [3,068 kB]                                      
 Get:11 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libhtml-template-perl all 2.95-2 [60.4 kB]                                         
 Get:12 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libmysqlclient20 amd64 5.7.18-0ubuntu0.16.04.1 [811 kB]                              
 Get:13 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 zlib1g-dev amd64 1:1.2.8.dfsg-2ubuntu4.1 [168 kB]                                 
 Get:14 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libmysqlclient-dev amd64 5.7.18-0ubuntu0.16.04.1 [1,160 kB]                            
 Get:15 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-client all 5.7.18-0ubuntu0.16.04.1 [10.0 kB]                                 
 Get:16 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-server all 5.7.18-0ubuntu0.16.04.1 [10.8 kB]                                 
 Fetched 24.3 MB in 1min 19s (304 kB/s)                                                                         
 Preconfiguring packages ...  
 Selecting previously unselected package mysql-common.  
 (Reading database ... 211032 files and directories currently installed.)  
 Preparing to unpack .../mysql-common_5.7.18-0ubuntu0.16.04.1_all.deb ...  
 Unpacking mysql-common (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package libaio1:amd64.  
 Preparing to unpack .../libaio1_0.3.110-2_amd64.deb ...  
 Unpacking libaio1:amd64 (0.3.110-2) ...  
 Selecting previously unselected package mysql-client-core-5.7.  
 Preparing to unpack .../mysql-client-core-5.7_5.7.18-0ubuntu0.16.04.1_amd64.deb ...  
 Unpacking mysql-client-core-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package mysql-client-5.7.  
 Preparing to unpack .../mysql-client-5.7_5.7.18-0ubuntu0.16.04.1_amd64.deb ...  
 Unpacking mysql-client-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package mysql-server-core-5.7.  
 Preparing to unpack .../mysql-server-core-5.7_5.7.18-0ubuntu0.16.04.1_amd64.deb ...  
 Unpacking mysql-server-core-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package libevent-core-2.0-5:amd64.  
 Preparing to unpack .../libevent-core-2.0-5_2.0.21-stable-2ubuntu0.16.04.1_amd64.deb ...  
 Unpacking libevent-core-2.0-5:amd64 (2.0.21-stable-2ubuntu0.16.04.1) ...  
 Processing triggers for libc-bin (2.23-0ubuntu7) ...  
 Processing triggers for man-db (2.7.5-1) ...  
 Setting up mysql-common (5.7.18-0ubuntu0.16.04.1) ...  
 update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode  
 Selecting previously unselected package mysql-server-5.7.  
 (Reading database ... 211200 files and directories currently installed.)  
 Preparing to unpack .../mysql-server-5.7_5.7.18-0ubuntu0.16.04.1_amd64.deb ...  
 Unpacking mysql-server-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package liberror-perl.  
 Preparing to unpack .../liberror-perl_0.17-1.2_all.deb ...  
 Unpacking liberror-perl (0.17-1.2) ...  
 Selecting previously unselected package git-man.  
 Preparing to unpack .../git-man_1%3a2.7.4-0ubuntu1.1_all.deb ...  
 Unpacking git-man (1:2.7.4-0ubuntu1.1) ...  
 Selecting previously unselected package git.  
 Preparing to unpack .../git_1%3a2.7.4-0ubuntu1.1_amd64.deb ...  
 Unpacking git (1:2.7.4-0ubuntu1.1) ...  
 Selecting previously unselected package libhtml-template-perl.  
 Preparing to unpack .../libhtml-template-perl_2.95-2_all.deb ...  
 Unpacking libhtml-template-perl (2.95-2) ...  
 Selecting previously unselected package libmysqlclient20:amd64.  
 Preparing to unpack .../libmysqlclient20_5.7.18-0ubuntu0.16.04.1_amd64.deb ...  
 Unpacking libmysqlclient20:amd64 (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package zlib1g-dev:amd64.  
 Preparing to unpack .../zlib1g-dev_1%3a1.2.8.dfsg-2ubuntu4.1_amd64.deb ...  
 Unpacking zlib1g-dev:amd64 (1:1.2.8.dfsg-2ubuntu4.1) ...  
 Selecting previously unselected package libmysqlclient-dev.  
 Preparing to unpack .../libmysqlclient-dev_5.7.18-0ubuntu0.16.04.1_amd64.deb ...  
 Unpacking libmysqlclient-dev (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package mysql-client.  
 Preparing to unpack .../mysql-client_5.7.18-0ubuntu0.16.04.1_all.deb ...  
 Unpacking mysql-client (5.7.18-0ubuntu0.16.04.1) ...  
 Selecting previously unselected package mysql-server.  
 Preparing to unpack .../mysql-server_5.7.18-0ubuntu0.16.04.1_all.deb ...  
 Unpacking mysql-server (5.7.18-0ubuntu0.16.04.1) ...  
 Processing triggers for systemd (229-4ubuntu17) ...  
 Processing triggers for ureadahead (0.100.0-19) ...  
 Processing triggers for man-db (2.7.5-1) ...  
 Processing triggers for libc-bin (2.23-0ubuntu7) ...  
 Setting up libaio1:amd64 (0.3.110-2) ...  
 Setting up mysql-client-core-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 Setting up mysql-client-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 Setting up mysql-server-core-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 Setting up libevent-core-2.0-5:amd64 (2.0.21-stable-2ubuntu0.16.04.1) ...  
 Setting up mysql-server-5.7 (5.7.18-0ubuntu0.16.04.1) ...  
 update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode  
 Renaming removed key_buffer and myisam-recover options (if present)  
 Setting up liberror-perl (0.17-1.2) ...  
 Setting up git-man (1:2.7.4-0ubuntu1.1) ...  
 Setting up git (1:2.7.4-0ubuntu1.1) ...  
 Setting up libhtml-template-perl (2.95-2) ...  
 Setting up libmysqlclient20:amd64 (5.7.18-0ubuntu0.16.04.1) ...  
 Setting up zlib1g-dev:amd64 (1:1.2.8.dfsg-2ubuntu4.1) ...  
 Setting up libmysqlclient-dev (5.7.18-0ubuntu0.16.04.1) ...  
 Setting up mysql-client (5.7.18-0ubuntu0.16.04.1) ...  
 Setting up mysql-server (5.7.18-0ubuntu0.16.04.1) ...  
 Processing triggers for libc-bin (2.23-0ubuntu7) ...  
 Processing triggers for systemd (229-4ubuntu17) ...  
 Processing triggers for ureadahead (0.100.0-19) ...  
 prawnhead@ubuntu:~$

Verify server is running

 prawnhead@ubuntu:~$ /etc/init.d/mysql status  
 ● mysql.service - MySQL Community Server  
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)  
   Active: active (running) since Wed 2017-06-07 16:59:58 AEST; 2min 41s ago  
  Main PID: 9447 (mysqld)  
   CGroup: /system.slice/mysql.service  
       └─9447 /usr/sbin/mysqld  
   
 Jun 07 16:59:57 ubuntu systemd[1]: Starting MySQL Community Server...  
 Jun 07 16:59:58 ubuntu systemd[1]: Started MySQL Community Server.  
 prawnhead@ubuntu:~$   

Locate the socket file

 prawnhead@ubuntu:~$ sudo find / -type s | grep mysql  
 [sudo] password for prawnhead:   
 find: ‘/run/user/1000/gvfs’/run/mysqld/mysqld.sock  
 : Permission denied

Configuration

Create the required database and restricted user and verify the correct privileges have been set.

 prawnhead@ubuntu:~$ mysql -u root -p  
 Enter password:   
 Welcome to the MySQL monitor. Commands end with ; or \g.  
 Your MySQL connection id is 6  
 Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)  
   
 Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.  
   
 Oracle is a registered trademark of Oracle Corporation and/or its  
 affiliates. Other names may be trademarks of their respective  
 owners.  
   
 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
   
 mysql> create database Conditions;  
 Query OK, 1 row affected (0.00 sec)  
   
 mysql> create user 'conditionsUser'@'localhost' identified by '6Thur5day';  
 Query OK, 0 rows affected (0.00 sec)  
   
 mysql> grant all privileges on Conditions . * to 'conditionsUser'@'localhost';  
 Query OK, 0 rows affected (0.00 sec)  
   
 mysql> flush privileges;  
 Query OK, 0 rows affected (0.01 sec)  
   
 mysql> create database junk;  
 Query OK, 1 row affected (0.00 sec)  
   
 mysql> exit  
 Bye  
 prawnhead@ubuntu:~$ mysql -u conditionsUser -p  
 Enter password:   
 Welcome to the MySQL monitor. Commands end with ; or \g.  
 Your MySQL connection id is 7  
 Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)  
   
 Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.  
   
 Oracle is a registered trademark of Oracle Corporation and/or its  
 affiliates. Other names may be trademarks of their respective  
 owners.  
   
 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
   
 mysql> create database test;  
 ERROR 1044 (42000): Access denied for user 'conditionsUser'@'localhost' to database 'test'  
 mysql> drop database junk;  
 ERROR 1044 (42000): Access denied for user 'conditionsUser'@'localhost' to database 'junk'  
 mysql> use Conditions;  
 Database changed  
 mysql> create table junk(  
   -> id int not null auto_increment,  
   -> title varchar(100) not null,  
   -> primary key (id));  
 Query OK, 0 rows affected (0.02 sec)  
   
 mysql> drop table junk;  
 Query OK, 0 rows affected (0.00 sec)  
   
 mysql> exit  
 Bye  
 prawnhead@ubuntu:~$ mysql -u root -p  
 Enter password:   
 Welcome to the MySQL monitor. Commands end with ; or \g.  
 Your MySQL connection id is 8  
 Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)  
   
 Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.  
   
 Oracle is a registered trademark of Oracle Corporation and/or its  
 affiliates. Other names may be trademarks of their respective  
 owners.  
   
 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
   
 mysql> drop database junk;  
 Query OK, 0 rows affected (0.00 sec)  
   
 mysql> exit  
 Bye  
 prawnhead@ubuntu:~$   
   

Code

We need to make changes to the code base to prepare for deployment on Passenger in a stand-alone configuration. You might use sqlite or MySQL in development but we'll set up on MySQL for production. Best security practice has us using environment variables to store the most critical security variables; the "Secret Key Base" that secures sessions with the server and the password used to access the database.

Gemfile

We modify the gemfile so that only Passenger is used to serve the app. Comment out other server options if they are present. Ensure "passenger" is present and not commented out. You will also need to include the mysql2 gem. This always gets me into trouble. The suffix I use currently is one I've found works for me at the moment.

 gem "passenger"  
 gem 'mysql2', '>= 0.3.18', '< 0.5'  
   
 # gem "unicorn"  
 # gem "thin"  
 # gem "puma"  

Passenger config file

This file would not have been created by Rails. You need to create a new text file in the root of your Rails application named Passenger.json. Paste in the content below and alter the "user" value to suit your own setting.

 {  
  // Run the app in a production environment. The default value is "development".  
  "environment": "production",  
  // Run Passenger on port 80, the standard HTTP port.  
  "port": 80,  
  // Tell Passenger to daemonize into the background.  
  "daemonize": true,  
  // Tell Passenger to run the app as the given user. Only has effect  
  // if Passenger was started with root privileges.  
  "user": "conditionsuser"  
 }  

Secrets

Ensure the secret_key_base definition is part of the production section of config/secrets.yml. Name the variable anything you like but keep the "rvm" prefix. This is required when we later use the "rvmsudo" command.

 production:  
  secret_key_base: <%= ENV["rvmSecretKeyBase"] %>  

Database

In config/database.yml ensure the production database configuration accesses an environment variable for the database password. Also, ensure the database name, database user name and socket have the correct values as seen above. Again, the environment variable can be named as you like but should start with "rvm".
 production:  
  <<: *default  
  database: Conditions  
  username: conditionsUser  
  password: <%= ENV['rvmConditionsDatabaseUserPassword'] %>  
  socket:  /run/mysqld/mysqld.sock  

Repository Setup

To move the application's code onto the server we will pull from a code repository using git (Github, BitBucket, etc.) If your development code is not already in a git repository now's the time to set that up.

Push code

After making all of the above changes to the development code base, 'bundle install', commit and push your code.

 ~/Versioning/BitBucket/FifthBit/Conditions/ConditionsServer: bundle install  
 Using rake 12.0.0  
 Using concurrent-ruby 1.0.5  
 Using i18n 0.8.1  
 Using minitest 5.10.2  
 Using thread_safe 0.3.6  
 Using builder 3.2.3  
 Using erubis 2.7.0  
 Using mini_portile2 2.1.0  
 Using rack 2.0.2  
 Using nio4r 2.0.0  
 Using websocket-extensions 0.1.2  
 Using mime-types-data 3.2016.0521  
 Using arel 7.1.4  
 Using bindex 0.5.0  
 Using byebug 9.0.6  
 Using coffee-script-source 1.12.2  
 Using execjs 2.7.0  
 Using method_source 0.8.2  
 Using thor 0.19.4  
 Using ffi 1.9.18  
 Using multi_json 1.12.1  
 Using rb-fsevent 0.9.8  
 Using mysql2 0.4.6  
 Using bundler 1.14.6  
 Using sass 3.4.23  
 Using tilt 2.0.7  
 Using turbolinks-source 5.0.3  
 Using tzinfo 1.2.3  
 Using nokogiri 1.7.2  
 Using rack-test 0.6.3  
 Using passenger 5.1.4  
 Using sprockets 3.7.1  
 Using websocket-driver 0.6.5  
 Using mime-types 3.1  
 Using coffee-script 2.4.1  
 Using uglifier 3.2.0  
 Using rb-inotify 0.9.8  
 Using turbolinks 5.0.1  
 Using activesupport 5.0.3  
 Using loofah 2.0.3  
 Using mail 2.6.5  
 Using listen 3.0.8  
 Using rails-dom-testing 2.0.3  
 Using globalid 0.4.0  
 Using activemodel 5.0.3  
 Using jbuilder 2.6.4  
 Using spring 2.0.1  
 Using rails-html-sanitizer 1.0.3  
 Using activejob 5.0.3  
 Using activerecord 5.0.3  
 Using spring-watcher-listen 2.0.1  
 Using actionview 5.0.3  
 Using actionpack 5.0.3  
 Using actioncable 5.0.3  
 Using actionmailer 5.0.3  
 Using railties 5.0.3  
 Using sprockets-rails 3.2.0  
 Using coffee-rails 4.2.1  
 Using jquery-rails 4.3.1  
 Using web-console 3.5.1  
 Using rails 5.0.3  
 Using sass-rails 5.0.6  
 Bundle complete! 15 Gemfile dependencies, 62 gems now installed.  
 Use `bundle show [gemname]` to see where a bundled gem is installed.  
 ~/Versioning/BitBucket/FifthBit/Conditions/ConditionsServer:  

 ~/Versioning/BitBucket/FifthBit/Conditions/ConditionsServer: git add . --all  
 ~/Versioning/BitBucket/FifthBit/Conditions/ConditionsServer: git commit -m "Ready for deployment on Passenger"  
 [master a2d01ca] Ready for deployment on Passenger  
  1 file changed, 0 insertions(+), 0 deletions(-)  
  create mode 100644 ConditionsServer/~conditionsuser/.ssh/authorized_keys  
 ~/Versioning/BitBucket/FifthBit/Conditions/ConditionsServer: git push  
 Counting objects: 5, done.  
 Delta compression using up to 8 threads.  
 Compressing objects: 100% (3/3), done.  
 Writing objects: 100% (5/5), 442 bytes | 0 bytes/s, done.  
 Total 5 (delta 2), reused 0 (delta 0)  
 To bitbucket.org:fifthbit/conditions.git  
   4924f5a..a2d01ca master -> master  
 ~/Versioning/BitBucket/FifthBit/Conditions/ConditionsServer:   

Install Passenger

For this I'm using open source Passenger in stand-alone configuration. Log back into your server machine.

Install prerequisites

 prawnhead@ubuntu:~$ sudo apt-get install -y curl gnupg build-essential  
 [sudo] password for prawnhead:   
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 build-essential is already the newest version (12.1ubuntu2).  
 gnupg is already the newest version (1.4.20-1ubuntu3.1).  
 The following packages were automatically installed and are no longer required:  
  linux-headers-4.8.0-36 linux-headers-4.8.0-36-generic linux-image-4.8.0-36-generic linux-image-extra-4.8.0-36-generic snap-confine  
 Use 'sudo apt autoremove' to remove them.  
 The following NEW packages will be installed:  
  curl  
 0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.  
 Need to get 139 kB of archives.  
 After this operation, 338 kB of additional disk space will be used.  
 Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 curl amd64 7.47.0-1ubuntu2.2 [139 kB]  
 Fetched 139 kB in 1s (95.3 kB/s)  
 Selecting previously unselected package curl.  
 (Reading database ... 244984 files and directories currently installed.)  
 Preparing to unpack .../curl_7.47.0-1ubuntu2.2_amd64.deb ...  
 Unpacking curl (7.47.0-1ubuntu2.2) ...  
 Processing triggers for man-db (2.7.5-1) ...  
 Setting up curl (7.47.0-1ubuntu2.2) ...  
 prawnhead@ubuntu:~$

Install

Get the GPG keys for RVM

Running the following command fails for me every time. See below for a solution.
 prawnhead@ubuntu:~$ sudo gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3  
 gpg: keyring `/home/prawnhead/.gnupg/secring.gpg' created  
 gpg: keyring `/home/prawnhead/.gnupg/pubring.gpg' created  
 gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net  
 gpg: keyserver timed out  
 gpg: keyserver receive failed: keyserver error  
 prawnhead@ubuntu:~$   

BEGIN OPTIONAL
 prawnhead@ubuntu:~$ ping keys.gnupg.net  
 PING pool.sks-keyservers.net (198.128.3.63) 56(84) bytes of data.  
 64 bytes from sks.es.net (198.128.3.63): icmp_seq=1 ttl=128 time=220 ms  
 64 bytes from sks.es.net (198.128.3.63): icmp_seq=2 ttl=128 time=220 ms  
 ^C  
 --- pool.sks-keyservers.net ping statistics ---  
 2 packets transmitted, 2 received, 0% packet loss, time 1072ms  
 rtt min/avg/max/mdev = 220.031/220.077/220.124/0.471 ms  
   
 prawnhead@ubuntu:~$ sudo gpg --keyserver hkp://198.128.3.63 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3  
 [sudo] password for prawnhead:   
 gpg: requesting key D39DC0E3 from hkp server 198.128.3.63  
 gpg: /home/prawnhead/.gnupg/trustdb.gpg: trustdb created  
 gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported  
 gpg: no ultimately trusted keys found  
 gpg: Total number processed: 1  
 gpg:        imported: 1 (RSA: 1)  
 prawnhead@ubuntu:~$   
END OPTIONAL

Get RVM

 prawnhead@ubuntu:~$ curl -sSL https://get.rvm.io | sudo bash -s stable  
 Downloading https://github.com/rvm/rvm/archive/1.29.1.tar.gz  
 Downloading https://github.com/rvm/rvm/releases/download/1.29.1/1.29.1.tar.gz.asc  
 gpg: Signature made Mon 20 Feb 2017 07:02:47 AM AEDT using RSA key ID BF04FF17  
 gpg: Good signature from "Michal Papis (RVM signing) <mpapis@gmail.com>" [unknown]  
 gpg:         aka "Michal Papis <michal.papis@toptal.com>" [unknown]  
 gpg:         aka "[jpeg image of size 5015]" [unknown]  
 gpg: WARNING: This key is not certified with a trusted signature!  
 gpg:     There is no indication that the signature belongs to the owner.  
 Primary key fingerprint: 409B 6B17 96C2 7546 2A17 0311 3804 BB82 D39D C0E3  
    Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36 166B E206 C29F BF04 FF17  
 GPG verified '/usr/local/rvm/archives/rvm-1.29.1.tgz'  
 Creating group 'rvm'  
   
 Installing RVM to /usr/local/rvm/  
 Installation of RVM in /usr/local/rvm/ is almost complete:  
   
  * First you need to add all users that will be using rvm to 'rvm' group,  
   and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.  
   
  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`  
   in all your open shell windows, in rare cases you need to reopen all shell windows.  
   
 # prawnhead,  
 #  
 #  Thank you for using RVM!  
 #  We sincerely hope that RVM helps to make your life easier and more enjoyable!!!  
 #  
 # ~Wayne, Michal & team.  
   
 In case of problems: https://rvm.io/help and https://twitter.com/rvm_io  
 prawnhead@ubuntu:~$   

Setup RVM

 prawnhead@ubuntu:~$ sudo usermod -a -G rvm `whoami`  
 prawnhead@ubuntu:~$ if sudo grep -q secure_path /etc/sudoers; then sudo sh -c "echo export rvmsudo_secure_path=1 >> /etc/profile.d/rvm_secure_path.sh" && echo Environment variable installed; fi  
 Environment variable installed  
 prawnhead@ubuntu:~$

IMPORTANT: Log out and back in.

Install Ruby

 prawnhead@ubuntu:~$ rvm install ruby-2.4.0  
 Searching for binary rubies, this might take some time.  
 Found remote file https://rvm_io.global.ssl.fastly.net/binaries/ubuntu/16.04/x86_64/ruby-2.4.0.tar.bz2  
 Checking requirements for ubuntu.  
 Installing requirements for ubuntu.  
 Updating systemprawnhead password required for 'apt-get --quiet --yes update':   
 .....  
 Installing required packages: gawk, libssl-dev, libyaml-dev, libsqlite3-dev, sqlite3, autoconf, libgmp-dev, libgdbm-dev, libncurses5-dev, automake, libtool, bison, libffi-dev, libgmp-dev, libreadline6-dev.................  
 Requirements installation successful.  
 ruby-2.4.0 - #configure  
 ruby-2.4.0 - #download  
  % Total  % Received % Xferd Average Speed  Time  Time   Time Current  
                  Dload Upload  Total  Spent  Left Speed  
 100 16.4M 100 16.4M  0   0  758k   0 0:00:22 0:00:22 --:--:-- 950k  
 ruby-2.4.0 - #validate archive  
 ruby-2.4.0 - #extract  
 ruby-2.4.0 - #validate binary  
 ruby-2.4.0 - #setup  
 ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0@global  
 ruby-2.4.0 - #importing gemset /usr/local/rvm/gemsets/global.gems..............................  
 ruby-2.4.0 - #generating global wrappers........  
 ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0  
 ruby-2.4.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list  
 ruby-2.4.0 - #generating default wrappers........  
 prawnhead@ubuntu:~$   

IMPORTANT: Log out and back in, again!

Install Bundler and nodejs

Set the Ruby version for RVM.

 prawnhead@ubuntu:~$ rvm --default use ruby-2.4.0  
 Using /usr/local/rvm/gems/ruby-2.4.0  

Install Bundler.

 prawnhead@ubuntu:~$ gem install bundler --no-rdoc --no-ri  
 Fetching: bundler-1.15.1.gem (100%)  
 Successfully installed bundler-1.15.1  
 1 gem installed  
 prawnhead@ubuntu:~$   
Install nodejs.
 prawnhead@ubuntu:~$ sudo apt-get install -y nodejs && sudo ln -sf /usr/bin/nodejs  
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following packages were automatically installed and are no longer required:  
  linux-headers-4.8.0-36 linux-headers-4.8.0-36-generic linux-image-4.8.0-36-generic linux-image-extra-4.8.0-36-generic snap-confine  
 Use 'sudo apt autoremove' to remove them.  
 The following additional packages will be installed:  
  libuv1  
 The following NEW packages will be installed:  
  libuv1 nodejs  
 0 upgraded, 2 newly installed, 0 to remove and 10 not upgraded.  
 Need to get 3,218 kB of archives.  
 After this operation, 13.4 MB of additional disk space will be used.  
 Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 libuv1 amd64 1.8.0-1 [57.4 kB]  
 Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 nodejs amd64 4.2.6~dfsg-1ubuntu4.1 [3,161 kB]  
 Fetched 3,218 kB in 9s (323 kB/s)                                                     
 Selecting previously unselected package libuv1:amd64.  
 (Reading database ... 245775 files and directories currently installed.)  
 Preparing to unpack .../libuv1_1.8.0-1_amd64.deb ...  
 Unpacking libuv1:amd64 (1.8.0-1) ...  
 Selecting previously unselected package nodejs.  
 Preparing to unpack .../nodejs_4.2.6~dfsg-1ubuntu4.1_amd64.deb ...  
 Unpacking nodejs (4.2.6~dfsg-1ubuntu4.1) ...  
 Processing triggers for libc-bin (2.23-0ubuntu7) ...  
 Processing triggers for doc-base (0.10.7) ...  
 Processing 1 added doc-base file...  
 Processing triggers for man-db (2.7.5-1) ...  
 Setting up libuv1:amd64 (1.8.0-1) ...  
 Setting up nodejs (4.2.6~dfsg-1ubuntu4.1) ...  
 update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode  
 Processing triggers for libc-bin (2.23-0ubuntu7) ...  
 prawnhead@ubuntu:~$   

Web Server and Security Configuration

Create Application User Account

 prawnhead@ubuntu:~$ sudo adduser conditionsuser  
 Adding user `conditionsuser' ...  
 Adding new group `conditionsuser' (1002) ...  
 Adding new user `conditionsuser' (1001) with group `conditionsuser' ...  
 Creating home directory `/home/conditionsuser' ...  
 Copying files from `/etc/skel' ...  
 Enter new UNIX password:   
 Retype new UNIX password:   
 passwd: password updated successfully  
 Changing the user information for conditionsuser  
 Enter the new value, or press ENTER for the default  
      Full Name []: Conditions User  
      Room Number []:   
      Work Phone []:   
      Home Phone []:   
      Other []:   
 Is the information correct? [Y/n]   
 prawnhead@ubuntu:~$   

Copy Admin User's SSH Security

 prawnhead@ubuntu:~$ sudo mkdir -p ~conditionsuser/.ssh  
 prawnhead@ubuntu:~$ sudo sh -c "cat $HOME/.ssh/authorized_keys >> ~conditionsuser/.ssh/authorized_keys"  
 prawnhead@ubuntu:~$ sudo sh -c "cat $HOME/.ssh/id_rsa >> ~conditionsuser/.ssh/id_rsa"  
 prawnhead@ubuntu:~$ sudo sh -c "cat $HOME/.ssh/id_rsa.pub >> ~conditionsuser/.ssh/id_rsa.pub"  
 prawnhead@ubuntu:~$ sudo chown -R conditionsuser: ~conditionsuser/.ssh  
 prawnhead@ubuntu:~$ sudo chmod 700 ~conditionsuser/.ssh  
 prawnhead@ubuntu:~$ sudo sh -c "chmod 600 ~conditionsuser/.ssh/*"  

Create web application directory structure

 prawnhead@ubuntu:~$ sudo mkdir -p /var/www/Conditions  
 prawnhead@ubuntu:~$ sudo chown conditionsuser: /var/www/Conditions  
 prawnhead@ubuntu:~$ cd /var/www/Conditions/  
 prawnhead@ubuntu:/var/www/Conditions$   

Application Code

Clone the Application Code

You'll need to modify this command string to invoke the user you've created for your application and the repository you are using.

 prawnhead@ubuntu:/var/www/Conditions$ sudo -u conditionsuser -H git clone git@bitbucket.org:fifthbit/conditions.git code  
 Cloning into 'code'...  
 The authenticity of host 'bitbucket.org (104.192.143.2)' can't be established.  
 RSA key fingerprint is SHA256:zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A.  
 Are you sure you want to continue connecting (yes/no)? yes  
 Warning: Permanently added 'bitbucket.org,104.192.143.2' (RSA) to the list of known hosts.  
 Enter passphrase for key '/home/conditionsuser/.ssh/id_rsa':   
 remote: Counting objects: 173, done.  
 remote: Compressing objects: 100% (144/144), done.  
 remote: Total 173 (delta 30), reused 0 (delta 0)  
 Receiving objects: 100% (173/173), 70.12 KiB | 17.00 KiB/s, done.  
 Resolving deltas: 100% (30/30), done.  
 Checking connectivity... done.  
 prawnhead@ubuntu:/var/www/Conditions$   

Have a look at your application's code. I have an extra folder level in my code as I'm storing code for the Rails server and embedded devices that will interface with the server all in the same repository. When you clone into the 'code' folder you should see your Rails project there.

 prawnhead@ubuntu:/var/www/Conditions$ ls -l code/ConditionsServer/  
 total 72  
 drwxr-xr-x 10 conditionsuser conditionsuser 4096 Jun 8 16:10 app  
 drwxr-xr-x 2 conditionsuser conditionsuser 4096 Jun 8 16:10 bin  
 drwxr-xr-x 3 conditionsuser conditionsuser 4096 Jun 8 16:10 ~conditionsuser  
 drwxr-xr-x 5 conditionsuser conditionsuser 4096 Jun 8 16:10 config  
 -rw-r--r-- 1 conditionsuser conditionsuser 130 Jun 8 16:10 config.ru  
 drwxr-xr-x 3 conditionsuser conditionsuser 4096 Jun 8 16:10 db  
 -rw-r--r-- 1 conditionsuser conditionsuser 2170 Jun 8 16:10 Gemfile  
 -rw-r--r-- 1 conditionsuser conditionsuser 4298 Jun 8 16:10 Gemfile.lock  
 drwxr-xr-x 4 conditionsuser conditionsuser 4096 Jun 8 16:10 lib  
 drwxr-xr-x 2 conditionsuser conditionsuser 4096 Jun 8 16:10 log  
 -rw-r--r-- 1 conditionsuser conditionsuser 409 Jun 8 16:10 Passengerfile.json  
 drwxr-xr-x 2 conditionsuser conditionsuser 4096 Jun 8 16:10 public  
 -rw-r--r-- 1 conditionsuser conditionsuser 227 Jun 8 16:10 Rakefile  
 -rw-r--r-- 1 conditionsuser conditionsuser 374 Jun 8 16:10 README.md  
 drwxr-xr-x 8 conditionsuser conditionsuser 4096 Jun 8 16:10 test  
 drwxr-xr-x 2 conditionsuser conditionsuser 4096 Jun 8 16:10 tmp  
 drwxr-xr-x 3 conditionsuser conditionsuser 4096 Jun 8 16:10 vendor  
 prawnhead@ubuntu:/var/www/Conditions$   

Stand Up the Code

Set Folder

Change folders into the root folder of your application. This should be /var/www/YourAppName/code.

 prawnhead@ubuntu:/var/www/Conditions$ cd code/ConditionsServer  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$  

Switch to Application's User Account

Switch users to the user you created to run your application.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ sudo -u conditionsuser -H bash -l  
 [sudo] password for prawnhead:   
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Set Default Ruby Version

As we have changed user context, we need to again set the default Ruby version for RVM to use.
ASIDE: I get this warning about the PATH but when I check it using printenv it's right. Whatever. The second time I run the command I don't get a warning.
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ rvm use ruby-2.4.0  
 Warning! PATH is not properly set up, '/usr/local/rvm/gems/ruby-2.4.0/bin' is not at first place.  
      Usually this is caused by shell initialization files. Search for 'PATH=...' entries.  
      You can also re-add RVM to your profile by running: 'rvm get stable --auto-dotfiles'.  
      To fix it temporarily in this shell session run: 'rvm use ruby-2.4.0'.  
      To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.  
 Using /usr/local/rvm/gems/ruby-2.4.0  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ rvm use ruby-2.4.0  
 Using /usr/local/rvm/gems/ruby-2.4.0  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$  

Bundle Install

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ bundle install --deployment --without development test  
 Fetching gem metadata from https://rubygems.org/.........  
 Fetching version metadata from https://rubygems.org/..  
 Fetching dependency metadata from https://rubygems.org/.  
 Fetching rake 12.0.0  
 Installing rake 12.0.0  
 Fetching concurrent-ruby 1.0.5  
 Installing concurrent-ruby 1.0.5  
 Fetching i18n 0.8.1  
 Installing i18n 0.8.1  
 Fetching minitest 5.10.2  
 Installing minitest 5.10.2  
 Fetching thread_safe 0.3.6  
 Installing thread_safe 0.3.6  
 Fetching builder 3.2.3  
 Installing builder 3.2.3  
 Fetching erubis 2.7.0  
 Installing erubis 2.7.0  
 Fetching mini_portile2 2.1.0  
 Installing mini_portile2 2.1.0  
 Fetching rack 2.0.2  
 Installing rack 2.0.2  
 Fetching nio4r 2.0.0  
 Installing nio4r 2.0.0 with native extensions  
 Fetching websocket-extensions 0.1.2  
 Installing websocket-extensions 0.1.2  
 Fetching mime-types-data 3.2016.0521  
 Installing mime-types-data 3.2016.0521  
 Fetching arel 7.1.4  
 Installing arel 7.1.4  
 Fetching method_source 0.8.2  
 Installing method_source 0.8.2  
 Fetching thor 0.19.4  
 Installing thor 0.19.4  
 Using bundler 1.15.1  
 Fetching mysql2 0.4.6  
 Installing mysql2 0.4.6 with native extensions  
 Fetching sass 3.4.23  
 Installing sass 3.4.23  
 Fetching tilt 2.0.7  
 Installing tilt 2.0.7  
 Fetching execjs 2.7.0  
 Installing execjs 2.7.0  
 Fetching coffee-script-source 1.12.2  
 Installing coffee-script-source 1.12.2  
 Fetching turbolinks-source 5.0.3  
 Installing turbolinks-source 5.0.3  
 Fetching multi_json 1.12.1  
 Installing multi_json 1.12.1  
 Fetching tzinfo 1.2.3  
 Installing tzinfo 1.2.3  
 Fetching nokogiri 1.7.2  
 Installing nokogiri 1.7.2 with native extensions  
 Fetching rack-test 0.6.3  
 Installing rack-test 0.6.3  
 Fetching sprockets 3.7.1  
 Installing sprockets 3.7.1  
 Fetching passenger 5.1.4  
 Installing passenger 5.1.4 with native extensions  
 Fetching websocket-driver 0.6.5  
 Installing websocket-driver 0.6.5 with native extensions  
 Fetching mime-types 3.1  
 Installing mime-types 3.1  
 Fetching uglifier 3.2.0  
 Installing uglifier 3.2.0  
 Fetching coffee-script 2.4.1  
 Installing coffee-script 2.4.1  
 Fetching turbolinks 5.0.1  
 Installing turbolinks 5.0.1  
 Fetching activesupport 5.0.3  
 Installing activesupport 5.0.3  
 Fetching loofah 2.0.3  
 Installing loofah 2.0.3  
 Fetching mail 2.6.5  
 Installing mail 2.6.5  
 Fetching rails-dom-testing 2.0.3  
 Installing rails-dom-testing 2.0.3  
 Fetching globalid 0.4.0  
 Installing globalid 0.4.0  
 Fetching activemodel 5.0.3  
 Installing activemodel 5.0.3  
 Fetching jbuilder 2.6.4  
 Installing jbuilder 2.6.4  
 Fetching rails-html-sanitizer 1.0.3  
 Installing rails-html-sanitizer 1.0.3  
 Fetching activejob 5.0.3  
 Installing activejob 5.0.3  
 Fetching activerecord 5.0.3  
 Installing activerecord 5.0.3  
 Fetching actionview 5.0.3  
 Installing actionview 5.0.3  
 Fetching actionpack 5.0.3  
 Installing actionpack 5.0.3  
 Fetching actioncable 5.0.3  
 Installing actioncable 5.0.3  
 Fetching actionmailer 5.0.3  
 Installing actionmailer 5.0.3  
 Fetching railties 5.0.3  
 Installing railties 5.0.3  
 Fetching sprockets-rails 3.2.0  
 Installing sprockets-rails 3.2.0  
 Fetching coffee-rails 4.2.1  
 Installing coffee-rails 4.2.1  
 Fetching jquery-rails 4.3.1  
 Installing jquery-rails 4.3.1  
 Fetching rails 5.0.3  
 Installing rails 5.0.3  
 Fetching sass-rails 5.0.6  
 Installing sass-rails 5.0.6  
 Bundle complete! 15 Gemfile dependencies, 53 gems now installed.  
 Gems in the groups development and test were not installed.  
 Bundled gems are installed into ./vendor/bundle.  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Secure Configuration Files

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ chmod 700 config  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ chmod 600 config/database.yml config/secrets.yml   
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Compile Code

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ bundle exec rake assets:precompile RAILS_ENV=production  
 I, [2017-06-08T16:37:18.045997 #28062] INFO -- : Writing /var/www/Conditions/code/ConditionsServer/public/assets/application-731eb6bd5d68d47eb9b931e7f005a458f11d76b23a517cc0bd892977bc948817.js  
 I, [2017-06-08T16:37:18.046571 #28062] INFO -- : Writing /var/www/Conditions/code/ConditionsServer/public/assets/application-731eb6bd5d68d47eb9b931e7f005a458f11d76b23a517cc0bd892977bc948817.js.gz  
 I, [2017-06-08T16:37:18.086212 #28062] INFO -- : Writing /var/www/Conditions/code/ConditionsServer/public/assets/application-d12397cbc3a266ce5a99f7b588223090c55ca04a439953bb5f9304395de93fc3.css  
 I, [2017-06-08T16:37:18.086410 #28062] INFO -- : Writing /var/www/Conditions/code/ConditionsServer/public/assets/application-d12397cbc3a266ce5a99f7b588223090c55ca04a439953bb5f9304395de93fc3.css.gz  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$  

Generate Secret

We need to generate a high entropy hexadecimal string for Rails to use in securing sessions. Run the following command, we'll come back in a minute to copy-paste this value into a file we'll open.

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ bundle exec rake secret  
 b52ca24cc75b75b7bbc707fa311f5978b225593f5d16d33cf0e5292b89f7f3159cac253ee81dccb4efb6efc1dd1e1949b0613b44529b7b5fd170c6d7244e93f7  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Edit rc.local

Commands in rc.local are executed under the root user when the system starts. When we define our environment variables here they are available when the system boots so it can start our application automatically.

Drop back from the application user account to the administrator account.

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ exit  
 logout  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Edit the rc.local file to make the Rails secret_key_base and database password available to the root user.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ sudo nano /etc/rc.local  
 [sudo] password for prawnhead:  

Add these two lines before 'exit 0'.
NOTE: If you're not familiar with nano you'll need to use your cursor keys to move the cursor down a row at a time until you get to just above 'exit 0'. You should then be able to copy the two lines below and paste them into the file.

 export rvmSecretKeyBase=HexadecimalTextFromBundleExecRakeSecret  
 export rvmConditionsDatabaseUserPassword=TheActualPasswordHere  

Close the file saving changes (Ctrl-x then Y then Enter).

In your terminal window, go back up to the line where we previously performed a 'bundle exec rake secret'. Copy the entire text that was produced. Go back into the rc.local file using 'sudo nano /etc/rc.local', remove 'HexadecimalTextFromBundleExecRakeSecret' and paste instead what you just copied. While in the file, replace TheActualPasswordHere with the password for the application's DATABASE user account. If you have changed the names of the environment variables alter them too (See settings above near the top of this document). The edited file will look like this:

 #!/bin/sh -e  
 #  
 # rc.local  
 #  
 # This script is executed at the end of each multiuser runlevel.  
 # Make sure that the script will "exit 0" on success or any other  
 # value on error.  
 #  
 # In order to enable or disable this script just change the execution  
 # bits.  
 #  
 # By default this script does nothing.  
   
 export rvmSecretKeyBase=b52ca24cc75b75b7bbc707fa311f5978b225593f5d16d33cf0e5292b89f7f3159cac253ee81dccb4efb6efc1dd1e1949b0613b44529b7b  
 5fd170c6d7244e93f7  
 export rvmConditionsDatabaseUserPassword=6Thur5day  
   
 exit 0  

Exit saving changes again.

Perform Database Migration

We need to run the database migrations. Until now we haven't had an environment variable to hold the database password. However, the one we've created is only accessible to the root account as the system boots. So we can use those environment variables now to do the database migrations, we just need to execute them in the context of the current user's session.

Print the rc.local file to the terminal.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ more /etc/rc.local   
 #!/bin/sh -e  
 #  
 # rc.local  
 #  
 # This script is executed at the end of each multiuser runlevel.  
 # Make sure that the script will "exit 0" on success or any other  
 # value on error.  
 #  
 # In order to enable or disable this script just change the execution  
 # bits.  
 #  
 # By default this script does nothing.  
   
 export rvmSecretKeyBase=b52ca24cc75b75b7bbc707fa311f5978b225593f5d16d33cf0e5292b89f7f3159cac253ee81dccb4efb6efc1dd1e1949b0613b44529b7b  
 5fd170c6d7244e93f7  
 export rvmConditionsDatabaseUserPassword=6Thur5day  
   
 exit 0  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Now copy and past the two export commands into the current terminal session.
NOTE: I found that if the Secret Key Base wraps around in the terminal display it won't paste in as a single command. You may need to widen your terminal and print the rc.local file out again.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ export rvmSecretKeyBase=b52ca24cc75b75b7bbc707fa311f5978b225593f5d16d33cf0e5292b89f7f3159cac253ee81dccb4efb6efc1dd1e1949b0613b44529b7b5fd170c6d7244e93f7  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ export rvmConditionsDatabaseUserPassword=6Thur5day  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

You can verify these environment variables are now available to the current user.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ printenv | grep rvm  
 rvm_bin_path=/usr/local/rvm/bin  
 GEM_HOME=/usr/local/rvm/gems/ruby-2.4.0  
 IRBRC=/usr/local/rvm/rubies/ruby-2.4.0/.irbrc  
 MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-2.4.0  
 rvm_path=/usr/local/rvm  
 rvmSecretKeyBase=b52ca24cc75b75b7bbc707fa311f5978b225593f5d16d33cf0e5292b89f7f3159cac253ee81dccb4efb6efc1dd1e1949b0613b44529b7b5fd170c6d7244e93f7  
 rvm_prefix=/usr/local  
 PATH=/home/prawnhead/bin:/home/prawnhead/.local/bin:/usr/local/rvm/gems/ruby-2.4.0/bin:/usr/local/rvm/gems/ruby-2.4.0@global/bin:/usr/local/rvm/rubies/ruby-2.4.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/rvm/bin  
 rvmConditionsDatabaseUserPassword=6Thur5day  
 rvm_version=1.29.1 (latest)  
 GEM_PATH=/usr/local/rvm/gems/ruby-2.4.0:/usr/local/rvm/gems/ruby-2.4.0@global  
 rvmsudo_secure_path=1  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Having checked that, we can now run the database migration.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ rvmsudo bundle exec rake db:migrate RAILS_ENV=production  
 == 20170513045749 CreateStations: migrating ===================================  
 -- create_table(:stations)  
   -> 0.0082s  
 == 20170513045749 CreateStations: migrated (0.0083s) ==========================  
   
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Trial Run

Start Passenger

This is what a clean kick-off looks like.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ rvmsudo bundle exec passenger start  
  --> Downloading a Phusion Passenger agent binary for your platform  
   
  --> Installing Nginx 1.10.3 engine  
   
 --------------------------  
   
  [passenger_native_support.so] trying to compile for the current user (root) and Ruby interpreter...  
    (set PASSENGER_COMPILE_NATIVE_SUPPORT_BINARY=0 to disable)  
    Compilation successful. The logs are here:  
    /tmp/passenger_native_support-1b82isj.log  
  [passenger_native_support.so] successfully loaded.  
 =============== Phusion Passenger Standalone web server started ===============  
 PID file: /var/www/Conditions/code/ConditionsServer/passenger.80.pid  
 Log file: /var/www/Conditions/code/ConditionsServer/log/passenger.80.log  
 Environment: production  
 Accessible via: http://0.0.0.0/  
   
 Serving in the background as a daemon.  
 Problems? Check https://www.phusionpassenger.com/library/admin/standalone/troubleshooting/  
 ===============================================================================  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Verifying the App

You should now be able to hit your app using a web browser and just the IP address of the Passenger server (eg. http://10.211.55.15). If, like me, your app is a REST API, you will have to hit one of the views (eg. http://10.211.55.15/stations.json)

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ curl http://10.211.55.15/stations.json  
 [{"id":1,"description":"Ambient","created_at":"2017-06-08T07:59:35.000Z","updated_at":"2017-06-08T07:59:35.000Z","url":"http://10.211.55.15/stations/1.json"},{"id":2,"description":"Office Indoors","created_at":"2017-06-08T07:59:44.000Z","updated_at":"2017-06-08T07:59:44.000Z","url":"http://10.211.55.15/stations/2.json"}]  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Application Auto-Start

To make the application start automatically when the system boots we only need to make two changes to the rc.local file.

Locate your rvm-exec file and copy the path.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ which rvm-exec  
 /usr/local/rvm/bin/rvm-exec  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Edit rc.local

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ sudo nano /etc/rc.local   
 [sudo] password for prawnhead:  

Add the following two lines between the two export commands and the 'exit 0' at the end of the file. Of course you should modify them to suit your paths.

 cd /var/www/Conditions/code/ConditionsServer  
 /usr/local/rvm/bin/rvm-exec ruby-2.4.0 bundle exec passenger start  

The completed rc.local file looks like this

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ more /etc/rc.local   
 #!/bin/sh -e  
 #  
 # rc.local  
 #  
 # This script is executed at the end of each multiuser runlevel.  
 # Make sure that the script will "exit 0" on success or any other  
 # value on error.  
 #  
 # In order to enable or disable this script just change the execution  
 # bits.  
 #  
 # By default this script does nothing.  
   
 export rvmSecretKeyBase=b52ca24cc75b75b7bbc707fa311f5978b225593f5d16d33cf0e5292b89f7f3159cac253ee81dccb4efb6efc1dd1e1949b0613b44529b7b  
 5fd170c6d7244e93f7  
 export rvmConditionsDatabaseUserPassword=6Thur5day  
   
 cd /var/www/Conditions/code/ConditionsServer  
 /usr/local/rvm/bin/rvm-exec ruby-2.4.0 bundle exec passenger start  
   
 exit 0  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Make the rc.local file executable.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ sudo chmod +x /etc/rc.local   
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Reboot the Passenger server.

 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ sudo shutdown -r now  
 Connection to 10.211.55.15 closed by remote host.  
 Connection to 10.211.55.15 closed.  
 ~/Versioning/BitBucket/FifthBit/Conditions/ConditionsServer:   

Verify Passenger auto-starts. After the reboot, log back in to an SSH session or console. Check the passenger processes are running

 prawnhead@ubuntu:~$ ps -ax | grep pass  
  1815 ?    Ss   0:00 nginx: master process /var/www/Conditions/code/ConditionsServer/vendor/bundle/ruby/2.4.0/gems/passenger-5.1.4/buildout/support-binaries/nginx-1.10.3 -c /tmp/passenger-standalone.19zju81/nginx.conf -p /tmp/passenger-standalone.19zju81  
  1830 ?    Ss   0:00 /var/www/Conditions/code/ConditionsServer/vendor/bundle/ruby/2.4.0/gems/passenger-5.1.4/buildout/support-binaries/PassengerAgent temp-dir-toucher /tmp/passenger-standalone.19zju81 --cleanup --daemonize --pid-file /tmp/passenger-standalone.19zju81/temp_dir_toucher.pid --log-file /var/www/Conditions/code/ConditionsServer/log/passenger.80.log --user conditionsuser  
  2545 pts/8  S+   0:00 grep --color=auto pass  
 prawnhead@ubuntu:~$  

Check your home page or an API view loads.

 prawnhead@ubuntu:~$ curl http://10.211.55.15/stations.json  
 [{"id":1,"description":"Ambient","created_at":"2017-06-08T07:59:35.000Z","updated_at":"2017-06-08T07:59:35.000Z","url":"http://10.211.55.15/stations/1.json"},{"id":2,"description":"Office Indoors","created_at":"2017-06-08T07:59:44.000Z","updated_at":"2017-06-08T07:59:44.000Z","url":"http://10.211.55.15/stations/2.json"}]  
 prawnhead@ubuntu:~$   

Deploy Updated Code

For this section I have added a new model to my Rails code on my development machine and pushed the change to the repo using git.

Stop Passenger

Log into the server

 ~: vm.ssh  
 Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.8.0-49-generic x86_64)  
  * Documentation: https://help.ubuntu.com  
  * Management:   https://landscape.canonical.com  
  * Support:    https://ubuntu.com/advantage  
 21 packages can be updated.  
 7 updates are security updates.  
 Last login: Thu Jun 22 18:07:42 2017 from 10.211.55.2  
 prawnhead@ubuntu:~$   

Check Passenger is running

 prawnhead@ubuntu:~$ ps -ax | grep pass
  1857 ?    Ss   0:00 nginx: master process /var/www/Conditions/code/ConditionsServer/vendor/bundle/ruby/2.4.0/gems/passenger-5.1.4/buildout/support-binaries/nginx-1.10.3 -c /tmp/passenger-standalone.1p0fi57/nginx.conf -p /tmp/passenger-standalone.1p0fi57  
  1866 ?    Ss   0:00 /var/www/Conditions/code/ConditionsServer/vendor/bundle/ruby/2.4.0/gems/passenger-5.1.4/buildout/support-binaries/PassengerAgent temp-dir-toucher /tmp/passenger-standalone.1p0fi57 --cleanup --daemonize --pid-file /tmp/passenger-standalone.1p0fi57/temp_dir_toucher.pid --log-file /var/www/Conditions/code/ConditionsServer/log/passenger.80.log --user conditionsuser  
  2452 pts/8  S+   0:00 grep --color=auto pass  
 prawnhead@ubuntu:$   

Disable Passenger auto-start

Remove the executable flag from /etc/rc.local to stop Passenger auto-starting

 prawnhead@ubuntu:$ sudo chmod -x /etc/rc.local 
 [sudo] password for prawnhead:   
 prawnhead@ubuntu:~$   

Restart

 prawnhead@ubuntu:~$ sudo shutdown -r now  
 Connection to 10.211.55.15 closed by remote host.  
 Connection to 10.211.55.15 closed.  
 ~:  

Verify

Login again and check Passenger is not running

 ~: vm.ssh  
 Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.8.0-49-generic x86_64)  
  * Documentation: https://help.ubuntu.com  
  * Management:   https://landscape.canonical.com  
  * Support:    https://ubuntu.com/advantage  
 21 packages can be updated.  
 7 updates are security updates.  
 Last login: Thu Jun 22 18:07:42 2017 from 10.211.55.2  
 prawnhead@ubuntu:~$ ps -ax | grep pass  
  2327 pts/8  S+   0:00 grep --color=auto pass  
 prawnhead@ubuntu:~$   

Pull code

Change to the application's root folder

 prawnhead@ubuntu:~$ cd /var/www/Conditions/code  
 prawnhead@ubuntu:/var/www/Conditions/code$   

Switch to the application user

 prawnhead@ubuntu:/var/www/Conditions/code$ sudo -u conditionsuser -H bash -l
 [sudo] password for prawnhead:   
 conditionsuser@ubuntu:/var/www/Conditions/code$   

Pull code

 conditionsuser@ubuntu:/var/www/Conditions/code$ git pull  
 Warning: Permanently added the RSA host key for IP address '104.192.143.3' to the list of known hosts.  
 Enter passphrase for key '/home/conditionsuser/.ssh/id_rsa':   
 remote: Counting objects: 40, done.  
 remote: Compressing objects: 100% (38/38), done.  
 remote: Total 40 (delta 14), reused 0 (delta 0)  
 Unpacking objects: 100% (40/40), done.  
 From bitbucket.org:fifthbit/conditions  
   3d39788..695eb33 master   -> origin/master  
 Updating 3d39788..695eb33  
 Fast-forward  
  ConditionsServer/Gemfile                   | 10 ++++---  
  ConditionsServer/Gemfile.lock                 | 2 ++  
  ConditionsServer/app/assets/javascripts/measures.coffee    | 3 +++  
  ConditionsServer/app/assets/stylesheets/measures.scss     | 3 +++  
  ConditionsServer/app/controllers/application_controller.rb  | 1 +  
  ConditionsServer/app/controllers/measures_controller.rb    | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++  
  ConditionsServer/app/helpers/measures_helper.rb        | 2 ++  
  ConditionsServer/app/models/measure.rb            | 2 ++  
  ConditionsServer/app/views/measures/_form.html.erb      | 27 +++++++++++++++++++  
  ConditionsServer/app/views/measures/_measure.json.jbuilder  | 2 ++  
  ConditionsServer/app/views/measures/edit.html.erb       | 6 +++++  
  ConditionsServer/app/views/measures/index.html.erb      | 29 ++++++++++++++++++++  
  ConditionsServer/app/views/measures/index.json.jbuilder    | 1 +  
  ConditionsServer/app/views/measures/new.html.erb       | 5 ++++  
  ConditionsServer/app/views/measures/show.html.erb       | 14 ++++++++++  
  ConditionsServer/app/views/measures/show.json.jbuilder    | 1 +  
  ConditionsServer/config/routes.rb               | 1 +  
  ConditionsServer/db/migrate/20170618093947_create_measures.rb | 10 +++++++  
  ConditionsServer/db/schema.rb                 | 16 ++++++++++-  
  ConditionsServer/test/controllers/measures_controller_test.rb | 48 +++++++++++++++++++++++++++++++++  
  ConditionsServer/test/fixtures/measures.yml          | 9 +++++++  
  ConditionsServer/test/models/measure_test.rb         | 7 +++++  
  22 files changed, 269 insertions(+), 4 deletions(-)  
  create mode 100644 ConditionsServer/app/assets/javascripts/measures.coffee  
  create mode 100644 ConditionsServer/app/assets/stylesheets/measures.scss  
  create mode 100644 ConditionsServer/app/controllers/measures_controller.rb  
  create mode 100644 ConditionsServer/app/helpers/measures_helper.rb  
  create mode 100644 ConditionsServer/app/models/measure.rb  
  create mode 100644 ConditionsServer/app/views/measures/_form.html.erb  
  create mode 100644 ConditionsServer/app/views/measures/_measure.json.jbuilder  
  create mode 100644 ConditionsServer/app/views/measures/edit.html.erb  
  create mode 100644 ConditionsServer/app/views/measures/index.html.erb  
  create mode 100644 ConditionsServer/app/views/measures/index.json.jbuilder  
  create mode 100644 ConditionsServer/app/views/measures/new.html.erb  
  create mode 100644 ConditionsServer/app/views/measures/show.html.erb  
  create mode 100644 ConditionsServer/app/views/measures/show.json.jbuilder  
  create mode 100644 ConditionsServer/db/migrate/20170618093947_create_measures.rb  
  create mode 100644 ConditionsServer/test/controllers/measures_controller_test.rb  
  create mode 100644 ConditionsServer/test/fixtures/measures.yml  
  create mode 100644 ConditionsServer/test/models/measure_test.rb  
 conditionsuser@ubuntu:/var/www/Conditions/code$   

Set the Ruby version

 conditionsuser@ubuntu:/var/www/Conditions/code$ rvm use ruby-2.4.0  
 Warning! PATH is not properly set up, '/usr/local/rvm/gems/ruby-2.4.0/bin' is not at first place.  
      Usually this is caused by shell initialization files. Search for 'PATH=...' entries.  
      You can also re-add RVM to your profile by running: 'rvm get stable --auto-dotfiles'.  
      To fix it temporarily in this shell session run: 'rvm use ruby-2.4.0'.  
      To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.  
 Using /usr/local/rvm/gems/ruby-2.4.0  
 conditionsuser@ubuntu:/var/www/Conditions/code$   

Deploy code

(Optional) Change folder

If necessary, change the folder to the root folder for the Rails application.

 conditionsuser@ubuntu:/var/www/Conditions/code$ cd ConditionsServer  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Perform a bundle install

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ bundle install --deployment --without development test  
 Fetching gem metadata from https://rubygems.org/.........  
 Fetching version metadata from https://rubygems.org/..  
 Fetching dependency metadata from https://rubygems.org/.  
 Using rake 12.0.0  
 Using concurrent-ruby 1.0.5  
 Using i18n 0.8.1  
 Using minitest 5.10.2  
 Using thread_safe 0.3.6  
 Using builder 3.2.3  
 Using erubis 2.7.0  
 Using mini_portile2 2.1.0  
 Using rack 2.0.2  
 Using nio4r 2.0.0  
 Using websocket-extensions 0.1.2  
 Using mime-types-data 3.2016.0521  
 Using arel 7.1.4  
 Using method_source 0.8.2  
 Using thor 0.19.4  
 Using bundler 1.15.1  
 Using mysql2 0.4.6  
 Using sass 3.4.23  
 Using tilt 2.0.7  
 Using execjs 2.7.0  
 Using coffee-script-source 1.12.2  
 Using turbolinks-source 5.0.3  
 Using multi_json 1.12.1  
 Using tzinfo 1.2.3  
 Using nokogiri 1.7.2  
 Using rack-test 0.6.3  
 Using sprockets 3.7.1  
 Using passenger 5.1.4  
 Using websocket-driver 0.6.5  
 Using mime-types 3.1  
 Using uglifier 3.2.0  
 Using coffee-script 2.4.1  
 Using turbolinks 5.0.1  
 Using activesupport 5.0.3  
 Using loofah 2.0.3  
 Using mail 2.6.5  
 Using rails-dom-testing 2.0.3  
 Using globalid 0.4.0  
 Using activemodel 5.0.3  
 Using jbuilder 2.6.4  
 Using rails-html-sanitizer 1.0.3  
 Using activejob 5.0.3  
 Using activerecord 5.0.3  
 Using actionview 5.0.3  
 Using actionpack 5.0.3  
 Using actioncable 5.0.3  
 Using actionmailer 5.0.3  
 Using railties 5.0.3  
 Using sprockets-rails 3.2.0  
 Using coffee-rails 4.2.1  
 Using jquery-rails 4.3.1  
 Using rails 5.0.3  
 Using sass-rails 5.0.6  
 Bundle complete! 16 Gemfile dependencies, 53 gems now installed.  
 Gems in the groups development and test were not installed.  
 Bundled gems are installed into ./vendor/bundle.  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Check environment variables

Verify the environment variables (rvmSecretKeyBase, rvmConditionsDatabaseUserPassword) are not already available
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ printenv | grep rvm  
 rvm_bin_path=/usr/local/rvm/bin  
 GEM_HOME=/usr/local/rvm/gems/ruby-2.4.0  
 IRBRC=/usr/local/rvm/rubies/ruby-2.4.0/.irbrc  
 MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-2.4.0  
 rvm_path=/usr/local/rvm  
 rvm_prefix=/usr/local  
 PATH=/usr/local/rvm/gems/ruby-2.4.0/bin:/usr/local/rvm/gems/ruby-2.4.0@global/bin:/usr/local/rvm/rubies/ruby-2.4.0/bin:/usr/local/rvm/bin:/home/conditionsuser/bin:/home/conditionsuser/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin  
 rvm_version=1.29.1 (latest)  
 GEM_PATH=/usr/local/rvm/gems/ruby-2.4.0:/usr/local/rvm/gems/ruby-2.4.0@global  
 rvmsudo_secure_path=1  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Display /etc/rc.local

Dump the /etc/rc.local file to the terminal so we can copy and paste the export commands

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ more /etc/rc.local  
 #!/bin/sh -e  
 #  
 # rc.local  
 #  
 # This script is executed at the end of each multiuser runlevel.  
 # Make sure that the script will "exit 0" on success or any other  
 # value on error.  
 #  
 # In order to enable or disable this script just change the execution  
 # bits.  
 #  
 # By default this script does nothing.  
 export rvmSecretKeyBase=fb3c334e6270ca03fda2dc5a782156c5f9e847d4c98017e7999b715452b1f0fa2e0e7e359299d9f7ee0d4d7a0e44d49ee0ed21795f28a21ce785c953aa633bba  
 export rvmConditionsDatabaseUserPassword=6Thur5day  
 cd /var/www/Conditions/code/ConditionsServer  
 /usr/local/rvm/bin/rvm-exec ruby-2.4.0 bundle exec passenger start  
 exit 0  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Export rvm... environment variables

Copy and paste into the terminal prompt the two export lines from /etc/rc.local
NOTE: To ensure rvmSecretKeyBase is not truncated, the terminal needs to be wide enough so that the rvmSecretKeyBase line is not wrapped.


 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ export rvmSecretKeyBase=fb3c334e6270ca03fda2dc5a782156c5f9e847d4c98017e7999b715452b1f0fa2e0e7e359299d9f7ee0d4d7a0e44d49ee0ed21795f28a21ce785c953aa633bba  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ export rvmConditionsDatabaseUserPassword=6Thur5day  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Check environment variables

Ensure the two rvm... environment variables are now available in the current session.

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ printenv | grep rvm  
 rvm_bin_path=/usr/local/rvm/bin  
 GEM_HOME=/usr/local/rvm/gems/ruby-2.4.0  
 IRBRC=/usr/local/rvm/rubies/ruby-2.4.0/.irbrc  
 MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-2.4.0  
 rvm_path=/usr/local/rvm  
 rvmSecretKeyBase=fb3c334e6270ca03fda2dc5a782156c5f9e847d4c98017e7999b715452b1f0fa2e0e7e359299d9f7ee0d4d7a0e44d49ee0ed21795f28a21ce785c953aa633bba  
 rvm_prefix=/usr/local  
 PATH=/usr/local/rvm/gems/ruby-2.4.0/bin:/usr/local/rvm/gems/ruby-2.4.0@global/bin:/usr/local/rvm/rubies/ruby-2.4.0/bin:/usr/local/rvm/bin:/home/conditionsuser/bin:/home/conditionsuser/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin  
 rvmConditionsDatabaseUserPassword=6Thur5day  
 rvm_version=1.29.1 (latest)  
 rvm_ruby_string=ruby-2.4.0  
 GEM_PATH=/usr/local/rvm/gems/ruby-2.4.0:/usr/local/rvm/gems/ruby-2.4.0@global  
 rvmsudo_secure_path=1  
 rvm_delete_flag=0  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Compile and migrate

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ bundle exec rake assets:precompile db:migrate RAILS_ENV=production  
 I, [2017-06-23T11:40:32.885001 #25520] INFO -- : Writing /var/www/Conditions/code/ConditionsServer/public/assets/application-dd6f875ecc583bcada287d338f50c844f7f601a21c89dc743e1a8a8cca8980b6.js  
 I, [2017-06-23T11:40:32.885358 #25520] INFO -- : Writing /var/www/Conditions/code/ConditionsServer/public/assets/application-dd6f875ecc583bcada287d338f50c844f7f601a21c89dc743e1a8a8cca8980b6.js.gz  
 == 20170618093947 CreateMeasures: migrating ===================================  
 -- create_table(:measures)  
   -> 0.0064s  
 == 20170618093947 CreateMeasures: migrated (0.0065s) ==========================  
 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$   

Start Passenger with the system

Set Passenger auto start

 conditionsuser@ubuntu:/var/www/Conditions/code/ConditionsServer$ exit  
 logout  
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ sudo chmod +x /etc/rc.local   
 [sudo] password for prawnhead:   
 prawnhead@ubuntu:/var/www/Conditions/code/ConditionsServer$ sudo reboot now  
 Connection to 10.211.55.15 closed by remote host.  
 Connection to 10.211.55.15 closed.  
 ~:   

Log back in

 ~: vm.ssh  
 Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.8.0-49-generic x86_64)  
  * Documentation: https://help.ubuntu.com  
  * Management:   https://landscape.canonical.com  
  * Support:    https://ubuntu.com/advantage  
 21 packages can be updated.  
 7 updates are security updates.  
 Last login: Fri Jun 23 09:58:03 2017 from 10.211.55.2  
 prawnhead@ubuntu:~$   

Verify Passenger started

 prawnhead@ubuntu:~$ ps -ax | grep pass  
  1847 ?    Ss   0:00 nginx: master process /var/www/Conditions/code/ConditionsServer/vendor/bundle/ruby/2.4.0/gems/passenger-5.1.4/buildout/support-binaries/nginx-1.10.3 -c /tmp/passenger-standalone.13aa0fy/nginx.conf -p /tmp/passenger-standalone.13aa0fy  
  1853 ?    Ss   0:00 /var/www/Conditions/code/ConditionsServer/vendor/bundle/ruby/2.4.0/gems/passenger-5.1.4/buildout/support-binaries/PassengerAgent temp-dir-toucher /tmp/passenger-standalone.13aa0fy --cleanup --daemonize --pid-file /tmp/passenger-standalone.13aa0fy/temp_dir_toucher.pid --log-file /var/www/Conditions/code/ConditionsServer/log/passenger.80.log --user conditionsuser  
  2601 pts/8  S+   0:00 grep --color=auto pass  
 prawnhead@ubuntu:~$   

Verify new functionality

 prawnhead@ubuntu:~$ curl http://localhost/measures  
 <!DOCTYPE html>  
 <html>  
  <head>  
   <title>ConditionsServer</title>  
   <meta name="csrf-param" content="authenticity_token" />  
 <meta name="csrf-token" content="+XzUI0SxjRVqR6ebhZMAT5wkAZyvqUqmgERGoPPkDS4qfu59FNbCbqlpraZ87VUb8GO8dWgK2f26m53r36mHyg==" />  
   <link rel="stylesheet" media="all" href="/assets/application-d12397cbc3a266ce5a99f7b588223090c55ca04a439953bb5f9304395de93fc3.css" data-turbolinks-track="reload" />  
   <script src="/assets/application-dd6f875ecc583bcada287d338f50c844f7f601a21c89dc743e1a8a8cca8980b6.js" data-turbolinks-track="reload"></script>  
  </head>  
  <body>  
   <p id="notice"></p>  
 <h1>Measures</h1>  
 <table>  
  <thead>  
   <tr>  
    <th>Temperature</th>  
    <th>Humidity</th>  
    <th colspan="3"></th>  
   </tr>  
  </thead>  
  <tbody>  
  </tbody>  
 </table>  
 <br>  
 <a href="/measures/new">New Measure</a>  
  </body>  
 </html>  
 prawnhead@ubuntu:~$   


That is all.

Comments