Nginx安装配置和Django前后端分离项目部署

所需安装环境: yum install -y pcre-devel, openssl-devel

  • 下载

wget -c https://nginx.org/download/nginx-1.10.3.tar.gz

  • 解压

[root@elk01 ~]# tar xf nginx-1.10.3.tar.gz

  • 生成

[root@elk01 nginx-1.10.3]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.10.3/ --with-http_stub_status_module --with-http_ssl_module --with-stream

  • 编译&&安装

[root@elk01 nginx-1.10.3]# make && make install

  • 做软连接

[root@elk01 nginx-1.10.3]# ln -s /usr/local/nginx-1.10.3 /usr/local/nginx

  • 创建nginx用户

useradd nginx -s /sbin/nologin -M

  • 检查nginx配置文件语法

[root@web01 nginx-1.10.3]# /usr/local/nginx/sbin/nginx -t

  • 启动nginx

[root@web01 nginx-1.10.3]# /usr/local/nginx/sbin/nginx

  • 添加环境变量

[root@web01 nginx-1.10.3]# vim /etc/profile.d/nginx.sh
export PATH="/usr/local/nginx/sbin:$PATH"

  • 加载环境变量

[root@web01 nginx-1.10.3]# source /etc/profile

  • 配置nginx(简化nginx配置文件)

[root@web01 nginx-1.10.3]# grep -Ev “#|^$” /usr/local/nginx/conf/nginx.conf.default > /usr/local/nginx/conf/nginx.conf

  • 编辑nginx主配置文件
    Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束,每一行使用;表示结束。

[root@web01 nginx-1.10.3]# vim /usr/local/nginx/conf/nginx.conf

#CoreModule核心模块
user nginx;                       #Nginx进程所使用的用户
worker_processes 1;             #Nginx运行的work进程数量(建议与CPU数量一致或auto)
error_log /log/nginx/error.log  #Nginx错误日志存放路径
pid /var/run/nginx.pid          #Nginx服务运行后产生的pid进程号

#events事件模块
events {            
    worker_connections  #每个worker进程支持的最大连接数
    use epool;          #事件驱动模型, epoll默认
}


#http内核模块
#公共的配置定义在http{}
http {  //http层开始
...  
#使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
    'server' {
        listen       80;        //监听端口, 默认80
        server_name  localhost; //提供服务的域名或主机名
        access_log host.access.log  //访问日志
        //控制网站访问路径
        'location' / {
            root   /usr/share/nginx/html;   //存放网站代码路径
            index  index.html index.htm;    //服务器返回的默认页面文件
        }
        //指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton
        error_page   500 502 503 504  /50x.html;
    }
    ...
    //第二个虚拟主机配置
    'server' {
    ...
    }
    include /etc/nginx/conf.d/*.conf;  //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
}   //http层结束

nginx多个虚拟主机
如果每台linux服务器只运行了一个小网站,那么人气低,流量小的草根站长需要承担高额的服务器租赁费,也造成了硬件资源浪费。

虚拟主机就是将一台服务器分割成多个“虚拟服务器”,每个站点使用各自的硬盘空间,由于省资源,省钱,众多网站都使用虚拟主机来部署网站。
1)虚拟主机的概念就是在web服务里的一个独立的网站站点,这个站点对应独立的域名(IP),具有独立的程序和资源目录,可以独立的对外提供服务。
2)这个独立的站点配置是在nginx.conf中使用server{}代码块标签来表示一个虚拟主机。
3)Nginx支持多个server{}标签,即支持多个虚拟主机站点。
虚拟主机类型

基于域名的虚拟主机
通过不同的域名区分不同的虚拟主机,是企业应用最广的虚拟主机。

基于端口的虚拟主机
通过不同的端口来区分不同的虚拟主机,一般用作企业内部网站,不对外直接提供服务的后台,例如blog.zengshuaishuai.com:8888

基于IP的虚拟主机
通过不同的IP区分不同的虚拟主机,此类比较少见,一般业务需要多IP的常见都会在负载均衡中绑定VIP

nginx可以自动识别用户请求的域名,根据不同的域名请求服务器传输不同的内容,只需要保证服务器上有一个可用的ip地址,配置好dns解析服务。

/etc/hosts是linux系统中本地dns解析的配置文件,同样可以达到域名访问效果

配置虚拟主机

#编辑nginx配置文件
[root@elkstack01 conf]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;  
            index  index.html index.htm;   //默认是访问
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  blog.driverzeng.com;
        location / {
            root   /code/drz;  //为站点目录 ,存放前端页面
            index  index.html index.htm;  //默认是当用户去访问blog.driverzeng.com的时候去站点目录下查找index.html,找不到就找index.htm
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • 创建站点目录

[root@web01 conf]# mkdir -p /code/drz

  • 编辑页面

[root@web01 conf]# echo ‘hello world’ >/code/drz/index.html

  • 检测nginx语法

[root@web01 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.3//conf/nginx.conf test is successful

  • 重新加载nginx(不间断业务)

[root@web01 conf]# /usr/local/nginx/sbin/nginx -s reload

  • 绑定hosts

MacBook-Pro:~ driverzeng$ sudo vim /etc/hosts
10.0.0.7 blog.driverzeng.com
windows+r 打开运行 输入:drivers,进入etc目录里面有一个hosts文件,在文件最后一行添加
10.0.0.7 blog.driverzeng.com

打开浏览器,访问:http://blog.driverzeng.com/

找不到页面优化

在网站运行过程中,可能因为页面不存在等原因,导致网站无法正常响应请求,此时web服务会返回系统的错误码,但是默认的错误页面很不友好,不仅很丑,还暴露了nginx的版本信息

  • 将页面移走

[root@web01 conf]# mv /code/drz/index.html /tmp/

Nginx安装配置和Django前后端分离项目部署_第1张图片

#在虚拟主机中 加一行配置
[root@web01 conf]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  blog.driverzeng.com;
        location / {
            root   /code/drz;
            index  index.html index.htm;
        }
        error_page 400 403 404 405 /40x.html;
        }
    }
#检测语法
[root@web01  conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.3//conf/nginx.conf test is successful
#重新加载
[root@web01  conf]# /usr/local/nginx/sbin/nginx -s reload
#编辑nginx错误页面
[root@web01  conf]# vim /code/drz/40x.html
<img style='width:100%;height:100%;' src=https://www.driverzeng.com/zenglaoshi/404_page.png>

再次打开浏览器,访问一个不存在的页面:http://blog.driverzeng.com/aaa

nginx部署前后端分离项目
既然要部署项目,那我们必须有代码,光是一个nginx肯定是没有用的。

代码下载
后端代码:https://files.cnblogs.com/files/pyyu/luffy_boy.zip
前端vue代码:https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip

#将代码下载到服务器上
[root@web01 ~]# wget https://files.cnblogs.com/files/pyyu/luffy_boy.zip
[root@web01 ~]# wget https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip

部署前端VUE
要在服务器上,编译打包vue项目,必须得有node环境

#下载node二进制包,此包已经包含node,不需要再编译
[root@web01 ~]# wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
#解压
[root@web01 ~]# tar -xf node-v8.6.0-linux-x64.tar.gz
#因为是二进制,所以解压开可以直接使用,移动到安装目录
[root@web01 ~]# mv node-v8.6.0-linux-x64 /usr/local/node-8.6.0
#做软链接
[root@web01 ~]# ln -s /usr/local/node-8.6.0 /usr/local/node
#进入node文件夹
[root@web01 ~]# cd /usr/local/node
#查看目录内容
[root@web01 node]# ll
总用量 152
drwxrwxr-x 2 nginx nginx  4096 927 2017 bin
-rw-rw-r-- 1 nginx nginx 53120 9月  27 2017 CHANGELOG.md
drwxrwxr-x 3 nginx nginx  4096 927 2017 include
drwxrwxr-x 3 nginx nginx  4096 927 2017 lib
-rw-rw-r-- 1 nginx nginx 56524 9月  27 2017 LICENSE
-rw-rw-r-- 1 nginx nginx 25984 9月  27 2017 README.md
drwxrwxr-x 5 nginx nginx  4096 927 2017 share
#查看bin目录下内容
[root@web01 node]# ll bin/
总用量 36264
-rwxrwxr-x 1 nginx nginx 37133148 927 2017 node
lrwxrwxrwx 1 nginx nginx       38 412 23:54 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx 1 nginx nginx       38 412 23:54 npx -> ../lib/node_modules/npm/bin/npx-cli.js
#我们需要将bin目录下的命令添加环境变量
[root@web01 node]# vim /etc/profile.d/node.sh
export PATH="/usr/local/node/bin:$PATH"
#重新加载环境变量
[root@web01 node]# source /etc/profile
#测试是否加载成功
[root@web01 node]# node -v
v8.6.0
[root@web01 node]# npm -v
5.3.0

node环境有了,安装node模块,以及打包node项目

#解压VUE代码
[root@web01 ~]# unzip 07-luffy_project_01.zip
#进入vue源码目录
[root@web01 ~]# cd 07-luffy_project_01/
#安装vue模块,默认去装package.json的模块内容,如果出现模块安装失败,手动再装
[root@web01 07-luffy_project_01]# npm install
#此时注意,你本地写的vue代码,接口很可能连接的服务器地址有问题,注意Axios.POST提交的地址,一定得发送给django应用(如果用了nginx,就发送给nginx的入口端口
#准备编译打包vue项目,替换配置文件所有地址,改为服务器地址
[root@web01 07-luffy_project_01]# sed -i 's#127.0.0.1#10.0.0.51#g' /root/07-luffy_project_01/src/restful/api.js
#此时打包vue项目,生成一个dist静态文件夹
[root@web01 07-luffy_project_01]# npm run build
#检查dist文件夹
[root@web01 07-luffy_project_01]# ll dist/
总用量 8
-rw-r--r-- 1 root root  556 4月  13 00:09 index.html
drwxr-xr-x 8 root root 4096 413 00:09 static

如果出现如下报错,那么我们需要更新npm版本

Nginx安装配置和Django前后端分离项目部署_第2张图片

[root@web01 07-luffy_project_01]# npm i -g npm

至此vue代码就结束了,只需要让nginx配置,找到vue的index.html首页文件即可.

让nginx找到vue的index.html方法有很多

1)再配置一个server主机

#先更改一个路径,将站点目录名字修改的简单一点
[root@web01 ~]# mv 07-luffy_project_01 /usr/local/luffy
#编辑nginx配置文件
[root@web01 ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  luffy.driverzeng.com;
        location / {
            root   /usr/local/luffy/dist;
            index  index.html index.htm;
        }
        error_page 400 403 404 405 /40x.html;
        }
#检测nginx语法
[root@web01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.3//conf/nginx.conf test is successful
#重新加载nginx
[root@web01 ~]# /usr/local/nginx/sbin/nginx -s reload
#配置hosts
10.0.0.51 luffy.driverzeng.com

打开浏览器,访问:http://luffy.driverzeng.com
Nginx安装配置和Django前后端分离项目部署_第3张图片

部署后端代码
这个路飞代码数据库用的是sqllite,不需要配置数据库了
购物车用的都是redis,因此要启动服务器的redis-server服务端

  • 安装redis

[root@web01 ~]# tar xf redis-3.2.12.tar.gz
[root@web01 ~]# mv redis-3.2.12 /usr/local/

  • 做软连接

[root@web01 ~]# ln -s /usr/local/redis-3.2.12 /usr/local/redis

  • 添加环境变量

[root@web01 ~]# vim /etc/profile.d/redis.sh
export PATH="/usr/local/redis/src:$PATH"

  • 进入redis的安装目录

[root@web01 ~]# cd /usr/local/redis
[root@web01 redis]# make

  • 加载环境变量

[root@web01 redis]# source /etc/profile

  • 创建redis配置文件

[root@web01 redis]# mkdir /data/6379
[root@web01 ~]# vim /data/6379/redis.conf

port 6379
daemonize yes
pidfile /data/6379/redis.pid
logfile "/data/6379/redis.log"
dbfilename dump.rdb
dir /data/6379
protected-mode no
appendonly yes
  • 启动redis

[root@web01 ~]# redis-server /data/6379/redis.conf

源码安装Python环境
Python官网:https://www.python.org/

#下载Python3.6.4安装包
[root@web01 ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
#解压
[root@web01 ~]# tar xf Python-3.6.4.tgz
#进入解压目录
[root@elk01 ~]# cd Python-3.6.4/
#生成Python环境安装文件
[root@elk01 python-3.6.4]# ./configure --prefix=/usr/local/python3.6.4 --with-ssl
#编译
[root@web01 python-3.6.4]# make
#安装
[root@web01 python-3.6.4]# make install
#软链接python3命令
[root@web01 python-3.6.4]# ln -s /usr/local/python3.6.4/bin/python3 /usr/bin/
#软链接pip3命令
[root@web01 python-3.6.4]# ln -s /usr/local/python3.6.4/bin/pip3  /usr/bin/
#安装redis驱动器
[root@eweb01 ~]# pip3 install redis
#安装uwsgi
[root@web01 ~]# pip3 install uwsgi
#安装Django
[root@web01 ~]# pip3 install django

#安装uwsgi
[root@web01 ~]# yum install -y uwsgi

#编写安装模块文件
[root@web01 ~]# vim requirements.txt
certifi==2018.11.29
chardet==3.0.4
crypto==1.4.1
Django==2.1.4
django-redis==4.10.0
django-rest-framework==0.1.0
djangorestframework==3.9.0
idna==2.8
Naked==0.1.31
pycrypto==2.6.1
pytz==2018.7
PyYAML==3.13
redis==3.0.1
requests==2.21.0
shellescape==3.4.1
urllib3==1.24.1
uWSGI==2.0.17.1

#一次性安装
[root@web01 ~]# pip3 install -r /root/requirements.txt

#解压代码
[root@web01 ~]# unzip luffy_boy.zip
#移动代码
[root@web01 ~]# mv luffy_boy /code/drz/
#配置uwsgi
[root@web01 ~]# vim /code/drz/uwsgi.ini
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir           = /code/drz/luffy_boy
# Django's wsgi file
module          = luffy_boy.wsgi
# the virtualenv (full path)
home            = /usr/local/python3
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
socket          = 0.0.0.0:8888
# clear environment on exit
vacuum          = true

#启动uwsgi
[root@web01 ~]# uwsgi --ini /code/drz/uwsgi.ini &

#配置nginx
[root@web01 ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
    listen       8000;
            server_name  blog.driverzeng.com;
            location / {
            uwsgi_pass 0.0.0.0:8888;
            include /usr/local/nginx/conf/uwsgi_params;
            }
            location /static {
            alias /code/drz/static;
           }
      }

你可能感兴趣的:(运维,Django,Nginx安装,Django前后端分离项目部署)