`

阿里云裸机部署rails运用

阅读更多
登录阿里云后首先
sudo apt-get update

apt-get install git curl clang make nodejs openssl libssl-dev libreadline6-dev zlib1g-dev libyaml-dev libxml2-dev  libxslt1-dev libpq-dev libmysqlclient-dev libsqlite3-dev libcurl4-openssl-dev unzip

———————————————
安装rvm

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

mac安装rvm下:
https://www.jianshu.com/p/c073e6fc01f5

reboot重启rvm groop分组权限
-----
# rvm如果安装失败参考命令
curl -sSL https://get.rvm.io | bash -s stable
#或者
curl -L https://rvm.io | bash -s stable


安装RVM依赖等
$ rvm requirements
$ rvm pkg install readline
$ rvm pkg install openssl

$ rvm install 2.4.1
$ rvm use 2.4.1 --default
rvm gemset create 3color244

----------
安装memcached:
sudo apt-get install memcached
ruby元编程的首次尝试和 memcached的使用
https://blog.csdn.net/select603/article/details/8472112

安装MySQL
$ sudo apt-get install mysql-server

sudo apt-get install mysql-server=5.5.40-0ubuntu0.12.04.1

----
Ubuntu 使用apt-get install安装制定版本的mysql
https://blog.csdn.net/dazhi_100/article/details/41980959

sudo apt install mysql-server-5.6
sudo apt install mysql-client-5.6

# 加数据库驱动
# mysql client is missing. You may need to 'apt-get install libmysqlclient-dev'
apt-get install libmysqlclient-dev

--------

在使用Ubuntu的时候发现不管是终端输入中文还是终端显示中文,都会出现中文显示为问号的问题。 .解决办法如下:
sudo locale-gen zh_CN.UTF-8   即可完成中文字符集的添加,之后中文就显示正常了。


出现上面问题的原因是rails生产环境没有配置secret_key_base变量,解决方法:
$ cd project
$ bundle exec rake secret # rails 4.2.6还需要bundle exec,请根据rails版本自行匹配


跑起来项目:
git clone your project 项目拉下来。
这样代码就下载到服务器上了,然后安装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

redis安装:
sudo apt-get install redis-server

后台启动sidekiq
RAILS_ENV=production bundle exec sidekiq -d -C config/sidekiq.yml
or
bundle exec sidekiq | logger -t sidekiq

sidekiq安装及使用
https://www.cnblogs.com/znsongshu/p/10349774.html


---------

Step6: 安装Passenger for Nginx (注意内存必须大于1g)
Nginx是HTTP服务器,运行nginx类似于本地开启rails server,才能实现网站的访问,首先安装passenger:
$ gem install passenger
然后通过source编译的方式安装Nginx
$ rvmsudo passenger-install-nginx-module


Step6: 安装Passenger for 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.


http {
    passenger_root /usr/local/rvm/gems/ruby-2.4.1@chemicalin/gems/passenger-5.0.30;
    passenger_ruby /usr/local/rvm/gems/ruby-2.4.1@chemicalin/wrappers/ruby;

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       3001;
        server_name  localhost;
        passenger_enabled on;
        root   /opt/www/chemicalin/public;
        #charset koi8-r;
    }
  }



启动linux

ps auxw | grep nginx

sudo /opt/nginx/sbin/nginx

/opt/nginx/sbin/nginx -s reload 

lsof -i:3001


参考:
[url] https://ruby-china.org/topics/32851[/url]

Integrating Action Cable with Passenger + Nginx:
https://www.phusionpassenger.com/library/config/nginx/action_cable_integration/


卸载 nginx 彻底删除
https://blog.csdn.net/sinat_34344123/article/details/79105254

-----------------由于用到了ActionCable需要用puma代替-------------------


使用 Nginx + Puma 部署 Rails 应用
https://www.jianshu.com/p/7a25c0e4daa6
Puma: 为并发而生的Ruby Web服务器
https://www.jianshu.com/p/1bbc6ef924ce?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Nginx + Puma 配置

puma服务启动:
puma -e production

bin/rails s -e production

1. 安装 Nginx
unbuntu 下直接通过 apt-get 安装就可以了。
sudo apt-get install nginx

查找错误,处理方法:
sudo nginx -t

重启 Nginx
$ sudo service nginx restart

nginx 配置文件
/etc/nginx/sites-enabled/default

   http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream app {
      server unix:///opt/www/chemicalin/tmp/sockets/puma.sock;
    }

    server {
        listen       3001;
        server_name  localhost;
        # passenger_enabled on;
        # root   /opt/www/chemicalin/public;
        #charset koi8-r;

         location /cable {
          proxy_pass  http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
        }

        location / {
            root   /opt/www/chemicalin/public;
            index  index.html index.htm;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;

            if (-f $request_filename.html) {
                    rewrite (.*) $1.html break;
            }

            if (!-f $request_filename) {
                proxy_pass http://app;
                break;
            }
        }
  }



puma.rb文件配置:

dir = ENV['PWD']
sdir = ENV['PWD']

plugin :tmp_restart

port 3000
workers 4
threads 5, 64
environment 'production'
directory dir
daemonize true
prune_bundler true
preload_app!

bind "unix://#{File.expand_path('tmp/sockets/puma.sock', sdir)}"
pidfile "#{File.expand_path('tmp/pids/puma.pid', sdir)}"
state_path "#{File.expand_path('tmp/sockets/puma.state', sdir)}"
activate_control_app "unix://#{File.expand_path('tmp/sockets/pumactl.sock', sdir)}"
stdout_redirect "#{File.expand_path('log/puma.stdout.log', sdir)}",
                "#{File.expand_path('log/puma.stdout.log', sdir)}"

on_worker_boot do
  ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
end

on_restart do
  puts '- * - On Production restart - * -'
  puts 'pidfile: '
  puts @options[:pidfile]
  puts 'binds: '
  puts @options[:binds]
  puts 'control_url: '
  puts @options[:control_url]
  puts '- * - * - * -'
end

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics