如何将 Rails 应用部署到 VPS(或专用服务器)? [英] How to deploy a Rails app to a VPS (or dedicated server)?
问题描述
如何将 Rails 应用部署到 VPS(虚拟专用服务器)或专用服务器?有一个易于遵循的指南会很好.
How can I deploy a Rails app to a VPS (virtual private server) or a dedicated server? It would be nice to have an easy to follow guide.
我知道自动化流程的脚本,但我认为最好控制一切,以便更好地理解流程.
I know about scripts to automate the process, but I think it's better to have everything under control, in order to understand better the process.
推荐答案
我已经成功地将一个重型 Rails 应用程序部署到 Linode 或 Digital Ocean,使用这些技术:
I have successfully deployed a heavy Rails application to Linode or Digital Ocean, using these technologies:
- rbenv 用于 Ruby 安装
- nginx + Passenger 用于应用服务器
- PostgreSQL 用于数据库服务器
- Capistrano 自动部署(首先在您的开发机器上使用您的服务器 IP 和设置进行配置,我不会在这里介绍)
- rbenv for Ruby installation
- nginx + Passenger for the application server
- PostgreSQL for the database server
- Capistrano to automate deploys (configure this first on your dev machine with your server IP and settings, I will not cover it here)
这些是对我有用的步骤:
These are the steps that work for me:
创建新的虚拟机
按照您的主机(Linode 或 Digital Ocean)的设置说明创建节点并进行设置.
Follow the setup instructions of your hosting, being Linode or Digital Ocean, to create the node and set it up.
设置日期
- dpkg-reconfigure tzdata
更新包
- apt-get 更新
- apt-get 升级
创建用户
- adduser 部署
- usermod -a -G sudo deploy
- 退出
设置 SSH 密钥验证
在本地:
- ssh-keygen
- 复制公钥:
- scp ~/.ssh/id_rsa.pub deploy@example.com:~
在服务器上:
- ssh deploy@example.com
- 启用别名以列出文件:
- vim ~/.bashrc
- 取消注释所有别名
设置 SSH
- sudo vim/etc/ssh/sshd_config
- 将 PermitRootLogin 切换为 no
- sudo 服务 ssh 重启
设置防火墙
- sudo iptables -L(它应该显示一个空表)
- sudo vim/etc/iptables.firewall.rules
- 粘贴此内容:https://gist.github.com/davidmles/89fc88e48e17cf8252bfca374e46355f#file-iptables-firewall-rules
- sudo iptables-restore
- /etc/iptables.firewall.rules
- sudo iptables -L(现在它应该显示配置的规则)
- sudo vim/etc/network/if-pre-up.d/firewall
- 粘贴此内容:https://gist.github.com/davidmles/89fc88e48e48e17cf8252bfca374e46355f#file-firewall"文件防火墙
- sudo chmod +x/etc/network/if-pre-up.d/firewall
设置fail2ban
如果您有足够的空闲内存,请进行设置,因为它往往会吃掉它.
Set up if you have enough free memory, as it tends to eat it.
- sudo apt-get install -y fail2ban
安装 Git
- sudo apt-get install -y git
安装rbenv
- git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
- echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
- echo 'eval "$(rbenv init -)"' >> ~/.bashrc
- 源代码 ~/.bashrc
- git 克隆 https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
- git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
- echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
- echo 'eval "$(rbenv init -)"' >> ~/.bashrc
- source ~/.bashrc
- git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
安装 Ruby
- sudo apt-get install -y curl gnupg build-essential
- rbenv install -l(查找最新版本)
- rbenv install 2.3.3(或目前可用的最新版本)
- rbenv 全局 2.3.3
- rbenv 重新哈希
- vim .gemrc
- 粘贴: gem: --no-document
安装nginx+Passenger
- 按照文档安装:
安装 PostgreSQL
- 按照文档安装:
安装 node.js
需要预编译资产.
- sudo apt-get install -y nodejs
安装捆绑器
- 获取安装包
- rbenv 重新哈希
在 PostgreSQL 中创建用户
- createuser 用户名 --pwprompt
- createdb -Ousername -Eutf8 db_name
- 测试一下:
- psql db_name --user 用户名 --password
部署代码* 在服务器上:* 须藤 mkdir -p/srv/yoursite.com* sudo chown deploy:deploy/srv/yoursite.com* 在您的开发机器上:* bundle exec cap production deploy:check(它会抛出错误,因为它没有找到数据库)* 在服务器上:* cd/srv/yoursite.com/shared/config* vim database.yml(粘贴你的数据库配置)* vim secrets.yml(粘贴你的秘密配置)* 在您的开发机器上:*捆绑执行帽生产部署* 捆绑执行上限生产每当:update_crontab
Deploy the code * On the server: * sudo mkdir -p /srv/yoursite.com * sudo chown deploy:deploy /srv/yoursite.com * On your dev machine: * bundle exec cap production deploy:check (it will throw an error because it doesn't find the database) * On the server: * cd /srv/yoursite.com/shared/config * vim database.yml (paste your database config) * vim secrets.yml (paste your secrets config) * On your dev machine: * bundle exec cap production deploy * bundle exec cap production whenever:update_crontab
配置 logrotate
这篇关于如何将 Rails 应用部署到 VPS(或专用服务器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 按照文档安装: