Linux---Nginx部署环境

Linux—Nginx部署环境

省事的话可以直接安装个宝塔面板,然后再宝塔种勾选安装mysql,nginx,python项目管理器即可。

mysql

mariadb是MySQL的分支

下载

wget http://mirrors.sohu.com/mysql/MySQL-5.6/MySQL-5.6.44-1.el7.x86_64.rpm-bundle.tar

tar -xf MySQL-5.6.44-2.sles12.x86_64.rpm-bundle.tar

安装

yum install -y *.rpm

报错:

解决:setenforce 0

Nginx Web服务

安装

下载: wget http://mirrors.sohu.com/nginx/nginx-1.9.9.tar.gz
解压: tar -xf nginx-1.9.9.tar.gz
**安装:**切换到你解压的目录里执行
./configure --prefix=/usr/local/nginx
make
make install
启动: 切换到安装目录下/usr/local/nginx执行
./sbin/nginx

命令格式

[root@localhost sbin]#nginx -h
nginx version: nginx/1.18.0
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: /www/server/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

目录结构

conf html logs sbin

配置文件

nginx.conf

user  www www;  # 使用哪个用户来启动子进程
worker_processes auto;  # 工作进程的个数,配置成cpu核心个数减一或减二
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
    {
    
    
        use epoll;  # select poll epoll
        worker_connections 51200;  # 每一个子进程可以处理的连接数
        multi_accept on;
    }

http
    {
    
    
        include       mime.types;  # 导入
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;  # 默认的请求方式

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;  # 保持长连接的超时时间

        tcp_nodelay on;


server
    {
    
    
        listen 888;  # 监听端口
        server_name phpmyadmin;
        index index.html index.htm index.php;  # 默认页面
        root  /www/server/phpmyadmin;

        #error_page   404   /404.html;
        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
    
    
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
    
    
            expires      12h;
        }

        location ~ /\.
        {
    
    
            deny all;
        }

        access_log  /www/wwwlogs/access.log;
    }
include /www/server/panel/vhost/nginx/*.conf;
}


404页面

error_page   404   /404.html;

root和alias区别

location /img{
    root /data/img;
}
root /data/img/img
location /img{
    alias /data/img;
}
root /data/img

修改域名,只能本地使用

要想外网访问,必须去注册域名

C:\Windows\System32\drivers\etc\hosts
在这里边追加
10.14.155.78   hahaha.com

多域名访问

写多个

server{}

默认server

server {
        listen       80 default_server;
        server_name  mengyao.com libo-sober.top www.libo-sober.top;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /www/wwwroot/libo-sober.top;
            index  index.html index.htm;
        }

        #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;
        }

nginx日志

#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 user-agent
http_x_forwarded_for 

禁止访问

# 黑名单 白名单
location / {
    
    
    	deny 192.168.1.1;   # 禁止访问
    	allow 192.168.1.2;  # 允许访问
        root   /www/wwwroot/libo-sober.top;
        index  index.html index.htm;
    }

反向代理

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

  • 可以缓存经他文件

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

    upstream xxx{
          
          
    	ip_hash; #ip_hash  每个请求的ip做hash,这样每个固定的方可都会被负载到后端固定的机器
    	server http://192.168.1.1 weight=3; # 权重,得到的结果是访问这个3次,菜访问下面其他的一次
    	server http://192.168.1.121 backup; # 当前边的server的都访问不到,则请求backup的备份,只要有一个通,就不会走backup
    	
    }
    server{
          
          
    	listen 80;
    	server_name localhost;
    	location /{
          
          
    	proxy_pass xxx;
    	}
    }
    

location

location  = / {
  # 精确匹配/,后边不能带其他的东西
  # matches the query / only.
  [ configuration A ] 
}
location  / {
  # 所有的以/开头的地址
  # matches any query, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be

  # matched first.
  [ configuration B ] 
}
location ^~ /images/ {
  # 匹配以/images/开头
  # ~严格区分大小写
  # matches any query beginning with /images/ and halts searching,
  # so regular expressions will not be checked.

  [ configuration C ] 
}
location ~* \.(gif|jpg|jpeg)$ {
  # 以.(gif|jpg|jpeg)结尾的文件
  # ~*不区分大小写
  # matches any request ending in gif, jpg, or jpeg. However, all
  # requests to the /images/ directory will be handled by

  # Configuration C.   
  [ configuration D ] 
}
优先级
=  完整路径  ^~|~*  /

location分离

server{
	listen 80;
	server_name localhost;
	location /{
	proxy_pass http://192.168.1.1;
	}
    location ~*\.(gif|jpg|jpeg)${
        root /data/img;
    }
}
访问顺序

status

location /status{
    stub_status on;
}

压缩

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

nginx+uwsgi部署环境

wsgi

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

uwsgi:协议

uWSGI:具体实现方式

环境

yum install -y python3 python3-pip python3-devel
pip3 install uwsgi
pip3 install django==1.11.9

准本django程序

[root@localhost ~]#mkdir mydata
[root@localhost ~]#ls
anaconda-ks.cfg  initial-setup-ks.cfg  install.sh  mydata
[root@localhost ~]#cd mydata/
[root@localhost mydata]#django-admin startproject mysite
[root@localhost mydata]#ls
mysite
[root@localhost mydata]#cd mysite
[root@localhost mysite]#ls
manage.py  mysite
# 关闭安全保护和防火墙
[root@localhost mysite]#setenforce 0
setenforce: SELinux is disabled
[root@localhost mysite]#systemctl stop firewalld
# 启动django
[root@localhost mysite]#python3 manage.py runserver 0.0.0.0:8080
# 浏览器访问
http://10.14.155.78:8080/

启动uwsgi

cd django目录
uwsgi --http:8080  --module mysite.wsgi  # 随系统启动
# 加载WSGI文件
[root@localhost mysite]#uwsgi --http :8080 --module mysite.wsgi

uwsgu配置文件

[root@localhost mysite]#vim /etc/uwsgi.ini

[uwsgi]
http = :8080
chdir = /root/mydata/mysite
# uwsgi的文件
wsgi-file = mysite/wsgi.py
# 虚拟环境
# virtualenv = /root/env
# 进程个数
processes = 4
# 线程个数
threads = 2
# 关闭的情况下后台运行,制定输出的日志位置
daemonize = /root/mydata/mysite/django.log
# 清除临时文件
vacuum = true
# python文件发生改变自动重启
py-autoreload = 1

# 启动
uwsgi --ini /etc/uwsgi.ini

Nginx配置

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    
    
    worker_connections  1024;
}


http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
    
    
        listen       80;
        server_name  localhost;

        location / {
    
    
            root   /www/server/nginx/html;
            index  index.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }


    }

    server {
    
    
        listen       80;
        server_name  libo-sober.top www.libo-sober.top;
        location / {
    
    
        	# 跳转到8080
            proxy_pass http://127.0.0.1:8080;
        }
        # 静态文件,迁移后的文件名static
        location /static {
    
    
            root /root/crm/modelform;
        }
    
    
    }
}

静态文件要加上,要执行:

# 先在配置文件中加入:
[root@localhost modelform]#vim ./modelform/settings.py 
# 末尾追加,迁移后的文件名static
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# 迁移静态文件
[root@localhost modelform]#python3 manage.py collectstatic


另一种方式

uwsgi.ini

# http = :8080
socket = :9090

nginx

server {
    
    
	listen 80;
	location / {
    
    
		include uwsgi_params;
		uwsgi_pass 127.0.0.1:9090;
		....
	}
}

另一种方式

uwsgi.ini

# http = :8080
socket = /mydata/modelform/xxx.sock
nginx

server {
	listen 80;
	location / {
		include uwsgi_params;
		uwsgi_pass unix://mydata/modelform/xxx.sock;
		....
	}
}

使用问题:

  • 要使用uwsgi_pass
  • 如果本地:使用文件
  • 否则:端口

猜你喜欢

转载自blog.csdn.net/qq_31910669/article/details/111028559
今日推荐