Giter Site home page Giter Site logo

ac3.2-pythonwebserver's Introduction

A Web Server in Python

  • Hosting: AWS EC2
  • Webserver: Apache
  • Language: Python
  • Framework: Flask

EC2 - Elastic Cloud Computing

Creating

  1. Create an EC2 instance
  2. New security group
  3. New keypair

Connecting

Locally (on your laptop)

After you download the key (pem file) from AWS, save it to the .ssh directory in your home directory.

  mv got.pem ~/.ssh

Change the permissions on that file in order for ssh to work.

  chmod 400 got.pem

Access the remote computer. ssh = Secure shell.

ssh -i ~/.ssh/got.pem [email protected]

This video is pretty good: https://www.youtube.com/watch?v=M2Wc8JIS-p8 However: Iโ€™d recommend keeping all your keys in ~/.ssh/

Installations

Environment

$ sudo yum update

Apache (Web Server)

$ sudo yum install -y httpd24 

Python/Apache interoperability

$ sudo yum install mod24_wsgi-python27.x86_64

Flask framework

$ sudo pip install flask

Operation

Run the web server

$ sudo service httpd restart

Flask

Reference

Flask in AWS (one way):

Some differences in our environment using AWS's default server. For one thing, symbolically linking into the ec2-user directory required setting permissions on that user's directory that prevented subsequent ssh sessions. Therefore, do not run sudo ln -sT ~/flaskapp /var/www/html/flaskapp or change the permissions on the home directory.

# this is the root directory of the server
$ cd /var/www/html

# create our directory as root
$ sudo mkdir flaskapp

# root can make us the owner again
$ sudo chown ec2-user flaskapp/

$ cd flaskapp

$ echo "Hello World" > index.html

Test access to flaskapp directory

At this point the url http:///flaskapp/ should display "Hello World". The following steps set up the flask framework to handle requests and routes.

Configure Apache to see Flask

Edit /etc/httpd/conf.modules.d/10-wsgi.conf (or /etc/httpd/conf/httpd.conf if the former doesn't exist):

$ sudo nano /etc/httpd/conf.modules.d/10-wsgi.conf

Adding these lines:

WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

<Directory flaskapp>
    WSGIProcessGroup flaskapp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

Restart Apache

$ sudo service httpd restart

Trivial Flask App

Save this configuration to a file named flaskapp.wsgi. You can use cat and redirection > to do this:

cat > flaskapp.wsgi

This will put the cursor on the next line. Paste in the content you want, make sure the cursor is on the beginning of a line and type CTRL-D. You will have a file named flaskapp.wsgi with the contents you typed.

import sys
sys.path.insert(0, '/var/www/html/flaskapp')

from flaskapp import app as application

Do the same thing for the python/Flask script.

cat > flaskapp.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
  return 'Hello from Flask!'
  
if __name__ == '__main__':
  app.run()

Database / Python

Installation

$ sudo yum install python27-pip
$ sudo yum install python27-devel
$ sudo yum install mysql27-devel
$ sudo yum install MySQL-python27

Flask Code with Database

from flask import Flask
from flask import json
from flask import jsonify

import MySQLdb

app = Flask(__name__)

@app.route('/')
def hello_world():
  return 'Hello from Flask!'

@app.route('/api/characters')
def characters():
  db = dbConnection()

  ret = ''
  # you must create a Cursor object. It will let
  #  you execute all the queries you need
  cur = db.cursor()

  # Use all the SQL you like
  cur.execute("""select c.name, h.name as house
  from houses as h, characters as c, characters_houses as ch
  where ch.house_id = h.id
  and ch.character_id = c.id""")

  # gather all the results in this array
  characters = []
  for row in cur.fetchall():
      name = row[0]
      house = row[1]

      # create an ad-hoc object
      characters.append({"name":name, "house":house})

  db.close()

  return jsonify(characters)

def dbConnection():
  conn = MySQLdb.connect(host="atlas.cf626xxbuyrf.us-east-1.rds.amazonaws.com",    # your host, usually localhost
                     user="gotuser",         # your username
                     passwd="winteriscoming",  # your password
                     db="got")        # name of the database
  return conn

if __name__ == '__main__':
  app.run()

Monitoring

# hits
$ sudo tail -f /var/log/httpd/access_log

# errors
$ sudo tail -f /var/log/httpd/error_log

Troubleshooting

  • Check the error log for hints about errors
  • Check the access log if you suspect the request isn't even being processed
  • Be sure you have firewall access (i.e. port 80 is open in Security Groups)
  • If new code seems not to be running or if all else fails try restarting apache sudo service httpd restart

Other References

  1. Helpful but in PHP http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.