Flask deploy into Apache on CentOS

Environment

  • CentOS Linux release 7.5.1804 (Core)
  • httpd 2.4.6 (Apache)
  • mod_wsgi 4.6.4
  • Python 3.5.1

Project Structure

img

  • log: sotre apache log such as error log and access log
  • static: sotre static file such as image, css, js
  • templates: store html file
  • web.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello world!"
  • app.wsgi
from web import app as application

Deployment Process

Install Python3.5

Enable Software Collections (SCL)

$ yum install centos-release-scl

Install Python3

$ yum install rh-python35

Check Python Version

$ python --version
Python 2.7.5

Set Python3 as Default

$ scl enable rh-python35 bash
# If you want to always set python3 as default, add the above command line in .bash_profile.

Check Python Version Again

$ python --version
Python 3.5.1

Install gcc compiler, for installing Python modules.

$ yum install gcc
$ yum install gcc-c++

Creating a Virtual Environment

Switch to the path where you want to create a virtual environment.

$ cd your_venv_path

Create a virtual environment

$ python -m venv venv_name

Activate the virtual environment

$ source venv_name/bin/activate

After activating the environment

(venv_name) user@host$

Install Apache

Inatall apache

$ yum install httpd

Install mod_wsgi

Inatall apache related package for the pip installation of mod_wsgi to work.

$ yum install httpd-devel

Install mod_wsgi for Python3

$ pip install mod_wsgi

Check wsgi_module path

$ mod_wsgi-express install-module
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so"

Add above full LoadModule command line in apache load module config.
First, create a 02-wsgi.conf.

$ vim /etc/httpd/conf.modules.d/02-wsgi.conf

Second, add following LoadModule command line in 02-wsgi.conf.

LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so"

Apache config settings

Create a app.conf file in apache config folder

$ vim /etc/httpd/conf.d/app.conf

And add the following content in app.conf.

# define a variable which is your project path
Define PROJECT_PATH /your_project_path/MyApp

Listen 80
<virtualhost *:80>
    # wsgi settings
    WSGIDaemonProcess myapp python-path=${PROJECT_PATH}:/root/venv/lib/python3.5/site-packages
    WSGIProcessGroup myapp
    WSGIScriptAlias / ${PROJECT_PATH}/app.wsgi

    # map server side static directory to {ip or domain_name}/static
    Alias /static  ${PROJECT_PATH}/static

    # allow all requests to access this project file
    <Directory ${PROJECT_PATH}/>
        Require all granted
    </Directory>

    # set log saved path
    ErrorLog ${PROJECT_PATH}/log/error.log
    CustomLog ${PROJECT_PATH}/log/access.log combined
</virtualhost>

Start Apache

$ systemctl start httpd

If you encounter startup error, using following command to see startup log.

$ systemctl -l status httpd.service

Reference

Others

User specific environment and startup programs.

  • .bashrc: alias settings
  • .bash_profile: proxy and other startup settings.
  • Activate settings
    $ source .bash_profile
    

results matching ""

    No results matching ""