How to run a Django project on Ubuntu in AWS EC2? - python

I have created a Django application and uploaded in to AWS EC2. I can access the site using public IP address only when I run the python manage.py in AWS command line.
If I close the Putty window, I am not able to access the site. How can I make sure that the site is available always even if I close the command line / putty?
I tried WSGI option but its not working at all. Appreciate your help to give us a solution to run the Python application in AWS.

It happens because you are running the app from within the SSH session, which means that ending the session (SIGHUP) will kill your application.
There are several ways to keep the app running after you disconnect the SSH, the simplest would be to run it inside a screen session and keeping this instance running while disconnecting from SSH, the advantage of this method is that you can still control the app when you are reconnecting to this machine and control the state of the app and also potentially see the logs.
Although it might be pretty cool it's considered a patch, the more stable and solid way would be to create a service that will run the app and will allow you to start, stop and look at logs using the nifty wrappers of systemd.
Keep the process running with screen:
First you'll have to make sure screen is installed (apt-get or yum) whatever suits your desired distro.
Run screen.
Run the app just like you did outside screen.
Detach from the screen session by pressing Ctrl+A and then d.
Disconnect from the SSH and see how the service is still running.
Creating a systemd service is a bit more complicated so try and read through the following manual.

Related

How to Host a Flask website from localhost 5000

Hello I coded this website that generates math problems (Here is the code: Here)
It is coded on flask and it is locally being hosted on this link that is not accessible to other people http://127.0.0.1:5000/ .I have a google domain and I want to have a website. What things / services do I need to use. I have been wait to see if I need to use AWS but I think I might need to. I have tried things like transferring it off of flask but I can't. If this is a repost sorry please post there answer thanks -Ben
I am assuming what you're asking is to host your flask web site so others can view it. The address you mention in your post is the local host address for your computer and is only accessible from your own computer. If you only want someone on your same network (WiFi) to access it, you would need to replace "127.0.0.1" with the IP address of your computer. You would also likely have to open up a firewall on your computer to allow the port 5000.
However, if you want anyone on the internet to access your site, there are a ton of ways to do this but since you mentioned AWS, you can do this easily by running a small EC2 instance (virtual server). If you have a new AWS account and have not already run any EC2 in that account, you can actually run a small EC2 instance for free for a whole year. Great for small projects. If you're just getting started with EC2, you may want to go here https://aws.amazon.com/ec2/getting-started/
Basic steps:
Spin up an EC2 instance. Choose the default Amazon Linxu 2 OS type, make sure to create/assign a key pair so you can later ssh into it, make sure the Allow SSH from anywhere setting is checked/selected and the Allow HTTP checkbox is checked (not HTTPS).
Wait for the instance to launch.
Log into your instance by clicking on your ec2 instance in the list of ec2 instnaces and click the Connect button, click the Connect button again (Instance connect tab). If that doesn't work, follow the steps on the SSH client tab.
Install flask
pip3 install flask
Clone your git repo
git clone https://github.com/some0ne14/Math-Ibex.git
Change to your repos' folder
cd Math-Ibex/Math-Practice-Website-master
Edit your main.py so that the app.run line looks like the following (you can do this on GitHub before you run git clone actually or use the nano command to edit the file easily). This allows the system to run on the standard web port 80.
app.run(host='0.0.0.0', port=80, debug=True)
Run the following to start the application. If you want to run it as a service so you can walk away or close the terminal and it will still stay running, just search on here how to run flask as a service.
python3 main.py
You can now connect to your server with any web browser using your EC2 instance's public IP address or generated AWS DNS name (available on the EC2 instnace property page).
Make sure to stop your instance when not using it to save those free runtime minutes.

Heroku - Python integration

I have a script that I run locally on my laptop, which polls a server and if certain criteria are met, it sends an email. I guess like a heartbeat app of sorts.
The server is a Raspberry Pi, it has a public IP address which is all working fine.
Id like to host the python script on Heroku, so that it can run without my laptop having to be on the local network or always running.
Does anyone have experience of Heroku to show me how I can have the script hosted and running constantly?
My other query is that free tiers of Heroku go to sleep after 30 mins, so would essentially stop the script until getting an http request and spin up the instance once again.
Trying to find some form of elegant solution.
Many thanks for any advice you can give,
All the best,
Simon
If you have ssh access, use screen to keep your script running.
Install screen :
sudo apt install screen
Open a new session:
screen -S session_name
Now, you can run whatever you want, then detach the session by pressing Ctrl+A then D
To go back to the session at any time, first list the active sessions:
screen -ls
Then resume the session using:
screen -r session_id_name
Different approachs would be:
1- Cloud Functions
Create lambda function in a cloud provider with your python code (free tier elegible)
Trigger that function every once in a while
2- Get VM on Cloud
Go to AWS, GCP, etc
Get a free tier VM
Run server from there

How do I set a program to run on startup and how desktop application's login check done

I have a small application built using python and tkinter, and I want that application to run on startup.
Here I have few questions
My question is how do I actually set the app to run at startup.
My application needs to login, but I don't understand how I can connect, application with database without placing database credentials in user's downloaded application.
There are 2 points here:
how to set-up an application to run at start-up?
If what you call startup if the machine startup before any user logs in, then the answer will depend on the OS. You will have rc scripts on Unix (and Unix-like) systems, or services on Windows.
how can an unattented application connect to the database?
A common way is to have the application run under a dedicated login (user name). Then you configure the database to accept passwordless connections from that specific login on that specific machine (ideally the same one). Of course that login should have the less possible privileges...
What platform do you want to do?
On Windows you can set your built (maybe .exe) in Task Scheduler.
press Start logo and Call Task Scheduler
Open it and Create task
and then setup what app do you want to run in Action tab and don't forget to Naming in General tab
On Linux I used to see somepeople follow this step you can follow from this link below :
How to Schedule Tasks on Linux: An Introduction to Crontab Files
Or you can search for Crontab to see how to setup task with Crontab commands

How can I deploy a flask app on Lightsail with crash protection?

I am looking for help deploying my flash app. I've already written the app and it works well. I'm currently using the following command in the directory of my flask code:
sudo uwsgi --socket 0.0.0.0:70 --protocol=http -w AppName:app --buffer-size=32768
This is on my Amazon Lightsail instance. I have the instance linked to a static public IP, and if I navigate to the website, it works great. However, to get the command to continuously run in the background even after logging out of the Lightsail, I first start a screen command, execute the above line of code, and then detach the screen using ctrl-a-d.
The problem is, if the app crashes (which is understandable since it is very large and under development), or if the command is left running for too long, the process is killed, and it is no longer being served.
I am looking for a better method of deploying a flask app on Amazon Lightsail so that it will redeploy the app in the event of a crash without any interaction from myself.
Generally you would write your own unit file for systemd to keep your application running, auto restart when it crashes and start when you boot your instances.
There are many tutorials out there showing how to write such a unit file. Some examples:
Systemd: Service File Examples
Creating a Linux service with systemd
How to write startup script for Systemd?
You can use pm2
Starting an application with PM2 is straightforward. It will auto
discover the interpreter to run your application depending on the
script extension. This can be configurable via the Ecosystem config
file, as I will show you later on this article.
All you need to install pm2 and then
pm2 start appy.py
Great, this application will now run forever, meaning that if the process exit or throw an exception it will get automatically restarted. If you exit the console and connect again you will still be able to check the application state.
To list application managed by PM2 run:
pm2 ls
You can also check logs
pm2 logs
Keeping Processes Alive at Server Reboot
If you want to keep your application online across unexpected (or expected) server restart, you will want to setup init script to tell your system to boot PM2 and your applications.
It’s really simple with PM2, just run this command (without sudo):
pm2 startup
Pm2 Manage-Python-Processes

Python Server in AWS

I have written some Web services in Python.I want to deploy it in AWS, I have created the instance.
I tried to run using putty and it was coming up well using the command python Flo.py, which starts the server 0.0.0.0:8080. But the problem is when I close the putty window the server is terminating. How i can start a server in 8080 just like httpd?
All helps are invited
I highly recommend you use screen (or tmux). And you may want to use upstart as well.
Screen:
Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).
tmux and screen are doing the same thing - which is terminal multiplexing. This will give you a terminal you can attach to and disconnect from to keep it running when you're not on the server.
To test it simply install using:
sudo apt-get install screen
Now use the following to open a screen terminal under the name my_screen, running your script as it starts:
screen -dmS my_screen python Flo.py
And attach to it using:
screen -r my_screen
Detach using ctrl+A followed by ctrl+D, and now you can leave the server (screen will keep running with the process in it)
Read more here.
Upstart:
Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.
Upstart is the new way to start services on debian as soon as the system starts.
To add an upstart service you need to add a configuration file under /etc/init (open one of the files there and see an example).
These files can be extremely simple so don't be intimidated by what you see there.
You can make a service to run your server / service and send output to a log file which you can then use to keep track of what's happening.
Read more here.

Categories