This article is about launching on a virtual hosting.
There is a separate article about the deployment on your server:
«Deploy Flask App on Linux Server (Nginx + Gunicorn)»
/p>
Of course, a lot depends on hosting but the main problems, I think, are the same everywhere.
There are a lot of articles on the Internet about how to run Flash locally. But there are few guidelines for setting up on a virtual hosting.
For the material for this guide, I thank the hosting technical support
beget.com
their article about the setup
Flask
on the hosting, you can study
here
Read more about the choice of hosting in my article
«How to choose hosting»
Connecting to the hosting account via ssh. To do this, you can use
Putty
or
MobaXterm
The easiest way to connect to an account is by IP. To find it out - execute
ping url_site
If on your hosting installed Docker - go to container with the command
ssh localhost -p222
If it is beget.com , or find out how to do it on your hosting by asking a question to tech support.
Let's create a simple web application at the root of your site.
If you have a public_html folder on your site, save it somewhere in a safe place and delete it
from the website.
We start in a completely empty directory.
Creating two folders HelloFlask and tmp
mkdir HelloFlask tmp
Creating two files in the root directory .htaccess and passenger_wsgi
touch .htaccess
touch passenger_wsgi.py
If you know how to edit files on hosting - keep reading this article.
If this causes problems - read my article
«Text editor vi»
In the file .htaccess we write
PassengerEnabled On
PassengerPython /home/a/username/.local/bin/python3
Moreover, /home/a/username/.local/bin/python3 you will need to replace with the path
up to your python3.
To find it out execute
which python3
The file .htaccess has been sorted out, now we are editing passenger_wsgi.py
# -*- coding: utf-8 -*-
import sys, os
# specifying the directory with the project
sys.path.append('/home/a/username/aredel.com/HelloFlask')
# we specify the directory with libraries where we put Flask
sys.path.append('/home/a/username/.local/bin/flask')
from HelloFlask import app as application
# When Flask starts, it searches for application.
# If you do not specify 'as application', the site will not work
from werkzeug.debug import DebuggedApplication
# Optional: connection of the debugging module
application.wsgi_app = DebuggedApplication(application.wsgi_app, True)
# Optional: enabling the debug module
application.debug = False
# Optional: True/False is set when debugging is necessary
Where /home/a/username/aredel.com/ needs to be replaced with the address of your site from the hosting root.
With the file passenger_wsgi.py figured out, go to the folder HelloFlask
cd HelloFlask
Create a __init__ file there.py
touch __init__.py
And write there
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello Flask!'
if __name__ == '__main__':
app.run()
С файлом __init__.py разобрались, возвращаемся в корневую директорию
cd ..
И выполняем
ln -s public_html public
touch tmp/restart.txt
Открывайте Ваш сайт и убедитесь, что там написано Hello Flask!
Когда Вы внесете какие-то обновления не забудьте после сохранения
выполнять перезагрузку командой.
touch tmp/restart.txt
Если Вы ещё не определилсь с выбором хостинга советую прочитать мою
статью
«Какой хостинг выбрать»
Python | |
Flask | |
Запуск Flask на Linux сервере | |
Первый проект на Flask | |
Шаблоны Jinja | |
Flask FAQ | |
Web Forms | |
Errors |
Share in social media:
|