积微成著 不积跬步,无以至千里

首页   >   web开发   >   linux环境下nginx1.13.6二进制方式安装教程

linux环境下nginx1.13.6二进制方式安装教程

Nginx是一个高性能的Http和反向代理服务器,稳定性高、丰富的功能集、低消耗、并发能力强,传说能够支持高达50000哥并发连接的响应,,而且可以在大多数平台上编译运行,还可以做简单的负载均衡和静态文件缓存,听起来这么牛叉,想来这玩意儿应该很笨重?非也,非但不大,而且还很轻巧,很好地诠释了什么叫浓缩的都是精华。

单纯用来做Http服务器,感觉有些浪费,今天就来教大家如何在linux环境下安装这一款大名鼎鼎的软件,并使用它的反向代理机制。

目前为止,官网的最新版本是1.13.6,老规矩,还是直接去官网(http://nginx.org/en/download.html)下载,安全,纯净。

linux环境下安装nginx

下载页面非常简洁,直接就可以看到红色框框的区域,点击下载,在linux服务器上可以先找到相应版本的下载链接,然后使用wget下载。

[root@jdu4e00u53f7 local]# wget http://nginx.org/download/nginx-1.13.6.tar.gz
--2017-10-18 15:59:42--  http://nginx.org/download/nginx-1.13.6.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ...
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 989760 (967K) [application/octet-stream]
Saving to: ‘nginx-1.13.6.tar.gz’

100%[====================================================================================================================================================================================================================================>] 989,760      389KB/s   in 2.5s   

2017-10-18 15:59:45 (389 KB/s) - ‘nginx-1.13.6.tar.gz’ saved [989760/989760]

这种跟mysql的二进制安装方式一样,直接是用tar命令解压出来

[root@jdu4e00u53f7 nginx]# tar -zxvf nginx-1.13.6.tar.gz

使用configure对即将安装的软件进行配置,此命令可以用来检测安装平台的目标特征,还可以用来指定安装位置。

[root@jdu4e00u53f7 nginx]# ./configure
checking for OS
 + Linux 3.10.0-514.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

诶,出错了,这错误相当熟悉了,在 linux安装redis报错cc not found 一文中已经讲过了,不再多说,直接yum install

[root@jdu4e00u53f7 nginx]# yum install -y gcc

再次configure,发现依旧出错了

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

虽然英语不咋地,第一句还是看懂了,缺失了pcre库

[root@jdu4e00u53f7 nginx]# yum install -y pcre pcre-devel

又失败了,依旧是缺失相关的库,这次是zlib

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

直接 yum install 一个

[root@jdu4e00u53f7 nginx]# yum install -y zlib zlib-devel

呼,总算可以了

[root@jdu4e00u53f7 nginx]# ./configure 
checking for OS
 + Linux 3.10.0-514.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

日志信息有些多,可以直接看底部,可以看到一些列的配置信息,一些默认的配置都是将nginx放到 /usr/local/nginx 文件夹下面,这也是我建议大家安装一些软件都放到 /usr/local 下面的原因,当然,大家也可以使用prefix参数来指定位置,还有很多参数,具体大家可以去查一下关于configure脚本的资料。

环境和配置都OK了,使用make命令来安装,关于make命令,在 redis3.2的安装 中已经讲过了

linux环境下nginx1.13.6二进制方式安装教程

如上图,日志信息是真的多,两屏都放不完,就不放了,太占地方了。

上一步执行完毕,就已经安装完毕了,启动命令在objs文件夹中,直接执行起来

[root@jdu4e00u53f7 nginx]# ./objs/nginx 
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2017/10/18 16:36:46 [emerg] 16398#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)

然而,又报错了,日志文件没有生成,之前没有碰到过这个问题,也不知道是不是最新版本的原因,不过没关系,我们手动创建logs文件夹和access.log以及error.log

linux环境下nginx1.13.6二进制方式安装教程

访问 http://localhost ,看到如下界面,说明nginx已经启动成功了

linux环境下nginx1.13.6二进制方式安装教程

但是此时仅仅是作为一个服务访问,并没有反向代理的功能,接下来我们就来配置一下它的反向代理

找到 nginx/conf 文件夹中的 nginx.conf 文件,在server区块中配置自己的转发规则,里面其他的配置可以不用改动

server {
    listen       80;
    server_name  www.test.com;
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

每一个server,就代表着一个转发,所以一台nginx服务器上,可能会有很多server,注意server_name和proxy_pass别写重复了

修改完毕之后,再次启动nginx,可以使用 -c 参数来指定启动时加载的配置文件

[root@jdu4e00u53f7 nginx]# ./objs/nginx -c conf/nginx.conf

只要这个服务没关闭,以后如果再有改动,直接重新加载就好,不用每次都先关闭服务再开启服务

[root@jdu4e00u53f7 nginx]# ./objs/nginx -s reload

好了,现在只需要去服务器的管理界面,更改一下dns配置,将域名绑定到nginx所在的服务器,搞定收工!

QQ群:积微成著官方群(686430774),验证消息:积微成著

站长Q:1347384268(加好友请注明来意)

分享到:

欢迎分享本文,转载请注明出处!

作者:不忘初心

发布时间:2017-10-18

永久地址:http://www.jiweichengzhu.com/article/0c829722d4ee41a1bf1bca84ca46488a