ubuntu-20.04环境搭建

安装下面的顺序操作就可以了

更换源

备份 /etc/apt/sources.list

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup

输入

1
sudo gedit /etc/apt/sources.list

将下面的源复制到文件中,注销一下在执行,问题少些。

1
2
sudo apt-get update
sudo apt-get upgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security multiverse

安装 ssh 方便后面操作

1
2
sudo apt install ssh
service ssh status

By default you will not be able to login using SSH remote connection as a root user.

Allow SSH root login on Ubuntu 18.04

  1. Set Root Password
1
sudo passwd
  1. Enable SSH root login
1
2
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sudo service ssh restart

安装 mysql

website

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# install
sudo apt install mysql-server

# 错误日志路径
/var/log/mysql/error.log

# Configuring MySQL
sudo mysql_secure_installation
# 根据提示设置即可

# Adjusting User Authentication and Privileges
# 如果想要远程登录 root 用户,需要配置认证
# 进入 mysql 命令行
sudo mysql
# 切换认证插件
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'mysql';
FLUSH PRIVILEGES;
exit
# 远程登录设置

# 进入命令行
mysql -u root -p
# 选择数据库
use mysql;
update user set host = '%' where user = 'root';
# 查看修改结果
select host, user from user;
# 现在应该可以远程连接了

# 需要把mysql 默认的配置文件是只允许本地连接的,所以需要先改改
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# 找到 bind-address = 127.0.0.1 注释掉
# 重启服务
service mysql restart

查看 python 版本

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04

1
python3 -V

安装虚拟环境

1
2
3
4
5
6
7
8
9
10
sudo apt-get install python3-venv
python3 -m venv books
# 激活虚拟环境
source books/bin/activate

# 在虚拟环境里面安装gunicorn和flask
pip install gunicorn flask

# 去激活
deactivate

验证是否成功

1
nano ~/test.py
1
2
3
4
5
6
7
8
9
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"

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

打开防火墙

sudo ufw allow 5000

运行服务
python3 test.py

打开浏览器
http://your_server_ip:5000

坚持原创技术分享,您的支持将鼓励我继续创作!