Python正课154 —— 2次重置系统后的上线(坎坷的luffy上线历程)
本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/13408104.html
前面2次翻车了,现在推到重来!!!
当前配置:
系统:CentOS-7.6(64bit)
CPU:E5-2682v4 单核
内存:2GB
硬盘:40G 机械硬盘
带宽:1M
一:软件包安装更新
1.更新系统软件包(耗时3m)
yum update -y
2.安装软件管理包和可能使用的依赖(耗时2m)
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
yum -y groupinstall "Development tools"
二:安装MySQL-5.7
1.下载(耗时8s)
wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2.安装(耗时1m)
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
3.启动mysql57并查看启动状态(耗时5s)
systemctl start mysqld.service
systemctl status mysqld.service
4.查看默认密码,并登陆
grep "password" /var/log/mysqld.log
mysql -uroot -p
5.修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Luffy1234?';
6.数据库设置
# 进入数据库
mysql -uroot -pXuexianqi12,.
# 创建数据库luffy
create database luffy default charset=utf8;
# 置权限账号密码:账号密码要与项目中配置的一致
grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy1234?';
grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy1234?';
flush privileges;
三:安装Redis-5.0.5
1.下载(耗时2m)
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
2.解压(耗时1s)
tar -xf redis-5.0.5.tar.gz
3.进入解压后的文件夹
cd redis-5.0.5
4.编译环境(耗时2m)
make
5.复制环境到指定路径完成安装
cp -r ~/redis-5.0.5 /usr/local/redis
6.配置redis可以后台启动:修改下方内容
vim /usr/local/redis/redis.conf
daemonize yes
7.建立软链接
ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli
8.后台运行Redis
cd /usr/local/redis
redis-server ./redis.conf &
按Ctrl+C
即可停止
9.测试redis环境
redis-cli
10.关闭redis服务
pkill -f redis -9
四:安装Python-3.6
1.下载(耗时30m.....???)
wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz
然后.....我选择本地下载好安装好,然后拖进FinalShell传上去
2.解压(耗时3s)
tar -xf Python-3.6.7.tar.xz
3.进入目标文件
cd Python-3.6.7
4.配置安装路径:/usr/local/python3
(耗时27s)
./configure --prefix=/usr/local/python3
5.编译并安装(耗时3m)
make && sudo make install
6.建立软连接:终端命令 python3,pip3
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3
五:安装虚拟环境
1.安装依赖(耗时15s)
# 先升级一下pip和setuptools
pip3 install --upgrade pip
pip3 install --upgrade setuptools
pip3 install virtualenv
pip3 install virtualenvwrapper
2.建立虚拟环境软链接
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
3.配置虚拟环境
vim ~/.bash_profile
添加下面内容
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh
退出编辑状态
Esc
保存修改并退出
:wq
或者
Shift + Z + Z
4.更新配置文件内容
source ~/.bash_profile
5.虚拟环境默认根目录:~/.virtualenvs
六:安装Nginx
0.什么是Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器
Apache和Nginx最核心的区别在于 apache 是同步多进程模型,一个连接对应一个进程;
而 nginx 是异步的,多个连接(万级别)可以对应一个进程
作用:
http请求转发
反向代理服务器
负载均衡
动静分离
1.下载(耗时70s)
wget http://nginx.org/download/nginx-1.13.7.tar.gz
2.解压(耗时1s)
tar -xf nginx-1.13.7.tar.gz
3.进入目标文件
cd nginx-1.13.7
4.配置安装路径:/usr/local/nginx(耗时2s)
./configure --prefix=/usr/local/nginx
5.编译并安装(耗时30s)
make && sudo make install
6.建立软链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
7.测试Nginx环境,服务器运行nginx,本地访问服务器ip
nginx
>: 服务器绑定的域名 或 ip:80
8.Nginx命令
# 启动
nginx
# 关闭nginx
nginx -s stop
# 重启nginx
nginx -s reload
# 查看端口,强行关闭
ps -aux|grep nginx
kill <pid:进程编号>
七:前端Vue项目上线
1.进入luffycity
目录,将项目打包
cnpm run build
2.生成一个dist
文件夹,用finalshell
上传到服务器的\root目录下
3.移动并重命名为html
mv ~/dist /home/html
4.去向Nginx配置目录
cd /usr/local/nginx/conf
5.备份配置
mv nginx.conf nginx.conf.bak
6.创建新的配置文件,完全更新配置:填入下方内容
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; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html访问路径
index index.html; # html文件名称
try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
}
}
}
八:后端luffyapi项目上线
1.将luffyapi
下的settings
下的dev.py
复制命名为pro.py
修改如下
DEBUG = False
# 运行的host,服务端的地址,买的服务器的公网ip,或者直接填写*
ALLOWED_HOSTS = ['*']
# 配置数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'luffy',
'USER': 'luffy',
'PASSWORD': 'Luffy1234?',
'HOST': '127.0.0.1',
'PORT': 3306
},
}
# 后台基URL
BASE_URL = 'http://47.98.233.34:8000'
# 前台基URL
LUFFY_URL = 'http://47.98.233.34'
2.将manage.py
复制命名为manage_pro.py
修改如下
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.pro')
3.将apps
下所有app的migrations
下的除了__init__.py
的其他文件都删除
4.导出项目依赖
# 1.进入项目根目录,导出
pip3 freeze > requirements.txt
# 2.把xadmin=右边替换
xadmin=https://codeload.github.com/sshwsfc/xadmin/zip/django2
5.创建存放后台项目的目录
mkdir /home/project
6.进入后台项目管理目录
cd /home/project
7.传输后端项目
将项目提交到git仓库,然后去服务器上拉下来,或者直接finalshell上传
六:配置虚拟环境
0.切换到后端项目下
cd /home/project/luffyapi
1.先在非虚拟环境下安装uwsgi(耗时5s)
# 在真实环境下安装
pip3 install uwsgi
# 建立软连接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
2.创建线上luffy项目虚拟环境(耗时5s)
mkvirtualenv luffy
3.切换到新建的虚拟环境
workon luffy
4.安装uwsgi(耗时15s)
# 先升级一下pip和setuptools
pip3 install --upgrade pip
pip3 install --upgrade setuptools
# 再安装uwsgi
pip3 install uwsgi
5.安装依赖(耗时40s)
pip3 install -r requirements.txt
七:完成uwsgi与nginx后台项目配置
1.进行uwsgi服务配置,内容如下
vim /home/project/luffyapi/luffyapi.xml
<uwsgi>
<socket>127.0.0.1:8808</socket> <!-- 内部端口,自定义 -->
<chdir>/home/project/luffyapi/</chdir> <!-- 项目路径 -->
<module>luffyapi.wsgi</module> <!-- luffyapi为wsgi.py所在目录名-->
<processes>4</processes> <!-- 进程数 -->
<daemonize>uwsgi.log</daemonize> <!-- 日志文件 -->
</uwsgi>
2.去向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; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html访问路径
index index.html; # html文件名称
try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
}
}
# 新增的server
server {
listen 8000;
server_name 127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi里配置的一样
uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目录名+.wsgi
uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 项目路径
}
}
}
八:完成项目的数据库迁移
1.切换到后端项目
cd /home/project/luffyapi/
2.数据迁移
python3 manage_pro.py makemigrations
python3 manage_pro.py migrate
3.创建超级用户
python3 manage_pro.py createsuperuser
4.录入课程数据
-- 老师表
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (1, 1, 1, 0, '2019-07-14 13:44:19.661327', '2019-07-14 13:46:54.246271', 'Alex', 1, '老男孩Python教学总监', '金角大王', 'teacher/alex_icon.png', '老男孩教育CTO & CO-FOUNDER 国内知名PYTHON语言推广者 51CTO学院2016\2017年度最受学员喜爱10大讲师之一 多款开源软件作者 曾任职公安部、飞信、中金公司、NOKIA中国研究院、华尔街英语、ADVENT、汽车之家等公司');
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (2, 2, 1, 0, '2019-07-14 13:45:25.092902', '2019-07-14 13:45:25.092936', 'Mjj', 0, '前美团前端项目组架构师', NULL, 'teacher/mjj_icon.png', '是马JJ老师, 一个集美貌与才华于一身的男人,搞过几年IOS,又转了前端开发几年,曾就职于美团网任高级前端开发,后来因为不同意王兴(美团老板)的战略布局而出家做老师去了,有丰富的教学经验,开起车来也毫不含糊。一直专注在前端的前沿技术领域。同时,爱好抽烟、喝酒、烫头(锡纸烫)。 我的最爱是前端,因为前端妹子多。');
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (3, 3, 1, 0, '2019-07-14 13:46:21.997846', '2019-07-14 13:46:21.997880', 'Lyy', 0, '老男孩Linux学科带头人', NULL, 'teacher/lyy_icon.png', 'Linux运维技术专家,老男孩Linux金牌讲师,讲课风趣幽默、深入浅出、声音洪亮到爆炸');
-- 课程分类表
INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (1, 1, 1, 0, '2019-07-14 13:40:58.690413', '2019-07-14 13:40:58.690477', 'Python');
INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (2, 2, 1, 0, '2019-07-14 13:41:08.249735', '2019-07-14 13:41:08.249817', 'Linux');
-- 课程表数据
INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (1, 1, 1, 0, '2019-07-14 13:54:33.095201', '2019-07-14 13:54:33.095238', 'Python开发21天入门', 'courses/alex_python.png', 0, 'Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土', 0, '2019-07-14', 21, '', 0, 231, 120, 120, 0.00, 1, 1);
INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (2, 2, 1, 0, '2019-07-14 13:56:05.051103', '2019-07-14 13:56:05.051142', 'Python项目实战', 'courses/mjj_python.png', 0, '', 1, '2019-07-14', 30, '', 0, 340, 120, 120, 99.00, 1, 2);
INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (3, 3, 1, 0, '2019-07-14 13:57:21.190053', '2019-07-14 13:57:21.190095', 'Linux系统基础5周入门精讲', 'courses/lyy_linux.png', 0, '', 0, '2019-07-14', 25, '', 0, 219, 100, 100, 39.00, 2, 3);
-- 章节表数据
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (1, 1, 1, 0, '2019-07-14 13:58:34.867005', '2019-07-14 14:00:58.276541', 1, '计算机原理', '', '2019-07-14', 1);
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (2, 2, 1, 0, '2019-07-14 13:58:48.051543', '2019-07-14 14:01:22.024206', 2, '环境搭建', '', '2019-07-14', 1);
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (3, 3, 1, 0, '2019-07-14 13:59:09.878183', '2019-07-14 14:01:40.048608', 1, '项目创建', '', '2019-07-14', 2);
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (4, 4, 1, 0, '2019-07-14 13:59:37.448626', '2019-07-14 14:01:58.709652', 1, 'Linux环境创建', '', '2019-07-14', 3);
-- 课时表数据
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (1, 1, 0, '2019-07-14 14:02:33.779098', '2019-07-14 14:02:33.779135', '计算机原理上', 1, 2, NULL, NULL, '2019-07-14 14:02:33.779193', 1, 1);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (2, 1, 0, '2019-07-14 14:02:56.657134', '2019-07-14 14:02:56.657173', '计算机原理下', 2, 2, NULL, NULL, '2019-07-14 14:02:56.657227', 1, 1);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (3, 1, 0, '2019-07-14 14:03:20.493324', '2019-07-14 14:03:52.329394', '环境搭建上', 1, 2, NULL, NULL, '2019-07-14 14:03:20.493420', 0, 2);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (4, 1, 0, '2019-07-14 14:03:36.472742', '2019-07-14 14:03:36.472779', '环境搭建下', 2, 2, NULL, NULL, '2019-07-14 14:03:36.472831', 0, 2);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (5, 1, 0, '2019-07-14 14:04:19.338153', '2019-07-14 14:04:19.338192', 'web项目的创建', 1, 2, NULL, NULL, '2019-07-14 14:04:19.338252', 1, 3);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (6, 1, 0, '2019-07-14 14:04:52.895855', '2019-07-14 14:04:52.895890', 'Linux的环境搭建', 1, 2, NULL, NULL, '2019-07-14 14:04:52.895942', 1, 4);
九:静态文件配置
1.编辑后端配置文件
vim /home/project/luffyapi/luffyapi/settings/pro.py
2.修改static配置,新增STATIC_ROOT、STATICFILES_DIRS
STATIC_URL = '/static/'
STATIC_ROOT = '/home/project/luffyapi/luffyapi/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
3.迁移静态样式(耗时1s)
mkdir /home/project/luffyapi/luffyapi/static
python3 /home/project/luffyapi/manage_pro.py collectstatic
4.Nginx配置静态路径
# 修改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; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html访问路径
index index.html; # html文件名称
try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
}
}
server {
listen 8000;
server_name 127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi里配置的一样
uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目录名+.wsgi
uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 项目路径
}
# 新增的配置静态文件
location /static {
alias /home/project/luffyapi/luffyapi/static;
}
}
}
十:开放端口
1.去服务器的 “网络与安全” - “安全组”添加如下端口
十一:启动服务
1.关闭 uwsgi
pkill -f uwsgi -9
2.启动uwsgi
uwsgi -x /home/project/luffyapi/luffyapi.xml
3.关闭 nginx
nginx -s stop
4.启动 nginx
nginx