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 .

Notes:

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.

爬蟲: 下載台股歷史股價 TWSE爬蟲: 下載台股歷史股價 TWSE

適用對象: 有 Python 基礎. 可以自己執行 Python 程式的人. 有興趣學習 Python 爬蟲程式的人. 知識基礎: Python Pandas : Python 數據分析的資料結構函式庫. 是爬蟲程式的主要核心. Requests : Python 網路連線功能. 作為 Http 資源下載使用. 爬蟲程式的概念: 其實爬蟲程式的概念很簡單. 找到你要的網路資源 (包含網頁, 檔案…) ,

python

Python in WordPressPython in WordPress

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

Apache2

如何在 Ubuntu 上使用一個IP,多個網域的設定如何在 Ubuntu 上使用一個IP,多個網域的設定

Know-How 在 Apache2 上, 如何使用多個網域, Domains, 指向同一台機器或 IP 你想了解的是… 對網站開發或管理人員來說, 資料的管理與運用, 是你重要的績效.  不管是來自客戶, 或是來自於你的主管的要求. 希望你可以在同一台主機上, 安裝多個網站. 讓資源做最大的利用. 以下我會針對不同的場景, 來解釋如何配置不同的 Web Server.  測試的環境 OS : Ubuntu . 指令 lsb_release -a No LSB