编译安装NGINX

1. 查看安装系统环境
[root@web01 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@web01 ~]# uname -r
2.6.32-573.el6.x86_64
[root@web01 ~]# uname -m
x86_64
 

2.  采用yum安装方式安装pcre库(rewrite模块所需)

[root@web01 ~]# yum install pcre pcre-devel -y
[root@web01 ~]# rpm -qa pcre pcre-devel
pcre-7.8-7.el6.x86_64
pcre-devel-7.8-7.el6.x86_64
 

3. 采用yum安装方式安装Nginx基础依赖包openssl、openssl-devel包

[root@web01 ~]# yum install openssl openssl-devel -y
[root@web01 ~]# rpm -qa  openssl openssl-devel
openssl-devel-1.0.1e-48.el6_8.1.x86_64
openssl-1.0.1e-48.el6_8.1.x86_64
 
4.创建下载目录存放软件并安装
[root@web01 ~]# mkdir -p /tools   
#下载安装包路径
[root@web01 nginx-1.6.3]# mkdir -p /application
#软件安装路径
[root@web01 ~]# cd /tools
[root@web01 tools]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
[root@web01 tools]# ls -l nginx-1.6.3.tar.gz 
-rw-r--r-- 1 root root 805253 Apr  8  2015 nginx-1.6.3.tar.gz
[root@web01 tools]# tar xf nginx-1.6.3.tar.gz 
[root@web01 tools]# cd nginx-1.6.3
安装编译参数
[root@web01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
[root@web01 nginx-1.6.3]# make && make install
[root@web01 nginx-1.6.3]# ln -s /application/nginx-1.6.3/  /application/nginx
#创建软连接便于后续软件版本升级
 
5.检查语法并启动Nginx,chakan nginx对应服务的端口是否启动成功
[root@web01 tools]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 tools]# lsof -i:80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1435  root    7u  IPv4  10146      0t0  TCP *:http (LISTEN)
nginx   1438 nginx    7u  IPv4  10146      0t0  TCP *:http (LISTEN)
或者
[root@web01 tools]# netstat -lnt|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN   
[root@web01 tools]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4456/nginx
[root@web01 tools]# ps -ef|grep nginx
root      4456     1  0 14:03 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx     4457  4456  0 14:03 ?        00:00:00 nginx: worker process        
root      4475  4347  0 14:12 pts/0    00:00:00 grep nginx
 
6. 检查Nginx启动实际效果,windows下浏览器输入IP地址,Linux下可以使用wget 127.0.0.1或者curl 127.0.0.1
 
小技能:
1.使用./configure --help查看帮助,编译参数详解:
 ./configure 
--user=nginx                                   #设置进程用户权限
--group=nginx                                 #设置进程用户组权限
--prefix=/application/nginx-1.6.3/    #设置安装路径
--with-http_stub_status_module    #激活状态信息
--with-http_ssl_module                  #激活ssl功能
 
2.查看服务器安装nginx编译时的参数(参数大写V)
[root@web01 tools]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
 
3.查看nginx.pid进程号并查杀
[root@web01 tools]# cat /application/nginx/logs/nginx.pid 
[root@web01 tools]#kill -HUP `cat /application/nginx/logs/nginx.pid`
 
 
错误实例:
1. Nginx服务无法启动,并出现如下提示
[root@web01 tools]# /application/nginx/sbin/nginx 
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
解决办法:
[root@web01 tools]# killall -9 nginx
 
2.无法正常启动nginx服务,提示nginx: [emerg] getpwnam("nginx") failed
        [root@web01 tools]# /application/nginx/sbin/nginx
        nginx: [emerg] getpwnam("nginx") failed
解决办法:
[root@web01 tools]# useradd nginx -s /sbin/nologin -M
 
3.编译安装pcre,gcc不全导致报错(yum安装不存在此错误)
[root@web01 nginx-1.6.3]# make && make install
make all-am
make【1】: entering directory .....
cxx pcrecpp.lo
libtool:complie:unrecongnized option '-DHAVE_CONFIG_H'
...........
解决办法:
执行“ yum install gcc-c++” 命令安装gcc-c++依赖包
 
4.浏览器无法访问nginx页面
[root@web01 nginx-1.6.3]# /etc/init.d/iptables stop
[root@web01 nginx-1.6.3]# chkconfig iptables off
[root@web01 nginx-1.6.3]# setenforce 0   #临时关闭
[root@web01 nginx-1.6.3]# vim /etc/selinux/config  #更改后重启永久关闭
 
 


常用编译参数选项说明

       nginx大部分常用模块,编译时./configure --help--without开头的都默认安装。

  • --prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
  • --conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
  • --user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。--group=name类似
  • --with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用--with-pcre自动找到库文件。使用--with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 - 8.30)并解压,剩下的就交给Nginx的./configuremake来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
  • --with-zlib=PATH : 指定 zlib(版本1.1.3 - 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
  • --with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
  • --with-http_stub_status_module : 用来监控 Nginx 的当前状态
  • --with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
  • --add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)
 

参考阅读

 
posted @ 2018-03-08 13:50  秋天的风吹过原野  阅读(704)  评论(0编辑  收藏  举报