Ubuntu上部署Flask+MySQL项目

发布于:2025-03-15 ⋅ 阅读:(11) ⋅ 点赞:(0)

一、服务器安装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

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

  • 启动
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
  • 开机启动
systemctl enable nginx
systemctl start nginx

五、uwsgi开机自启动

vi /etc/systemd/system/uwsgi.service
	[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
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

七、防火墙开启端口

sudo ufw allow 5000
sudo ufw allow 8080

网站公告

今日签到

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