Linux:部署基于Python的Web网页(使用的Flask框架)

发布于:2022-10-16 ⋅ 阅读:(697) ⋅ 点赞:(0)

Flask是一个轻量级的基于Python的web框架。

以自制的web网页为例:

一、版本需求

CentOS 7.6

Anaconda3

MySQL8

二、环境部署

安装wget

yum -y install wget

安装bzip2

yum install bzip2

安装Anaconda3

Wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.05-Linux-x86_64.sh --no-check-certificate
bash Anaconda3-2021.05-Linux-x86_64.sh

此时系统会有提示,系统提示命令如下:

Do you accept the license terms? [yes|no] [no] >>>

此时需要选择“yes”,回车后系统继续提示如下:

“Anaconda3 will now be installed into this location: /root/anaconda3
Press ENTER to confirm the location
Press CTRL-C to abort the installation
Or specify a different location below
[/root/anaconda3] >>> 
PREFIX=/root/anaconda3 Unpacking payload ...”
Do you wish the installer to initialize Anaconda3 by running conda init? [yes|no] [no] >>>

此时需要选择“yes”,回车后输入命令

source ~/.bashrc

安装Python的一些包

pip3 install requests
pip3 install flask
pip3 install pymysql

安装MySQL

sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
sudo yum --enablerepo=mysql80-community install mysql-community-server

此时系统会有提示,系统提示命令如下:

安装 3 软件包 (+31 依赖软件包) 升级 ( 1 依赖软件包)
总下载量:504 M Is this ok [y/d/N]:

此时需要选择“y”,回车后系统继续提示:

总计 5.9 MB/s | 504 MB 00:01:25 从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥 导入 GPG key 0x5072E1F5: 用户ID : "MySQL Release Engineering mysql-build@oss.oracle.com" 指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5 软件包 : mysql80-community-release-el7-1.noarch (installed) 来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 是否继续?[y/N]:

此时需要选择“y”,回车后输入命令:

sudo service mysqld start
service mysqld status
grep "A temporary password" /var/log/mysqld.log
mysql_secure_installation

此时系统会有提示,系统提示命令如下:

(base) [root@localhost ~]# grep "A temporary password" /var/log/mysqld.log 2021-10-11T08:36:00.922417Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: sif+s-!(Q4*#【初始密码】 (base) [root@localhost ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: 
The existing password for the user account root has expired. Please set a new password.
New password:

此时自行设置一个密码,本文章以“Root_1026”为例

Re-enter new password:

再输入一遍自己设置的密码,然后系统会有如下提示:

The 'validate_password' component is installed on the server. The subsequent steps will run with the existing configuration of the component. Using existing password for root.
Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) :

此时需要选择“y”,回车后系统继续提示:

New password:

这个新密码就是前面设置的新密码,回车后系统会继续提示:

Re-enter new password:

在输入一遍新密码,然后系统会继续提示如下:

Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : 

此时需要选择“y”,回车后系统继续提示:

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :

此时需要选择“y”,回车后系统继续提示:

Success.
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 

此时需要选择“n”,回车后系统继续提示:

By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 

此时需要选择“y”,回车后系统继续提示:

Dropping test database... Success.
Removing privileges on test database... Success.
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

此时需要选择“y”,回车后系统继续提示:

Success.
All done!

输入以下命令:

mysql -uroot -p
CREATE USER 'guest'@'%' IDENTIFIED BY 'Guest_1026';
GRANT ALL ON *.* TO 'guest'@'%';

到此处环境部署就全部完成,只需要建立数据表,修改python文件的相应位置,例如:


def get_conn():
   conn = pymysql.connect(
       host='111.67.202.138', # MySQL主机地址
       port=3306, # MySQL端口
       user='root', # 用户名
       password='Qyc-1234', # 密码
       db='mydb', # 数据库名
       charset='utf8'
  )
   return conn

 修改get_conn函数,用于连接MySQL,数据表名为data暂不修改。

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001) # host='0.0.0.0'

为使用当前的IP,port可按需修改,访问网站页面应在对应地址访问,如“111.67.202.138:5001”

三、开放端口(使用防火墙)

      开启防火墙

systemctl start firewalld

      开启端口,如5000

firewall-cmd --add-port=5000/tcp --permanent
firewall-cmd --reload

四、运行系统(使用screen可以保证进程持续运行)

       安装screen

yum install screen

       进入app.py(启动网页的程序文件)所在的目录

       创建一个窗口,命名如system

screen -S system

       创建完后会进去窗口,接下来运行app.py

python3 app.py

运行成功的话会显示地址“内网IP:端口”

访问时使用“公网IP:端口”即可。

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到