This post is step by step process to enable python flask on a VPS or your Linux server. This article is written for Ubuntu server. But you can install on any other Linux flavor by changing commands as per your Linux flavor. Please follow step by step, if any issue inbox me.
Prerequisite
Your should have sudo or root access on your Linux system and system should be connected to internet.1) Run following command to install python3 and apache2 on your system. It will ask for input multiple times, please press 'Y' when ever it ask. In the end python3 and apache2 both will be installed in
sudo apt-get update sudo apt-get install apache2sudo apt-get install python3
2) If you already have python2 installed on your machine. python command will still point to old python interpreter, using below commands you can make your system understand python3 is your default interprator.
python3 -V1. run this command and add this in your ~/.bashrc file or use option 2.alias python=python3.5 source ~/.bashrc2. cp /usr/bin/python3 /usr/bin/pythonpython -- Version>>Python 3.6.9
3) Now install wsgi and enable it using following commands.
sudo apt-get install libapache2-mod-wsgi-py3sudo a2enmod wsgi
4) Now start apache2 server, a default page will be displayed to ensure server is been started. Check page on your server ip like http://your server ip/
sudo systemctl start apache2
5) Install pip3. pip is an important command line tool to install python packages. It can search and install any python package available in repository. Now make pip3 as your, default pip for your machine, its important because all the flask related api will be installed using pip.
sudo apt-get update sudo apt-get -y install python3-pipcp /usr/bin/pip3 /usr/bin/pip pip --version>> pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
6. Now install virtual environment which is a virtual environment for python, which will be used to execute python and related libraries.
sudo apt-get install virtualenvpip list --format=columns| grep virtualenv>>virtualenv 20.0.18sudo pip install flask
7. Now create your Flask test application folders. Also create the virtual environment for python flask.
mkdir /var/www/FlaskApp
virtualenv -p python3 venv
source venv/bin/activate
8) Now create folders and files given below. __init__.py is any empty file.
/var/www/FlaskApp/ /FlaskApps/__init__.py /FlaskApps/FlaskApp.py /FlaskApp.wsgi
9) Add below content to your python file FlaskApp.py
from flask import Flaskapp = Flask(__name__) @app.route("/") def hello(): return "Hello world Flask!" if __name__ == "__main__": app.run()
10) Now add following content to FlaskApp.wsgi file. This file basic configuration file where apache2 configuration meet with flask project.
#! /usr/bin/python3.6
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/FlaskApp/')
from FlaskApps.FlaskApp import app as application
application.secret_key = 'XXXX109001ddlo'
11) Now go to directory /etc/apache2/sites-available and create the file FlaskApp.conf. This is basic configuration which apache2 read to run the flask configuration. Please modify this file to add your server ip address, If you are doing testing then use localhost or 127.0.0.1
<VirtualHost *:80> # Add machine's IP address (use ifconfig command) ServerName <your server ip> # Give an alias to to start your website url with WSGIScriptAlias /testFlask /var/www/FlaskApp/FlaskApp.wsgi <Directory /var/www/FlaskApp/> # set permissions as per apache2.conf file Options FollowSymLinks AllowOverride None Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
12) Now reload apache2 configuration, make the site available using following commands.
cd /etc/apache2/sites-availablesudo a2ensite FlaskApp.confsystemctl reload apache2
13) Now go to your browser, and browser using the following url http://<your server>/testFlask/ . It should appear like this.
No comments:
Post a Comment