Quick VPS Setup for Phoenix Web Framework Beginners

configure-phoenix
configure-elixir
vps
#1

Ok, so I am so excited to share with you the most interesting setup I have made for Elixir/Phoenix today. Why? Because if you trust me, it will save you a lot of time.

In my humble opinion, if you want to do a fast local network/computer setup at home, then the quickest way is to do so with VirtualBox. Download the Ubuntu server 18.04 LTS and following the guides on elixir-lang should get you going But here is the problem, the official Ubuntu repositories are outdated to Elixir version 1.1 I think, so you have to:

  1. wget version elixir 1.8 from the provided links.
  2. compile with “make clean test”.
  3. put elixir folder (that now includes bin folder) into home path.
  4. define environment variable to point to /home/username/elixir/bin/

As you can see this setup is a bit exhausting but it’s the best shot you’ve got. BUT if you intend to work on a VPS, then let me tell you the fastest setup I was able to build is surprisingly on Arch Linux.

First of all, I love Arch Linux. It is highly performant, has a great package manager pacman with enormous set of packages and the community provided beautiful documentation on their official wiki. However, their installation was always daunting and difficult for me. It is not productive for a desktop user.

Yet, if you intend to use Arch Linux ready made image on a VPS, the story is so different. You get to enjoy all the benefits of Arch with its minimalism and performance, without the cost of extensive initial configuration (for the setup).

So why I believe Arch Linux is the fastest setup for Phoenix webframework on a VPS? Simply because the default packages that come with ArchLinux ready for production are the following:

  1. Erlang OTP 21.
  2. Elixir version 1.8
  3. Postgresql-11

All you have to do is:

pacman -Syyu
pacman -S erlang elixir postgresql

And all your preferred versions will be readily installed and environment variables configured. In other words, elixir -v, or erl -v will work without any extra effort.

The only thing you will need to know is how to initialize the Postgresql database It is a simple line that specifies database location but for more advanced details or specific needs go to this Arch Wiki

sudo -iu postgres
initdb -D /var/lib/postgres/data

Then you can configure the postgres user:

sudo -iu postgres psql postgres
\password postgres

<it will ask you to change password for user postgres, just write postgres again>

I am assuming you are just setting up this initially for learning purposes. But of course you can change the password later if you wish. Phoenix configuration assumes postgres/postgres user/pass by default.

Then you can simply set postgresql to start automatically on boot:
sudo systemctl enable postgresql

Now the only thing remains, at least for a beginner like me is to configure a way to access the Postgresql database on the VPS from pgAdmin3 on my desktop computer. This can be easily done by the following, but remember all of this is for educational purposes not recommended for strong security practices.

After installing pgaAdmin3 on your desktop. Go to your VPS and edit the following configuration files for Postgresql:

sudo vi /var/lib/postgres/data/postgresql.conf

Uncomment the following that implies accepting connections from all IP addresses.
listen_addresses = '*'

Then add a line at the end of this configuration file to allow connection from your desktop IP host (your desktop IP instead of x):

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

host all all x.x.x.x/32 md5

My personal suggestion is to install uncomplicated firewall (ufw), it’s very easy to manage and will save a lot of time:

sudo pacman -S ufw
sudo ufw allow 4000
sudo ufw allow 5432
sudo ufw allow ssh
sudo ufw enable

For the record Arch Linux does not give you a sudo user by default. You have to do the following simple steps to get started:

useradd -m -s /bin/bash your-username

Then add your-username to the sudoers group easily by this command:
visudo

Add this line at the end of the file:
your-username ALL=(ALL) ALL

If you don’t know how to use vi do the following just after clicking visudo:

ESC             #makes sure you are in command mode
Shift+G        #Goes to the end of the file
o                  #goes to a new line
<paste the line above>
ESC             #command mode
:x!                #saves and exits file

So someone may wonder now, are there really VPS hosting services that host Arch Linux Well to be honest they are so few, but through Arch Wiki I found Linode to be the best fitting for me personally, but I think Vultr will be ok too since I was checking the only providers that give you the latest Arch Linux release out of the box.

Anyway, I was so happy with this setup and I hope this small guide will be of use to beginners who want to use a VPS and get up started quickly.

8 Likes

#2

Are you mostly interested in just playing around with deployment and devops type of stuff? If you just want to get something up and running quickly and as pain free as possible why not just use a service like Gigalixir.

2 Likes

#3

Thanks for sharing. I considered nanobox.io and didn’t know about gigalixir. Do you know details about what they offer behind the scenes? Like which version of elixir phoenix erlang postgresql?

0 Likes

#4

I have just looked at Gigalixir documentation, from the look of it, it will be useful if I need to do massively scale able apps. So it is simple for that purpose, I guess.

0 Likes

#5

For the record. Arch Linux footprint by default was 78 mb after installing all phoenix platform dependencies. In Ubuntu server that number will be higher specifically after postgresql deployment.

0 Likes

#6

Just a minor nitpick: You might want to change the CIDR to x.x.x.x/32, otherwise you’re allowing to everybody in your /24 network (i.e. if your IP is 1.2.3.4, everybody from 1.2.3.0 to 1.2.3.255 is allowed)

1 Like

#7

Thanks for the headsup. Moderator please open the edit, so I can change it.

0 Likes

#8

Done! :slightly_smiling_face:

1 Like