BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Flask blog app tutorial 8 : Deploy

Python-Flask.png




Bookmark and Share





bogotobogo.com site search:



Note

This is the tutorial for Flask blog app deployment.

We have 8 parts of tutorials including this one. However, in this production version, we exclude the previous part which is mostly related to the "Likes" button and count on dashboard. So, we're deploying from part 1 to part 2 of the series.

In this part of the series, we'll deploy the Blog app to CentOS 7 server (etaman.com).

The source code is available from FlaskApp/p-deploy.







Download the source

Download the source from FlaskApp/p-deploy:

$ git clone https://github.com/Einsteinish/FlaskBlogApp.git




virtualenvwrapper

We'll use virtualenvwrapper. So, let's install it using pip:

$ sudo pip install virtualenvwrapper

We need to find virtualenvwrapper.sh. It could be in either /usr/local/bin/ or /usr/bin. Let's put env into ~/.bashrc:

# virtuanvwrapper
export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh

Then run the virtualenvwrapper.sh by running ~/.bashrc:

$ source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/initialize
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/predeactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/postdeactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/preactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/postactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/get_env_details
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/premkproject
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/postmkproject

Let's create our first virtualenv:

[sfvue@sf bin]$ mkvirtualenv env1
New python executable in env1/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/env1/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/env1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/env1/bin/preactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/env1/bin/postactivate
virtualenvwrapper.user_scripts creating /home/sfvue/Envs/env1/bin/get_env_details
(env1)[sfvue@sf ~]$  




Creating mysql database

We'll need 3 tables and 12 stored procedures as listed in Python Flask Blog App Tutorial : Appendix.

Let's go into MySQL and create a database for our app, FlaskBlogApp:

[sfvue@sf ~]$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 27566
Server version: 5.5.50-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE FlaskBlogApp;
Query OK, 1 row affected (0.02 sec)




Creating db user

The root user has full access to all of the databases, however, in the cases where more restrictions may be required, we should create users with custom permissions.

Let's create a new user within the MySQL shell:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

The newuser has no permissions to do anything with the databases. In fact, if newuser even tries to login (with the password, password), they will not be able to reach the MySQL shell.

So, the first thing to do is to provide the user with access to the information they will need:

GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

The asterisks in this command refer to the database and table (respectively) that they can access. The command allows to the user to read, edit, execute and perform all tasks across all the databases and tables.

Once we have finalized the permissions that we want to set up for our new users, always be sure to reload all the privileges:

FLUSH PRIVILEGES;

With the "FLUSH" command, our changes will now be in effect.





Creating mysql tables

We need to create a table (blog_user):

CREATE TABLE `FlaskBlogApp`.`blog_user` (
  `user_id` BIGINT NOT NULL AUTO_INCREMENT,
  `user_name` VARCHAR(45) NULL,
  `user_username` VARCHAR(45) NULL,
  `user_password` VARCHAR(85) NULL,
  PRIMARY KEY (`user_id`));

MariaDB [(none)]> use FlaskBlogApp;

MariaDB [FlaskBlogApp]> desc blog_user;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| user_id       | bigint(20)  | NO   | PRI | NULL    | auto_increment |
| user_name     | varchar(45) | YES  |     | NULL    |                |
| user_username | varchar(45) | YES  |     | NULL    |                |
| user_password | varchar(85) | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

tbl_blog:

CREATE TABLE `tbl_blog` (
  `blog_id` int(11) NOT NULL AUTO_INCREMENT,
  `blog_title` varchar(45) DEFAULT NULL,
  `blog_description` varchar(5000) DEFAULT NULL,
  `blog_user_id` int(11) DEFAULT NULL,
  `blog_date` datetime DEFAULT NULL,
  PRIMARY KEY (`blog_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

ALTER TABLE `FlaskBlogApp`.`tbl_blog` 
ADD COLUMN `blog_file_path` VARCHAR(200) NULL AFTER `blog_date`,
ADD COLUMN `blog_accomplished` INT NULL DEFAULT 0 AFTER `blog_file_path`,
ADD COLUMN `blog_private` INT NULL DEFAULT 0 AFTER `blog_accomplished`;

MariaDB [FlaskBlogApp]> desc tbl_blog;
+-------------------+---------------+------+-----+---------+----------------+
| Field             | Type          | Null | Key | Default | Extra          |
+-------------------+---------------+------+-----+---------+----------------+
| blog_id           | int(11)       | NO   | PRI | NULL    | auto_increment |
| blog_title        | varchar(45)   | YES  |     | NULL    |                |
| blog_description  | varchar(5000) | YES  |     | NULL    |                |
| blog_user_id      | int(11)       | YES  |     | NULL    |                |
| blog_date         | datetime      | YES  |     | NULL    |                |
| blog_file_path    | varchar(200)  | YES  |     | NULL    |                |
| blog_accomplished | int(11)       | YES  |     | 0       |                |
| blog_private      | int(11)       | YES  |     | 0       |                |
+-------------------+---------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

CREATE TABLE `FlaskBlogApp`.`tbl_likes` (
  `blog_id` INT NOT NULL,
  `like_id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NULL,
  `blog_like` INT NULL DEFAULT 0,
  PRIMARY KEY (`like_id`));

MariaDB [FlaskBlogApp]> desc tbl_likes;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| blog_id   | int(11) | NO   |     | NULL    |                |
| like_id   | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id   | int(11) | YES  |     | NULL    |                |
| blog_like | int(11) | YES  |     | 0       |                |
+-----------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Here are the three tables we've created so far:

MariaDB [FlaskBlogApp]> show tables;
+------------------------+
| Tables_in_FlaskBlogApp |
+------------------------+
| blog_user              |
| tbl_blog               |
| tbl_likes              |
+------------------------+
3 rows in set (0.00 sec)




Creating mysql stored procedures

Here is the list of the stored procedures that should be created:

  1. sp_addBlog:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_addBlog`;
     
    DELIMITER $$
    USE `FlaskBlogApp`$$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_addBlog`(
        IN p_title varchar(45),
        IN p_description varchar(1000),
        IN p_user_id bigint,
        IN p_file_path varchar(200),
        IN p_is_private int,
        IN p_is_done int
    )
    BEGIN
        insert into tbl_blog(
            blog_title,
            blog_description,
            blog_user_id,
            blog_date,
            blog_file_path,
            blog_private,
            blog_accomplished
        )
        values
        (
            p_title,
            p_description,
            p_user_id,
            NOW(),
            p_file_path,
            p_is_private,
            p_is_done
        );
    END$$
     
    DELIMITER ;
    

  2. sp_createUser:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_createUser`;
    
    DELIMITER $$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_createUser`(
        IN p_name VARCHAR(20),
        IN p_username VARCHAR(20),
        IN p_password VARCHAR(85)
    )
    BEGIN
        IF ( select exists (select 1 from blog_user where user_username = p_username) ) THEN
         
            select 'Username Exists !!';
         
        ELSE
         
            insert into blog_user
            (
                user_name,
                user_username,
                user_password
            )
            values
            (
                p_name,
                p_username,
                p_password
            );
         
        END IF;
    END$$
    DELIMITER ;
    

  3. sp_deleteBlog:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_deleteBlog`;
    
    DELIMITER $$
    USE `FlaskBlogApp`$$
    CREATE PROCEDURE `sp_deleteBlog` (
    IN p_blog_id bigint,
    IN p_user_id bigint
    )
    BEGIN
    delete from tbl_blog where blog_id = p_blog_id and blog_user_id = p_user_id;
    END$$
     
    DELIMITER ;
    

  4. sp_GetAllBlogs:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_GetAllBlogs`;
     
    DELIMITER $$
    USE `FlaskBlogApp`$$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_GetAllBlogs`()
    BEGIN
        select blog_id,blog_title,blog_description,blog_file_path from tbl_blog where blog_private = 0;
    END$$
     
    DELIMITER ;
    

  5. sp_GetBlogById:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_GetBlogById`;
    
    DELIMITER $$
    USE `FlaskBlogApp`$$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_GetBlogById`(
    IN p_blog_id bigint,
    In p_user_id bigint
    )
    BEGIN
    select blog_id,blog_title,blog_description,blog_file_path,blog_private,blog_accomplished from tbl_blog where blog_id = p_blog_id and blog_user_id = p_user_id;
    END$$
     
    DELIMITER ;
    

  6. sp_GetBlogByUser:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_GetBlogByUser`;
     
    DELIMITER $$
    USE `FlaskBlogApp`$$
    CREATE PROCEDURE `sp_GetBlogByUser` (
    IN p_user_id bigint
    )
    BEGIN
        select * from tbl_blog where blog_user_id = p_user_id;
    END$$
     
    DELIMITER ;
    

  7. sp_updateBlog:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_updateBlog`;
     
    DELIMITER $$
    USE `FlaskBlogApp`$$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_updateBlog`(
    IN p_title varchar(45),
    IN p_description varchar(1000),
    IN p_blog_id bigint,
    In p_user_id bigint,
    IN p_file_path varchar(200),
    IN p_is_private int,
    IN p_is_done int
    )
    BEGIN
    update tbl_blog set
        blog_title = p_title,
        blog_description = p_description,
        blog_file_path = p_file_path,
        blog_private = p_is_private,
        blog_accomplished = p_is_done
        where blog_id = p_blog_id and blog_user_id = p_user_id;
    END$$
     
    DELIMITER ;
    

  8. sp_validateLogin:
    USE `FlaskBlogApp`;
    DROP procedure IF EXISTS `sp_validateLogin`;
    
    DELIMITER $$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_validateLogin`(
    IN p_username VARCHAR(20)
    )
    BEGIN
    	select * from blog_user where user_username = p_username;
    END$$
    
    DELIMITER ;
    

Here is the stored procedures created so far:

MariaDB [FlaskBlogApp]> select db,name,definer from mysql.proc WHERE db = 'FlaskBlogApp';
+--------------+------------------+----------------+
| db           | name             | definer        |
+--------------+------------------+----------------+
| FlaskBlogApp | sp_addBlog       | root@localhost |
| FlaskBlogApp | sp_createUser    | root@localhost |
| FlaskBlogApp | sp_deleteBlog    | root@localhost |
| FlaskBlogApp | sp_GetAllBlogs   | root@localhost |
| FlaskBlogApp | sp_GetBlogById   | root@localhost |
| FlaskBlogApp | sp_GetBlogByUser | root@localhost |
| FlaskBlogApp | sp_updateBlog    | root@localhost |
| FlaskBlogApp | sp_validateLogin | root@localhost |
+--------------+------------------+----------------+
8 rows in set (0.00 sec)




Calling the MySQL Stored Procedure

We make calls to the MySQL stored procedure to create the new user etc.

To connect with MySQL from Flask, we need to install Flask-MySQL:

(env1)[sfvue@sf p-deploy]$ pip install flask-mysql




Install Flask

Let's install Flask with pip package manager:

$ pwd
/home/sfvue/MySites/etaman/FlaskBlogApp/FlaskApp/p-deploy

[sfvue@sf p-deploy]$ workon env1

(env1)[sfvue@sf p-deploy]$ pip install flask

Here is the directory structure:

(env1)[sfvue@sf p-deploy]$ tree -L 2
.
|-- app.py
|-- config.py
|-- static
|   |-- css
|   |-- images
|   |-- js
|   |-- Uploads
|
|-- templates
    |-- addBlog.html
    |-- dashboard.html
    |-- error.html
    |-- index.html
    |-- signin.html
    |-- signup.html
    |-- userHome.html




config.py

Here is the configuration file, config.py:

#secret key for signing cookies
SECRET_KEY = "spooky action at a distance-Einstein"

# MySQL configurations
MYSQL_DATABASE_USER = 'khong'
MYSQL_DATABASE_PASSWORD = 'khong'
MYSQL_DATABASE_DB = 'FlaskBlogApp'
MYSQL_DATABASE_HOST = 'localhost'

# file upload configurations
UPLOAD_FOLDER = 'static/Uploads'

In app.py, we need the following a line of code to get the configuration:

app = Flask(__name__)

app.config.from_object('config')




Permission - static directory

We need to set proper permission to apache user:

(env1)[sfvue@sf p-deploy]$ sudo chown -R apache:apache static




Apache port forwarding configuration

We use Apache with port forwarding - forward a request on port 80 to 5050, and here the the configuration file, /etc/httpd/sites-available/etaman.com.conf:

<VirtualHost *:80>

     ServerName www.etaman.com
     ServerAlias etaman.com

     ProxyPreserveHost On

     ProxyPass / http://127.0.0.1:5050/
     ProxyPassReverse / http://127.0.0.1:5050/

     ErrorLog /var/www/etaman.com/logs/error.log
     CustomLog /var/www/etaman.com/logs/access.log combined
     LogLevel warn
     ServerSignature Off
</VirtualHost>

Set a soft-link and restart the server:

$ sudo ln -s /etc/httpd/sites-available/etaman.com.conf /etc/httpd/sites-enabled/etaman.com.conf

$ sudo service httpd restart
Redirecting to /bin/systemctl restart  httpd.service




Running the app

We'll run our app using pm2:

(env1)[sfvue@sf p-deploy]$ pm2 start etaman.sh

The etaman.sh looks like this:

workon env1
/home/sfvue/Envs/env1/bin/python app.py &

Check the status of app:

etaman-pm2.png

Actually, at this point, the pm2 run keeps failing. So, I had to use the traditional "nohup" instead:

[sfvue@sf p-deploy]$ nohup /home/sfvue/Envs/env1/bin/python /home/sfvue/MySites/etaman/FlaskBlogApp/FlaskApp/p-deploy/app.py &
[1] 20772

The screen shots of the run:


etaman-home-page.png
SignUp.png

If not errors in the sign-up, we may want to go to sign-in page to login.


Once signed in, we can see the dashboard with the posts:

DashBoardWithPics.png

WE can edit/delete the posts:

Edit-Delete.png







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong








Flask



Deploying Flask Hello World App with Apache WSGI on Ubuntu 14

Flask Micro blog "Admin App" with Postgresql

Flask "Blog App" with MongoDB - Part 1 (Local via Flask server)

Flask "Blog App" with MongoDB on Ubuntu 14 - Part 2 (Local Apache WSGI)

Flask "Blog App" with MongoDB on CentOS 7 - Part 3 (Production Apache WSGI )

Flask word count app 1 with PostgreSQL and Flask-SQLAlchemy

Flask word count app 2 via BeautifulSoup, and Natural Language Toolkit (NLTK) with Gunicorn/PM2/Apache

Flask word count app 3 with Redis task queue

Flask word count app 4 with AngularJS polling the back-end

Flask word count app 5 with AngularJS front-end updates and submit error handling

Flask word count app 0 - Errors and Fixes

Flask with Embedded Machine Learning I : Serializing with pickle and DB setup

Flask with Embedded Machine Learning II : Basic Flask App

Flask with Embedded Machine Learning III : Embedding Classifier

Flask with Embedded Machine Learning IV : Deploy

Flask with Embedded Machine Learning V : Updating the classifier

Flask AJAX with jQuery

Flask blog app with Dashboard 1 - SignUp page

Flask blog app with Dashboard 2 - Sign-In / Sign-Out

Flask blog app with Dashboard 3 - Adding blog post item

Flask blog app with Dashboard 4 - Update / Delete

Flask blog app with Dashboard 5 - Uploading an image

Flask blog app with Dashboard 6 - Dash board

Flask blog app with Dashboard 7 - Like button

Flask blog app with Dashboard 8 - Deploy

Flask blog app with Dashboard - Appendix (tables and mysql stored procedures/functions

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Python tutorial



Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Python Coding Questions IX

Python Coding Questions X

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Selenium WebDriver

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...









Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master