Flask deploy into Apache on CentOS without Internet

Environment

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

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 Apache and Additional Modules for Apache

$ yum install httpd
$ yum install httpd-devel

Install Required Tools

python3 requires

$ yum -y groupinstall development

pip requires

$ yum install openssl-devel

Install Python 3.5.1

$ wget -q 'https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz'
$ tar -xzf 'Python-3.5.1.tgz'
$ cd Python-3.5.1
$ CXX=g++ ./configure --enable-shared
$ make
$ make altinstall
$ cd ..
$ rm Python-3.5.1.tgz
$ rm -rf Python-3.5.1
$ ln -fs /usr/local/bin/python3.5 /usr/bin/python3.5
$ echo "/usr/local/lib/python3.5" > /etc/ld.so.conf.d/python35.conf
$ echo "/usr/local/lib" >> /etc/ld.so.conf.d/python35.conf
$ ldconfig

Install mod_wsgi 4.6.4

$ wget -q "https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.4.tar.gz"
$ tar -xzf 'mod_wsgi-4.6.4.tar.gz'
$ cd mod_wsgi-4.6.4
$ ./configure --with-python=/usr/local/bin/python3.5
$ make
$ make install

After installing, the wsgi_module will be installed into /usr/lib64/httpd/modules/mod_wsgi.so

Create Python Virtual Environment

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

$ cd your_venv_path

Create a virtual environment

$ python3.5 -m venv venv_name

Activate the virtual environment

$ source venv_name/bin/activate

After activating the environment

(venv_name) $

Prepare requirements.txt and Download all the Packages from your Developing Virtual Environment

get requirements.txt

(devel_venv) $ pip freeze > requirements.txt

download all the packages into your path

(devel_venv) $ pip install --download=/your/path/packages -r requirements.txt

Take requirements.txt and all the Downloaded Packages into your Deployment Server

install packages into venv of deployment server

(venv_name) $ pip install --no-index --find-links=/your/path/packages -r requirements.txt

Set Apache Configure

create a 02-wsgi.conf to load mod_wsgi into apache

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

add following command line in 02-wsgi.conf

LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi.so"

create a app.conf to set your project execution environment

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

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

References

results matching ""

    No results matching ""