• 项目上线整体流程


    一、上线前准备

    1.把项目中需要的文件保留住 防止被.gitignore文件忽略导致上传运行失败(Logs做成包__init__)
    2.把测试的dev.py文件复制进prod.py中把里面数据都改成线上的配置即可(Debug=False, Alllwed_Hosts=['*'],数据库设置线上数据库,设置在配置文件中的路径改为线上)
    3.新建一个文件manage用来作为上线的主要文件(里面的Django配置文件设置成线上的配置),线上迁移变成python manage_pro.py makemigrations
    4.讲项目上传到git方便后期好做修改上传于拉取
    5.重点配置后台和前台地址 修改成上线服务器的地址 提交Git
    6.前端项目发送请求地址改为线上
    7.前端项目进行打包 npm run build(会出现一个Dist文件夹)压缩成压缩文件上传至云服务器即可
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    二、阿里云机器购买

    一般的项目都是上传至公司的服务器运行 个人开发可以去购买网络云机器 把自己的项目跑在上面即可
    购买之后机器会得到一个公网Ip地址 任何人可以通过这个IP来访问到你的项目了
    把项目的安全组端口打开(项目常用的端口80808033066379
    • 1
    • 2
    • 3

    在这里插入图片描述

    三、上线架构图

    在这里插入图片描述

    四、安装Git

    方式一
    	yum install git -y
    方式二:# 包含了git和一些别的开发需要软件
    	yum -y groupinstall "Development tools" 
    源码安装python、redis需要有别的依赖支持
    	yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel -y
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    五、云服务器安装MySQL

    1.根路径
    	cd ~  cd
    2.下载Mysql5.7
    	wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
    3.安装mysql5.7
    	yum -y install mysql57-community-release-el7-10.noarch.rpm
    	yum install mysql-community-server --nogpgcheck。# 注意输入Y
    4.启动mysql5.7
    	systemctl start mysqld.service	# 开启
    	systemctl status mysqld.service	# 查看状态
    5.查看默认密码并登录
    	grep "password" /var/log/mysqld.log		# 最后一句
    	mysql -u root -p
    	输入密码
    6.修改密码
    	ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码'
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    六、云服务器安装Redis

    1.根路径
    	cd ~  cd
    2.下载redis-5.0.5(源码包)
    	wget http://download.redis.io/releases/redis-5.0.5.tar.gz
    3.解压安装包
    	tar -xf redis-5.0.5.tar.gz
    4.进入目标文件
    	cd redis-5.0.5
    5.编译环境
    	make
    6.复制环境到指定路径完成安装
    	cp -r /root/redis-5.0.5 /usr/local/redis
    7.配置redis可以后台启动:修改下方内容
    	vim /usr/local/redis/redis.conf
    	修改内容: daemonize yes
    	完成配置保存退出 esc :wq
    8.建立软链接
    	ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server	# 就可以在根路径使用命令
    	ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli
    9.后台运行redis
    	cd /usr/local/redis
    	redis-server ./redis.conf &
    10.测试redis环境
    	redis-cli
    	ctrl+c结束
    11.关闭redis服务
    	pkill -f redis -9
    
    • 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

    七、云服务器安装源码安装Python

    1.源码安装python,依赖一些第三方zlib* libffi-devel
    	yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel  -y
    2.根路径
    	cd ~  cd
    3.下载Python3.8.6
    	wget https://registry.npmmirror.com/-/binary/python/3.8.6/Python-3.8.6.tgz
    4.解压安装包
    	tar -xf Python-3.8.6.tgz
    5.进入目标文件
    	cd Python-3.8.6
    6.配置安装路径 /usr/local/python3
    	./configure --prefix=/usr/local/python38
    7.编译并安装(如果报错说明缺少依赖)
    	make && make install
    	yun install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel  -y
    8.建立软链接
    	ln -s /usr/local/python38/bin/python3 /usr/bin/python3.8
    	ln -s /usr/local/python38/bin/pip3 /usr/bin/python3.8
    9.删除安装包于文件
    	rm -rf Python-3.8.8
    	rm -rf Python-3.8.8.tar.xz
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    八、云服务器安装Uwsgi

    1.根路径
    	cd ~  cd
    2.安装uwsgi
    	pip3.8 install uwsgi
    3.建立软连接
    	ln -s /usr/local/python38/bin/uwsgi /usr/bin/uwsgi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    九、安装虚拟环境

    1.安装依赖
    	pip3.8 install virtualenv
    	pip3.8 install -U virtualenvwrapper -i https://pypi.douban.com/simple/
    	pip3.8 install virtualenvwrapper
    	
    	如果出现报错 敲以下命令
    	python3.8 -m pip install --upgrade pip
    	python3.8 -m pip install --upgrade setuptools
    	pip3.8 install pbr
    2.建立软连接
    	ln -s /usr/local/python38/bin/virtualenv /usr/bin/virtualenv
    3.配置虚拟环境
    	vim ~/.bash_profile		# 复制以下内容
    	VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.8
    	source /usr/local/python38/bin/virtualenvwrapper.sh
    4.保存退出
    	esc :wq
    5.更新配置文件内容
    	source ~/.bash_profile
    6.创建虚拟环境
    	mkvirtualenv -p python3.8 luffy
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    十、安装nginx

    1.根路径
    	cd ~  cd
    2.下载nginx1.13.7
    	wget http://nginx.org/download/nginx-1.13.7.tar.gz
    3.解压压缩包
    	 tar -xf nginx-1.13.7.tar.gz
    4.进入目标文件
    	cd nginx-1.13.7
    5.配置安装路径
    	./configure --prefix=/usr/local/nginx
    6.编译并安装
    	make && make install
    7.建立软链接
    	ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
    8.删除安装与文件
    	cd ~
    	rm -rf nginx-1.13.7
    	rm -rf nginx-1.13.7.tar.xz
    9.测试Nginx环境 
    	nginx			# 启动
    	nginx -s stop 	# 停止
    10.起进程
    	netstat -nlp | grep 80
    11.查看进程
    	ps aux | grep 80
    
    • 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

    十一、前端项目部署

    1.把前端build的文件上传至服务器
    2.安装软件
    	yum install -y unzip
    	yum install lrzsz -y
    3.解压文件
    	unzip dist.zip
    4.移动并重命名
    	mv ~/dist /home/html
    5.备份nginx配置 更新配置 定制nginx做请求转发 负载均衡 静态文件代理
    	cd /usr/local/nginx/conf
    	mv nginx.cong nginx.conf.bak	# 备份
    	vim nginx.conf
    	
    	events {
        worker_connections  1024;
    	}
    	http {
    	    include       mime.types;
    	    default_type  application/octet-stream;
    	    sendfile        on;
    	    server {
    	        listen 80;
    	        server_name  127.0.0.1;
    	        charset utf-8;
    	        location / {
    	            root /home/html;
    	            index index.html;
    	        }
    	    }
    	} 
    6.重新加载配置文件
    	nginx -s reload
    7.开启nginx与停止nginx
    	nginx  nginx -s stop
    
    • 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

    十二、后端项目部署

    1)Git拉取最新代码安装依赖

    1. 在自己的项目里面把依赖 生成一个文件 上传push
    	pip freeze > requirements.txt
    	pip add.
    	pip commit -m '提交'
    	pip push origin master
    2.克隆文件到云机器
    	git clone 库地址
    3.拉取项目
    	git pull origin master
    4.切换到luffy虚拟环境
    	workon luffy
    5.安装依赖
    	pip install -r requirements.txt		# 如果出现安装不了的 编辑文件注释掉
    6.安装mysqlclient
    	yum install mysql-devel -y
    	yum install python-devel -y
    	rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
    	pip install mysqlclient
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2)配置数据库

    1.管理员链接数据库
    	mysql -u root -p
    2.创建数据库
    	create database luffy default charset=utf8;
    3.设置权限账号密码:账号密码要与项目中配置的一致
    	grant all privileges on 用户名称.* to '数据库名称'@'%' identified by '数据库密码';
    	grant all privileges on 用户名称.* to '数据库名称'@'localhost' identified by '数据库密码';
    	flush privileges # 刷新权限
    4.退出mysql
    	quit;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3)迁移数据库

    1. python manage_pro.py makemigrations
    2. python manage_pro.py migrate
    
    • 1
    • 2

    4)使用Uwsgi启动Django

    1.使用Uwsgi跑之前运行Django
    	python manage_pro.py runserver 0.0.0.0:8000 	# 这个时候使用公网就可以访问到了
    2.进行Uwsgi服务配置
    	vim ./luffyapi.xml
    		<uwsgi>
    		   <socket>127.0.0.1:8888</socket>
    		   <chdir>/home/luffy_api/</chdir>
    		   <module>luffy_api.wsgi</module>
    		   <processes>4</processes>
    		   <daemonize>uwsgi.log</daemonize>
    		</uwsgi>
    3.启动uwsgi
    	uwsgi -x ./luffyapi.xml
    4.查看uwsgi进程
    	ps aux | grep uwsgi
    5.配置nginx 把8080的动态请求转发给8888端口
    	vim /usr/local/nginx/conf/nginx.conf
    		events {
    		    worker_connections  1024;
    		}
    		http {
    		    include       mime.types;
    		    default_type  application/octet-stream;
    		    sendfile        on;
    		    server {
    		        listen 80;
    		        server_name  127.0.0.1;
    		        charset utf-8;
    		        location / {
    		            root /home/html;
    		            index index.html;
    		        }
    		    }
    		    # 新增的server
    		    server {
    		        listen 8080;
    		        server_name  127.0.0.1;
    		        charset utf-8;
    		        location / {
    		           include uwsgi_params;
    		           uwsgi_pass 127.0.0.1:8888;
    		           uwsgi_param UWSGI_SCRIPT luffy_api.wsgi;
    		           uwsgi_param UWSGI_CHDIR /home/luffy_api/;
    		        }
    		    }
    		} 
    6.重启nginx
    	nginx -s reload
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    十三、后台样式处理

    1. vim /usr/local/nginx/conf/nginx.conf
    	
    		events {
    		    worker_connections  1024;
    		}
    		http {
    		    include       mime.types;
    		    default_type  application/octet-stream;
    		    sendfile        on;
    		    server {
    		        listen 80;
    		        server_name  127.0.0.1;
    		        charset utf-8;
    		        location / {
    		            root /home/html;
    		            index index.html;
    		            try_files $uri $uri/ /index.html; # 解决vue路由问题
    		        }
    		    }
    		    # 新增的server
    		    server {
    		        listen 8080;
    		        server_name  127.0.0.1;
    		        charset utf-8;
    		        location / {
    		           include uwsgi_params;
    		           uwsgi_pass 127.0.0.1:8888;
    		           uwsgi_param UWSGI_SCRIPT luffy_api.wsgi;
    		           uwsgi_param UWSGI_CHDIR /home/luffy_api/;
    		        }
    		    }
    		} 
    2 .重启nginx
    	nginx -s reload
    
    • 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

    十四、路飞后台管理样式处理

    1.编辑线上配置文件
    	vim /home/project/luffyapi/luffyapi/settings/pro.py
    2.修改static配置新增STATIC_ROOT、STATICFILES_DIRS
    	STATIC_URL = '/static/'
    	STATIC_ROOT = '/home/luffy_api/luffy_api/static'  
    	STATICFILES_DIRS = (os.path.join(BASE_DIR, "../static"),)
    	esc
    	:wq
    3.项目目录下没有 static 文件夹需要新建
    	mkdir /home/luffy_api/static
    	mkdir /home/luffy_api/luffyapi/static		# 两个luffy都创建
    4.完成静态文件迁移
    	python /home/luffy_api/manage_pro.py collectstatic
    5.修改nginx配置
    	vim /usr/local/nginx/conf/nginx.conf
    	
    	events {
    	    worker_connections  1024;
    	}
    	http {
    	    include       mime.types;
    	    default_type  application/octet-stream;
    	    sendfile        on;
    	    server {
    	        listen 80;
    	        server_name  127.0.0.1;
    	        charset utf-8;
    	        location / {
    	            root /home/html;
    	            index index.html;
    	            try_files $uri $uri/ /index.html; # 解决vue路由问题
    	        }
    	    }
    	    server {
    	        listen 8080;
    	        server_name  127.0.0.1;
    	        charset utf-8;
    	        location / {
    	           include uwsgi_params;
    	           uwsgi_pass 127.0.0.1:8888;
    	           uwsgi_param UWSGI_SCRIPT luffyapi.wsgi;
    	           uwsgi_param UWSGI_CHDIR /luffy_api/luffy_api/;
    	        }
    	        location /static {
    	            alias /home/luffy_api/luffy_api/static;		# 添加了这一句
    	        }
    	    }
    	
    	}
    	
    6.重启nginx
    	nginx -s reload
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
  • 相关阅读:
    Vue源码分析(高阶函数)
    计算机毕业设计ssm青年志愿者社团管理36uiu系统+程序+源码+lw+远程部署
    C++之std::atomic解决多线程7个问题(二百四)
    六月集训(第27天) —— 图
    【Day-35慢就是快】代码随想录-二叉树-二叉搜索树的最近公共祖先
    安卓抓jdwskey
    Spring 中如何使用SpEL表达式语言呢?
    【面试】个人宝典 查缺补漏
    如何做好性能压测:压测环境的设计和搭建
    《网络协议》02. 物理层 · 数据链路层 · 网络层
  • 原文地址:https://blog.csdn.net/MeiJin_/article/details/128010946