centos7系统nginx安装及dist部署**
1.开通外网访问
2.安装依赖包:
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
3.下载并解压nginx
//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
4.安装nginx:
//进入nginx目录
cd /usr/local/nginx
//进入目录
cd nginx-1.13.7
//执行命令
./configure
//执行make命令
make
//执行make install命令
make install
5.配置nginx:
vi /usr/local/nginx/conf/nginx.conf
修改server–listen为8000
修改server–location–root为/user/www/dist(vue生成的静态文件所在目录)
6启动nginx
/usr/local/nginx/sbin/nginx -s reload
如果出现报错:
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed
则运行:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
再次启动:
/usr/local/nginx/sbin/nginx -s reload
7.查看nginx是否启动:
ps -ef | grep nginx
8.安装完成后(启动,重启,关闭)
进入安装目录中,
cd /usr/local/nginx/sbin
启动,关闭,重启,命令:
./nginx 启动
./nginx -s stop 关闭
./nginx -s reload 重启
8.配置nginx开机启动
切换到/lib/systemd/system/目录,创建nginx.service文件, 编辑nginx.service文件
cd /lib/systemd/system/
touch nginx.service
vim nginx.service
文件内容如下:
[Unit]
Description=nginx
After=network.target[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true[Install]
WantedBy=multi-user.target
退出并保存文件
9.设置nginx开机启动前先关闭nginx
进入安装目录中
cd /usr/local/nginx/sbin
./nginx -s stop 关闭
10.执行systemctl enable nginx.service使nginx开机启动
systemctl start nginx.service 启动nginx
systemctl stop nginx.service 结束nginx
systemctl restart nginx.service 重启nginx
systemctl status nginx.service 查看nginx状态
11.nginx添加反向代理(内部接口服务可以不开放外网访问)
http–server中添加
location /api/ {
proxy_pass http://127.0.0.1:8080/api/;
proxy_set_header host server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/static/ {
alias /var/www/dist/static/;
}
location /api/media/ {
alias /var/www/media/;
}
12.nginx添加文件大小限制
http–server中添加
client_max_body_size 20M;
13.修改运行nginx用户为root(因为django项目运行用户为root,如果nginx运行用户不调整为root,会导致上传的文件显示403无法访问)
第一行改为
user root;
14.平滑升级nginx(以前版本为1.13.7,升级的新版本为1.18.0,不升级有nginx漏洞,官网下载)()(http://nginx.org/en/download.html)
14.1升级前准备
14.1.1对nginx的配置文件nginx.conf做备份;
14.1.2新建目录/root/nginx,将安装包和脚本上传到该目录下;
14.2平滑升级nginx
14.2.1开始编译新版本的nginx
cd /root/nginx
tar -xf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module&&make
14.2.2开始重命名旧版本nginx的可执行文件,替换新版本nginx的可执行文件
cd /usr/local/nginx/sbin
mv nginx nginx.old
cp -r /root/nginx/nginx-1.10.3/objs/nginx .
14.2.3检查nginx配置文件的正确性
/usr/local/nginx/sbin/nginx -t
14.2.4开始升级新版nginx
kill -USR2 cat /usr/local/nginx/logs/nginx.pid
14.2.5旧版本nginx的pid变成nginx.pid.oldbin
cd /usr/local/nginx/logs
ls -lrt nginx.pid.oldbin
14.2.6关闭旧版本nginx
kill -QUIT cat /usr/local/nginx/logs/nginx.pid.oldbin
14.2.7查看新版nginx的版本号
/usr/local/nginx/sbin/nginx -v
15.执行systemctl enable nginx.service使nginx开机启动
systemctl start nginx.service 启动nginx
systemctl stop nginx.service 结束nginx
systemctl restart nginx.service 重启nginx
systemctl status nginx.service 查看nginx状态
后端接口服务部署*****
1.安装MySQL
(1)cd /var/www/needed_bags/mysql
(2)rpm -ivh MySQL-python-1.2.5-1.el7.x86_64.rpm
(3)rpm -ivh mysql-community-release-el7-5.noarch.rpm
(3)rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
(4)清除yum里所有MySQL依赖包
(4.1)rpm -qa|grep mysql
(4.2)yum remove mysql-libs
(5)rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm
(6)rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm
(7)rpm -ivh mysql-community-client-5.7.22-1.el7.x86_64.rpm
(8)rpm -ivh mysql-community-server-5.7.22-1.el7.x86_64.rpm
2.MySQL操作
2.1启动mysql: systemctl start mysqld.service
2.2查看MySQL状态: systemctl status mysqld.service
2.3关闭mysql: systemctl stop mysqld.service
2.4查看mysql进程: ps -ef|grep mysql
2.5查看3306端口: netstat -anop|grep 3306
2.6查看临时登录密码: grep ‘temporary password’ /var/log/mysqld.log
2.7使用临时密码登录: mysql -uroot -p
2.8修改密码策略: set global validate_password_policy=0;
2.9修改密码长度:set global validate_password_length=4;
2.10修改密码: set password for root@localhost=password(‘XXXXXX’);
2.11创建数据库: CREATE DATABASE XXXXXX DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
3.cd /var/www/django项目manage所在目录
4.数据结构创建:
python manage.py makemigrations django项目app名称
python manange.py migrate
5.数据导入:
5.1 mysql -uroot -pXXXXXX
5.2 source 导出的数据库文件所在位置
6.设置后端服务开机自启
6.1 cd /var/www/django项目manage所在目录
6.2创建启动脚本run.sh,脚本内容如下
#!/bin/bash
nohup python manage.py runserver 0.0.0.0:8080 &
6.3 使脚本具有执行权限: chmod +x ./run.sh
6.4 将脚本加入开机自启:
6.4.1 vim /etc/rc.d/rc.local
6.4.2 rc.local中加入脚本路径: run.sh文件所在路径
6.4.3 为rc.local添加执行权限: chmod +x /etc/rc.d/rc.local
开启防火墙端口***
1.开启防火墙端口
1.1 查看8000端口是否开启:firewall-cmd --query-port=8000/tcp
1.2 开启8000端口: firewall-cmd --znotallow=public --add-port=8000/tcp --permanent
1.3 重启防火墙: firewall-cmd --reload
1.4 查看8000端口是否开启:firewall-cmd --query-port=8000/tcp
1.5 查看已开启端口: firewall-cmd --list-port