Hybrid system in one domain section IIHybrid system in one domain section II

Following the previous post, now it is about time to describe the steps how to implement WordPress CMS and Flask apps in an Apache web server.

Install WordPress CMS in Apache

For installing WordPress CMS in Ubuntu, that is not so difficult. You may refer to DigitalOcean for more details, which is quite clear for newbies.

configuration of sites in Apache

Afterwards, you just activate sites of HTTP and HTTPS in Apache. Check the settings in the path of /etc/apache2/sites-availables. There are 2 configuration files, one is for HTTP , 000-default.conf , another is for HTTPS, default-SSL.conf.

Configure Flask apps in Apache

Now, we are going through the key notes for this post. I am trying to introduce the necessary steps to set up Flask apps in Apache. See demo. (Automatically http link will transfer to https link, and Flask app return message).

steps for configuration:

  1. The first step is to install module of mod_wsgi in Apache。 WSGI is only for Python, and is a abbreviation of Web Server Gateway Interface. WSGI is not a server, neither an API and real codes , but an protocol for a web server and Python web application.
  2. Then activate the mod_wsgi module, which is a module of Apache web server, and could be activated with Apache native command, a2enmod。 The full command is ‘ a2enmod wsgi’. Afterwards, you could find wsgi.conf and wsgi.load files in path of /etc/apache2/mods_enabled .
  3. Create a Flask Application。In path of /var/www , you could group all Flask applications in one entry folder, say Flaskapps。Underneath, you could create Flask applications one by one with its own context. Practically, each Flask application have its own functions and purposes, then Python will ‘import’ necessary modules for packages. That is why you could see most of posts asking you to install virtualenv.
  4. Installing virtualenv is a critical step to build up a virtual python environment for each application. The difference between pipenv and virtualenv is that pipenv is build upon .local/share folder, and virtualenv is dedicated for the application . Take Python3 for example, the command is ‘virtualenv -p /usr/bin/python3 <folder_name>’ , which create virtualenv in the folder you assigned.
  5. Create the Flask application in folder you just assigned.
  6. In the assigned folder, you create an important file , WSGI file. WSGI file is to tell the Apache where to find the virtual environment and where to load Flask application.
  7. Setup site-config. The file is for Apache server to manage your web applications. The path is /etc/apache2/sites-available . By default, you would find 000-default.conf in the folder. In the file, you have to define daemon process group and instruct WSGI applications to run in context of daemon process group . Also it assign the prefix or endpoint of URI of Flask Application and its corresponding directory.
  8. However, you activate Apache SSL, the configuration of WSGI daemon process has to be moved to default-ssl.conf .


While all configuration are done, you just keep your Flask application simple, that is, app.run() . You don’t have to assign IP or port number for Flask application. Apache will take care of rest. But if you have more than one Flask applications, you still need to assign port numbers for each to get rid of ports conflict.

For debugging in the whole setting process, you have to know 3 ways to get rid of errors:

  1. Apache log: in /var/log/apache2/error.log
  2. Apache activation status: sudo systemctl status apache2.service
  3. Flask running error: you would see the error code in browsers.

Leave a Reply

Your email address will not be published.

如何將 Flask 系統安裝在 Apache 下如何將 Flask 系統安裝在 Apache 下

Flask application 是一套成長快速的 web framework。它的核心小,擴充性佳。對中小型的系統來說,是很方便的工具。 如果你對使用 Flask 開發有興趣,可以參考官方的教學文件。在未來,我也會逐一向大家介紹說明。 本篇文章主要是用來說明,如何將已開發完成的 Flask application 安裝在 Apache 下。觀看實際的 demo. (http 自動轉向到 https, 並啟動 Flask application ) . 這樣做的目的,或是好處: 你可以將你所有開發的系統,都掛在同一個網域(domain) 下。就如同本站,只有一個 comhawk.com 的網域. 使用 WordPress


Python in WordPressPython in WordPress

本頁是用來示範如何將 Python 執行的結果顯示在 WordPress 中. 以下的內容, 是由 Python Flask 回傳的簡單訊息. 並內嵌在 WordPress 的頁面中. 它的優點: WordPress 具有完整的 CMS 功能. 可以讓後端工程式簡化 UI 程式的開發. 而且界面容易變化管理 WordPress 有會員管理功能, 可以透過它來管理使用者 Python 適合用於後端資料處理. 開發速度快. 兩者結合方便很多.