编译安装Nginx(二)

2017年11月7日16:05:37 发表评论 616 views

1.1 查看系统环境

[root@web01 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@web01 ~]# uname -r
2.6.32-696.el6.x86_64

1.2 安装依赖库pcre-devel和openssl-devel

[root@web01 tools]# yum install -y pcre-devel openssl-devel
...
Installed:
  openssl-devel.x86_64 0:1.0.1e-57.el6           
  pcre-devel.x86_64 0:7.8-7.el6                  

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.4-5.el6         
  krb5-devel.x86_64 0:1.10.3-65.el6              
  libcom_err-devel.x86_64 0:1.41.12-23.el6       
  libkadm5.x86_64 0:1.10.3-65.el6                
  libselinux-devel.x86_64 0:2.0.94-7.el6         
  libsepol-devel.x86_64 0:2.0.41-4.el6           
  zlib-devel.x86_64 0:1.2.3-29.el6               

Complete!

[root@web02 ~]# rpm -qa "pcre|openssl"
pcre-7.8-7.el6.x86_64
openssl-1.0.1e-57.el6.x86_64

pcre全称为perl compatible regular expressions,中文译名为“perl兼容正则表达式”,安装pcre库是为了使Nginx支持具备URI重写功能的pcre库,则Nginx无法使用rewrite模块功能,Nginx的rewrite模块功能几乎是企业应用必须的。

安装openssl是为了使网站支持https加密连接。

1.3 添加nginx服务映射用户

  • CentOS6:

[root@web01 ~]# useradd -s /sbin/nologoin -M www
[root@web01 ~]# id www
uid=501(www) gid=501(www) groups=501(www)

  • CentOS 7

[root@centos7 ~]# useradd -s /sbin/nologoin -M nginx

1.4 下载nginx二进制软件包

[root@web01 ~]# cd /server/tools/
[root@web01 tools]# wget -q http://nginx.org/download/nginx-1.10.3.tar.gz
[root@web01 tools]# ls
nginx-1.10.3.tar.gz

1.5 解压并编译软件

  • CentOS 6:

[root@web01 tools]# tar xf nginx-1.10.3.tar.gz
[root@web01 tools]# ls
nginx-1.10.3  nginx-1.10.3.tar.gz

[root@web01 nginx-1.10.3]# ./configure --prefix=/application/nginx-1.10.3 --user=www --group=www --with-http_stub_status_module  --with-http_ssl_module
...
Configuration summary               #配置编译时使用了哪些库
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/application/nginx-1.10.3"
  nginx binary file: "/application/nginx-1.10.3/sbin/nginx"
  nginx modules path: "/application/nginx-1.10.3/modules"
  nginx configuration prefix: "/application/nginx-1.10.3/conf"
  nginx configuration file: "/application/nginx-1.10.3/conf/nginx.conf"
  nginx pid file: "/application/nginx-1.10.3/logs/nginx.pid"
  nginx error log file: "/application/nginx-1.10.3/logs/error.log"
  nginx http access log file: "/application/nginx-1.10.3/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"

#软件编译过程中,利用返回值,判断操作是否正确
[root@web01 nginx-1.10.3]# echo $ 
0

#编译软件
[root@web01 nginx-1.10.3]# make
...
sed -e "s|%%PREFIX%%|/application/nginx-1.10.3|" \
        -e "s|%%PID_PATH%%|/application/nginx-1.10.3/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/application/nginx-1.10.3/conf/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/application/nginx-1.10.3/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/server/tools/nginx-1.10.3'

[root@web01 nginx-1.10.3]# echo $ 
0

#编译安装
[root@web01 nginx-1.10.3]# make install
...
make[1]: Leaving directory `/server/tools/nginx-1.10.3'

[root@web01 nginx-1.10.3]# echo $ 
0

#检查安装
[root@web01 nginx-1.10.3]# cd /application/
[root@web01 application]# ll
total 4
drwxr-xr-x 6 root root 4096 Sep 11 16:02 nginx-1.10.3

  • CentOS 7

[root@centos7 tools]# tar xf nginx-1.12.1.tar.gz
[root@centos7 tools]# cd nginx-1.12.1/
[root@centos7 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx-1.12.1 --user=nginx --group=nginx --with-http_stub_status_module  --with-http_ssl_module
[root@centos7 nginx-1.12.1]# make && make install

1.5.1 编译参数说明

[root@web01 nginx-1.10.3]# ./configure --help
--prefix=PATH                     set installation prefix                       #设置安装路径
--user=USER                       set non-privileged user for worker processes  #进程用户权限
--group=GROUP                     set non-privileged group for worker processes #进程用户组权限
--with-http_ssl_module            enable ngx_http_ssl_module                    #激活ssl功能
--with-http_stub_status_module    enable ngx_http_stub_status_module            #激活状态信息
...

提示:Nginx的大部分模块功能都会默认编译到软件中,不需要单独制定编译参数,如果不指定安装路径,则默认安装到/usr/local/目录中

1.6 创建软链接

1.6.1 创建目录软链接

[root@web01 ~]# ln -s /application/nginx-1.10.3/ /application/nginx
[root@web01 application]# ll
total 4
lrwxrwxrwx 1 root root   26 Sep 11 16:13 nginx -> /application/nginx-1.10.3/
drwxr-xr-x 6 root root 4096 Sep 11 16:02 nginx-1.10.3

安装时指定版本号路径是为了便于查看区分当前使用的Nginx版本,方便以后升级;内部人员使用/application/nginx/,nginx程序升级后,只要重新建立新的到/application/nginx/的软链接即可, 这样就可以不用每次都修改程序代码,因为升级后的路径还是/application/nginx。

  • CentOS 7

[root@centos7 ~]# ln -s /usr/local/nginx-1.12.1/ /usr/local/nginx

1.6.2 创建程序软链接

[root@web01 ~]# ln -s /application/nginx-1.10.3/sbin/nginx /usr/local/bin/nginx

因为/application目录不在PATH变量中,每次需要使用全路径执行命令,在/usr/local/sbin/中创建软链接后即可直接使用nginx命令。

  • CentOS 7

[root@centos7 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

1.7 编辑配置文件

[root@web01 conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[root@web01 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

1.8 启动并检查安装结果

1.8.1 启动nginx并检查结果

[root@web01 ~]# /application/nginx/sbin/nginx  #或直接使用nginx命令
[root@web01 ~]# ps -ef | grep nginx
root      26875      1  0 16:15          00:00:00 nginx: master process /application/nginx/sbin/nginx
www       26876  26875  0 16:15          00:00:00 nginx: worker process       
root      26878   1375  0 16:16 pts/0    00:00:00 grep nginx
[root@web01 ~]# lsof -i:80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   26875 root    6u  IPv4  34405      0t0  TCP *:http (LISTEN)
nginx   26876  www    6u  IPv4  34405      0t0  TCP *:http (LISTEN)

1.8.2 停止Nginx并检查结果

[root@web01 ~]# nginx -s stop
[root@web01 ~]# ps -ef | grep nginx
root      26906   1375  0 16:33 pts/0    00:00:00 grep nginx
[root@web01 ~]# lsof -i:80

1.8.3 平滑重启Nginx并检查结果

[root@web01 ~]# nginx                   #必须要服务已经启动后才能使用 -s reload,否则报错
[root@web01 ~]# nginx -s reload
[root@web01 ~]# ps -ef | grep nginx
root      26911      1  0 16:34          00:00:00 nginx: master process nginx
www       26914  26911  0 16:34          00:00:00 nginx: worker process
root      26919   1375  0 16:35 pts/0    00:00:00 grep nginx
[root@web01 ~]# lsof -i:80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   26911 root    6u  IPv4  35376      0t0  TCP *:http (LISTEN)
nginx   26914  www    6u  IPv4  35376      0t0  TCP *:http (LISTEN)

1.8.4 实际检查启动结果

编译安装Nginx(二)

1.9 Nginx命令配置参数

[root@web01 ~]# nginx -h
nginx version: nginx/1.10.3
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: /application/nginx-1.10.3/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

  • 检查配置文件语法是否正确

[root@web01 ~]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful

  • 查看软件编译安装配置信息

[root@web01 ~]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.10.3 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

weinxin
我的微信
如果有技术上的问题可以扫一扫我的微信
版权声明
1. 本网站名称:Leon的博客
2. 本站永久网址:https://blog.leonshadow.cn
3. 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长QQ632113590进行删除处理。
4. 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5. 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6. 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
liyang