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.

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

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

Apache2

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

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

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

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