部署 如何在 Ubuntu 云服务器上部署自己的 Rails 应用

xufeisofly · 2017年04月23日 · 最后由 nouse 回复于 2017年05月04日 · 8224 次阅读

前言

自学 rails 一段时间了,之前只用 heroku 部署了网站,想尝试把网站以一个更“正经”的方式呈现出来,就买了一个阿里云服务器。参考了网上部分 rails 部署教程,过程中也遇到了一些问题,所以在完成之后总结了一下,撰写此文,方便其他像我一样的初学者日后能够快速的将一个 rails 网站部署到云服务器上,不求原理,只讲操作,力求简单易懂。

注:本文大量参考了 ruby china 中的另一篇文章:《在 Aliyun 上快速部署 Ruby on Rails》,并根据自己遇到的一些问题做了细微补充

我的本地环境

操作系统:Ubuntu 16.04 32 位
Ruby: 2.3.0
Rails: 4.2.6

Step1: 买个云服务器

在此以阿里云服务器为例,我买的是最低配置,Ubuntu 16.04 操作系统,成功后会给你一个公网 IP,使用 ssh 连接到这个 IP 就可以配置这台 server ubuntu 了,阿里云控制台上可配置云服务器的密码。现在假设我买到的云服务器信息如下:

操作系统:Ubuntu 16.04 32 位
公网 IP:190.74.8.177
登录密码:******
数据库:MySQL

注:第一次买成了虚拟主机,然后发现虚拟主机是不能通过 ssh 配置的,不能安装 rails 环境…还好可以退款…

Step2: 连接云服务器

现在相当于买了台电脑回来,这个电脑没有实体,但我在任何地方都可以远程登录。 登录并输入密码:

$ ssh [email protected]
[email protected]'s password: ******

然后就会进入服务器账户,但永远以 root 用户进入不安全,所以登录后我们在 server 端新建一个用户:

~# adduser sofly

然后我们 ctrl+D 退出 server,在本地重新以 sofly 的用户登录:

$ ssh [email protected]
[email protected]'s password: ******

注:这里可以通过配置 ssh-keygen 来省去输入密码的过程,此处不介绍,可参考其他文章

Step3: 在云服务器上安装 Ruby

以下基本参考《在 Aliyun 上快速部署 Ruby on Rails》,不予详述。首先安装 RVM:

$ \curl -L https://get.rvm.io | bash -s stable
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"' >>~/.bashrc
$ source ~/.bashrc
$ rvm -v

把 RVM 源替换为 taobao 源:

$ sed -i -e 's/ftp\.ruby-lang\.org\/pub\/ruby/ruby\.taobao\.org\/mirrors\/ruby/g' ~/.rvm/config/db

安装 RVM 依赖等

$ rvm requirements
$ rvm pkg install readline
$ rvm pkg install openssl

安装 Ruby,版本和我本地 ruby 一样

$ rvm install 2.3.0
$ rvm use 2.3.0 --default

安装完毕后把 Rubygems 的源修改成阿里云的源

$ gem source -r https://rubygems.org/
$ gem source -a http://mirrors.aliyun.com/rubygems/

Step4:在云服务器上安装 Rails 及数据库

继续抄袭该文。 安装 Rails,版本和我本地 rails 一样

$ gem install rails -v 4.2.6

安装 MySQL

$ sudo apt-get install mysql-server

或 PostgreSQL

$ sudo apt-get install postgresql postgresql-client libpq-dev

Step5:上传本地 Rails 工程到云服务器

因为之前我的 rails 工程都提交到 github 或者 bitbucket 上了(如何提交至 git 此处不再赘述,请查阅相关文章),所以直接通过 git 的方式把代码下载到 server 上即可

$ git clone https://XXXXXX/project.git (在github或bitbucket上面就可以找到,直接复制)

这样代码就下载到服务器上了,然后安装 gem

$ cd project
$ bundle install

创建生产环境数据库并执行迁移

$ RAILS_ENV=production rake db:create
$ RAILS_ENV=production rake db:migrate

否则最终网站页面会显示(之前 heroku 部署时也经常遇到)

We're sorry, but something went wrong

重新 compile assets,这样所有的图片,CSS,scripts 才会加载

$ RAILS_ENV=production rake assets:precompile

Step6: 安装 Passenger for Nginx

Nginx 是 HTTP 服务器,运行 nginx 类似于本地开启 rails server,才能实现网站的访问,首先安装 passenger:

$ gem install passenger

然后通过 source 编译的方式安装 Nginx

$ rvmsudo passenger-install-nginx-module

一路回车即可,在这里选择 1 回车:

Automatically download and install Nginx?Nginx doesn't support loadable modules such as some other web servers do, so in order to install Nginx with Passenger support, it must be recompiled.Do you want this installer to download, compile and install Nginx for you?
1. Yes: download, compile and install Nginx for me. (recommended) The easiest way to get started. A stock Nginx 1.4.4 with Passenger support, but with no other additional third party modules, will be installed for you to a directory of your choice.
2. No: I want to customize my Nginx installation. (for advanced users) Choose this if you want to compile Nginx with more third party modules besides Passenger, or if you need to pass additional options to Nginx's 'configure' script. This installer will 1) ask you for the location of the Nginx source code, 2) run the 'configure' script according to your instructions, and 3) run 'make install'.
Whichever you choose, if you already have an existing Nginx configuration file, then it will be preserved.Enter your choice (1 or 2) or press Ctrl-C to abort:1[ENTER]

最后看到这句话即安装成功

Nginx with Passenger support was successfully installed.

Step7:说两句 Nginx 的坑点

很多教程上是通过 apt 的方式安装 nginx 的,而我们是通过 recompile Nginx from source 的方式安装的。前者的 nginx 目录是/usr/sbin/nginx,配置文件目录是/etc/nginx/nginx.conf,后者的 nginx 目录是/opt/nginx/sbin/nginx,配置文件目录是/opt/nginx/conf/nginx.conf,是不一样的,最好先确认一下这些文件的位置再进行后续操作。 另一个区别是 nginx 启动和停止方式不同,apt-get 安装的 nginx 可以通过初始化脚本/etc/init.d/nginx 或命令 service nginx restart 启动,但目前 source 安装方式是不能通过这两种方法启动的。 启动方式如下:

$ sudo /opt/nginx/sbin/nginx

停止方式如下:

$ ps auxw | grep nginx
root     29743  0.0  0.0  10192   564 ?        Ss   13:39   0:00 nginx: master process /opt/nginx/sbin/nginx
sofly    29744  0.0  0.4  10428  4500 ?        S    13:39   0:00 nginx: worker process
sofly    30080  0.0  0.0   5108   792 pts/1    S+   13:42   0:00 grep --color=auto nginx
$ sudo kill 29743

所以,如果想重启 nginx 就要先 kill 在启动。虽然有点麻烦,但亲测可用。

此处参考:[https://www.phusionpassenger.com/library/install/nginx/install/oss/rubygems_rvm/]

启动 Nginx 后,在浏览器中输入你的公网 IP,即:190.74.8.177,看到

Welcome to nginx page

说明 nginx server 已成功启动,但还没有连接 rails

Step8:配置 Nginx 启动 rails

编辑 nginx 配置文件

$ sudo vim /opt/nginx/conf/nginx.conf

我的配置文件是这样的,加注释的几处注意一下就行了

user  sofly; #此处设置为部署时的用户名
worker_processes  1; #此处为云服务器核数
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        /run/nginx.pid; #此处为nginx.pid的目录,位置应该是在这里
events {
    worker_connections  1024;
}
http {
    passenger_root /home/sofly/.rvm/gems/ruby-2.3.0/gems/passenger-5.1.2; #去掉这两处前面的注释符号#
    passenger_ruby /home/sofly/.rvm/gems/ruby-2.3.0/wrappers/ruby; #去掉这两处前面的注释符号#

    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    gzip_disable "msie6";

    server {
        listen       80;
        server_name  localhost;
        passenger_enabled on;
        root         /home/sofly/project/public; #此处设着为rails工程public文件夹位置
    }
}

配置完毕后重启 nginx

$ ps auxw | grep nginx
root     29743  0.0  0.0  10192   564 ?        Ss   13:39   0:00 nginx: master process /opt/nginx/sbin/nginx
sofly    29744  0.0  0.4  10428  4500 ?        S    13:39   0:00 nginx: worker process
sofly    30080  0.0  0.0   5108   792 pts/1    S+   13:42   0:00 grep --color=auto nginx
$ sudo kill 29743
$ sudo /opt/nginx/sbin/nginx

在浏览器中输入服务器 IP,发现并没有显示网页,而是出现如下的话

incomplete response received from application

What happened...

Step9: 配置 rails 工程的 production secret_key_base

出现上面问题的原因是 rails 生产环境没有配置 secret_key_base 变量,解决方法:

$ cd project
$ bundle exec rake secret # rails 4.2.6还需要bundle exec,请根据rails版本自行匹配

将输出的一大串字码粘贴到 rails 工程中/config/secrets.yml 去,替换掉该文件中的<%= ENV["SECRET_KEY_BASE"] %>,如下:

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

然后再重启 passenger,(一定要有否则不生效)

$ touch project/tmp/restart.txt

现在再刷新浏览器,就可以发现网站成功显示了。

此处参考:[http://stackoverflow.com/questions/29241053/incomplete-response-received-from-application-from-nginx-passenger]

注:若还是失败,请查看/opt/nginx/logs/error.log 中的错误日志

PS. 从硬件转行后端之后的第一篇贴,有问题处希望大家指正。以上。

=====================================================

持续补充:部署过程中遇到的坑

  1. Rails production 环境上传并 resize 图片报错:

"We're sorry, but something went wrong."

原因:没有安装 imagemagick

$ sudo apt-get install imagemagick libmagickcore-dev libmagickwand-dev

楼主再研究一下为网站挂上 SSL 吧,现在 HTTPS 属于标配了,当应用作为 iOS 应用的后端服务的时候也强制 HTTPS 方式连接。这个可以通过购买商业证书,或者免费的 Let's encrypt 来解决。另外,上 HTTPS 之后,类似 HSTS 之类的应用层的安全技术,还有浏览器里的混合内容之类的知识点也会浮现出来,如何提高网站 SSL 的强度也是一个话题。

部署的话,如果不使用 Docker 方案,Rails 世界最常用的是 Mina 或 Capistrano,话说虽然后者也是 Ruby 写成,但并不是 Rails 专利,很多 PHP、Python 甚至 .Net 应用,都由它部署。论坛里相关的文章很多,可以搜索一下看看。

然后还有大坑,运维。

这样整套网站部署运维的体系就算全了

啊 另外建议升级到 Rails 5 来做后续的学习,即将发布的 Rails 5.1 的变化还是挺革命性的,上一次革命性变化还是五年前的 Rails 3.1。

用 passenger 的话,建议参考官网提供的文档安装,这样就可以使用包提供的 services 来启动和停止了

jasl 回复

受教了,最近就遇到了 SSL 的问题,正在试图搞明白~

xufeisofly 回复

论坛里应该有相关文章可以搜下,期待搞定之后更新咯~

ACzero 回复

谢谢~我也觉得上面 kill 线程的方法太繁琐,后面仔细研究下

rvm,个人觉得 ruby 还是用系统包比较方便点……

nginx 1.4.4 这是什么千年老妖……

rails,为什么不用 bundle 安装要单独提前安装?

用 mina 自动部署哇

又看到了熟悉的 RMagick,这个和 database driver 还有 nokogiri 一样,每个 Rails 项目都要用,但安装和部署都很头疼。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号