Understanding each component of a web application architecture - python

Here is a scenario for a system where I am trying to understand what is what:
I'm Joe, a novice programmer and I'm broke. I've got a Flask app and one physical machine. Since I'm broke, I cannot afford another machine for each piece of my system, thus the web server, application and database all live on my one machine.
I've never deployed an app before, but I know that a server can refer to a machine or software. From here on, lets call the physical machine the Rack. I've loaded an instance of MongoDB on my machine and I know that is the Database Server. In order to handle API requests, I need something on the rack that will handle HTTP/S requests, so I install and run an instance of NGINX on it and I know that this is the Web Server. However, my web server doesnt know how to run the app, so I do some research and learn about WSGI and come to find out I need another component. So I install and run an instance of Gunicorn and I know that this is the WSGI Server.
At this point I have a rack that is home to a web server to handle API calls (really just acts as a reverse proxy and pushes requests to the WSGI server), a WSGI server that serves up dynamic content from my app and a database server that stores client information used by the app.
I think I've got my head on straight, then my friend asks "Where is your Application Server?"
Is there an application server is this configuration? Do I need one?

Any basic server architecture has three layers. On one end is the web server, which fulfills requests from clients. The other end is the database server, where the data resides.
In between these two is the application server. It consists of the business logic required to interact with the web server to receive the request, and then with the database server to perform operations.
In your configuration, the WSGI serve/Flask app is the application server.
Most application servers can double up as web servers.

Related

Dash/Flask - Deployment of an application on my own server

I am a newbie in web development, I am an energy engineering student trying to make a project, so I apologize if I say something weird.
I've made an application using Dash (python). And now, I would like to deploy that app in my server. I have a remote server (debian, adress.com, IP, opened ports: 80 and 443...) with my ssh public key and all the required stuff.
I have read all the official documentation in the plotly dash page, and also in flask's, but I dont understand very well the thing. I need either Heroku, OpenShift... And I dont understand very well how them work.
Could you please recommend me an easy tutorial (for dummies!) or give me advices/examples about the deployment? I would really apreciate I've searched the Internet a lot, but I can't figure it out.
You describe two options:
1. using your own server to host your app and
2. using a service which will host your app (heroku, etc.)
Using your own server to host your app
You need to decide for a web server, which is serving your page on your server. A host will not magically answer on any port. There is for example apache, nginx, etc... After having chosen one, you need to find a tutorial: how to run your flask app on [yourwebserver] or something like that.
Suppose you have chosen apache, you will find something like this:
https://jackhalpinblog.wordpress.com/2016/08/27/getting-your-python-3-flask-app-to-run-on-apache/
(In this case, you will have to figure out how to run your flask app with python3 instead of python2, if you are running debian)
When your page serves your page, you need an ssl certificate in order to make use of your domain. An easy way of doing this is https://letsencrypt.org/getting-started/ (there are probably other similar services)
Using a service, which will host your app for you (heroku, etc.)
Alternatively do not host you app on your own server, but on heroku, aws, gcd, etc., in my opinion this is much easier than hosting it on your own server. The documentation on hosting service websites is normally very good.
For heroku a good starting point would be here:
https://devcenter.heroku.com/articles/getting-started-with-python. Nevertheless the other services are easy to use as well, i just use this as an example.

Set keep-alive timeout for Flask server with parameter [duplicate]

Setting up Flask with uWSGI and Nginx can be difficult. I tried following this DigitalOcean tutorial and still had trouble. Even with buildout scripts it takes time, and I need to write instructions to follow next time.
If I don't expect a lot of traffic, or the app is private, does it make sense to run it without uWSGI? Flask can listen to a port. Can Nginx just forward requests?
Does it make sense to not use Nginx either, just running bare Flask app on a port?
When you "run Flask" you are actually running Werkzeug's development WSGI server, and passing your Flask app as the WSGI callable.
The development server is not intended for use in production. It is not designed to be particularly efficient, stable, or secure. It does not support all the possible features of a HTTP server.
Replace the Werkzeug dev server with a production-ready WSGI server such as Gunicorn or uWSGI when moving to production, no matter where the app will be available.
The answer is similar for "should I use a web server". WSGI servers happen to have HTTP servers but they will not be as good as a dedicated production HTTP server (Nginx, Apache, etc.).
Flask documents how to deploy in various ways. Many hosting providers also have documentation about deploying Python or Flask.
First create the app:
import flask
app = flask.Flask(__name__)
Then set up the routes, and then when you want to start the app:
import gevent.pywsgi
app_server = gevent.pywsgi.WSGIServer((host, port), app)
app_server.serve_forever()
Call this script to run the application rather than having to tell gunicorn or uWSGI to run it.
I wanted the utility of Flask to build a web application, but had trouble composing it with other elements. I eventually found that gevent.pywsgi.WSGIServer was what I needed. After the call to app_server.serve_forever(), call app_server.stop() when to exit the application.
In my deployment, my application is listening on localhost:port using Flask and gevent, and then I have Nginx reverse-proxying HTTPS requests to it.
You definitely need something like a production WSGI server such as Gunicorn, because the development server of Flask is meant for ease of development without much configuration for fine-tuning and optimization.
Eg. Gunicorn has a variety of configurations depending on the use case you are trying to solve. But the development flask server does not have these capabilities. In addition, these development servers show their limitations as soon as you try to scale and handle more requests.
With respect to needing a reverse proxy server such as Nginx is concerned it depends on your use case.
If you are deploying your application behind the latest load balancer in AWS such as an application load balancer(NOT classic load balancer), that itself will suffice for most use cases. No need to take effort into setting up NGINX if you have that option.
The purpose of a reverse proxy is to handle slow clients, meaning clients which take time to send the request. These reverse load balancers buffer the requests till the entire request is got from the clients and send them async to Gunicorn. This improves the performance of your application considerably.

How does server-side rendering work with a non-Node.js backend on Heroku?

I have been developing a Python app that serves a React frontend with server-side rendering.
Locally, this has worked fine as I'm able to run two servers on separate ports to handle different parts of my application. My Python backend receives the initial request and then sends an http request to my Node.js server which does my server-side rendering. The result is then sent back to my Python backend which injects the server-rendered frontend into the HTML which is sent to the client.
However, Heroku limits applications to a single, dynamically generated port. This limits me to only running one web server which means I'm no longer able to run my Node.js server to do my server-side rendering. I have thought of some gimmicky ways to make this work, but I don't want to have to create an entirely new app on Heroku just to run the Node.js server I need.
I'm not sure how I can make this work with these limitations in place so I'm hoping I can learn some alternative ways to make this work on Heroku. What are some viable workarounds to handle this problem?
I think you need to create to separate apps on Heroku(even though you don't want to), as far as I know there's no other available options on Heroku.
I use Heroku for a SSR application running on two apps. One for frontend(react) and one for backend(nodejs). Works like a charm

gunicorn server in Google App Engine Standard Environment

I'm developing a Flask app with a microservices architecture in Google App Engine's Standard Environment. This app will need to sustain bursts of intense traffic, so it seems perfect for the Standard Environment. My question is this:
In other environments I've used (Heroku, App Engine Flexible), you must configure your Flask app to use a production-quality WSGI web server like gunicorn, as the built-in Flask server is only suitable for development. There is documentation on this for the Flexible Environment, but not for Standard. Am I correct in assuming that this is because the Standard Environment (as configured in app.yaml) manages the request handling and everything else that gunicorn would in a production-ready way? Could it really be that easy?
Yes, it really is that easy. As a PaaS, GAE takes care of all that for you. As it did for SnapChat when it grew to 150 million users (yes, hosted on GAE).
Watch "App Engine Architecture and Services" and "You Can Run That On App Engine?". These describe some of this. Incoming requests are first captured in a queue on a GAE front-end servers. From there it decides what to do with it. If an instance of your app isn't running, it starts one and then passes the request to it. If an instance is running and not too busy, it passes the request to it immediately. If all running instances are busy, it will hold the request until an instance is able to take on another request. If the request sits in the front-end queue for too long (based on parameters you can set), GAE will start up more instances to handle the backlog.
With GAE, your app now runs in an environment with load balancers, front-end request queuing servers, edge servers, auto-scaling app servers, private global fibre networks, etc, etc. This means all the "production quality" worries that would lead you to gunicorn, etc on a self-designed server are more than handled by Google's smart engineers in GAE.
Since you're looking a microservices, you might also want to read Microservices Architecture on Google App Engine, in GAE docs.

how to write a client/server app in heroku

I am quite new to heroku and I reached a bump in my dev...
I am trying to write a server/client kind of application...on the server side I will have a DB(I installed postgresql for python) and I was hoping I could reach the server, for now, via a python client(for test purposes) and send data/queries and perform basic tasks on the DB.
I am using python with Heroku, I manage to install the DB and it seems to be working(i.e i can query, insert, delete, etc...)
now all i want is to write a server(in python) that would be my app and would listen on a port and receive messages and then perform whatever tasks it is asked to do...I tought about using sockets for this and have managed to write a basic server/client locally...however when I deploy the app on heroku i cannot connect to the server and my code is basically worthless
can somebody plz advise on the basic framework for this sort of requirements...surely I am not the first guy to want to write a client/server app...if you could point to a tutorial/doc i would be much obliged.
Thx
Heroku is for developing Web (HTTP, HTTPS) applications. You can't deploy code that uses socket to Heroku.
If you want to run your app on Heroku, the easier way is to use a web framework (Flask, CherryPy, Django...). They usually also come with useful libraries and abstractions for you to talk to your database.

Categories