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

自学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依赖等

扫描二维码关注公众号,回复: 118828 查看本文章
$ 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

猜你喜欢

转载自www.cnblogs.com/lmg-jie/p/8999083.html
今日推荐