Ubuntu上部署Flask+MySQL项目

一、服务器安装python环境

1、安装gcc(Ubuntu默认已安装)

2、安装python源码

wget https://www.python.org/ftp/python/3.13.2/Python-3.13.2.tar.xz

3、安装Python依赖库

4、配置python豆瓣源

二、服务器安装虚拟环境

1、安装virtualenv

pip3.10 install virtualenv

2、创建虚拟环境

mkdir -p /usr/data/envs

mkdir -p virtualenv /usr/data/envs/nb --python=python3.10

3、激活虚拟环境

source /usr/data/envs/nb/bin/activate

三、uwsgi环境

1、安装

source /usr/data/envs/nb/bin/activate

pip install uwsgi

2、基于uwsgi运行flask项目

cd 项目目录

-命令行的方式

uwsgi --http :8080 --wsgi-file app.py --callable app

-配置文件的方式(推荐)

创建nb_uwsgi.ini文件

uwsgi

socket = 127.0.0.1:8001

chdir=项目目录

wsgi-file = app.py

callable = app

processes = 2

virtualenv = /usr/data/envs/nb

复制代码
启动进程
source /usr/data/envs/nb/bin/activate
uwsgi --ini nb_uwsgi.ini

关闭进程
ps -ef | grep nb_uwsgi
ps -ef | grep 进程id

四、Nginx环境

1、安装ngix

sudo apt update

sudo apt install nginx

2、修改配置文件

-普通请求 ->8081端口

-/static ->项目静态资源目录

Nginx的默认配置文件路径 /etc/nginx/nginx.conf

c 复制代码
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	upstream flask {
		server 127.0.0.1:8001;
	}

	server {
		listen	80:
		listen	[::]:80;
	
		location /static {
			alias /root/zjbj_revit_family/EQPlatformServer/static;
		}

		location / {
			uwsgi_pass	falsk;
			include		uwsgi_params;
		}
	}

}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
#
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

3、启动Nginx

  • 启动
bash 复制代码
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
  • 开机启动
bash 复制代码
systemctl enable nginx
systemctl start nginx

五、uwsgi开机自启动

bash 复制代码
vi /etc/systemd/system/uwsgi.service
bash 复制代码
	[Unit]
	Description=uWSGI instance to serve myapp
	After=network.target
	
	[Service]
	User=root
	Group=root
	WorkingDirectory=/root/zjbj_revit_family/EQPlatformServer
	Environment="PATH=/usr/data/envs/nb/bin"
	ExecStart=/usr/data/envs/nb/bin/uwsgi --ini /root/zjbj_revit_family/EQPlatformServer/nb_uwsgi.ini
	 
	[Install]
	WantedBy=multi-user.target
bash 复制代码
chmod +x /etc/systemd/system/uwsgi.service

sudo systemctl daemon-reload
sudo systemctl enable uwsgi.service
sudo systemctl start uwsgi.service

六、安装MySQL

1、安装MySQL服务

sudo apt update

sudo apt install mysql-server

检查服务状态: sudo systemctl status mysql

创建数据库

CREATE DATABASE IF NOT EXISTS eqplatform CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci

2、授权

复制代码
   创建用户
CREATE USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY 'Password123!';
	授权
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;
	刷新权限
FLUSH PRIVILEGES;

3、测试:

-远程测试: 配置 MySQL 以允许远程连接

编辑MySQL的配置文件/etc/mysql/mysql.conf.d/mysqld.cnf(或相应的配置文件,具体取决于你的MySQL版本和安装方式),找到bind-address行,并将其值改为0.0.0.0以允许任何地址的连接,或者指定一个特定的IP地址。

4、启动MySQL

5、连接MySQL

七、防火墙开启端口

bash 复制代码
sudo ufw allow 5000
sudo ufw allow 8080
相关推荐
invicinble17 小时前
对于Mysql深入理解
数据库·mysql
霖霖总总19 小时前
[小技巧56]深入理解 MySQL 聚簇索引与非聚簇索引:原理、差异与实践
数据库·mysql
伐尘19 小时前
【MySQL】间隙锁 与 排他锁 的区别
数据库·mysql
bjxiaxueliang20 小时前
一文解决蓝牙连接难题:Ubuntu命令行蓝牙强制配对
linux·ubuntu·蓝牙连接命令
Will_Ye20 小时前
Ubuntu22.04 蓝牙设备快速自动连接
ubuntu·bluetoothctl
快乐非自愿21 小时前
【面试题】MySQL 的索引类型有哪些?
数据库·mysql·面试
霖霖总总21 小时前
[小技巧55]深入解析数据库日志机制:逻辑日志、物理日志与物理逻辑日志在 MySQL InnoDB 中的实现
数据库·mysql
lihe7581 天前
每天凌晨3点自动同步UBUNTU源
linux·运维·ubuntu·本地源
zbliquan1 天前
SS928v100远程ubuntu交叉编译开发环境搭建
linux·运维·ubuntu
luoluoal1 天前
基于python的人脸识别的酒店客房入侵检测系统(源码+文档)
python·mysql·django·毕业设计·源码