aws v2.2.exe_如何在AWS Elastic Beanstalk上部署Rails 5.2 PostgreSQL应用
aws v2.2.exe
by Evrim Persembe
通過埃夫里姆·佩塞姆貝
如何在AWS Elastic Beanstalk上部署Rails 5.2 PostgreSQL應(yīng)用 (How to deploy a Rails 5.2 PostgreSQL app on AWS Elastic Beanstalk)
It’s official, using Heroku for all my Rails projects so far has spoiled me rotten. After receiving some AWS credits thanks to a pitch competition, I decided to deploy my latest project on Elastic Beanstalk (AWS’ Heroku competitor). All I have to say is that I miss Heroku.
官方的說法 ,到目前為止,在我所有的Rails項目中使用Heroku都使我很爛。 在一次激烈的競爭中獲得了一些AWS積分后,我決定在Elastic Beanstalk ( AWS的Heroku競爭對手)上部署我的最新項目。 我只想說我想念Heroku。
Alas, if you are in a similar situation, here are step-by-step instructions to deploying your Rails 5.2 / PostgreSQL app on Elastic Beanstalk.
las,如果您的情況類似,這里是在Elastic Beanstalk上部署Rails 5.2 / PostgreSQL應(yīng)用程序的分步說明。
安裝Elastic Beanstalk CLI (Installing the Elastic Beanstalk CLI)
We will use the terminal in this tutorial. Let’s begin with installing the “Elastic Beanstalk Command Line Interface.” Here is how to do it on macOS using Homebrew:
我們將在本教程中使用終端。 讓我們從安裝“ Elastic Beanstalk命令行界面”開始。 這是使用Homebrew在macOS上執(zhí)行操作的方法:
brew install awsebcliIf you are using another platform, googling “how to install awsebcli on [your platform]” should lead you in the right direction.
如果您使用的是其他平臺,則搜索“如何在[您的平臺]上安裝awsebcli”可以引導(dǎo)您朝正確的方向發(fā)展。
初始化Elastic Beanstalk (Initializing Elastic Beanstalk)
I will assume that you already have an Amazon Web Services account, if not go ahead and create one. Now, go into the directory of your project, and initialize Elastic Beanstalk:
我將假設(shè)您已經(jīng)有一個Amazon Web Services帳戶,如果沒有,請繼續(xù)創(chuàng)建一個。 現(xiàn)在,進(jìn)入項目目錄,并初始化Elastic Beanstalk:
cd my_projecteb initThen the EB CLI will ask you a few questions to initialize the Elastic Beanstalk application. The initialization part is straightforward. If you get stuck anywhere you can check out the “Configure the EB CLI” page from the documentation.
然后,EB CLI將詢問您一些問題來初始化Elastic Beanstalk應(yīng)用程序。 初始化部分很簡單。 如果您卡在任何地方,都可以從文檔中查看“配置EB CLI”頁面。
創(chuàng)造新環(huán)境 (Creating a new environment)
As you already know, your application can have many environments (think of them as different configurations). For example, you might have a “production” environment. This is the environment that you use for the user-facing version of your app. But you might want to have another environment named “staging.” This is where you try new versions of your app, before pushing it to the production environment.
如您所知,您的應(yīng)用程序可以具有許多環(huán)境(將它們視為不同的配置)。 例如,您可能有一個“生產(chǎn)”環(huán)境。 這是您用于應(yīng)用程序的面向用戶版本的環(huán)境。 但是您可能想要另一個名為“ staging”的環(huán)境。 在將應(yīng)用推送到生產(chǎn)環(huán)境之前,您可以在這里嘗試新版本的應(yīng)用。
We can create an environment using the command below:
我們可以使用以下命令創(chuàng)建環(huán)境:
eb create production部署到Elastic Beanstalk (Deploying to Elastic Beanstalk)
Assuming you are using Git, commit your changes before deploying your application. The EB CLI deploys your last commit. If you deploy before committing, you will deploy an earlier version of your app.
假設(shè)您正在使用Git,請在部署應(yīng)用程序之前提交更改。 EB CLI會部署您的最后一次提交。 如果在提交之前進(jìn)行部署,則將部署應(yīng)用程序的早期版本。
After committing your changes, deploy using the following:
提交更改后,請使用以下內(nèi)容進(jìn)行部署:
eb deploySo far so good, now we need to set a few things before our app actually starts working.
到目前為止,到目前為止,我們需要在應(yīng)用程序?qū)嶋H開始工作之前進(jìn)行一些設(shè)置。
設(shè)置主密鑰 (Setting up the master key)
You can use the CLI for this purpose as well, but I prefer using the web panel for this. Here is how:
您也可以為此使用CLI,但是我更喜歡使用Web面板。 方法如下:
Under “Environment properties,” add a new key named RAILS_MASTER_KEY. Set its value to the content of your “master.key” file. You can find this file in the “config” directory of your Rails app.
在“環(huán)境屬性”下,添加一個名為RAILS_MASTER_KEY的新密鑰。 將其值設(shè)置為“ master.key”文件的內(nèi)容。 您可以在Rails應(yīng)用程序的“ config”目錄中找到此文件。
設(shè)置PostgreSQL數(shù)據(jù)庫 (Setting up a PostgreSQL database)
Elastic Beanstalk provides an easy way to set up a database, which you can reach through “Configuration -> Database.” I prefer not to use that because if you need to rebuild your Elastic Beanstalk environment, your database will be deleted. So, we will set up the database separate from our Elastic Beanstalk environment.
Elastic Beanstalk提供了一種建立數(shù)據(jù)庫的簡便方法,您可以通過“配置->數(shù)據(jù)庫”來訪問它。 我不想使用它,因?yàn)槿绻枰亟‥lastic Beanstalk環(huán)境,則數(shù)據(jù)庫將被刪除。 因此,我們將與Elastic Beanstalk環(huán)境分開設(shè)置數(shù)據(jù)庫。
在RDS上創(chuàng)建PostgreSQL數(shù)據(jù)庫 (Creating a PostgreSQL database on RDS)
Here, you can try different options, and see what the estimated monthly costs are. Settle with something that is within your budget. You can start with a db.t2.micro instance, no multi-AZ deployment and a general purpose SSD.
在這里,您可以嘗試其他選項,并查看估計的每月費(fèi)用。 用您預(yù)算范圍內(nèi)的東西解決。 您可以從db.t2.micro實(shí)例開始,無需多db.t2.micro區(qū)部署和通用SSD。
Pick a database name, such as my_app_production.
選擇一個數(shù)據(jù)庫名稱,例如my_app_production 。
允許訪問數(shù)據(jù)庫 (Allowing access to the database)
In the meantime, let’s add Postgres access to your security group:
同時,讓我們向您的安全組添加Postgres訪問權(quán)限:
Click on “Add Rule.” For “Type,” choose “PostgreSQL,” and for “Source” type in the ID of the security group that you are adding this rule to. It should be right above the “Inbound” tab and should look like sg-*.
點(diǎn)擊“添加規(guī)則”。 對于“類型”,選擇“ PostgreSQL”,對于“源”,輸入要添加此規(guī)則的安全組的ID。 它應(yīng)該在“入站”選項卡的正上方,并且應(yīng)該看起來像sg-* 。
設(shè)置生產(chǎn)數(shù)據(jù)庫配置 (Setting up the production database configuration)
Now, in your Rails directory, open config/database.yml. Change it as such:
現(xiàn)在,在您的Rails目錄中,打開config/database.yml 。 這樣更改:
# ...production: <<: *default database: <%= ENV['RDS_DB_NAME'] %> username: <%= ENV['RDS_USERNAME'] %> password: <%= ENV['RDS_PASSWORD'] %> host: <%= ENV['RDS_HOSTNAME'] %> port: <%= ENV['RDS_PORT'] %>將相關(guān)環(huán)境變量添加到Elastic Beanstalk (Adding relevant environment variables to Elastic Beanstalk)
We told Rails to get the information for the production database using the above environment variables. Now we need to make sure that our Elastic Beanstalk environment includes these variables:
我們告訴Rails使用上述環(huán)境變量來獲取生產(chǎn)數(shù)據(jù)庫的信息。 現(xiàn)在,我們需要確保我們的Elastic Beanstalk環(huán)境包含以下變量:
RDS_DB_NAME: Database name you picked when setting up your database.
RDS_DB_NAME :設(shè)置數(shù)據(jù)庫時選擇的數(shù)據(jù)庫名稱。
RDS_USERNAME: Username you picked when setting up your database.
RDS_USERNAME :設(shè)置數(shù)據(jù)庫時選擇的用戶名。
RDS_PASSWORD: Password you picked when setting up your database.
RDS_PASSWORD :設(shè)置數(shù)據(jù)庫時選擇的密碼。
RDS_HOSTNAME: Go to “Services -> RDS,” and you can find this information under the “Connect” section of your database instance information page. It is called “Endpoint.”
RDS_HOSTNAME :轉(zhuǎn)到“服務(wù)-> RDS”,您可以在數(shù)據(jù)庫實(shí)例信息頁面的“連接”部分下找到此信息。 它稱為“端點(diǎn)”。
RDS_PORT: Set this to 5432.
RDS_PORT :將此設(shè)置為5432。
After this, commit your Rails app directory again, and run eb deploy. You might want to wait a few minutes before doing this because Elastic Beanstalk does some stuff in the background after updating environment variables.
之后,再次提交您的Rails應(yīng)用程序目錄,然后運(yùn)行eb deploy 。 您可能需要等待幾分鐘,因?yàn)閳?zhí)行更新環(huán)境變量后,Elastic Beanstalk在后臺執(zhí)行了一些操作。
After these steps, your Rails app “should” be running.
完成這些步驟后,“應(yīng)”運(yùn)行Rails應(yīng)用程序。
還是行不通? (Still not working?)
If there are any issues, you can go to your EB environment on the AWS web panel, click on “Logs,” and choose “Request Logs -> Last 100 Lines” to see the logs. But before doing that, I’d recommend trying to run your Rails app using the production environment on your local machine by using the command rails s RAILS_ENV=production.
如果有任何問題,您可以在AWS Web面板上轉(zhuǎn)到EB環(huán)境,單擊“日志”,然后選擇“請求日志->最后100行”以查看日志。 但是,在此之前,我建議您嘗試使用本地nd rails s RAILS_ENV=product ion在本地計算機(jī)上的生產(chǎn)環(huán)境中運(yùn)行Rails應(yīng)用程序。
I’ll be the first to admit that I’m not the most experienced person when it comes to deployment. As I said, I always used Heroku in the past, and I probably will use it for my future projects as well. These steps worked for me after a few days of scratching my head trying to set up my Rails app on Elastic Beanstalk, so I wanted to share these in hopes of saving time for people who are in the same situation I was. So, take this all with a grain of salt, and good luck!
我將是第一個承認(rèn)我在部署方面不是最有經(jīng)驗(yàn)的人。 就像我說的那樣,我過去一直使用Heroku,將來我也可能會使用它。 經(jīng)過幾天的努力,嘗試在Elastic Beanstalk上設(shè)置我的Rails應(yīng)用程序之后,這些步驟對我有用。因此,我想與大家分享這些信息,以期為處于同樣狀況的人們節(jié)省時間。 因此,帶上一粒鹽和好運(yùn)來帶走這一切!
If you like this article, follow me on Twitter or sign up to my newsletter to get notified when I write new articles. I write about software and startups.
如果您喜歡這篇文章,請在Twitter上關(guān)注我或注冊我的新聞通訊,以在我撰寫新文章時得到通知。 我寫有關(guān)軟件和創(chuàng)業(yè)公司的文章。
If you are looking for a Rails developer, I’m currently available for remote work. Feel free to get in touch with me at hi{at}evrim.io.
如果您正在尋找Rails開發(fā)人員,那么我現(xiàn)在可以進(jìn)行遠(yuǎn)程工作。 歡迎訪問hi {at} evrim.io與我聯(lián)系。
Originally published at evrim.io on November 28, 2018.
最初于2018年11月28日發(fā)布在evrim.io上。
翻譯自: https://www.freecodecamp.org/news/how-to-deploy-a-rails-5-2-postgresql-app-on-aws-elastic-beanstalk-34e5cec3a984/
aws v2.2.exe
總結(jié)
以上是生活随笔為你收集整理的aws v2.2.exe_如何在AWS Elastic Beanstalk上部署Rails 5.2 PostgreSQL应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到姐姐改嫁什么意思
- 下一篇: mysql 数据库 安全_如何确保您My