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.

python

Python in WordPressPython in WordPress

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

regexp

正則表達式與爬蟲正則表達式與爬蟲

不知道什麼是 Regexp 別說你懂爬蟲~~ 帶你了解 Regexp 正則的強大文字處理功能 什麼是 正則表達式 ( Regexp ) ? Regular Expression , 中文稱為正則表達式.  它的前世今生, 請看 WIKI  .   為什麼要在這裏說明什麼是 Regexp 呢? 即使是很多的程式設計師, 對於正則的使用, 也未必清楚. 在大數據時代來源之前, 正則的使用, 耳熟能詳的, 就只是在