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]

参考资料:

  • 中文文档 http://www.nginx.cn/doc/
  • 官方文档 http://nginx.org/en/docs/

你可能感兴趣的:(nginx)