项目上线
|
1
2
|
# 购买阿里云服务器# 短期或是测试使用,创建 按量收费 服务器,可以随时删除,删除后不再计费,但要保证账户余额100元以上 |
连接服务器
|
1
2
3
4
5
|
1)账号>: ssh root@39.98.144.2212)密码>: ******** |
服务器命令
管理员权限
|
1
2
|
1)以下所有的服务器命令均可以在管理员权限下执行>: sudo 命令 |
配置终端
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1)编辑配置文件>: vim ~/.bash_profile2)将原来内容全部删除掉>: ggdG3)进入编辑状态:填入下方两行>: iexport PATH=$PATH:$HOME/binPS1='Path:\w\n>:'4)退出编辑状态>: esc5)保存修改并退出>: :wq6)生效配置>: source ~/.bash_profile |
重要
更新系统软件包
|
1
|
>: yum update -y |
安装软件管理包和可能使用的依赖
|
1
2
|
>: yum -y groupinstall "Development tools">: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel |
安装Mysql
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
1)前往用户根目录>: cd ~2)下载mysql57>: wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm也可以本地上传,这条命令要在本地终端上执行>: scp -r C:\Users\dell\Desktop\pkg\mysql57-community-release-el7-10.noarch.rpm root@39.98.144.221:~3)安装mysql57>: yum -y install mysql57-community-release-el7-10.noarch.rpm>: yum -y install mysql-community-server4)启动mysql57并查看启动状态>: systemctl start mysqld.service>: systemctl status mysqld.service5)查看默认密码并登录>: grep "password" /var/log/mysqld.log>: mysql -uroot -p6)修改密码>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'Owen1234?'; |
安装Redis
|
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
|
1)前往用户根目录>: cd ~2)下载redis-5.0.5>: wget http://download.redis.io/releases/redis-5.0.5.tar.gz>: scp -r C:\Users\dell\Desktop\pkg\redis-5.0.5.tar.gz root@39.98.144.221:~3)解压安装包>: tar -xf redis-5.0.5.tar.gz4)进入目标文件>: cd redis-5.0.55)编译环境>: make6)复制环境到指定路径完成安装>: cp -r ~/redis-5.0.5 /usr/local/redis7)配置redis可以后台启动:修改下方内容>: vim /usr/local/redis/redis.confdaemonize yes8)完成配置修改>: esc>: :wq9)建立软连接>: ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server>: ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli10)后台运行redis>: redis-server &ctrl + c11)测试redis环境>: redis-clictrl + c12)关闭redis服务>: pkill -f redis -9 |
安装Python3.6
|
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
|
1)前往用户根目录>: cd ~2)下载 或 上传 Python3.6.7>: wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz>: scp -r 本地Python-3.6.7.tar.xz ssh root@39.98.144.221:服务器路径>: scp -r C:\Users\dell\Desktop\pkg\Python-3.6.7.tar.xz ssh root@39.98.144.221:~3)解压安装包>: tar -xf Python-3.6.7.tar.xz4)进入目标文件>: cd Python-3.6.75)配置安装路径:/usr/local/python3>: ./configure --prefix=/usr/local/python36)编译并安装>: make && sudo make install7)建立软连接:终端命令 python3,pip3>: ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3>: ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip38)删除安装包与文件:>: rm -rf Python-3.6.7>: rm -rf Python-3.6.7.tar.xz |
配置pip源:阿里云不用配置,默认配置阿里源
|
1
2
3
4
5
6
7
8
9
10
11
12
|
1)创建pip配置路径>: mkdir ~/.pip2)进入目录编辑配置文件:填入下方内容cd ~/.pip && vim pip.conf[global]index-url = http://pypi.douban.com/simple[install]use-mirrors =truemirrors =http://pypi.douban.com/simple/trusted-host =pypi.douban.com |
安装uwsgi
|
1
2
3
4
5
|
1)在真实环境下安装pip3 install uwsgi2)建立软连接ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi |
安装虚拟环境
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
1)安装依赖>: pip3 install virtualenv>: pip3 install virtualenvwrapper2)建立虚拟环境软连接>: ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv3)配置虚拟环境:填入下方内容>: vim ~/.bash_profileVIRTUALENVWRAPPER_PYTHON=/usr/bin/python3source /usr/local/python3/bin/virtualenvwrapper.sh4)退出编辑状态>: esc5)保存修改并退出>: :wq6)更新配置文件内容>: source ~/.bash_profile7)虚拟环境默认根目录:~/.virtualenvs |
了解:服务器运行测试Django项目
|
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
|
1)创建虚拟环境>: mkvirtualenv test_venv2)安装依赖>: pip install django3)前往目标目录,创建项目工作目录,再进入>: cd /home>: mkdir project>: cd project4)创建Django项目,并进入>: django-admin startproject test_site>: cd test_site5)完成项目配置:修改下方几行内容>: vim /home/project/test_site/test_site/settings.pyALLOWED_HOSTS = ['*']#DATABASES = {# 'default': {# 'ENGINE': 'django.db.backends.sqlite3',# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),# }#}6)跑原生服务>: python3 manage.py runserver 0.0.0.0:80 |
安装Nginx
|
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
|
1)前往用户根目录>: cd ~2)下载nginx1.13.7>: wget http://nginx.org/download/nginx-1.13.7.tar.gz3)解压安装包>: tar -xf nginx-1.13.7.tar.gz4)进入目标文件>: cd nginx-1.13.75)配置安装路径:/usr/local/nginx>: ./configure --prefix=/usr/local/nginx6)编译并安装>: make && sudo make install7)建立软连接:终端命令 nginx>: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx8)删除安装包与文件:>: rm -rf nginx-1.13.7>: rm -rf nginx-1.13.7.tar.xz9)测试Nginx环境,服务器运行nginx,本地访问服务器ip>: nginx>: 服务器绑定的域名 或 ip:80 |
Nginx命令
|
1
2
3
4
5
6
7
8
9
10
11
12
|
1)启动>: nginx2)关闭nginx>: nginx -s stop3)重启nginx>: nginx -s reload4)查看端口,强行关闭>: ps -aux|grep nginx>: kill <pid:进程编号> |
了解:Nginx & uwsgi 运行Django
|
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
53
54
55
56
57
58
|
1)在项目的虚拟环境安装uwsgi>: workon test_venv>: pip install uwsgi2)项目根目录配置uwsgi:填入下方内容>: vim /home/project/test_site/test_site.xml<uwsgi> <socket>127.0.0.1:8808</socket> <!-- 内部端口,自定义 --> <chdir>/home/project/test_site/</chdir> <!-- 项目路径 --> <module>test_site.wsgi</module> <!-- test_site为wsgi.py所在目录名--> <processes>4</processes> <!-- 进程数 --> <daemonize>uwsgi.log</daemonize> <!-- 日志文件 --></uwsgi>3)完成项目配置:修改下方几行内容>: vim /home/project/test_site/test_site/settings.pyDEBUG = FalseALLOWED_HOSTS = ['*']4)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容>: cd /usr/local/nginx/conf>: cp nginx.conf nginx.conf.bak>: vim nginx.conf>: ggdG>: ievents { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; 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 test_site.wsgi; #wsgi.py所在的目录名+.wsgi uwsgi_param UWSGI_CHDIR /home/project/test_site/; # 项目路径 } }}5)启动uwsgi>: uwsgi -x /home/project/test_site/test_site.xml6)启动nginx>: nginx7)浏览器测试:http://39.98.144.221/admin8)关闭uwsgi所有进程>: pkill -f uwsgi -9 |
路飞项目部署:Nginx + uwsgi + django + vue
配置前台项目
上线前配置
assets/settings.js
|
1
|
base_url: 'http://39.98.144.221:8000', // 设置公网ip |
上线
|
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)本地项目打包,前往luffycity项目目录下>: cnpm run build2)上传>: scp -r dist root@39.98.144.221:~3)移动并重命名mv ~/dist /home/html4)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容>: cd /usr/local/nginx/conf>: cp nginx.conf nginx.conf.bak>: vim nginx.conf>: ggdG>: ievents { 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问题 } }} |
路飞后台部署
上线前配置
prod.py:上线的配置文件,内容拷贝dev.py,前身就是settings.py
|
1
2
3
4
5
6
7
8
9
10
11
12
|
1)需要做上线修改的内容DEBUG = FalseALLOWED_HOSTS = [ '39.98.144.221' # 公网ip地址]CORS_ORIGIN_ALLOW_ALL = True # 允许所有跨域CORS_ORIGIN_WHITELIST = []UP_BASE_URL = 'http://39.98.144.221:8080'END_BASE_URL = 'http://39.98.144.221:8000' |
wsgi.py 和 manage.py
|
1
2
|
1)需要做上线修改的内容os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod') |
清空日志文件
上线
|
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
53
54
55
56
57
58
59
60
61
|
1)在项目的虚拟环境安装uwsgi>: mkvirtualenv luffy>: workon luffy# 走下方 pip导入导出依赖 说明,将本地的环境依赖同步到服务器环境中>: pip install uwsgi2)项目根目录配置uwsgi:填入下方内容>: mkdir /home/project# 注:将后台项目移至到/home/project,可以上传,也可以git,项目设置公开(私密需要配置ssl)>: cd /home/project && git clone https://gitee.com/doctor_owen/luffyapi.git >: 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> ---- 3)配置上线项目的settings:见后台项目部署准备视频4)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容>: vim /usr/local/nginx/conf/nginx.conf5)在原来基础上添加一个serverevents { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; 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/; # 项目路径 } }}见下方配置:pip环境 + 数据库设置 + django2.0源码 配置完成后再进行往下(配置教程在下方)5)启动uwsgi>: uwsgi -x /home/project/luffyapi/luffyapi.xml6)启动nginx>: nginx -s stop>: nginx>: nginx -s reload7)浏览器测试:http://39.98.144.221:8000/xadmin8)关闭uwsgi所有进程>: pkill -f uwsgi -9 |
pip导入导出依赖
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
1) 本地导出项目环境,上传线上,导入到线上环境中本地操作# 桌面新建env文件夹,开启终端进入文件夹,执行下方命名>: cd Desktop\env>: pip3 freeze > packages.txt# 注:把xadmin删掉>: scp -r packages.txt root@39.98.144.221:~服务器操作# 进入虚拟环境>: workon luffy# 导入>: pip3 install -r packages.txt# 安装xadmin>: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2 |
数据库设置 + django2.0源码(2.0.7不用修改源码)
|
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
|
1.管理员连接数据库>: mysql -uroot -pOwen1234?2.创建数据库>: create database luffy default charset=utf8;# 3.设置权限账号密码# 拥有公网或局域网,其他主机连mysql>: grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?';# 要是本机连mysql连不上,再增加localhost域,本机就可以登录了>: grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?';# 设置完有权限限制的账号后一定要刷新权限>: flush privileges;4.退出mysqlquit5.修改 prod.py | manage.py>: vim /home/project/luffyapi/luffyapi/settings/prod.py"PASSWORD": "Luffy123?">: vim /home/project/luffyapi/manage.pyos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod')6.源码修改>: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/base.py# 36,37行注释>: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/operations.py# 146行添加 query = query.encode()7.数据库迁移>: cd /home/project/luffyapi/>: python3 manage.py makemigrations>: python3 manage.py migrate8.创建超级用户>: python3 manage.py createsuperuser# 账号密码:admin|admin123 |
后台样式问题
设置文件中配置STATIC_ROOT
|
1
2
3
4
5
6
7
|
# >: vim /home/project/luffyapi/luffyapi/settings/prod.py# 在STATIC_URL下方再添加两句STATIC_URL = '/static/'STATIC_ROOT = '/home/project/luffyapi/luffyapi/static' # 服务器的绝对路径STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),) # 小luffyapi下要有static文件夹# >: esc# >: :wq |
迁移静态样式:项目目录下
|
1
2
3
|
可能错误>: mkdir /home/project/luffyapi/luffyapi/static>: python3 /home/project/luffyapi/manage.py collectstatic |
Nginx配置静态路径
|
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
|
>: vim /usr/local/nginx/conf/nginx.confevents { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; 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; } } 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问题 } }}>: esc>: :wq |
重启服务
|
1
2
3
|
>: pkill -f uwsgi -9>: uwsgi -x /home/project/luffyapi/luffyapi.xml>: nginx -s reload |
重点 重点 重点
|
1
2
3
4
5
6
7
|
# 1、真实环境和虚拟环境都要安装uwsgi,将真实环境下的uwsgi建立软连接# 2、redis服务一定要后台启动:redis-server &# 3、uwsgi启动django项目一定要进入虚拟环境下,因为环境都是安装在虚拟环境中# 4、服务器的日志都会被记录在于uwsgi配置文件 luffyapi.xml 同类目下的 uwsgi.log 中 |

浙公网安备 33010602011771号