Deploy Python webApp on AWS (without using Elastic Beanstalk) - python

I'm hosting a website on AWS. Its a web interface with a SQL database. The website will be used to:
1. View results of query from Database
2. Insert data into database
3. View the data and update it where needed.
The codes and connections works file when I run the application on localhost (Apache on my C drive). But we want to host it on AWS so that people around me can use it.
So, In AWS I uploaded the code on EC2 and installed apache on it, all the html links are working but the python file is simply displaying the code.
I'm guessing it has something to do with the shebang. Currently my code has the following shebang:
#!C:\Python27\python.exe
Can someone guide me if its the shebang or if there is something else i need to do.
I have installed boto, but not sure what to do next. The AWS website and most of the forums talk about using Elastic Beanstalk. I want to host a fully functioning Python webApp on AWS without using Elastic Beanstalk.

When apache displays code, that is a clear sign that Apache is not configured properly to execute python. You should look to see if mod_python is installed and configured correctly.
Also, #! is generally used with Linux not windows. If apache/mod_python is installed and configured correctly I can't imagine what code you'd have that would need #! since the .py extension would be enough.
IF your EC2 instance is indeed running Linux, and your code does indeed need #! try:
#!/bin/python
OR
#!/usr/local/bin/python
(Depends on where the python binary is, and those are the most common locations.)
If your EC2 instance is running Windows then "Unless you are using cygwin, windows has no shebang support"

Hi have you logged into your EC2 instance through the endpoint and then run your script, from the command line. I have some experience with EC2 running apache2 only my application was written in Java, having previously used python scripts I was able to run them by logging into my EC2 instance, you can do this from AWS management console. hope this helps you somewhat.

Related

How to deploy a Python Dash Application on an internal company server?

I have written a Python Dash Application and it works completely fine on my local computer. Now, I want to be able to deploy this application on a server within the corporate network. I do NOT want to deploy this on Heroku etc because the datasource is an internal API. How do I go about deploying this application on the server? It's a Linux based machine.
I found this post that says use the code below but not quite sure where to add this piece of code.
waitress-serve --host=0.0.0.0 --port=8080 appname:app.server
The code you are referring to, waitress-serve, is a command-line wrapper bound to the function waitress.serve provided by Waitress.
You run it in your terminal or from a shell script.
Waitress is a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 3.7+.
You can install it with pip install waitress.
#see waitress-serve documentation here.

How to deploy python flask application on another windows 10?

I have develop python flask application(REST API). Now I want to deploy this application on client system(Windows 10 Professional ). My client dont have any internet service.
Previously, I done in java that time I make a .war file and deployed in tomcat on client system. He was able to access REST API.
Now I want know any similar way to deploy python app on client system, on start system his able to access my REST API
use PyInstaller.
pip install pyinstaller
go to project dir
cd C:\Users\sandip\Desktop\MyPython
use
pyinstaller --onefile HelloFlask.py
If you just want to make your rest APIs accessible by other users in same network, you can simply do it without installing anything on client side by replacing the app.run() in your code to app.run(host= '0.0.0.0'). By default flask app runs on localhost, by changing it to latter causes it to run on your machines IP address, thus making it accessible by all the users under same network. You can read more on flask's documentation under the heading Externally Visible Server.
To deploy your app in production, you need a WSGI server, you can read about deployment of flask app here

How can I run a simple python script hosted in the cloud on a specific schedule?

Say I have a file "main.py" and I just want it to run at 10 minute intervals, but not on my computer. The only external libraries the file uses are mysql.connector and pip requests.
Things I've tried:
PythonAnywhere - free tier is too limiting (need to connect to external DB)
AWS Lambda - Only supports up to Python 2.7, converted my code but still had issues
Google Cloud Platform + Heroku - can only find tutorials covering deploying applications, I think these could do what I'm looking for but I can't figure out how.
Thanks!
I'd start by taking a look at this question/answer that I asked previously on unix.stackexchange - I went with an AWS redhat installation and it was free to use.
Once you've decided on your VM, you can add SSH onto your server using any SSH client and upload your Python script. A personal preference is this application.
If you need to update the Python version on the server, you can do this by installing the required Python RPMs. A quick google should return the yum [or whichever RPM management system you're using] repository for the required RPMs.
Once you've installed the version of Python that you need, I'd suggest looking into the 'crontab' which can be used to schedule jobs. You can set a cronjob to run every 10minutes which will call your script.
See this site for more information on how to use the crontab
This sounds like a perfect use case for AWS Lambda which supports Python. You can invoke your Lambda on a schedule using Scheduled Events.
I see that you tried Lambda and it didn't work out for you which is too bad as that seems like the easiest route. You could also launch an EC2 instance and use userdata to schedule a cron when the instance starts.
Another option would be an Elastic Beanstalk worker with a cron.yml that defines your schedule. Elastic Beanstalk supports Python 3.4.
Update: AWS does now support Python 3.6. Just select Python 3.6 from the runtime environments when configuring.

What do you use for running scheduled tasks in Production for Python?

The thing is, I read this post stating best practices to set up a code to run at every specified interval over a period of time using the python library - APS Scheduler. Now, it obviously works perfectly fine if I do it on a test environment and run the application from the command prompt.
However, I come from a background where most my projects are university level and never ran in production but for this one, I would like to. I have access to AWS and can configure any kind of server on AWS and I am open to other options as well. It would be great if I could get a headstart on what to look if I have to run this application as a service from a server or a remote machine without having to constantly monitoring it and providing interrupts on command prompt.
I do not have any experience of running Python applications in production so any input would be appreciated. Also, I do not know how to execute this code in production (except for through aws cli) but that session expires once I close my CLI so that does not seem like the most appropriate way to do it so any help on that end would be appreciated too.
The Answer was very simple and does not make a lot of sense and might not be applicable to all.
Now, what I had was a python flask application so I configured the app in a virtual environment using eb-virt on the aws server and then created an executable wsgi script which I then ran as a service using mod_wsgi plugin for the apache http server and then I was able to run my app.

Execute python script in localhost

I have downloaded and installed python on my PC. I am learning python at this moment so I am a beginner.
Is there anyway I could execute python scripts in localhost (Apache) and view them in a web browser as we do with PHP? I am executing the python scripts from CLI at this moment.
Although there are multiple solutions, I would take a look at mod_python. Unfortunately, in my experience it is not the easiest thing in the world to set up and requires making changes to httpd.conf, but this tutorial is helpful (and gives some examples).
Google App Engine SDK for Python includes a local web server application that simulates the App Engine environment. It allows for instant server script changes by just saving the file and refreshing the browser.
The development tutorial is here
Once you have your web site working locally, it is easy to deploy it live on Google's App Engine servers.

Categories