Get the App
SLTechnology News&Howtos  ›  Servers  › 

CentOS7 deployment Flask (Apache, mod_wsgi, Python36, venv)

Shulou Source: shulou.com Published: 2022-06-02 01:31:19 09月15日 Update

First, install Apache

# yum install-y httpd httpd-devel# systemctl start httpd.service # Startup # systemctl stop httpd.service # shutdown # systemctl restart httpd.service # restart # systemctl enable httpd.service # Boot

Firewall opens port 80

# firewall-cmd-zone=public-add-port=80/tcp-permanent# firewall-cmd-reload

Enable Apache, and the public network can already access the default page of Apache through ip.

Second, install Python36, pip3, virtualenv

# yum install- y epel-release# yum install- y python36# python36- VPython 3.6.The yum install- y python36-setuptools# easy_install-3.6 pip# pip3-Vpip 18.1 from / usr/local/lib/python3.6/site-packages/pip-18.1-py3.6.egg/pip (python3.6) # pip3 install virtualenv

Third, create a project

Create a flask project (at its simplest, one project folder, one startup file)

# mkdir / var/www/flask_test # Project folder # vi / var/www/flask_test/app.py # Startup File

Example of startup file:

From flask import Flask, request app = Flask (_ _ name__) @ app.route ('/') def hello_world (): return 'Hello World' @ app.route (' / hello') def hello (): name = request.args.get ('name','') return' Hello'+ name +'! If _ _ name__ = ='_ _ main__': app.run ()

Create a virtual environment under the project folder and install flask

# cd / var/www/flask_test# virtualenv py3env # create a virtual environment # source py3env/bin/activate # enter the virtual environment (py3env) # pip install flask # install flask (py3env) # deactivate # exit the virtual environment

4. Install mod_wsgi with pip in virtual environment

# yum install-y python36-devel.x86_64 # A dependency. If not installed, the following pip will report an error. # source py3env/bin/activate # enter the virtual environment (py3env) # pip install mod_wsgi # install mod_wsgi (py3env) # mod_wsgi-express install-module # execute this command Copy the output LoadModule wsgi_module "/ usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so" WSGIPythonHome "/ var/www/flask_test/py3env" (py3env) # deactivate # exit the virtual environment

Modify the configuration of Apache

# vi / etc/httpd/conf/httpd.conf

Copy the line above and add it to the end of the configuration file

The copy code is as follows: LoadModule wsgi_module "/ usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

5. Configure mod_wsgi

# vi / var/www/html/flask_test/app.wsgi

Write the following (according to: https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html)

Activate_this ='/ var/www/flask_test/py3env/bin/activate_this.py'with open (activate_this) as file_: exec (file_.read (), dict (_ _ file__=activate_this)) import syssys.path.insert (0,'/ var/www/flask_test') from app import app as application

Configure Apache

# vi / etc/httpd/conf/httpd.conf

Write the following (according to: https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html#id1)

ServerName example.com WSGIScriptAlias / / var/www/flask_test/app.wsgi Require all granted

OK, start Apache, and you can access the web page through the ip of this server

Test the two paths written in some app.py

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Tags: File environment content project configuration folder code two in use example you can pass the command that is server port web page path firewall page will report Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS vpn MariaDB OPPO Reno