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

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

1. Install Apache

# yum install -y httpd httpd-devel
# systemctl start httpd.service # Start # systemctl stop httpd.service # Shutdown # systemctl restart httpd.service # Restart # systemctl enable httpd.service # Start automatically at boot

Open port 80 on the firewall

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

Open Apache, and the external network can access Apache's default page through IP

2. Install Python36, pip3, and virtualenv

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

3. Create a project

Create a flask project (the simplest, a project folder, a 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 in 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 a virtual environment

# yum install -y python36-devel.x86_64 # A dependency. If it is not installed, pip will report an error below. .
# source py3env/bin/activate # Enter the virtual environment (py3env) # pip install mod_wsgi # Install mod_wsgi
(py3env) # mod_wsgi-express install-module # Execute the command and 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 Apache configuration

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

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

Copy the code 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 sys
sys.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)

<VirtualHost *:80>
  ServerName example.com
  WSGIScriptAlias ​​/ /var/www/flask_test/app.wsgi
  <Directory /var/www/flask_test>
    Require all granted
  </Directory>
</VirtualHost>

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

Test some two paths written in app.py

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Python wsgiref source code analysis
  • Introduction to the Python WSGI specification
  • A brief analysis of the WSGI interface and the operation of WSGI services in Python
  • Docker builds python Flask+ nginx+uwsgi container
  • Python solves the problem that flask uwsgi cannot obtain global variables
  • Python web framework Django WSGI principle analysis
  • Detailed explanation of how to configure python, mysql, nginx, uwsgi, django on VPS CENTOS
  • Python Development Nginx+uWSGI+virtualenv Multi-Project Deployment Tutorial
  • How to run Python WSGI applications in Apache
  • Use of Python built-in library wsgiref (WSGI Basics)

<<:  Detailed example of MySQL exchange partition

>>:  MySQL deduplication methods

Recommend

Example of using CSS filter to write mouse over effect

Use CSS filter to write mouse over effect <div...

Introduction to the process of installing MySQL 8.0 in Linux environment

Table of contents Preface 1. Linux changes the yu...

Install Docker for Windows on Windows 10 Home Edition

0. Background Hardware: Xiaomi Notebook Air 13/In...

How to backup MySQL regularly and upload it to Qiniu

In most application scenarios, we need to back up...

JS implements user registration interface function

This article example shares the specific code of ...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...

Two ways to build Docker images

Table of contents Update the image from an existi...

Example explanation of alarm function in Linux

Introduction to Linux alarm function Above code: ...

Detailed explanation of GaussDB for MySQL performance optimization

Table of contents background Inspiration comes fr...

Solution to MySQL error code 1862 your password has expired

The blogger hasn't used MySQL for a month or ...

Getting Started with Mysql--sql execution process

Table of contents 1. Process 2. Core Architecture...

HTML reuse techniques

HTML reuse is a term that is rarely mentioned. Tod...

Modularity in Node.js, npm package manager explained

Table of contents The basic concept of modularity...