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:
- 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.
- 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 .
- 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.
- 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.
- Create the Flask application in folder you just assigned.
- 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.
- 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.
- 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:
- Apache log: in /var/log/apache2/error.log
- Apache activation status: sudo systemctl status apache2.service
- Flask running error: you would see the error code in browsers.