rails部署到服务器上_将Rails应用程序部署到数字海洋

rails部署到服务器上

You probably have deployed your rails applications to Heroku, it is pretty simple and straight forward. But what about deploying your app to Digital Ocean, I found it a little bit more challenging and tricky.

您可能已经将Rails应用程序部署到了Heroku,这非常简单直接。 但是,将您的应用程序部署到Digital Ocean方面又如何呢,我发现它更具挑战性和棘手性。

We’ll go through step by step to deploy a rails app to Digital Ocean.

我们将逐步完成将Rails应用程序部署到Digital Ocean的过程。

Let’s create a simple test app.

让我们创建一个简单的测试应用程序。

rails new tesp_app -d=postgresqlrails db:create

We’ll create a scaffold for Users.

我们将为用户创建一个支架。

rails g scaffold User name emailrails db:migrate

Create a repository on Github and push the project to the new repository.

在Github上创建一个存储库,并将项目推送到新的存储库。

git init
git add .
git commit -m “Initial commit”
git remote add origin “your origin”
git push origin master

This is all for the basic set up.

这就是基本设置。

数字海洋配置 (Digital Ocean Configuration)

First of all, you need to create an account on Digital Ocean, after creating a new account we can go ahead and create a new droplet. On the sidebar menu go to Droplets and then Create Droplet.

首先,您需要在Digital Ocean上创建一个帐户,在创建新帐户之后,我们可以继续创建新的Droplet。 在边栏菜单上,转到“ Droplet”,然后单击“创建Droplet”。

We’ll use Marketplace to get a predefined configuration for rails. Choose the standard droplet plan.

我们将使用Marketplace获取导轨的预定义配置。 选择标准液滴计划。

Image for post
Digital Ocean
数字海洋

Scroll down to the Authentication section and here we can choose to use a password or an ssh key for authentication. I prefer using ssh key, so select that one and then new SSH Key.

向下滚动到“身份验证”部分,在这里我们可以选择使用密码或ssh密钥进行身份验证。 我更喜欢使用ssh密钥,因此选择该密钥,然后选择新的SSH密钥。

Image for post
Digital Ocean
数字海洋

To generate a new ssh key on Ubuntu open your terminal and run this command to generate one.

要在Ubuntu上生成新的ssh密钥,请打开您的终端并运行此命令以生成一个。

ssh-keygen

It will ask you to enter the path of the file in which to save the key, you can provide a custom path or just click enter and it will generate the key on the default path. Then it will ask you to enter a password, you can also click enter if you don’t want to set a password for you ssh key.

它将要求您输入保存密钥的文件的路径,您可以提供自定义路径,也可以单击Enter,它将在默认路径上生成密钥。 然后它会要求您输入密码,如果您不想为ssh键设置密码,也可以单击Enter。

After that, you can get the key from the specified path, you’ll find two files one is a private key and the other one is public, so we’ll copy the public key and also add a name.

之后,您可以从指定的路径中获取密钥,您会发现两个文件,一个是私钥,另一个是公钥,因此我们将复制公钥并添加一个名称。

Image for post
Digital Ocean
数字海洋

Let’s add a name to our droplet as well. After that, we can click Create Droplet. Now we can connect to our droplet by using the IP address.

让我们也为液滴添加一个名称。 之后,我们可以单击创建液滴。 现在,我们可以使用IP地址连接到我们的Droplet。

Image for post
Digital Ocean
数字海洋

Go to your terminal and run this command.

转到终端并运行此命令。

ssh root@droplet_ip_address

It will ask you for a password if you set one for your ssh key. Now if we open the IP address on the browser we should see the Rails welcome page.

如果您为ssh密钥设置了一个密码,它将询问您一个密码。 现在,如果我们在浏览器中打开IP地址,我们应该看到Rails的欢迎页面。

Image for post
Default Rails Application
默认的Rails应用

Now lets’ change the path of the default rails app, first open the rails file under this route.

现在,让我们更改默认rails应用程序的路径,首先在此路由下打开rails文件。

nano /etc/nginx/sites-available/rails

Here we have the root of the default rails project, so let’s change that to be our project.

在这里,我们有了默认的rails项目,因此我们将其更改为我们的项目。

Image for post
Site Available Root Path
站点可用根路径

ctrl + x and y to close and save the file.

ctrl + xy关闭并保存文件。

Now we also need to change the working directory.

现在,我们还需要更改工作目录。

nano /etc/systemd/system/rails.service

Change it for the name of your app.

将其更改为您的应用程序的名称。

Image for post
Working Directory
工作目录

ctrl + x and y to close and save the file.

ctrl + xy关闭并保存文件。

Now we have to give all privileges to the rails user and we can do that with this command.

现在,我们必须将所有特权赋予rails用户,我们可以使用此命令来做到这一点。

gpasswd -a rails sudo

Then run

然后跑

sudo -i -u rails

Go to the home directory

转到主目录

cd ..

We can now clone the repo that contains our project. I’ll use HTTPS to clone the repo for simplicity if you want to clone the repo using ssh key you could generate a new key inside the droplet and add it to Github. I’ll just go with HTTPS and provide a password and username.

现在,我们可以克隆包含我们项目的仓库。 为了简单起见,我将使用HTTPS克隆存储库,如果您想使用ssh key克隆存储库,则可以在Droplet内生成一个新密钥并将其添加到Github。 我将使用HTTPS并提供密码和用户名。

sudo git clone “your Github URL”

Now if we run ls we will see the rails folder and the folder of our application as well.

现在,如果我们运行ls,我们将看到rails文件夹以及我们应用程序的文件夹。

Let’s give read and write permissions to our project.

让我们为项目授予读写权限。

chmod 777 -R test_appls -la
Image for post

Then we can go to our project directory cd testa_app and it will automatically tell us that we need to install ruby, in my case I need the version 2.6.3. Go ahead and install ruby with this command.

然后,我们可以转到项目目录cd testa_app ,它将自动告诉我们需要安装ruby,在我的情况下,我需要版本2.6.3。 继续并使用此命令安装ruby。

rvm install ruby-2.6.3
Image for post

Now we have to install bundler

现在我们必须安装捆绑器

gem install bundler:2.1.4

And finally, we can run bundle install

最后,我们可以运行捆绑安装

bundle install
Image for post

Now we can run

现在我们可以运行

npm install

And change the webpacker.yml configuration

并更改webpacker.yml配置

nano config/webpacker.yml

And change this line to false

并将此行更改为false

check_yarn_integrity: false

Create and migrate the DB.

创建并迁移数据库。

rails db:create
rails db:migrate

We are almost done.

我们快完成了。

exit

Then let’s restart Nginx and rails

然后让我们重启Nginx和rails

systemctl restart nginx
systemctl restart rails

We are done, we can now use the IP address on the browser and our rails app is running. Go to /users to see that our app is working correctly.

完成了,我们现在可以在浏览器上使用IP地址,并且我们的rails应用程序正在运行。 转到/ users以查看我们的应用程序是否正常运行。

I hope you enjoyed and learned something new, share this article if you liked it and thank you for reading.

希望您喜欢并学到一些新东西,如果喜欢可以分享这篇文章,并感谢您的阅读。

翻译自: https://medium.com/swlh/deploy-rails-app-to-digital-ocean-f1aeb0991470

rails部署到服务器上

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值