How to Install the Phoenix Framework on CentOS 7

Posted on

Using a Different System?

  • How to Install Elixir and Phoenix Framework on Ubuntu 16.04

Are we missing a guide for your target system? Request one, or submit your own!


Phoenix is an emerging Elixir-based web development framework. It is designed to provide high development productivity, rich features, and powerful runtime performance.

This tutorial will show you how to install Phoenix on a Vultr CentOS 7 server instance for development purposes.

Prerequisites

Before proceeding, I assume that you have:

  • Deployed a new Vultr CentOS 7 server instance.
  • Logged into this CentOS 7 system as a non-root sudo user.

Step 1: Update the system

sudo yum install epel-release
sudo yum update
sudo reboot

Step 2: Install Erlang

First of all, you need to install Erlang on your system. Phoenix is a framework written in the Elixir programming language, and any Elixir-based application has to be compiled to Erlang byte code before it can be executed.

cd ~
wget http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
sudo rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
sudo yum install erlang

You can confirm your installation of Erlang with:

erl

This command will take you into the Erlang shell. When starting the Erlang shell, you will see the following output.

Erlang/OTP 18 [erts-7.3] [source-d2a6d81] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1>

Press Ctrl+C twice to exit the Erlang shell.

Step 3: Install Elixir

Because the version of Elixir in the CentOS 7 system YUM repository is rather dated, you should use the official pre-compiled Elixir archive to install the latest version of Elixir.

Download and unzip the latest Elixir precompiled archive:

cd /usr/bin
sudo mkdir elixir
cd /usr/bin/elixir
sudo wget https://github.com/elixir-lang/elixir/releases/download/v1.2.5/Precompiled.zip
sudo yum install unzip
sudo unzip Precompiled.zip

Elixir is now installed on your system. You can run Elixir commands by specifying the path of each Elixir-related command, such as:

/usr/bin/elixir/bin/elixir -v

This command will tell you the version of Elixir on your system.

As a matter of convenience, you can add Elixir’s bin path (along with to-be-installed node.js bin path) to your PATH environment variable:

sudo vi /etc/profile

Append the following line to the end of the file:

export PATH="$PATH:/usr/bin/elixir/bin:/usr/bin/node-v6.1.0-linux-x64/bin"

Save and quit:

:wq

Reload the profile:

source /etc/profile

From now on, you can run an Elixir-related command without specifying its complete path, such as:

elixir -v

Now, install the Hex package manager by running the mix command in the same fashion:

cd ~
mix local.hex

Answer Y during the installation process.

Step 4: Install Phoenix

Use the following command to install Phoenix:

mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

Answer Y during the installation process.

Step 5: Install Node.js (optional)

If you want to use brunch.io, the default building tool of Phoenix, to compile static assets (javascript, css, etc.), you need to install Node.js (>= 5.0.0):

cd ~
wget https://nodejs.org/dist/v6.1.0/node-v6.1.0-linux-x64.tar.xz
sudo yum install xz
xz -d node-v6.1.0-linux-x64.tar.xz
tar -xvf node-v6.1.0-linux-x64.tar
sudo mv ~/node-v6.1.0-linux-x64 /usr/bin/

Remember, the Node.js path was added into the PATH environment variable in step 3. You can test the Node.js installation with this command:

node -v

Step 6: Install PostgreSQL

By default, Phoenix uses PostgreSQL to configure applications. On CentOS 7, You can install PostgreSQL using YUM:

sudo yum install -y postgresql-server
sudo postgresql-setup initdb

Start the postgresql service:

sudo systemctl start postgresql.service
sudo systemctl enable postgresql.service

Set a password for the default PostgreSQL user “postgres”:

sudo -u postgres psql

In the PostgreSQL shell (after the prompt turns into postgres=#), set a password for “postgres”:

/password postgres

Enter the password postgres twice, which is the preferred one of Phoenix.

Finally, use the following command to quit the PostgreSQL shell.

/q

Setup the database user authentication method:

sudo vi /var/lib/pgsql/data/pg_hba.conf

Find the following section:

# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                ident

Modify the authentication method of IPv4 local connections to md5:

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

Save and quit:

:wq

Restart the postgresql service:

sudo systemctl restart postgresql.service

Step 7: Install inotify-tools

Use the following command to install a required component “inotify-tools”:

sudo yum install inotify-tools

Step 8: Create a Phoenix application

Assume that you want to create a Phoenix application in the directory ~/phoenix_project_1:

mix phoenix.new ~/phoenix_project_1

Answer Y during the process to fetch and install dependencies.

This command will create the application directory ~/phoenix_project_1 for you. Get into the directory and create a database:

cd ~/phoenix_project_1
mix ecto.create

Answer Y to install “rebar” during the first database creation.

Fire up your application with the following command:

mix phoenix.server

While keeping the current SSH connection alive, initiate another SSH connection and modify the firewall rules to grant access to your application:

sudo firewall-cmd --zone=public --permanent --add-port=4000/tcp
sudo firewall-cmd --reload

Finally, use a web browser to visit your application from:

http://[your-server-IP]:4000

That concludes our tutorial. Welcome to Phoenix!

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article

Leave a Reply

Your email address will not be published. Required fields are marked *