Ubuntu16安装nginx

做事难, 只是因为没去做! 本文记录在Ubuntu 16上安装Nginx步奏.

环境

  • utuntu 16
  • pcre-8.37
  • JDK 1.7
  • Tomcat 7

安装

切换到指定目录

$ sudo su
  • 安装PCRE
# cd /usr/local/src/
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz

# cd pcre-8.41
# ./configure
# make
# make install
  • 安装Zlib
# cd /usr/local/src/
# wget http://zlib.net/zlib-1.2.11.tar.gz

# cd zlib-1.2.11
# ./configure
# make
# make install
  • 安装ssl
# cd /usr/local/src
# wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
# tar -zxvf openssl-1.0.1t.tar.gz
  • 安装Nginx
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.12.1.tar.gz
# tar -zxvf nginx-1.12.1.tar.gz
# cd nginx-1.4.2
# ./configure \
    --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-pcre=/usr/local/src/pcre-8.41 \
    --with-zlib=/usr/local/src/zlib-1.2.11 \ --with-openssl=/usr/local/src/openssl-1.0.1t 
# make
# sudo make install

启动/关闭/重启

  • 校验配置文件
# cd /usr/local/nginx
# ./nginx -t -c nginx.conf

// nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
// nginx: configuration file /usr/local/nginx/nginx.conf test is successful

// -t 测试
// -c 指定配置文档位置
  • 启动
// 使用默认配置
# sudo /usr/local/nginx/nginx
// 使用指定配置
# ./nginx -c nginx.conf
  • 关闭
# ps -ef|grep nginx

// 找到 master 进程
// nginx    33821 33820  0 12:06 pts/17   00:00:00 -bash
// root     33862     1  0 12:09 ?        00:00:00 nginx: master process /usr/local/nginx/nginx
// nobody   33863 33862  0 12:09 ?        00:00:00 nginx: worker process
// 这里主进程号: 33862

# kill -QUIT 33862

或者直接停止

# cd /usr/local/nginx
# ./nginx -s stop
// $ sudo ./nginx -s stop
  • 重启
# cd /usr/local/nginx

# ./nginx -s reload
  • 查看配置信息
# cd /usr/local/nginx

# ./nginx -v
// nginx version: nginx/1.12.1

# ./nginx -V
// nginx version: nginx/1.12.1
// built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 
// built with OpenSSL 1.0.1t  3 May 2016
// TLS SNI support enabled
// configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/npinx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.0.1t
root@nginx-virtual-machine:/usr/local/nginx# 

# ./nginx -t
// 检查配置文件是否正确
// nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
// nginx: configuration file /usr/local/nginx/nginx.conf test is successful
  • 查看帮助
# cd /usr/local/nginx
# ./nginx -h
// 或者使用 ./nginx -?

// nginx version: nginx/1.12.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: /usr/local/nginx/)
//   -c filename   : set configuration file (default: /usr/local/nginx/nginx.conf)
//   -g directives : set global directives out of configuration file

更新配置文件

  • 使用新的配置文件, 替换正在工作的配置文件(查看更多)
# cd /usr/local/nginx

// 测试配置文件
# ./nginx -t -c conf/new_nginx.conf
# kill -HUP [Nginx Master PID]

代理配置

修改nginx.conf配置文件

  • 不同请求, 转发到不同的服务器
http {

    include mime.types;

    # 配置第一个代理规则
    server {

        # 配置要监听的端口
        listen        9180;

        # 域名, 多个用空格' '隔开
        server_name   localhost 127.0.0.1;   

        # 代理所有 http://localhost:9180 请求
        location / {

            # 服务器所在位置
            root        /usr/local/tomcat/apache-tomcat-7.0.64;


            index       index.html index.htm index.jsp;

            # tomcat 动态解析, 直接代理地址即可
            proxy_pass  http://localhost:8180; 
        }
    }

    # 配置第二个代理规则
    server {
        listen        9080;
        server_name   localhost;    
        location / {
            root        /usr/local/tomcat/apache-tomcat-7.0.64;
            index       index.html index.htm index.jsp; 
            proxy_pass  http://localhost:8080;
        }
    }

}
  • 不同请求, 转发到相同的服务器
http {
    include mime.types;
    server {
        listen      8888;
        server_name localhost;
        location    / {
            root        /usr/local/tomcat/apache-tomcat-7.0,64;
            index       index.html index.jsp;
            proxy_pass  http://localhost:8080;
        }
    }
    server {
        listen      9999;
        server_name localhost;
        location    / {
            root        /usr/local/tomcat/apache-tomcat-7.0,64;
            index       index.html index.jsp;
            proxy_pass  http://localhost:8080;
        }
    }
}
  • 相同域名, 通过目录反向代理到不同的服务器
    下面的配置代理所有 http://[localhost]:9090/ 地址, /t1 由8080服务器处理, /t2 由 8081服务器处理, 其它路径由 location / {} 处理
http {
    server {
        listen          9090;
        server_name     localhost;

        # 匹配路径 http://[ip|localhost]:9090/t1
        location /t1 {
            root        /usr/local/tomcat/apache-tomcat-7.0.64;
            index       index.html index.htm index.jsp;
            proxy_pass  http://localhost:8080/test; 
        }

        # 匹配路径 http://[ip|localhost]:9090/t2
        location /t2 {
            root        /usr/local/tomcat/apache-tomcat-7.0.64;
            index       index.html index.htm index.jsp;
            proxy_pass  http://localhost:8180/test; 
        }

        # 其它路径, 如 /r1, /other1...
        location / {
            root        /usr/local/tomcat/apache-tomcat-7.0.64;
            index       index.html index.htm index.jsp;
            proxy_pass  http://localhost:8180/test; 
        }

    }
}
  • 通过配置Upstream实现负载均衡
http {
    server {
        listen          7777;
        server_name     localhost 127.0.0.1;
        location / {
            proxy_pass  http://backend;
        }
    }

    upstream backend {
        server          192.168.1.128:8080;
        server          192.168.1.128:8180;
    }
}

遇到的问题:

  • SSH 客户端链接失败
$ sudo apt-get install openssh-server
  • 提示丢失nginx.pid解决方案
# ./nginx -s stop
// 或找到 master PID 
// kill -QUIT [master PID]

// 重新启动
# ./nginx -c nginx.config
// $ sudo ./nginx -c nginx.conf
  • 文件上传下载
    如果没有安装, 根据系统提示安装即可(lrzsz)
// 上传
# rz

// 下载
# sz [file_name]

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值