Home > Article > Backend Development > Get Started Quickly: Steps and Tips for Flask Application Deployment
#Flask is a lightweight Python web framework that is easy to learn and use, and has very powerful and flexible scalability, so it has become the first choice of many web developers. After using Flask for web development and completing the application, we need to deploy the application to the server. This article will introduce the steps and techniques of Flask application deployment, and provide specific code examples to help you get started quickly.
sudo apt-get update sudo apt-get install python3 python3-pip python3-venv
python3 -m venv venv
Among them, venv
is the name of the virtual environment and can be replaced according to application needs. After successful creation, you can use the following command to activate the virtual environment.
source venv/bin/activate
pip install -r requirements.txt
Among them, requirements.txt
is the file that stores the name and version number of the dependent library. You can use the following command to generate a list of dependent libraries.
pip freeze > requirements.txt
config.py
file in the root directory of the application and write the configuration information into it. class Config: DEBUG = True SECRET_KEY = 'secret key' SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://username:password@localhost/db_name'
Among them, SQLALCHEMY_DATABASE_URI
is the URL to connect to the database. It needs to be modified according to the actual situation.
In addition, when deploying to the server, the application needs to be configured in production mode and debug mode disabled. The following configuration can be added in the config.py
file.
class ProductionConfig(Config): DEBUG = False config = ProductionConfig()
On the server, you also need to modify the firewall settings and open the application port. Rules can be added using the following command.
sudo ufw allow 5000
Among them, 5000
is the default port number of the application, which can be modified as needed.
flask run --host=0.0.0.0 --port=5000
Among them, the --host
parameter specifies the IP address of the application, and the --port
parameter specifies the port number of the application. In order to access the application from the external network, you need to replace 0.0.0.0
with the public IP address of the server.
sudo apt-get install nginx
After the installation is complete, you can create a configuration file in the /etc/nginx/sites-available
directory, such as myapp
.
server { listen 80; server_name example.com; root /path/to/app; location / { proxy_pass http://127.0.0.1:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location /static { alias /path/to/app/static; } }
Among them, example.com
is the domain name or IP address of the server, and /path/to/app
is the root directory of the application. In the configuration file, location /static
represents the path of the static file, which needs to be modified according to the needs of the application.
After creating the configuration file, you need to link it to the /etc/nginx/sites-enabled
directory and restart Nginx.
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/ sudo nginx -s reload
sudo apt-get install supervisor
After the installation is complete, create a configuration file in the /etc/supervisor/conf.d
directory, such as myapp.conf
.
[program:myapp] directory=/path/to/app command=/path/to/venv/bin/gunicorn -w 4 -b 127.0.0.1:5000 app:app user=user autostart=true autorestart=true redirect_stderr=true
Among them, /path/to/app
is the root directory of the application, /path/to/venv
is the root directory of the virtual environment, user
is the user under which the service is running.
After creating the configuration file, you can use the following command to start Supervisor.
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start myapp
Among them, myapp
is the program name of the configuration file.
pip install gunicorn
After the installation is complete, you can use the following command to start Gunicorn in the root directory of the application.
gunicorn -w 4 -b 0.0.0.0:5000 app:app
其中,-w
参数表示工作进程的数量,-b
参数表示绑定的IP地址和端口号,app:app
表示应用的模块名和应用对象。
The above is the detailed content of Get Started Quickly: Steps and Tips for Flask Application Deployment. For more information, please follow other related articles on the PHP Chinese website!