4 min read 0 comments

I have previously deployed an app using Heroku, which I think was really easy for a newbie like me. Recently, I tried using AWS to deploy an app and found Lightsail (there is a $5-fee and a month-free trial). Using the instructions I found from the book “Flask by Example” by Gareth Dwyer, I was able to deploy an app using AWS.

From looking at available documentation on deploying Flask apps using AWS, it looks like Lightsail is new and much easier than how others have done it in the past, which involves using, in one instance, a virtual private cloud (VPC) and then EC2. This looked like a lot of steps. But I chanced upon Lightsail and I didn’t have to go through many steps as that example I have seen. Here are the steps I used to deploy using Lightsail on AWS.

  • Created an AWS account (which I have done a long time ago, but have not used my account for anything until now).

  • Went to Lightsail site from the AWS Services site.

  • Clicked on the “Create instance” button on the Amazon Lightsail site.

  • Chose “OS only”.

  • Chose “Ubuntu” as server.

  • Clicked on “Create” button found below the page, letting the instance take the default name. It will take a while for the instance to become active.

After this, SSH connection to virtual private server is as easy as clicking on the orange square on the top right of the instance box (encircled green below).

A console will appear so commands can be ran in the server. After these, I followed the instruction in the “Flask by Example” book, making sure I used the right name for the folders containing my app.

These are the steps I took (after the “Creating our development environment” topic of the book). On the vps terminal:

  1. I ran the following commands.

    sudo apt-get update

    sudo apt-get install python-pip (probably not needed)

    sudo apt-get install python-flask (instead of pip install –user flask)

    sudo apt-get install apache2

    sudo apt-get install libapache2-mod-wsgi

    After running all these, I went to my public IP on a browser and got the default page saying that the server works (“Apache2 Ubuntu Default Page”).

  2. I also went ahead and installed any Python package I needed in my web app using:

    sudo apt-get install python-package

    command, with package to be replaced by whatever python package I needed. I decided to use this command instead of using pip because when I tried deploying for the first time, I got errors saying that the needed modules were not found. This stumped me so I decided to use sudo apt-get install instead and it worked for me.

  3. Putting the code for the app on Github makes it easier to copy it to the VPS. But first, git needed to be installed by running the following:

    sudo apt-get update

    sudo apt-get install git

  4. I then cloned the web app from Github:

    cd /var/www git clone <git-url>

    (If this did not work, I changed the ownership of /var/www using sudo chown -R user /var/www with ‘user’ being my username.)

  5. Changing the directory to the folder of the cloned app, I checked for its content, making sure I have all the right files and folders. The name of the folder I cloned for this case was headlines, with the name of the .py file as headlines.py.

  6. I created a .wsgi file which serves the Flask app. I did this using the Nano text editor. I proceeded to run the following:

    nano headlines.wsgi

    Here, I typed the following:

    Ctrl-X and enter Y to exit and save this file on /var/www/headlines folder.

    After this, /var/www/headlines has headines.py and headlines.wsgi aside from any other files (such as the templates folder and its corresponding files).

  7. Next was the creation of an Apache configuration file.

    cd /etc/apache2/sites-available nano headlines.conf

    (If there were any permission issues, I just ran sudo chown -R user /etc/apache2 and this fixed the issue.)

    In the headlines.conf file, I wrote:

    I found that making sure this file is correctly written the first time was critical. When I was trying to deploy for the first time, I typed this instead of copy and pasting and typed with mistakes. This caused my server to give errors, which I cannot fix by editing the files I wrote. I was only successful when I deleted the instance, created a new one and started fresh.

    I saved this file by hitting Ctrl X and Y.

  8. At this point, I needed to change the default page with the app. This was done by running the commands:

    sudo a2dissite 000-default.conf

    sudo a2ensite headlines.conf

    sudo service apache2 reload

    Afterwards, I went to the site (using the public IP) and checked whether the app has been deployed.

    If there were errors, I used the command, as per the book:

    sudo tail -f /var/log/apach2/error.log

    However, I wasn’t successful fixing errors by editing any files I created within the VPS. I was able to fix anything by just starting fresh (deleting the instance in Lightsail, and starting all over again).