Web application with Python + Flask ② ③

Continuation of Last time.

  1. Environment preparation (OS setup)
  2. Environment preparation (setup in OS) ★
  3. Trace the contents of Flask's QuickStart (installation and minimal setup) ★
  4. Trace the contents of Flask's Tutorial (learning how to make a basic application)
  5. Make original content

This time, a memo when I did 2. and 3.

SSH permissions on Root

In the default box of CentOS, ssh access is not possible as root, and vagrant user⁺ authentication with private key is required. This can be confirmed with vagrant ssh-config.

2017/01/30 19:26:06|C:\Vagrant\CentOS0-2>vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/Vagrant/CentOS0-2/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Since it is troublesome to promote root one by one, allow SSH access as root. First, edit the following two places with sshd_config on the guest. PermitRootLogin yes PasswordAuthentication yes

Access to guests is possible with the following settings. SSH port: 2222 User: vagrant Private key: C: \ Vagrant \ CentOS0-2 \ .vagrant \ machines \ default \ virtualbox \ private_key

After logging in, use su - to promote root (password is vagrant)

[root@cnenyuy5l3c ~]# cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
[root@cnenyuy5l3c ~]# vi /etc/ssh/sshd_config
[root@cnenyuy5l3c ~]# diff /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
42c42
< PermitRootLogin yes
---
> #PermitRootLogin yes
66c66
< PasswordAuthentication yes
---
> PasswordAuthentication no
[root@cnenyuy5l3c ~]#

Next, reload the guest by writing the following in the .Vagrantfile. config.ssh.username = 'root' config.ssh.password = 'vagrant' config.ssh.insert_key = 'true'

2017/01/30 19:41:18|C:\Vagrant\CentOS0-2>vagrant reload
==> default: Attempting graceful shutdown of VM...
    default: Guest communication could not be established! This is usually because
    default: SSH is not running, the authentication information was changed,
    default: or some other networking issue. Vagrant will force halt, if
    default: capable.
==> default: Forcing shutdown of VM...
==> default: Checking if box 'centos/6' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 5000 (guest) => 5000 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: root
    default: SSH auth method: password
==> default: Machine booted and ready!
[default] GuestAdditions 5.1.12 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Vagrant/CentOS0-2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

Python installation

First, install the prerequisite modules.

[root@cnenyuy5l3c ~]# yum install openssl-devel
[root@cnenyuy5l3c ~]# yum install sqlite-devel
[root@cnenyuy5l3c ~]# yum groupinstall "Development tools"

Then install Python. 2.6.6 is included by default, but since it is old, the latest (2.7.12) is included.

[root@cnenyuy5l3c ~]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[root@cnenyuy5l3c ~]# 
[root@cnenyuy5l3c ~]# curl -O https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz
[root@cnenyuy5l3c ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  Python-2.7.12.tar.xz
[root@cnenyuy5l3c ~]# xz -dv Python-2.7.12.tar.xz
[root@cnenyuy5l3c ~]# tar xvf Python-2.7.12.tar
[root@cnenyuy5l3c ~]# cd Python-2.7.12
[root@cnenyuy5l3c Python-2.7.12]# ./configure --prefix=/opt/local
[root@cnenyuy5l3c Python-2.7.12]# make && make altinstall
[root@cnenyuy5l3c Python-2.7.12]# ls -l /usr/bin/python*
-rwxr-xr-x. 2 root root 9032 Aug 18 15:14 /usr/bin/python
lrwxrwxrwx. 1 root root    6 Dec 15 11:11 /usr/bin/python2 -> python
-rwxr-xr-x. 2 root root 9032 Aug 18 15:14 /usr/bin/python2.6

[root@cnenyuy5l3c Python-2.7.12]# rm /usr/bin/python
rm: remove regular file `/usr/bin/python'? y
[root@cnenyuy5l3c Python-2.7.12]# 
[root@cnenyuy5l3c Python-2.7.12]# cp -p /opt/local/bin/python2.7 /usr/bin/python
[root@cnenyuy5l3c Python-2.7.12]# python
Python 2.7.12 (default, Jan 30 2017, 11:04:49) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[root@cnenyuy5l3c Python-2.7.12]# 

Note that yum only works with 2.6, so only yum should be set to refer to 2.6.

[root@cnenyuy5l3c Python-2.7.12]# which yum
/usr/bin/yum
[root@cnenyuy5l3c Python-2.7.12]# vi /usr/bin/yum
[root@cnenyuy5l3c Python-2.7.12]# head -1 /usr/bin/yum
#!/usr/bin/python2.6

After that, install virtualenv (and pip used for its installation) to keep the development environment separate.

[root@cnenyuy5l3c Python-2.7.12]# curl -kL https://bootstrap.pypa.io/get-pip.py | /opt/local/bin/python2.7
[root@cnenyuy5l3c Python-2.7.12]# ls /opt/local/bin/
2to3  easy_install  easy_install-2.7  idle  pip  pip2  pip2.7  pydoc  python2.7  python2.7-config  smtpd.py  wheel
[root@cnenyuy5l3c Python-2.7.12]# cd ~
[root@cnenyuy5l3c ~]# vi .bashrc
[root@cnenyuy5l3c ~]# cat .bashrc

## CUSTOM START
export PATH=$PATH:/opt/local/bin/
## CUSTOM END

[root@cnenyuy5l3c ~]# 
[root@cnenyuy5l3c ~]# source .bashrc
[root@cnenyuy5l3c ~]# /opt/local/bin/pip2.7 install virtualenv

Pass it through the pip path. The last test so far.

[root@cnenyuy5l3c ~]# mkdir testpj
[root@cnenyuy5l3c ~]# cd testpj
[root@cnenyuy5l3c testpj]# 
[root@cnenyuy5l3c testpj]# 
[root@cnenyuy5l3c testpj]# virtualenv env
New python executable in /root/testpj/env/bin/python2.7
Also creating executable in /root/testpj/env/bin/python
Installing setuptools, pip, wheel...done.
[root@cnenyuy5l3c testpj]# ls
env
[root@cnenyuy5l3c testpj]# 
[root@cnenyuy5l3c testpj]# . env/bin/activate
(env) [root@cnenyuy5l3c testpj]# 
(env) [root@cnenyuy5l3c testpj]# python
Python 2.7.12 (default, Jan 30 2017, 11:04:49) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> quit()
(env) [root@cnenyuy5l3c testpj]# 
(env) [root@cnenyuy5l3c testpj]# deactivate
[root@cnenyuy5l3c testpj]#

Virtualenv started and stopped in Python 2.7.

Flask installation

Install flask in virtualenv.

[root@cnenyuy5l3c testpj]# . env/bin/activate
(env) [root@cnenyuy5l3c testpj]#
(env) [root@cnenyuy5l3c testpj]# easy_install Flask

Create the following test app file and start it.

app.py


from flask import Flask
app = Flask(__name__)

@app.route(‘/’)
def hello_world():
    return "Hello World!"

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Launch the application.

(env) [root@cnenyuy5l3c testpj]# python app.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Access 5000 on the host machine and check the operation. 20170130_008.jpg

Up to here for this time.

Recommended Posts

Web application with Python + Flask ② ③
Web application with Python + Flask ④
Web application development with Flask
Application development with Docker + Python + Flask
Parse and visualize JSON (Web application ⑤ with Python + Flask)
[Python] A quick web application with Bottle!
Easy web app with Python + Flask + Heroku
Run a Python web application with Docker
Programming with Python Flask
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Launch a web server with Python and Flask
Web scraping with python + JupyterLab
Web application creation with Django
Web API with Python + Falcon
Web scraping beginner with python
Streamline web search with python
Launch a Python web application with Nginx + Gunicorn with Docker
Web application made with Python3.4 + Django (Part.1 Environment construction)
Hobby Web engineer develops web application with Vue.js + Flask (& GCP)
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Web application production course learned with Flask of Python Part 2 Chapter 1 ~ JSON exchange ~
SNS Python basics made with Flask
Creating a web application using Flask ②
Web scraping with Python ① (Scraping prior knowledge)
Web application development memo in python
Getting Started with Python Web Applications
Web scraping with Python First step
I tried web scraping with python.
[Python] Use Basic/Digest authentication with Flask
Monitor Python application performance with Dynatrace ♪
Get web screen capture with python
Build a web application with Django
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Python application: Data cleansing # 2: Data cleansing with DataFrame
Python x Flask x Tensorflow.Keras Web application for predicting cat breeds 2
I made a simple book application with python + Flask ~ Introduction ~
Web application with Python3.3.1 + Bottle (1) --Change template engine to jinja2
Easy deep learning web app with NNC and Python + Flask
[Python] Web application from 0! Hands-on (2) -Hello World-
[Python] Web application from 0! Hands-on (3) -API implementation-
FizzBuzz with Python3
Scraping with Python
If you know Python, you can make a web application with Django
The first artificial intelligence. Challenge web output with python. ~ Flask introduction
Let's make a WEB application for phone book with flask Part 1
Statistics with python
Try using the web application framework Flask
Getting Started with Flask with Azure Web Apps
Scraping with Python
Python with Go
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Python x Flask x PyTorch Easy construction of number recognition web application
Getting Started with Python Web Scraping Practice
POST variously with Python and receive with Flask
Page cache in Python + Flask with Flask-Caching
Twilio with Python
Let's make a WEB application for phone book with flask Part 2
Play with 2016-Python
Daemonize a Python web app with Supervisor