nginx 配置来部署python的项目, 动静分离项目 与 不分离的项目

nginx 的作用

1. 做静态页展示的web服务

2.nginx做负载均衡

  四层

  七层

3. 反向代理


二. 源码安装nginx

1)解压nginx
[root@db01 ~]# tar xf nginx-1.10.3.tar.gz
2)进入nginx目录,并查看
[root@db01 ~]# cd nginx-1.10.3

[root@db01 nginx-1.10.3]# ll
total 672
drwxr-xr-x. 6 1001 1001   4096 May 13 09:04 auto
-rw-r--r--. 1 1001 1001 265299 Jan 31  2017 CHANGES
-rw-r--r--. 1 1001 1001 404694 Jan 31  2017 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 May 13 09:04 conf
-rwxr-xr-x. 1 1001 1001   2481 Jan 31  2017 configure
drwxr-xr-x. 4 1001 1001     72 May 13 09:04 contrib
drwxr-xr-x. 2 1001 1001     40 May 13 09:04 html
-rw-r--r--. 1 1001 1001   1397 Jan 31  2017 LICENSE
drwxr-xr-x. 2 1001 1001     21 May 13 09:04 man
-rw-r--r--. 1 1001 1001     49 Jan 31  2017 README
drwxr-xr-x. 9 1001 1001     91 May 13 09:04 src

3)创建nginx用户
[root@db01 nginx-1.10.3]# useradd nginx -s /sbin/nologin -M

4)生成make file
./configure  
--user=nginx 
--group=nginx 
--prefix=/usr/local/nginx-1.10.3/ 
--with-http_stub_status_module  
--with-http_ssl_module --with-stream

5)编译 && 安装
[root@db01 nginx-1.10.3]# make && make install

6)做软连接
[root@db01 ~]# ln -s /usr/local/nginx-1.10.3 /usr/local/nginx

[root@db01 ~]# ll /usr/local/nginx/
total 4
drwxr-xr-x. 2 root root 4096 May 13 09:13 conf
drwxr-xr-x. 2 root root   40 May 13 09:13 html
drwxr-xr-x. 2 root root    6 May 13 09:13 logs
drwxr-xr-x. 2 root root   19 May 13 09:13


7)检测nginx语法
[root@db01 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


8)启动nginx
[root@db01 conf]# /usr/local/nginx/sbin/nginx


9)检查nginx的端口
[root@db01 conf]# netstat -lntup|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      61777/nginx: master 

10)重新加载
[root@db01 conf]# /usr/local/nginx/sbin/nginx  -s reload

11)简化nginx配置文件
[root@db01 conf]# grep -Ev "#|^$" nginx.conf.default  > nginx.conf
编译安装nginx
12)创建conf.d目录
[root@db01 conf]# mkdir /usr/local/nginx/conf/conf.d
13)编辑虚拟主机
[root@db01 conf.d]# vim www.oldboy.com.conf
server {
    listen       80;
    server_name  localhost;
    location / {
        站点目录的根目录位置
        root   html; =============> root   /code/;

        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
nginx做虚拟主机的配置建议配置在conf.d目录下
14)创建站点目录
[root@db01 html]# mkdir /code

15)上传代码

16)重新加载nginx
[root@db01 code]# /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@db01 code]# /usr/local/nginx/sbin/nginx -s reload

####每次做好配置之后都需要 -t测试下


三. 部署VUE

1)下载node
wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz

2)解压
[root@db01 opt]# tar xf node-v8.6.0-linux-x64.tar.gz

3)移动目录
[root@db01 opt]# mv node-v8.6.0-linux-x64 /usr/local/node-8.6.0

4)做软连接
ln -s /usr/local/node-8.6.0 /usr/local/node

5)进入node目录,查看bin目录
[root@db01 opt]# cd /usr/local/node
[root@db01 node]# ll bin/
total 36264
-rwxrwxr-x. 1 500 500 37133148 Sep 27  2017 node
lrwxrwxrwx. 1 500 500       38 Sep 27  2017 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx. 1 500 500       38 Sep 27  2017 npx -> ../lib/node_modules/npm/bin/npx-cli.js


6)添加环境变量
[root@db01 node]# vim /etc/profile.d/node.sh
export PATH="/usr/local/node/bin:$PATH"

7)加载环境变量
[root@db01 node]# source /etc/profile

8)检测node和npm版本
[root@db01 node]# npm -v
5.3.0
[root@db01 node]# node -v
v8.6.0
安装VUE
9)解压vue代码
[root@db01 opt]# unzip 07-luffy_project_01.zip 

10)进入代码目录,安装package.json中的模块
[root@db01 opt]# cd 07-luffy_project_01
[root@db01 07-luffy_project_01]# npm install

11)更改js文件中的IP
 sed -i 's#127.0.0.1#10.0.0.51#g' /opt/07-luffy_project_01/src/restful/api.js

12)生成静态文件目录
npm run build

13)编辑 nginx配置文件,创建一个虚拟主机
[root@db01 dist]# vim /usr/local/nginx/conf/conf.d/www.luffy.com.conf

server {
    listen       80;
    server_name  www.luffy.com;
    location / {
        root   /opt/07-luffy_project_01/dist;
        index  index.html index.htm;
    }
    error_page 400 403 404 405 /40x.html;
}

14)配置hosts解析
10.0.0.51 www.luffy.com

15)重新加载nginx
[root@db01 dist]# /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@db01 dist]# /usr/local/nginx/sbin/nginx -s reload
前端代码vue部署,这个是前后端分离的部署方法
四.源码安装python3.6环境
1)下载
[root@db01 ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz

2)解压
[root@db01 ~]# tar xf Python-3.6.4.tgz

3)进入python3目录
[root@db01 ~]# cd Python-3.6.4

4)生成make file
./configure --prefix=/usr/local/python3.6.4 --with-ssl

5)编译 && 安装
[root@db01 Python-3.6.4]# make && make install

#软链接python3命令
[root@db03 ~]# ln -s /usr/local/python3.6.4/bin/python3 /usr/bin/
#软链接pip3命令
[root@db03 ~]# ln -s /usr/local/python3.6.4/bin/pip3  /usr/bin/
#安装redis驱动器
[root@elkstack01 ~]# pip3 install redis
#安装uwsgi
[root@elkstack01 ~]# pip3 install uwsgi
#安装Django
[root@elkstack01 ~]# pip3 install django

#安装uwsgi
[root@elkstack01 ~]# yum install -y uwsgi
编译安装python3
6)编辑python需要安装的库文件
[root@db01 opt]# 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

7)安装库文件
[root@db01 opt]# pip3 install -r requirements.txt
安装项目所需要的库文件
8)解压后端代码
[root@db01 opt]# unzip luffy_boy.zip

9)编辑uwsgi文件
[root@db01 opt]# vim /opt/uwsgi.ini 
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir           = /opt/luffy_boy    #后端代码存放的位置
# Django's wsgi file
module          = luffy_boy.wsgi    #这里是项目内的wsgi文件
# the virtualenv (full path)
home            = /usr/local/python3.6.4
# 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      #打开uwsgi的端口,这里的是8888
# clear environment on exit
vacuum          = true
10)启动uwsgi
[root@db01 opt]# uwsgi --ini /opt/uwsgi.ini &


绝对路径:
/usr/local/python3.6.4/bin/uwsgi --ini /opt/uwsgi.ini &


检测是否启动成功:
打开浏览器,访问:10.0.0.51:8888
[root@elkstack01 ~]# 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;
           }
      }
这个是动静分离的后端代码映射



[root@lb01 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {
    server 172.16.1.7:80  weight=1;
    server 172.16.1.8:80  weight=1;
}
    server {
        listen       80;
        server_name  www.driverzeng.com;
        location / {
            index  index.html index.htm;
            proxy_pass http://backend;
            proxy_set_header Host  $host;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_connect_timeout 60; 
            proxy_send_timeout 60; 
            proxy_read_timeout 60; 
            proxy_buffer_size 4k; 
            proxy_buffers 4 32k;
            proxy_busy_buffers_size 64k; 
            proxy_temp_file_write_size 64k;
        }
    }
}
这个是前后端不分离的nginx的配置方法
原文地址:https://www.cnblogs.com/gukai/p/10858572.html