So, probably a dumb question, but I am beginning to learn all this so your feedback will be valuable for me.
The question is: In flask documentation it says start the flask server by entering the command 'python hello.py' and I do it successfully to see the output on localhost:5000. Now, I have a shared hosting plan and if I upload this file over there will i need to initiate the server over there as well like this? If so, when I close the terminal over there, will the flask server shut down (because when I close the terminal on my computer it shuts down the flask server and the results are no more available on localhost:5000)?.. It basically suggests me that I have to keep running the terminal all the time..please tell me what is the basic idea here? Thanks.
What you're asking is how you deploy your app. There are many options, that will depend on your needs, your hosting service, etc.
You should check the flask docs for the options. http://flask.pocoo.org/docs/deploying/
In essence, you'll have your flask app running as a local service on the server, so it's not shut down when you close the terminal, and an HTTP server that somehow proxies requests to that service. I guess the most popular is uWSGI with nginx.
When you upload your code to a remote host, you will need to provide a way to start the server and get things running. How this works is host- and software-dependent. As an example, here is some documentation for how you would fire Flask up on Heroku.
Related
I have an ionos server, and I am trying to run a python flask server on it. I connected to it via a Linux Terminal with ssh, however doing python3 main.py runs it locally:
I am new to this, am I doing something wrong? I logged into the ssh with the username and password IONOS gave me in the Web Hosting Essential Page.
I connected to it with SFTP and added this python code in a file named main.py:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "Hello, world"
app.run()
This runs it locally, but I can't figure out how to run it on my site. I think I am doing this completely wrong. I tried following the IONOS tutorials, but they don't work.
You try to access it by using a web browser in your computer:
http://your_server_ip:5000
If it can be accessed, everything is good. You can follow this tutorial to deploy it (you can choose a different version same with your version's sever near the top of the tutorial)
If it cannot be accessed, maybe your server IP is a shared IP. Ask IONOS, or use other suppliers such as digitalocean
Flask does require a server to run (apache 2 or Nginx) if you run just app.py you will launch flask server locally which is meant to be used as a way to quickly modify your server files without taking down ur web server. I highly recommend Apache2 as it’s fairly simple to use and very reliable.
I have been learning Flask by making a little website and using the built in flask server that runs with python. I have a page where you press a button, and it flashes a message using the flash system inside of flask. These flashes work fine when I am using the built in flask server on my windows machine. However, I have deployed the website to a Linux server, using uWSGI which goes through Nginx. My issue is that when I access this server, the flashes don't work. Most things like loading pages work fine on both servers, but flashing is broken. I don't see any error messages from uWSGI's logs.
The code I am using for the flash is implemented as follows:
flash("Made new post.")
return redirect(url_for("posts"))
The redirect takes me to the correct page, and if I run a print() statement before the redirect the statements are clearly being reached, the flash just doesn't do anything.
The main other issue I am running into is with sessions and trying to store session variables. Nothing happens when I try to do this either. (but it works on my personal machine)
Any ideas why this might be, or at least a way to get an error message from uWSGI?
To properly set cookies (cookies are what make message Flashing work), both nginx and the Flask application need to agree on the server name.
So make sure your server_name in nginx.conf matches SERVER_NAME (or
SESSION_COOKIE_DOMAIN, if set) in your flask configuration.
There are also limits enforced by nginx on the size of cookies, but this should only be a problem if your flashed messages are really large.
I'm learning AWS and I'm currently trying to deploy a Flask API over HTTPS. I set up an EC2 instance running Apache. I've already set up SSL on the site using ELB, and I tried to deploy flask over HTTPS with the following:
if __name__ == '__main__':
context = ("server.crt", "server.key")
app.run(host="0.0.0.0",port=5000,debug=True,ssl_context=context)
However, I don't think the site is even starting the Flask server properply, as even though everything loads and I receive a message that Flask is running on https://0.0.0.0:5000/, sending a simple GET request over the browser doesn't work as the request just never loads and it eventually times out. It's almost behaving as if there is no server running on port 5000.
On the other hand, when I ran this program over http instead of https, it worked perfectly fine. Can anyone help me out in terms of what I should do? Thank you.
Based on the comments.
The solution to the issue was termination HTTPS connections on the ELB. This way, the communication between ELB and EC2 instance can be conducted using HTTP which is simple and easier to manage on the instances.
I'm following Flask Quickstart guide and can run my web app via http://myip.com:5000.
One issue is that my web is only accessible as long as I keep my SSH remote connection session - when I sleep/shutdown my PC, the website shutdown too.
How can I make it permanent available?
You need to use a regular web server, such as apache2. You can't use the python server for production purposes. Here is how you do it with apache: http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/
I was tasked with making some changes to a Django application. I've never worked with Django and I am having trouble figuring out how to get my changes to compile and be available online.
What I know so far is that the application is currently available online. netstat tells me that httpd is listening on port 80. My change was made in the myapp/views.py file.
I tried to restart httpd using services httpd restart but my changes did not take effect. I've been looking into the issue a bit an I believe that I need to run a command along the lines of:
I tried calling python manage.py runserver MY.IP.AD.DR:8000 and I get:
python manage.py runserver 129.64.101.14:8000
Validating models...
0 errors found
Django version 1.4.1, using settings 'cutsheets.settings'
Development server is running at http://MY.IP.AD.DR:8000/
Quit the server with CONTROL-C.
Nice that no errors are found but when I navigate to http://MY.IP.AD.DR:8000/ I just get a "Unable to connect" message from my browser. I tried with port 81 too and had the same problem.
Without knowing exactly how your application is set up, I can't really say exactly how to solve this problem.
I can tell you that it's quite common to use two web servers with Django - one handles the static content, and reverse proxies everything else to a different port where the Django app is listening. Restarting the normal HTTP daemon therefore wouldn't affect the Django app, so you need to restart the one handling the Django app. Until you restart it, the prior version of the code will be running.
I generally use Nginx as my static server and Gunicorn with the Django app, with Supervisor used to run Gunicorn, and this is a common setup. I recommend you take a look at the config for the main web server to see if it forwards anything to another port. If so, you need to see what server is running on that port and restart it.
Also, is there a Fabric configuration (fabfile.py)? A lot of people use Fabric to automate Django deployments, and if there is one then there may be a command already defined for deploying.