For creating your first project in Phoenix framework at this point you should have, Elixir, Erlang, Hex, and the Phoenix archive installed. You should also have PostgreSQL and node.js installed to build a default application.
Open terminal or command prompt and go to location on your file system where you want to create application. phoenix.new
is the mix command which will create new project for you. Assuming that the name of our application is hello_phoenix_world
, then type
$ mix phoenix.new hello_phoenix_world
Alternately, We can run mix phoenix.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
$ mix phoenix.new /Users/username/work/elixir-projects/hello_phoenix_world
Output
mix phoenix.new hello_phoenix_world
* creating hello_phoenix_world/config/config.exs
* creating hello_phoenix_world/config/dev.exs
* creating hello_phoenix_world/config/prod.exs
...
* creating hello_phoenix_world/web/views/layout_view.ex
* creating hello_phoenix_world/web/views/page_view.ex
Fetch and install dependencies? [Yn]
Phoenix will generate the directory structure for your project and it will create all the files required for application. Mix will ask you if you want it to install other required dependencies. Let's say yes to that.
Fetch and install dependencies? [Yn] Y
* running mix deps.get
* running npm install && node node_modules/brunch/bin/brunch build
Once dependencies are installed, the task will prompt you to change into our project directory and start application.
Move into your new project folder:
$cd hello_phoenix_world
You now need to setup the postgres username and password unless its already setup with the default postgres useranme and postgres password. Edit your config/dev.exs
file and set the username and password:
# config/dev.exs
config :hello_phoenix_world, HelloPhoenixWorld.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "hello_phoenix_world_dev",
hostname: "localhost",
pool_size: 10
Now, create the database with the ecto mix task:
$ mix ecto.create
We have a working application! Run your Phoenix application:
$ mix phoenix.server
You can also run your app inside IEx (Interactive Elixir) as:
$ iex -S mix phoenix.server
Load `http://localhost:4000` into your browser and you will see the default landing page of your application.
Now, lets add hello world to the Phoenix application. Open the web/templates/page/index.html.eex
file and replace the contents with the following and save the file:
<h2>Hello World</h2>
If you have not quit the server, the new code will be automatically compiled and your browser should now display your "Hello World" message.
You can now create CRUD resource.
Finally, to exit out of the server, type ctrl-c
crtl-c
(press the control key
and the c
key together) twice in a row.