Deploying Flask App on AWS ec2 instance with Mysql without using elastic bean stalk

 

Prerequisites:

Your flask app works on your local machine and the code is hosted(Github).

Steps

1.

Launch a Ec2 instance and log in using your key-pair or ssh keys. Make sure you store the key.pem file when you create the instance.

$ ssh -i key.pem ubuntu@52.43.196.102  # (Your public IP)

If you get an error like:

Permissions 0644 for ‘Downloads/ishaan-key.pem’ are too open.

It is required that your private key files are NOT accessible by others.

This private key will be ignored.

Then change your permission on your key.pem file

$ chmod 700 key.pem

 

2. Before you can install python specific packages if you are using MYsql yo need to install mysql.(Ubuntu 16.04)

  • $ sudo apt-get update
  • $ sudo apt-get install mysql-server
  • $ sudo mysql_secure_installation

3.

Once you are logged in clone your code repository from Github.

$ git clone https://github.com/username/repo_name.git

Now once you have the code install pip (For ubuntu)

sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get install python-pip

Now that you have installed pip use your requirements.txt file to install all the python packages your flask app needs.

$ pip install -r requirements.txt

If you do have a requirements.txt you can create one from your local machine

$ pip freeze > requirements.txt

Install all the packges required by your flask app using

$ pip install -r requirements.txt

(If you get an error

EnvironmentError: mysql_config not found

Then run this command

sudo apt-get install libmysqlclient-dev

 

4.

Install gunicorn if you were not using it on your local machine

$ pip install gunicorn

We’ll need a web server installed on our instance, because we’re going to forward requests from port 80 to our Flask app running internally. It is best practice to have a web server handle our port 80 requests, because the Flask application server (Gunicorn) we are using is designed to serve Python code, but not ideal for handling port 80 requests from the Internet.

  • sudo apt-get update
  • sudo apt-get install nginx

Nginx Configaration:

$ sudo rm /etc/nginx/sites-enabled/default

$ sudo nano /etc/nginx/sites-available/flask_app

Paste in the below: Here We are passing on to port 8000 as gunicorn by default binds to 8000.

server {
    listen       80;
    server_name  your_public_dnsname_here;

    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

 

Now we can enable the site (by creating a symlink), test our configuration and restart to allow our changes to take place.

  
$ sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled/
$ sudo nginx -t
$ sudo systemctl restart nginx 
  

And finally, let’s start our Gunicorn process to serve our Flask app:

$ gunicorn app:app 

This should be it. You can create a gunicorn service so that it starts up when the server boots.

Advertisements

2 thoughts on “Deploying Flask App on AWS ec2 instance with Mysql without using elastic bean stalk

  1. Thanks so much! worked like a charm. I dont want to setup EBS just yet and was looking exactly for this sequence.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s