Initial requirements for deployment into production

If we want to deploy our application to Production, we're going to need a few things installed on the target server first. The deploy requires us essentially taking our code from our machine and throwing that onto a target server where it will run. The advantage of this process is that if we have our code on a server, doing things like running migrations becomes a much simpler ask than if we do binary releases or docker releases.

If we want to deploy our application for production, we will need:

  • A suitable target production environment
  • A suitable production database with the correct permissions
  • A completed application (which we now have) 

The simplest implementation requires us to have a server with SSH access and git installed, although there are even a number of different ways for us to approach even this one way to deploy our application!

We'll start off by checking our code into some sort of source control, whether it's git or something else. This will allow us to log in to our server and deploy our code! We'll start by logging into our server (the deployment target we mentioned previously).

$ ssh [username]@[server]
... Logging in
$ cd [applications directory]
$ git clone [application]
$ cd [created application directory]
$ MIX_ENV=prod mix deps.get --only prod
$ MIX_ENV=prod mix compile

The next step, having logged into our server and started running our application, will be to start working on setting up all of the pertinent connection information and secrets information! You'll need to create your prod.secret.exs file in the config directory to set up the connection information for your production environment. This is also where you will set up the production secret that will be used for things like token creation and encryption. If you're using postgres, you'll also likely want to create a user, a database, and set up the appropriate password and permissions:

$ sudo -u postgres createuser vocial
$ sudo -u postgres createdb vocial_prod
$ sudo -u postgres psql
$> alter user vocial with encrypted password '...'
ALTER ROLE
$> grant all privileges on database vocial_prod to vocial
GRANT

We'll also want to generate a new secret that we can use in production! You'll get a different secret key every time you run this:

$ mix phx.gen.secret
GtLKgzRJpAnSt15...8G6313RsTwWG4e9lfZl

Using all of this information, our prod.secret.exs file should look something like this:

use Mix.Config

config :vocial, VocialWeb.Endpoint,
secret_key_base: "somegeneratedsecret"

config :vocial, Vocial.Repo,
adapter: Ecto.Adapters.Postgres,
username: "vocial",
password: "somepw",
database: "vocial_prod",
pool_size: 25

You'll want to tune the pool_size depending on the resources available to you and your database. The bigger this number is the less likely you'll run into issues where your Phoenix application is waiting for a worker in the poll to service its request, but it also means more strain on your database, so there is a fine line to walk for how this should be tuned overall.

Next, we'll create the prod database environment by running mix ecto.migrate in a production environment:

$ MIX_ENV=prod mix ecto.migrate
13:47:36.912 [info] == Running Vocial.Repo.Migrations.AddPollsTable.change/0 forward
13:47:36.912 [info] create table polls
13:47:36.924 [info] == Migrated in 0.0s
13:47:36.969 [info] == Running Vocial.Repo.Migrations.AddOptionsTable.change/0 forward
13:47:36.969 [info] create table options
13:47:36.973 [info] == Migrated in 0.0s

We'll also want to make sure that we've set up everything for the front end code as well, so we'll go into the assets directory and run the deploy command, and then run the production Phoenix digest command:

$ cd ./assets && npm install
$ npm run deploy && cd -
$ MIX_ENV=prod mix phx.digest

And finally, you're ready to run this in production mode! 

You'll want to set the RUN_ERL_LOG_MAXSIZE  and RUN_ERL_LOG_GENERATIONS environment variables since the default that any Erlang application uses is 10,000 bytes (roughly 10kb), which is far too small for use in a production environment! I typically will use something like this:

RUN_ERL_LOG_MAXSIZE=10000000 RUN_ERL_LOG_GENERATIONS=10

This makes sure that the logs generated are actually big enough that I can catch any issues that do occur! Finally, let's run our application:

RUN_ERL_LOG_MAXSIZE=10000000 RUN_ERL_LOG_GENERATIONS=10 MIX_ENV=prod PORT=4001 elixir --detached -S mix do compile, phx.server

And that's it! Our application is now officially deployed and running in production!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset