-
Notifications
You must be signed in to change notification settings - Fork 1
Running Rails app with Puma and Nginx with SSL certificate
Reference:
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
https://stackoverflow.com/questions/42217660/cannot-run-puma-upstart-script-on-ubuntu-16-04
https://github.com/puma/puma/blob/master/docs/systemd.md
All online sources and documents referred to in this document are detailed here
cd ~/.rbenv/plugins
git clone https://github.com/sstephenson/rbenv-vars.git
Create a rake secret
cd ~/data2paper
rake secret
Copy the secret key that is generated, then open the .rbenv-vars and paste it in there
vim .rbenv-vars
Add the line to the file, save and exit
SECRET_KEY_BASE=your_generated_secret_which_you_copied
Add the file .rbenv-vars to .gitignore
vim .gitignore
Add the line to the file, save and exit
.rbenv-vars
Install Nginx
sudo apt-get install nginx
Configure the site
Create a file in /etc/nginx/sites-available/data2paper
An example site is available in docs/data2paper if using Puma
An example site is available in docs/data2paper-3000 if running the webserver on port 3000
Enable the site
ln -s /etc/nginx/sites-available/data2paper /etc/nginx/sites-enabled/data2paper
Disable the default site if it exists
rm /etc/nginx/sites-enabled/default
Restart Nginx to put the changes into effect
sudo service nginx restart
See what profile application are available
sudo ufw app list
Install OpenSSH and Nginx application profile
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
Enable UFW
sudo ufw enable
Check the status
sudo ufw status
Note: Puma is already installed in Hyrax See https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04#configure-puma
The configuration file after all the changes, looks like https://github.com/anusharanganathan/data2paper/blob/master/config/puma.rb
Create the shared directories mentioned in the puma config
cd ~/data2paper
mkdir -p shared/pids shared/sockets shared/log
copy the files docs/puma.service and docs/puma.socket to /etc/systemd/system/
sudo cp docs/puma* /etc/systemd/system/
After installing or making changes to either puma.socket or puma.service, reload the systemctl daemon
sudo systemctl daemon-reload
Enable both socket and service so they start on boot.
sudo systemctl enable puma.socket puma.service
Initial start up. The Requires directive in puma.service ensures the socket is started before the service.
sudo systemctl start puma.socket puma.service
Check status of both socket and service.
sudo systemctl status puma.socket puma.service
Visit the web page http://data2paper_dev_ip_address
-
Get the public IP address from the Azure portal.
-
Visit Google domains and add a custom resource record for the data2paper domain.
For exampledev A 1h 52.166.126.144 -
Edit the site in Ngnix, add the server name, save and exit
sudo vim /etc/nginx/sites-enabled/data2paper
Replace the lineserver_name localhost;withserver_name dev.data2paper.org; -
Restart Nginx
sudo systemctl restart nginx.service -
Visit http://dev.data2paper.org and test that the page is being served
-
Enable port 80 and 443 outbound traffic in Azure portal
data2paperdev - Networking - Add Outbound port -
Follow instructions in Step 1 — Installing Certbot
-
Follow instructions in Step 2 — Setting up Nginx
-
Follow instructions in Step 3 — Updating the Firewall
-
IMPORTANT Obtaining a test ssl certificate
Similar to the next step, but run with the additional argument--test-cert.
Note: If there are errors, you may get locked out and so it is safe to first do this. I got locked out for an hour due to authz errors (port 80 was not opened in Azure portal for inbound traffic)
sudo certbot --test-cert --nginx -d dev.data2paper.org
Selected option to configured with http traffic redirected to https. -
Follow instructions in Step 4 — Obtaining an SSL Certificate
Important You should get the message a certificate already exists. Select re-install
sudo certbot --nginx -d dev.data2paper.org
Selected option to configured with http traffic redirected to https. -
Follow instructions in Step 5 — Updating Diffie-Hellman Parameters
Generate a strong Diffie-Hellman group to strengthen security. This command will generate a 2048-bit groupsudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048Add or Replace the location of ssl_dhparam in the nginx site config. Edit the file /etc/nginx/sites-available/data2paper
ssl_dhparam /etc/ssl/certs/dhparam.pem;Test the configuration to make sure that there are no syntax errors in any of the Nginx files
sudo nginx -tIf no problems were found, restart Nginx to enable the changes
sudo service nginx restart -
Test the SSL certificate
Visit https://www.ssllabs.com/ssltest/analyze.html?d=dev.data2paper.org -
Follow instructions in Step 6 — Setting Up Auto Renewal