Rails on Ubuntu

2017-11-02 12:54:57来源:https://bananaappletw.github.io/computer science/2017/06/22/作者:bananaappletw.github.io人点击

分享
Introduction

简单记录一下 Ruby on Rails server setup 这里用的是最新版的 Ubuntu 17.04 server 版 这篇会教你设定


nginx
passenger
Let’s Encrypt(https)
postfix(mail server)

因为要设定 Let’s Encrypt 和 postfix,所以要有一个 DNS 的 A Record 指向你的 ip 位置


Passenger

这里主要有三种方法可以选



详细内容可以参考这篇


Standalone mode

只有 passenger


Nginx integration mode

passenger 和 nginx 整合


Apache integration mode

passenger 和 apache 整合


这里选用的是第二种方法


因为 nginx 的效能比 apache 好



接着就开始安装 nginx + passenger,大部分的步骤都跟这篇
一样


稍微要注意一下 ubuntu 的 code name,像是 17.04 的 code name 是 Zesty 在加 apt 的 repo 的时候要稍微改一下 code name


Install passenger packages


deb /2014th7cj/d/file/p/20171102/ xenial main
=>deb /2014th7cj/d/file/p/20171102/ zesty main


sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
# Add our APT repository
sudo sh -c 'echo deb /2014th7cj/d/file/p/20171102/ zesty main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
# Install Passenger + Nginx
sudo apt-get install -y nginx-extras passenger
Enable the passenger nginx module and restart nginx


修改/etc/nginx/nginx.conf
档案 将# include /etc/nginx/passenger.conf
取消注解


重启 nginx


sudo service nginx restart
Notice

另外网路上还有另一种装 passenger 和 nginx 的方法


是自行下载下来编译,所有的套件最后会放在 /opt 资料夹底下


我自己是不推荐这种方法,因为还要自己去设定 service script


Let’s Encrypt


详细可以参考这篇


Postfix

设定好 Let’s Encrypt 后就会有 ssh key 了


Install postfix package
sudo apt install -y postfix
Configure postfix


修改/etc/postfix/main.cf


原本应该是


# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

修改成


# TLS parameters
smtpd_tls_cert_file=/etc/letsencrypt/live/bamboofox.nctucs.net/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/bamboofox.nctucs.net/privkey.pem
smtp_tls_security_level=may
smtpd_tls_security_level=may


我的 domain name 是bamboofox.nctucs.net
,这里要修改成自己的 domain name



smtp_tls_security_level=may
和smtpd_tls_security_level=may
是让 email 寄信和收信加密


将自己的 ip 也加进 relay 的 list



修改/etc/postfix/main.cf


mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

修改成


mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 140.113.209.18


这边很重要因为之后用devise
寄信的时候会有certification
的问题


Restart postfix
sudo service postfix restart
Deploy ruby on rails project


参考这篇



直接从创deploy
帐号开始做


Create deploy user
sudo adduser deploy
sudo adduser deploy sudo
su deploy
Install rvm
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
/curl -sSL /2014th7cj/d/file/p/20171102/rvm-installer | bash -s stable
echo "source ~/.rvm/scripts/rvm" >> .bashrc
source ~/.rvm/scripts/rvm
rvm install 2.4.0
rvm use 2.4.0 --default
Install basic gems
gem install bundler
gem install rails
Show passenger config

执行指令


passenger-config about ruby-command

会得到


passenger-config was invoked through the following Ruby interpreter:
Command: /home/deploy/.rvm/gems/ruby-2.4.0/wrappers/ruby
Version: ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
To use in Apache: PassengerRuby /home/deploy/.rvm/gems/ruby-2.4.0/wrappers/ruby
To use in Nginx : passenger_ruby /home/deploy/.rvm/gems/ruby-2.4.0/wrappers/ruby
To use with Standalone: /home/deploy/.rvm/gems/ruby-2.4.0/wrappers/ruby /usr/bin/passenger start

这要用在等下的 nginx config


Install nvm
curl -o- /2014th7cj/d/file/p/20171102/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
nvm install stable
Install yarn
curl -sS /2014th7cj/d/file/p/20171102/pubkey.gpg | sudo apt-key add -
echo "deb /2014th7cj/d/file/p/20171102/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
Create basic app
rails new myapp --webpack=react
Deploy on nginx


修改 myapp 里的config/secrets.yml



这里 secret 可以使用rake secret
产生


production:
secret_key_base: d7913c3e87fadd4312ee1c5c1b13320caeecc72548f20b9122e2a1bf9ccdf0e9ecb86675168e578b5b3e960a81daa967c0081f69b082eb0c0e5df4b5810d71a9


修改/etc/nginx/sites-available/default


server {
# SSL configuration
#
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-bamboofox.nctucs.net.conf;
include snippets/ssl-params.conf;
# Add these three lines
passenger_enabled on;
server_name bamboofox.nctucs.net;
root /home/deploy/myapp/public;
}

重启 nginx


sudo service nginx restart

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台