Professional Documents
Culture Documents
The aim of this first guide is to get a Phoenix application up and running as quickly as possible.
Before we begin, please take a minute to read the Installation Guide. By installing any necessary
dependencies beforehand, we'll be able to get our application up and running smoothly.
At this point, we should have Elixir, Erlang, Hex, and the Phoenix archive installed. We should
also have PostgreSQL and node.js installed to build a default application.
We can run mix phx.new from any directory in order to bootstrap our Phoenix application.
Phoenix will accept either an absolute or relative path for the directory of our new project.
Assuming that the name of our application is hello, let's run the following command:
A note about webpack before we begin: Phoenix will use webpack for asset management by
default. Webpack's dependencies are installed via the node package manager, not mix. Phoenix
will prompt us to install them at the end of the mix phx.new task. If we say "no" at that point,
and if we don't install those dependencies later with npm install, our application will raise
errors when we try to start it, and our assets may not load properly. If we don't want to use
webpack at all, we can simply pass --no-webpack to mix phx.new.
A note about Ecto: Ecto allows our Phoenix application to communicate with a data store, such
as PostgreSQL, MySQL, and others. If our application will not require this component we can
skip this dependency by passing the --no-ecto flag to mix phx.new. This flag may also be
combined with --no-webpack to create a skeleton application.
To learn more about mix phx.new you can read the Mix Tasks Guide.
Phoenix generates the directory structure and all the files we will need for our application. When
it's done, it will ask us if we want it to install our dependencies for us. Let's say yes to that.
$ cd hello
$ mix ecto.create
$ mix phx.server
You can also run your app inside IEx (Interactive Elixir) as:
Once our dependencies are installed, the task will prompt us to change into our project directory
and start our application.
Phoenix assumes that our PostgreSQL database will have a postgres user account with the
correct permissions and a password of "postgres". If that isn't the case, please see the Mix Tasks
Guide to learn more about the mix ecto.create task.
Ok, let's give it a try. First, we'll cd into the hello/ directory we've just created:
$ cd hello
$ mix ecto.create
Compiling 13 files (.ex)
Generated hello app
The database for Hello.Repo has been created
Note: if this is the first time you are running this command, Phoenix may also ask to install
Rebar. Go ahead with the installation as Rebar is used to build Erlang packages.
$ mix phx.server
[info] Running HelloWeb.Endpoint with cowboy 2.5.0 at http://localhost:4000
$ cd hello
$ mix deps.get
$ cd assets && npm install && node node_modules/webpack/bin/webpack.js
--mode development
$ mix ecto.create
$ mix phx.server
You can also run your app inside IEx (Interactive Elixir) as:
By default Phoenix accepts requests on port 4000. If we point our favorite web browser at
http://localhost:4000, we should see the Phoenix Framework welcome page.
Using MySQL
Phoenix applications are configured to use PostgreSQL by default, but what if we want to use
MySQL instead? In this guide, we’ll walk through changing that default whether we are about to
create a new application, or whether we have an existing one configured for PostgreSQL.
If we are about to create a new application, configuring our application to use MySQL is easy.
We can simply pass the --database mysql flag to phoenix.new and everything will be
configured correctly.
This will set up all the correct dependencies and configuration for us automatically. Once we
install those dependencies with mix deps.get, we’ll be ready to begin working with Ecto in our
application.
If we have an existing application, all we need to do is switch adapters and make some small
configuration changes.
To switch adapters, we need to remove the Postgrex dependency and add a new one for Mariaex
instead.
defmodule HelloPhoenix.Mixfile do
use Mix.Project
. . .
# Specifies your project dependencies
#
# Type `mix help deps` for examples and options
defp deps do
[{:phoenix, "~> 1.2.0"},
{:phoenix_ecto, "~> 3.0"},
{:mariaex, "~> 0.6.1", override: true},
{:phoenix_html, "~> 2.3"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:cowboy, "~> 1.0"}]
end
end
We also need to remove the :postgrex app from our list of applications and substitute the
:mariaex app instead. Let’s do that in mix.exs as well.
defmodule HelloPhoenix.Mixfile do
use Mix.Project
. . .
def application do
[mod: {MysqlTester, []},
applications: [:phoenix, :cowboy, :logger,
:phoenix_ecto, :mariaex]]
end
. . .
Next, we need to configure our new adapter. Let’s open up our config/dev.exs file and do that.
If we have an existing configuration block for our HelloPhoenix.Repo, we can simply change
the values to match our new ones. The most important thing is to make sure we are using the
MySQL adapter adapter: Ecto.Adapters.MySQL,.
Now all we need to do is fetch our new dependency, and we’ll be ready to go.
With our new adapter installed and configured, we’re ready to create our database.
$ mix ecto.create
The database for HelloPhoenix.repo has been created.
We’re also ready to run any migrations, or do anything else with Ecto that we might choose.
$ mix ecto.migrate
[info] == Running HelloPhoenix.Repo.Migrations.CreateUser.change/0 forward
[info] create table users
[info] == Migrated in 0.2s
mix phx.new View Source
Creates a new Phoenix project.
A project at the given PATH will be created. The application name and module name will be
retrieved from the path, unless --module or --app is given.
Options
--umbrella - generate an umbrella project, with one application for your domain, and a
second application for the web interface.
Please check the driver docs, between parentheses, for more information and
requirements. Defaults to "postgres".
--no-webpack - do not generate webpack files for static asset building. When choosing
this option, you will need to manually handle JavaScript dependencies if building HTML
apps
When passing the --no-ecto flag, Phoenix generators such as phx.gen.html, phx.gen.json
and phx.gen.context may no longer work as expected as they generate context files that rely
on Ecto for the database access. In those cases, you can pass the --no-context flag to generate
most of the HTML and JSON files but skip the context, allowing you to fill in the blanks as
desired.
Similarly, if --no-html is given, the files generated by phx.gen.html will no longer work, as
important HTML components will be missing.
Examples
mix phx.new hello_world
Is equivalent to:
As an umbrella:
hello_umbrella/ Hello.Umbrella
apps/
hello/ Hello
hello_web/ HelloWeb
You can read more about umbrella projects using the official Elixir guide
mix phx.new -v
run(argv)
A task needs to implement run which receives a list of command line args.