文章目錄
- 1、簡介
- 2、安裝
-
- 2.1 安裝Centos
- 2.2 安裝python
- 2.3 安裝虛拟環境
- 2.4 修改國内源
- 2.5 安裝flask庫
- 3、測試
-
- 3.1 flask官方例子
- 結語
1、簡介
CentOS 大家應該很熟悉了,英文全稱:Community Enterprise Operating System(社群企業作業系統),是 RHEL(紅帽企業 Linux) 的免費發行版本,也是目前市面上用得最多的最火的商用 Linux 發行版。
CentOS Stream 是 RHEL(紅帽企業 Linux) 搞的一個滾動更新的 Linux 發行版,即沒有像 CentOS Linux 6/7/8 這樣的大版本了,以後都是滾動的小版本更新,它會優先使用各種新特性和新核心,待穩定之後再釋出 RHEL 版本。
CentOS Linux 發行版是一個穩定、可預測、可管理和 源自紅帽企業 Linux 源代碼的可重制平台 (RHEL)。自 2004 年 3 月起,CentOS Linux 成為社群支援的發行版 來源于紅帽免費向公衆提供的來源。是以,CentOS Linux的目标是在功能上與RHEL相容。我們主要更換包裝 删除上遊供應商品牌和插圖。CentOS Linux是免費且可以免費重新分發的。
2、安裝
2.1 安裝Centos
https://www.centos.org/
Centos是一個主流的Linux作業系統,免費穩定;
首先我們去Centos官網下載下傳新版本的Centos:
由于是國外線路比較慢,我們一般是建議下載下傳内部版本。
提供有DVD安裝版本,完整版以及最低版本;我們為了友善在虛拟機中安裝,需要下載下傳DVD版本,
推薦阿裡雲可以下載下傳:https://http://mirrors.aliyun.com/centos/7/isos/x86_64/下載下傳那個DVD版本即可
http://mirrors.nju.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirrors.qlu.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirrors.cqu.edu.cn/CentOS/7.9.2009/isos/x86_64/
http://ftp.sjtu.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirrors.bfsu.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirrors.163.com/centos/7.9.2009/isos/x86_64/
http://mirrors.bupt.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirrors.njupt.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirrors.ustc.edu.cn/centos/7.9.2009/isos/x86_64/
http://mirror.lzu.edu.cn/centos/7.9.2009/isos/x86_64/
- 可擷取系統核心版本
- 擷取系統版本資訊
cat /proc/version
- 擷取系統發行版資訊
cat /etc/issue
cat /etc/redhat-release
2.2 安裝python
Centos7預設自帶了Python2.7版本。
# python的目前版本
python -V
# python的位置
whereis python
- python在 /usr/bin目錄
cd /usr/bin/
ll python*
python指向的是python2,python2指向的是python2.7,是以我們可以裝個python3,然後将python指向python3,然後python2指向python2.7,那麼兩個版本的python就能共存了。
運作了如上指令以後,就安裝了編譯python3所用到的相關依賴:
預設的,centos7也沒有安裝pip。
#運作這個指令添加epel擴充源
yum -y install epel-release
yum -y install libffi-devel
#安裝pip
yum install python-pip
- 用pip裝wget
- 用wget下載下傳python3的源碼包
wget http://npm.taobao.org/mirrors/python/3.8.10/Python-3.8.10.tar.xz
- 編譯python3源碼包
#解壓
xz -d Python-3.8.10.tar.xz
tar -xf Python-3.8.10.tar
#進入解壓後的目錄,依次執行下面指令進行手動編譯
cd Python-3.8.10
./configure prefix=/usr/local/python3
make && make install
# 如果出現can't decompress data; zlib not available這個錯誤,則需要安裝相關庫
#安裝依賴zlib、zlib-devel
yum install zlib zlib
yum install zlib zlib-devel
- 添加軟連接配接
#添加python3的軟連結
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
#添加 pip3 的軟連結
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
python3 -V
pip3 -V
這裡沒有連結到python上,是因為yum要用到python2才能執行,是以現在輸入python的話還是會進入python2.7,輸入python3才會進入python3.8。如果執意想要連結到python的話,就得修改一下yum的配置:
#将原來的連結備份
mv /usr/bin/python /usr/bin/python.bak
#添加python3的軟連結
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python
#測試是否安裝成功了
python -V
更改yum配置,因為其要用到python2才能執行,否則會導緻yum不能正常使用。
vi /usr/bin/yum
把 #! /usr/bin/python 修改為 #! /usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
把 #! /usr/bin/python 修改為 #! /usr/bin/python2
2.3 安裝虛拟環境
python -m venv myvenv
2.4 修改國内源
-
臨時使用pip鏡像源:
可以在使用pip的時候加參數-i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
-
永久修改pip鏡像源:
Linux下,修改 ~/.pip/pip.conf (沒有就建立一個檔案夾及檔案。檔案夾要加“.”,表示是隐藏檔案夾)
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
2.5 安裝flask庫
pip安裝flask庫。
pip3 install flask
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
pip安裝pyecharts庫。
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
pip安裝pyinstaller庫。
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
3、測試
3.1 flask官方例子
- test.py
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World,愛看書的小沐!'
@app.route('/test', methods=['GET', 'POST'])
def train_status():
if request.method == 'GET':
return jsonify({'code': 200, 'status': 'false', 'msg': 'hello'})
else:
return jsonify({'code': 500, 'msg': '不支援該請求'})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080)
python test.py
CentOS防火牆操作:開啟端口、開啟、關閉、配置。
- 基本使用
啟動: systemctl start firewalld
關閉: systemctl stop firewalld
檢視狀态: systemctl status firewalld
開機禁用 : systemctl disable firewalld
開機啟用 : systemctl enable firewalld
- 配置firewalld-cmd
檢視版本: firewall-cmd --version
檢視幫助: firewall-cmd --help
顯示狀态: firewall-cmd --state
檢視所有打開的端口: firewall-cmd --zone=public --list-ports
更新防火牆規則: firewall-cmd --reload
檢視區域資訊: firewall-cmd --get-active-zones
檢視指定接口所屬區域: firewall-cmd --get-zone-of-interface=eth0
拒絕所有包:firewall-cmd --panic-on
取消拒絕狀态: firewall-cmd --panic-off
檢視是否拒絕: firewall-cmd --query-panic
- 開啟防火牆端口
# –permanent永久生效,沒有此參數重新開機後失效
firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-port=2888/tcp --permanent
sudo firewall-cmd --add-port=3888/tcp --permanent
- 重新開機防火牆
- 檢視開放端口号
firewall-cmd --list-all
在設定了防火牆之後,浏覽器終于可以成功通路flask伺服器的頁面,如下:
http://192.168.136.129:8080
http://192.168.136.129:8080/test
結語
如果您覺得該方法或代碼有一點點用處,可以給作者點個贊,或打賞杯咖啡;
╮( ̄▽ ̄)╭
如果您感覺方法或代碼不咋地
//(ㄒoㄒ)//
,就在評論處留言,作者繼續改進;
o_O???
如果您需要相關功能的代碼定制化開發,可以留言私信作者;
(✿◡‿◡)
感謝各位大佬童鞋們的支援!
( ´ ▽´ )ノ ( ´ ▽´)っ!!!