自学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 root@190.74.8.177
root@190.74.8.177's password: ******

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

~# adduser sofly

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

$ ssh sofly@190.74.8.177
sofly@190.74.8.177'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

Ubuntu 云服务器上部署自己的 Rails 应用的更多相关文章

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

    安装步骤  参考:https://ruby-china.org/topics/32851 在云服务器上安装Ruby|Rails : http://www.cnblogs.com/znsongshu/p ...

  2. PHP--腾讯云服务器上部署LNMP环境

    本文参考 云服务器上部署Laravel的实例教程 腾讯云 创建 Laravel5.4 项目 阿里云服务器部署php的laravel项目,在阿里云买ECS 搭建 Linux+Nginx+Mysql+PH ...

  3. unigui在阿里云服务器上部署

    unigui在阿里云服务器上部署 客户租用了阿里云WINDOWS2008服务器,部署UNIGUI发现死活不行,WINDOWS2008自带的IE9浏览器打开URL,卡死在loading...... 我远 ...

  4. 使用Nginx+uwsgi在亚马逊云服务器上部署python+django项目完整版(二)——部署配置及相关知识

    ---恢复内容开始--- 一.前提: 1.django项目文件已放置在云服务器上,配置好运行环境,可正常运行 2.云服务器可正常连接 二.相关知识 1.python manage.py runserv ...

  5. Linux学习2-在阿里云服务器上部署禅道环境

    前言 以前出去面试总会被问到:测试环境怎么搭建?刚工作1-2年不会搭建测试环境还可以原谅自己,工作3-5年后如果还是对测试环境搭建一无所知,面试官会一脸的鄙视. 本篇以最简单的禅道环境搭建为例,学习下 ...

  6. 关于前期在云服务器上部署TOMCAT服务器的问题

    如果你要将你的项目放在服务器上(针对云服务器),首先你必须要学会前期部署Tomcat的准备, 第一步:安装jdk,配置环境变量 这个比较简单,配置java环境变量最为简单的方法是将java的 (../ ...

  7. 在云服务器上部署node.js服务器

    本文档介绍腾讯云·万象优图服务端nodejs的部署和集成,搭建一个nodejs+nginx为基础,对web端或者移动端提供http签名接口服务的例子程序.注意:本文档只是简单的示例,展示了服务端为终端 ...

  8. Linux-在新买的阿里云服务器上部署Tomcat并支持外网访问的配置(步骤记录)

    一.首先你得有一台外网上的服务器 华为.腾讯.阿里都有云服务售卖,我这里是在阿里云打折时购买的. 二.使用Xshell和XFTP连接上云服务 当然了,连接工具有很多种,可随意.购买服务器之后,你会收到 ...

  9. 记一些云服务器上部署koa2项目遇到的问题

    云服务器系统版本centos7.2,部署的项目koa2,node版本: 10.16.0 一.pm2的一些问题 1.安装pm2: npm install -g pm2 2.建立软链接,使pm2能全局使用 ...

随机推荐

  1. [洛谷P1273] 有线电视网

    类型:树形背包 传送门:>Here< 题意:给出一棵树,根节点在转播足球赛,每个叶子节点是一个观众在收看.每个叶子结点到根节点的路径权值之和是该点转播的费用,每个叶子节点的观众都会付val ...

  2. 【AGC018F】Two Trees 构造 黑白染色

    题目描述 有两棵有根树,顶点的编号都是\(1\)~\(n\). 你要给每个点一个权值\(a_i\),使得对于两棵树的所有顶点\(x\),满足\(|x\)的子树的权值和\(|=1\) \(n\leq 1 ...

  3. Android stadio 生成项目 Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE)

    解决方法 File-->Project-->Structrue-->SDK Location-->JDK location Use embedded JDK 前面勾去掉,指定一 ...

  4. 「CodeForces - 598B」Queries on a String

    BUPT 2017 summer training (for 16) #1I 题意 字符串s(1 ≤ |s| ≤ 10 000),有m(1 ≤ m ≤ 300)次操作,每次给l,r,k,代表将r位置插 ...

  5. Hdoj 1312.Red and Black 题解

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  6. 自学Python4.5-装饰器举例

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  7. android 读取EXcel 文件 读取文件内存卡的权限

    android 采用Java的读取xls文件的方式实现. 需要导入第三方Jxl.jar 包.  代码改自 其他博主  : 这只摘录下 读取xls文件的部分代码,当然这个代码在安卓平台需要添加下面的权限 ...

  8. 【HDU1848】Fibonacci again and again(博弈论)

    [HDU1848]Fibonacci again and again(博弈论) 题面 Hdu 你有三堆石子,每堆石子的个数是\(n,m,p\),你每次可以从一堆石子中取走斐波那契数列中一个元素等数量的 ...

  9. Listener监听器和Filter过滤器

    Listener监听器 WEB中的监听器 WEB 中的 Listener 和 Filter 是属于 Servlet 规范中的高级的技术.WEB中的监听器共有三类八种(监听三个域对象)* 事件源:Ser ...

  10. 浅析 @PathVariable 和 @RequestParam(转发,非原创)

    首先 上两个地址:地址①http://localhost:8989/SSSP/emps?pageNo=2地址②http://localhost:8989/SSSP/emp/7如果想获取地址①中的 pa ...