前端打包
# 配置base_url
export default {
base_url: '公网IP:8000'
}
npm run build
# 将前端包传到公网服务器
scp -r dist/ root@182.xx.xx.xx:/home/ #远程传输文件夹
scp test.log root@182.xx.xx.xx:/home/ #远程传输文件
服务器相关配置
# 更新系统软件包
yum update -y
# 安装软件管理包和可能用到的依赖
yum -y groupinstall "Development tools"
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
MySQL
的安装
# 安装包地址下载
https://dev.mysql.com/downloads/mysql/5.6.html
# 卸载系统自带的Mariadb
[root@kingstar home]# rpm -qa|grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@kingstar home]# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
[root@kingstar home]#
#删除/etc下的my.cnf,如无此文件,可以跳过此步骤
[root@kingstar home]# rm -rf /etc/my.cnf
# 创建mysql用户以及用户组
# 创建mysql用户组
[root@kingstar home]# groupadd mysql
# 创建mysql用户,并将其加入mysql用户组
[root@kingstar home]# useradd -g mysql mysql
[root@kingstar home]#
# 将下载好的二进制包放到/usr/local目录下
[root@kingstar home]# mv mysql-5.6.48-linux-glibc2.12-x86_64.tar.gz /usr/local/
# 解压文件,并将其命名为mysql
[root@kingstar local]# tar xf mysql-5.6.48-linux-glibc2.12-x86_64.tar.gz
[root@kingstar local]# mv mysql-5.6.48-linux-glibc2.12-x86_64 mysql
# 在/etc目录下创建新的my.cnf文件
[root@kingstar etc]# vim my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port=3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
# 创建步骤7中的用到的目录,并将其用户设置为mysql
[root@kingstar etc]# mkdir /var/lib/mysql
[root@kingstar etc]# mkdir /var/lib/mysql/mysql
[root@kingstar etc]# chown -R mysql:mysql /var/lib/mysql
[root@kingstar etc]# chown -R mysql:mysql /var/lib/mysql/mysql
[root@kingstar etc]#
# 进入mysql安装目录
# 安装autoconf库和libaio库文件
yum -y install autoconf
yum install libaio* -y
chown -R mysql:mysql ./
./scripts/mysql_install_db --user=mysql
chown -R mysql:mysql data
# 配置mysql
<1> 修改my.cnf文件权限
[root@kingstar etc]# chmod 777 my.cnf
<2>设置开机自动启动服务的脚本
[root@kingstar mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@kingstar mysql]# ./scripts/mysql_install_db --user=mysql
<3>增加mysqld服务控制脚本执行权限
[root@kingstar mysql]# chmod +x /etc/rc.d/init.d/mysqld
<4>将mysql服务加入到系统服务中
[root@kingstar mysql]# chkconfig --add mysqld
<5>检查服务是否生效
root@kingstar mysql]# chkconfig --list mysqld
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@kingstar mysql]#
<6>启动mysqld服务
[root@kingstar mysql]# systemctl restart mysqld.service
[root@kingstar mysql]#
<7>将mysql的bin目录进入Path环境变量
[root@kingstar mysql]# vim /etc/profile
export MYSQL_HOME="/usr/local/mysql"
export PATH="$PATH:MYSQL_HOME/bin"
[root@kingstar mysql]# source /etc/profile
<8>开发3306端口和8080端口(tomcat)
[root@kingstar mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@kingstar mysql]# firewall-cmd --zone=public --add-port=3306/udp --permanent
success
[root@kingstar mysql]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@kingstar mysql]# firewall-cmd --zone=public --add-port=8080/udp --permanent
success
[root@kingstar mysql]# firewall-cmd --reload
success
[root@kingstar mysql]#
<9>创建mysql软连接
[root@kingstar ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
<10>修改用户密码
[root@kingstar bin]# ./mysqld_safe --skip-grant-tables
200430 21:22:19 mysqld_safe Logging to '/usr/local/mysql/data/kingstar.err'.
200430 21:22:19 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
<11>启动mysql客户端
[root@kingstar ~]# mysql -uroot -p
Enter password:
show databases;
use mysql;
show tables;
desc user;
select USER,HOST,Password from user;
mysql> update user set password=password("123456") where user="root" and host="%";
flush privileges;
<12>退出mysqld_safe
ps aux|grep mysqld
kill -9 75792 # mysqld_safe的PID号
systemctl restart mysqld.service
创建项目数据库以及用户
# 使用root用户创建数据库
mysql> create database luffyapi;
# 给项目创建用户
mysql> grant all privileges on luffyapi.* to 'luffyapi'@'%' identified by 'surpass@123';
Query OK, 0 rows affected (0.02 sec)
mysql> grant all privileges on luffyapi.* to 'luffyapi'@'localhost' identified by 'surpass@123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
redis
的安装
# cd 到 home目录下
# 下载redis-5.0.5
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
# 解压redis-5.0.5.tar.gz
[root@alisurpass home]# tar -zxvf redis-5.0.5.tar.gz
# 进入redis-5.0.5编译环境
[root@alisurpass redis-5.0.5]# make
#复制环境到指定路径完成安装
[root@alisurpass home]# cp -r redis-5.0.5 /usr/local/redis
# 创建软链接
[root@alisurpass home]# ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
[root@alisurpass home]# ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli
[root@alisurpass bin]# ls -al|grep redis
lrwxrwxrwx 1 root root 30 Jul 30 18:49 redis-cli -> /usr/local/redis/src/redis-cli
lrwxrwxrwx 1 root root 33 Jul 30 18:49 redis-server -> /usr/local/redis/src/redis-server
[root@alisurpass bin]#
# 前台运行redis
[root@alisurpass ~]# redis-server # 打开Redis服务端
[root@alisurpass ~]# redis-cli # 打开redis客户端
关闭操作
(1)服务端 ctrl + c 退出
或者,(2)客户端发送shutdown命令
# 后台运行redis
[root@alisurpass ~]# redis-server &
[root@alisurpass ~]# redis-cli
127.0.0.1:6379>
# 关闭Redis服务
[root@alisurpass ~]# ps -ajx|grep redis
10423 16305 16305 10423 pts/0 16339 Sl 0 0:00 redis-server *:6379
10423 16339 16339 10423 pts/0 16339 S+ 0 0:00 redis-cli
15754 16386 16385 15754 pts/2 16385 R+ 0 0:00 grep --color=auto redis
[1]+ Killed redis-server
[root@alisurpass ~]# kill -9 16305
[root@alisurpass ~]# ps -ajx|grep redis
[root@alisurpass ~]# kill -9 16339
[root@alisurpass ~]# ps -ajx|grep redis
或者 pkill -f redis -9
安装python环境以及pip环境(更新yum源和pip下载源)
# 第三方依赖库安装(如已安装,可以忽略)
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc gcc-c++ make
yum -y install libffi-devel
# 将下载好的Python-3.8.0.taz传到服务器
scp Python-3.8.0.taz root@182.xx.xx.xx:/home/
tar -zxvf Python-3.8.0.taz
cd Python-3.8.0
# 创建一个python3的安装路径
mkdir -p /usr/local/python3
# 编译安装
./configure --with-ssl --prefix=/usr/local/python3
make && make install
# 删除原来的python2软连接&建立新的python3软连接
rm -rf /usr/bin/python
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python
# 查看python的版本
[root@kingstar bin]# ls -al|grep python
[root@kingstar bin]# python -V
Python 3.8.0
[root@kingstar bin]# python2 -V
Python 2.7.5
[root@kingstar bin]#
# 更改yum对应的路径
vim /usr/bin/yum
将首行的python改成python2.7
# 如果在安装pip的过程中报错: SyntaxError: invalid syntax
vim /usr/libexec/urlgrabber-ext-down
将首行的python改成python2.7
# pip安装
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
[root@Kingstar home]# python -m pip install --upgrade pip
# 建立pip软连接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip
# 添加path到环境变量
echo PATH='/usr/local/python/bin/:$PATH' >> /etc/profile
source /etc/profile
centos虚拟机更换yum源
# 备份本地的yum源
cd /etc/yum.repos.d
[root@Cherry yum.repos.d]# ls
CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo
mv CentOS-Base.repo CentOS-Base.repo_bak
# 选择更换的源
# 阿里源
wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 网易源
wget -O CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
# 清理yum缓存
yum clean all
# 重建缓存
yum makecache
# 升级Linux系统
yum -y update
pip跟换下载源
国内常用的源:
阿里源:http://mirrors.aliyun.com/pypi/simple/
清华源:https://pypi.tuna.tsinghua.edu.cn/simple/
豆瓣源:http://pypi.douban.com/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
临时使用方法
# 这样就会从清华这边的镜像去安装gevent库
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gevent
在家目录下创建.pip文件
mkdir .pip
cd ~/.pip
vim pip.conf
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
Nginx环境的安装
下载nginx的安装包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
解压nginx
[root@alisurpass home]# tar -zxvf nginx-1.13.7.tar.gz
安装PCRE库支持
yum install pcre-devel pcre openssl openssl-devel gcc gcc-c++ zlib zlib-devel -y
进入解压目录,然后sed修改Nginx版本信息为JWS
sed -i -e 's/1.13.7//g' -e 's/nginx//JWS/g' -e 's/"NGINX"/"JWS"/g' src/core/nginx.h
预编译Nginx
创建用户
useradd www
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
执行编译命令
make &&make install
创建软连接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/
[root@alisurpass bin]# ls -al|grep nginx
lrwxrwxrwx 1 root root 27 Jul 30 20:52 nginx -> /usr/local/nginx/sbin/nginx
将ngnix添加到系统服务中
vim /etc/init.d/nginx
内如下:
#!/bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
再执行以下的命令
#chmod a+x /etc/init.d/nginx
#chkconfig --add nginx
#chkconfig nginx on
#systemctl start nginx
[root@alisurpass init.d]# netstat -tnlp|grep 80
[root@alisurpass init.d]# systemctl start nginx
[root@alisurpass init.d]# netstat -tnlp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25178/nginx: master
NGINX服务
//服务
#启动nginx服务
systemctl start nginx.service
#停止nginx服务
systemctl stop nginx.service
#重启nginx服务
systemctl restart nginx.service
#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service
测试Nginx服务安装是否正确,同时启动Nginx WEB 服务,代码命令如下:
[root@alisurpass init.d]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip 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/; # 项目路径
}
}
}
前端项目的部署
[root@alisurpass home]# mv dist/ /home/html
nginx -s reload
后端生成依赖模块文件
requirement.txt
celery==4.4.6
Django==2.2.14
django-cors-headers==3.4.0
django-crispy-forms==1.9.2
django-filter==2.3.0
django-formtools==2.2
django-import-export==2.3.0
django-redis==4.12.1
django-reversion==3.0.7
djangorestframework==3.11.0
djangorestframework-jwt==1.11.0
Pillow==7.2.0
PyMySQL==0.9.3
pyOpenSSL==19.1.0
python-alipay-sdk==2.0.1
qcloudsms-py==0.1.4
redis==3.5.3
xadmin @ https://codeload.github.com/sshwsfc/xadmin/zip/django2
后端环境部署
# 创建project文件夹
[root@alisurpass home]# mkdir project
# 将后端文件放到project中
[root@alisurpass ~]# cp -r luffyapi/ /home/project/
# 创建虚拟环境
[root@alisurpass luffyapi]# pip install virtualenv
[root@alisurpass luffyapi]# pip install virtualenvwrapper
# 建立虚拟环境软连接
[root@alisurpass bin]# ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
# 找到virtualenvwrapper.sh文件
[root@alisurpass bin]# cp virtualenvwrapper.sh /usr/local/bin/
# 创建虚拟环境工作目录
mkdir -p ~/.virtualenvs
cd ~/.virtualenvs
vim .bashrc
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
source export WORKON_HOME=$HOME/.virtualenvs
source .bashrc
# 创建luffy的虚拟环境
[root@alisurpass ~]# mkvirtualenv -p python luffy
# 安装项目的依赖环境
pip install -r requirement.txt
# django2.0.7以上版本改源码
[root@alisurpass .virtualenvs]# cd /root/.virtualenvs/luffy/lib/python3.8/site-packages/django/db/backends/mysql/
vim base.py
将版本判断的那两行代码注释掉
安装uwsgi
# 虚拟环境和项目中都安装uwsgi
pip install uwsgi
# 创建软连接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
# 在项目根路径下配置uwsgi,luffyapi.xml
cd /home/project/luffyapi
vim 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>
# 启动uwsgi
(luffy) [root@alisurpass luffyapi]# uwsgi -x luffyapi.xml
[uWSGI] parsing config file luffyapi.xml
(luffy) [root@alisurpass luffyapi]# ps -ajx|grep uwsgi
1 1566 1565 1565 ? -1 S 0 0:00 uwsgi -x luffyapi.xml
1566 1569 1565 1565 ? -1 S 0 0:00 uwsgi -x luffyapi.xml
1566 1570 1565 1565 ? -1 S 0 0:00 uwsgi -x luffyapi.xml
1566 1571 1565 1565 ? -1 S 0 0:00 uwsgi -x luffyapi.xml
29396 1582 1581 29396 pts/0 1581 R+ 0 0:00 grep --color=auto uwsgi
(luffy) [root@alisurpass luffyapi]# netstat -tnlp|grep 8808
tcp 0 0 127.0.0.1:8808 0.0.0.0:* LISTEN 1566/uwsgi
(luffy) [root@alisurpass luffyapi]#
# 重启 nginx
nginx -s reload
收集静态资源
step1:修改pro.py配置文件
1)编辑线上配置文件
>: vim /home/project/luffyapi/luffyapi/settings/prod.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"),)
迁移静态样式:项目目录下
1)项目目录下没有 static 文件夹需要新建
mkdir /home/project/luffyapi/luffyapi/static
2)完成静态文件迁移
python /home/project/luffyapi/manage_pro.py collectstatic
Nginx配置静态路径
1)修改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)关闭 uwsgi,重新启动 uwsgi
pkill -f uwsgi -9
uwsgi -x /home/project/luffyapi/luffyapi.xml
2)关闭 nginx,重新启动 nginx
nginx -s reload
nginx -s stop
nginx
拓展
什么是nginx
nginx是一个高性能的http和反向代理服务器
Apache和Nginx最核心的区别在于 apache 是同步多进程模型,一个连接对应一个进程;而 nginx 是异步的,多个连接(万级别)可以对应一个进程
利用nginx可以实现:
http请求转发
反向代理服务器
负载均衡
动静分离