I have an ubuntu EC2 server and want to run a flask server. I want to hit the server using my domain name, api.example.com, without having to include the port number. Right now, I can successfully access the server by doing api.example.com:5000/... but I can't figure out how to do api.example.com/....
Right now I'm just running the flask server directly, using python flask_server.py.
In flask_server.py:
if __name__ == '__main__':
app.run(host=0.0.0.0)
The run
method takes a port optional argument:
if __name__ == '__main__':
app.run(host="0.0.0.0", port=80)
You can do this for testing, but for production I highly recommend you read the deployment options section in the documentation which details ways to run flask with various front end WSGI servers.
If you need help understanding how all these components work together and how to set them up; this gist has a nice summary.
Update: The host param needs to be a string.
You need sudo privilege in order to use port 80.
sudo python3 app.py
That will solve the problem.
Correct syntax to use Flask server on port 80 is:
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
Keep in mind that you might need super user privileges.
This kind of workaround wound be acceptable if your were still building your application.
If, as I understood, you plan to deploy your app in production then you'd need to do it properly.
Here you find step by step information for Ubuntu, as requested:
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps