sudo apt install apache2 -y<\/pre>\nOnce installed, start and enable the service.<\/p>\n
sudo systemctl enable apache2 && sudo systemctl start apache2<\/pre>\nCheck if the service is up and running:<\/p>\n
sudo systemctl status apache2<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# sudo systemctl status apache2\r\n\u25cf apache2.service - The Apache HTTP Server\r\n Loaded: loaded (\/lib\/systemd\/system\/apache2.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Mon 2022-11-07 13:30:41 CST; 1 week 0 days ago\r\n Docs: https:\/\/httpd.apache.org\/docs\/2.4\/\r\n Main PID: 747 (apache2)\r\n Tasks: 55 (limit: 4575)\r\n Memory: 11.3M\r\n CPU: 49.680s\r\n CGroup: \/system.slice\/apache2.service\r\n \u251c\u2500 747 \/usr\/sbin\/apache2 -k start\r\n \u251c\u250070464 \/usr\/sbin\/apache2 -k start\r\n \u2514\u250070465 \/usr\/sbin\/apache2 -k start\r\n\r\nNov 13 00:00:02 host.test.vps systemd[1]: Reloading The Apache HTTP Server...\r\n<\/pre>\n<\/span>Step 3. Install Python<\/span><\/h2>\nWe need to install the latest version of python3 for Ubuntu 22.04 and the python package manager pip<\/b> along with the python virtual environment. To achieve this execute the following commands:<\/p>\nsudo apt-get install python3 python3-pip python3-venv<\/pre>\nTo check the installed python version, execute the following command:<\/p>\n
python3 -V<\/pre>\nYou should get the following output:<\/p>\n
root@host:~# python3 -V\r\nPython 3.10.6\r\n<\/pre>\n<\/span>Step 4. Install Flask Application<\/span><\/h2>\nThe next step is to install and deploy the flask application in the virtual environment.<\/p>\n
To create the virtual environment in the opt<\/b> directory, execute the following command:<\/p>\ncd \/opt && mkdir flask-app\r\n\r\ncd flask-app\r\n\r\npython3 -m venv flask-venv<\/pre>\nOnce the virtual flask environment is created, you need to activate it using the following command:<\/p>\n
source flask-venv\/bin\/activate<\/pre>\nThe command prompt will change and will look as described below:<\/p>\n
root@host:\/opt\/flask-app# source flask-venv\/bin\/activate\r\n(flask-venv) root@host:\/opt\/flask-app#<\/pre>\nThe next step is to install the flask application inside the virtual environment with the following command:<\/p>\n
pip3 install flask<\/pre>\nThe successfully installed flask application will have the following output:<\/p>\n
(flask-venv) root@host:\/opt\/flask-app# pip3 install flask\r\nCollecting flask\r\n Downloading Flask-2.2.2-py3-none-any.whl (101 kB)\r\n \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 101.5\/101.5 KB 2.4 MB\/s eta 0:00:00\r\nCollecting click>=8.0\r\n Downloading click-8.1.3-py3-none-any.whl (96 kB)\r\n \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 96.6\/96.6 KB 4.3 MB\/s eta 0:00:00\r\nCollecting itsdangerous>=2.0\r\n Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)\r\nCollecting Werkzeug>=2.2.2\r\n Downloading Werkzeug-2.2.2-py3-none-any.whl (232 kB)\r\n \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 232.7\/232.7 KB 4.8 MB\/s eta 0:00:00\r\nCollecting Jinja2>=3.0\r\n Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)\r\n \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 133.1\/133.1 KB 4.0 MB\/s eta 0:00:00\r\nCollecting MarkupSafe>=2.0\r\n Downloading MarkupSafe-2.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)\r\nInstalling collected packages: MarkupSafe, itsdangerous, click, Werkzeug, Jinja2, flask\r\nSuccessfully installed Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.2.2 click-8.1.3 flask-2.2.2 itsdangerous-2.1.2\r\n<\/pre>\nNext is to create a sample flask application python file:<\/p>\n
sudo nano app.py<\/pre>\nInsert the following lines of code:<\/p>\n
from flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef index():\r\n return 'Hello World'\r\n<\/pre>\nSave the file, close it and set up the FLASK_APP environment variable.<\/p>\n
export FLASK_APP=app.py<\/pre>\nNow, we can test the application by running the following command:<\/p>\n
flask run --host=0.0.0.0<\/pre>\nYou will receive the following output:<\/p>\n
(flask-venv) root@host:\/opt\/flask-app# flask run --host=0.0.0.0\r\n * Debug mode: off\r\nWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\r\n * Running on all addresses (0.0.0.0)\r\n * Running on http:\/\/127.0.0.1:5000\r\n * Running on http:\/\/YourServerIP<\/b>:5000\r\nPress CTRL+C to quit\r\nHello World\r\nYourServerIP<\/b> - - [15\/Nov\/2022 07:37:13] \"GET \/ HTTP\/1.1\" 200 -\r\n<\/pre>\nAs you can see in the output, this is a development server and is not recommended in a production deployment. We need to use the WSGI server instead. You can quit the running process with the CTRL+C<\/b> command and deactivate the environment with the deactivate<\/b> command for now. In the next step, we will configure the Apache Web server with mod WSGI.<\/p>\n(flask-venv) root@host:\/opt\/flask-app# deactivate<\/b>\r\nroot@host:\/opt\/flask-app#\r\n<\/pre>\n