linux项目部署软件(nginx、uwsgi)

一、nginx

作用是web服务

apache(开源软件,nginx) iis(windows服务器的web服务)

django web框架

lvs 负载均衡(多台服务器如何分配访问量) 章文嵩博士

vue 尤雨溪

Tengine(淘宝在nginx基础上进一步开发)

F5 硬件负载 A10

官网

https://nginx.org/en/download.html

编译安装

wget http://nginx.org/download/nginx-1.16.1.tar.gz

tar xf nginx-1.16.1.tar.gz 

cd nginx-1.16.1

# c语言编译器
yum install gcc zlib2-devel pcre-devel openssl-devel

# 配置设置
# --with-http_stub_status_module状态页面设置
# --with-http_ssl_module支持https
 ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module
 
 # 编译
 make && make install

目录结构

[root@localhost nginx]#ls
conf  html  logs  sbin
conf 配置文件
html 存放静态文件 index.html 是默认的欢迎页面
logs 日志目录
sbin 二进制文件

命令格式

./sbin/nginx -h
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit 显示版本号
  -V            : show version and configure options then exit 显示版本+编译时选项
  -t            : test configuration and exit  测试配置文件
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /opt/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

启动nginx

./sbin/nginx 

重新启动nginx

./sbin/nginx -s reload

关闭防火墙

iptables -F

输入ip查看网页,是否成功

配置文件

打开配置文件

vim /conf/nginx.conf

启动以后会生成一个主进程,根据配置文件的选项来生成子进程(工作进程),主进程不负责处理用户的请求,用来转发用户的请求,真正负责处理用户请求的是子进程

#user  nobody;    使用哪个用户来启动子进程
worker_processes  1; #工作进程的个数,配置成cpu的核心数-1或者-2
# cpu亲缘性绑定,让nginx的子进程工作在哪个核心上

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    #use [epoll|select|poll];
    worker_connections  102400; # 每一个子进程可以处理的连接数
}


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  localhost; 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;  # 指定静态文件地址
            index  index.html index.htm; # 指定默认的index页面
        }
        # 错误页面 找不到页面
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        # 错误页面 服务端错误
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

yum安装

添加 Nginx 官方仓库

vim /etc/yum.repos.d/nginx.repo

写入以下内容:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# yum安装
yum install -y nginx

常用管理命令

操作命令
启动 systemctl start nginx
停止 systemctl stop nginx
重启 systemctl restart nginx
重载配置 systemctl reload nginx
查看状态 systemctl status nginx
开机自启 systemctl enable nginx
禁用开机自启 systemctl disable nginx

文件配置

vim /etc/nginx/nginx.conf

404页面

error_page  404              /404.html;

root和alias的区别

 location /img {
     root /data/img;
 }
# root /data/img 里面必须有/img

 location /img {
     alias /data/img;
 }
# alias /data/img 里面不需要有 /img

域名

server_name ms.s22.com

多域名访问

  server  {
        listen 80;
        server_name www.taobao.com taobao.com;
        location / {
        root /data/taobao;
        index index.html;
        }

}
   server  {
        listen 80;
        server_name www.jd.com jd.com;
        location / {
        root /data/jd;
        index index.html;
        }
        }

默认server

listen 80 default_server;

nginx的日志

查看log

tail -f logs/access.log
 #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;
remote_addr 访问ip地址
remote_user 访问的用户
time_local 本地时间
request 包括请求方式  请求地址  请求协议版本
status 状态码
body_bytes_sent 发送的大小
http_user_agent 用户的请求头
http_x_forwarded_for

禁止访问

可以写在server或者location里面

location / {
deny 192.168.21.1; # 禁止该ip访问
allow 192.168.21.131; # 白名单,和网段禁止配合使用
deny 192.168.21.0/24; # 禁止网段访问
}

反向代理

  • 起到保护网站安全的作用

  • 可以缓存静态文件

  • 实现负载均衡 F5 A10 lvs haproxy nginx

upstream django {
        server 192.168.21.128:81;
}
 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://django;
        }
权重

weight

upstream django {
    server 192.168.21.128:81 weight=3;
    server 192.168.21.131:81
}
 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://django;
        }
        }
得到的结果是:
访问128的3次,才访问131的一次
ip_hash

每个请求的ip做hash运算,这样每个固定的访客都会被负载到后端固定的机器

upstream django {
    ip_hash;
    server 192.168.21.128:81
    server 192.168.21.131:81
}
backup
当前面的都访问不到,则请求backup的备份,只要有一个通,则不会走backup
upstream django {
    server 192.168.21.128:81;
    server 192.168.21.131:81;
    server 192.168.21.131:82 backup;
}

nginx location匹配规则

location = / {
    精确匹配/ ,后面不能带其他的东西
    [ configuration A ]
}

location / {
    所有的以/开头的地址
    [ configuration B ]
}

location /documents/ {
    只匹配/documents/
    [ configuration C ]
}

location ^~ /images/ {
    # 匹配以/images/开头。
    ~严格大小写
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    # 以(gif|jpg|jpeg)结尾的文件
    ~* 不区分大小写
    [ configuration E ]
}
优先级
= > 完整路径 > ^~ > /

location分离(动静分离)

server  {

        listen 80 ;
        server_name www.taobao.com taobao.com;
        location / {
        proxy_pass http://192.168.21.131:82;
        }
        # ~*\.(jpg|gif|png)$ 比 /优先级高,先走~*\.(jpg|gif|png)$ 所匹配的路径,从而实现动静分离
        location ~*\.(jpg|gif|png)$ {
        root /data/img;
        }

status

location /status {
    stub_status on;
}

压缩

gzip on
提高响应速度,节省带宽

二、WSGI

django自带的wsgiref 在调试模式(debug)下使用的wsgi的文件,网关接口,协议

uwsgi:协议

uWSGI:具体实现方式

安装

pip3 install uwsgi -i https://mirrors.aliyun.com/pypi/simple/

准备django程序

在nginx/data目录下启动

(django11) m[root@qiao data]#django-admin startproject mysite

cd mysite后

vim /mystie/settings.py # 修改为mysql数据库

如下:

 'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db1',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '127.0.0.1',
        'PORT': 3306,

启动django项目

(django11) m[root@qiao mysite]#python3 manage.py runserver 0.0.0.0:8080

启动uwsgi

cd mysite
m[root@qiao mysite]#uwsgi --http :8080 --module mysite.wsgi

配置文件格式(额外知识)

conf
py
cnf
xml
json
ini
yaml

配置文件启动

[uwsgi]
http = :8080
#项目路径
chdir= /data/mysite
# uwsgi的文件
wsgi-file= mysite/wsgi.py
# 虚拟环境
# virtualenv = /root/env
# 进程个数
processes = 2
# 线程个数
threads=2
# 后台启动,指定日志的输出
daemonize=/data/mysite/django.log
# 清除临时文件
vacuum = true
# python文件发生改变自动重启(debug模式下)
py-autoreload=1

启动配置文件(通过uwsgi启动django):

uwsgi --ini file

nginx的配置文件

server {
    listen 80;
    server_name crm.oldboy.com;
    location / {
        proxy_pass http://127.0.0.1:8080; # 通用的方式
    }
    # 静态文件的配置
    location /static {
        root /data/supercrm;
    }
}

在django的配置中要写入

SATAIC_ROOT=os.path.join(BASE_DIR,'static/')

执行命令

python3 manager.py collectstatic #用来收集静态文件

第二种配置方式(nginx的配置文件)

uwsgi
socket= :9090
nginx的配置文件
location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8080;
}

第三种配置方式(nginx的配置文件)

uwsgi
socket = file.sock
nginx的配置文件
location /{
    include uwsgi_params;
    uwsgi_pass unix://file.sock
}

使用问题:

1.要使用uwsgi_pass

2.如果是本地的,则使用unix文件

3.如果不是本地,则使用端口的方式

 

 

posted @ 2025-07-02 22:37  coder雪山  阅读(38)  评论(0)    收藏  举报