nginx服务(四)_编译安装

概述
Nginx的安装版本分为Mainline version(主要开发版本,其实就是还处于开发版)、Stable version(当前最新稳定版)
和Legacy versions(旧的稳定版), Nginx安装可以使⽤yum或源码安装,但是推荐使⽤源码,⼀是yum的版本⽐较
旧,⼆是编译安装可以更⽅便⾃定义相关路径,三是使⽤源码编译可以⾃定义相关功能,更⽅便业务的上的使⽤,
源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即
LGPL许可,是⾃由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语⾔,所以原
名为GNU C语⾔编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective-C,java以及Ada等其他
语⾔,此外还需要Automake⼯具,以完成⾃动创建Makefile的⼯作,Nginx的⼀些模块需要依赖第三⽅库,⽐如
pcre(⽀持rewrite),zlib(⽀持gzip模块)和openssl(⽀持ssl模块)等。

一、准备编译
安装的基础环境:

[root@s2 ~]# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel nettools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed

注:
gcc为GNU Compiler Collection的缩写,可以编译C和C++源代码等,它是GNU开发的C和C++以及其他很多种语⾔ 的编译器(最早的时候只能编译C,后来很快进化成⼀个编译多种语⾔的集合,如Fortran、Pascal、Objective- C、Java、Ada、 Go等。)

gcc 在编译C++源代码的阶段,只能编译 C++ 源⽂件,⽽不能⾃动和 C++ 程序使⽤的库链接(编译过程分为编 译、链接两个阶段,注意不要和可执⾏⽂件这个概念搞混,相对可执⾏⽂件来说有三个重要的概念:编译 (compile)、链接(link)、加载(load)。源程序⽂件被编译成⽬标⽂件,多个⽬标⽂件连同库被链接成⼀个最 终的可执⾏⽂件,可执⾏⽂件被加载到内存中运⾏)。因此,通常使⽤ g++ 命令来完成 C++ 程序的编译和连接,该 程序会⾃动调⽤ gcc 实现编译。

gcc-c++也能编译C源代码,只不过把会把它当成C++源代码,后缀为.c的,gcc把它当作是C程序,⽽g++当作是 c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。

automake是⼀个从Makefile.am⽂件⾃动⽣成Makefile.in的⼯具。为了⽣成Makefile.in,automake还需 ⽤到perl,由于automake创建的发布完全遵循GNU标准,所以在创建中不需要perl。libtool是⼀款⽅便⽣成各种程 序库的⼯具。

pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的 Rewrite模块和HTTP 核⼼模块会使⽤到PCRE正则表达式语法。

zlip zlib-devel:nginx启⽤压缩功能的时候,需要此模块的⽀持。

openssl openssl-devel:开启SSL的时候需要此模块的⽀持。

二、安装Nginx:
官⽅源码包下载地址:
https://nginx.org/en/download.html

[root@s2 ~]# cd /usr/local/src/
[root@s2 src]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@s2 src]# tar xf nginx-1.16.1.tar.gz
[root@s2 src]# cd nginx-1.16.1/

编译是为了检查系统环境是否符合编译安装的要求,⽐如是否有gcc编译⼯具,是否⽀持编译参数当中的模块,并根据
开启的参数等⽣成Makefile⽂件为下⼀步做准备:

./configure --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/nginx-1.16.1/src/echo-nginx-module --with-file-aio --add-module=/usr/local/src/nginx-1.16.1/src/openssl-1.1.1d
./configure \
--prefix=/apps/nginx \
--with-http_ssl_module \
--with-http_v2_module \
 --with-http_realip_module \
 --with-http_stub_status_module\
 --with-http_gzip_static_module \
 --with-pcre --with-stream \
 --with-stream_ssl_module \
 --with-stream_realip_module \
 --add-module=/usr/local/src/nginx-1.16.1/src/echo-nginx-module \
 --with-file-aio --add-module=/usr/local/src/nginx-1.16.1/src/openssl-1.1.1d
[root@s2 nginx-1.16.1]# make #编译步骤,根据Makefile⽂件⽣成相应的模块
[root@s2 nginx-1.16.1]# make install #创建⽬录,并将⽣成的模块和⽂件复制到相应的⽬录:
[root@s2 nginx-1.16.1]# useradd nginx -s /sbin/nologin -u 2000 #以普通⽤⼾启动nginx
[root@s2 nginx-1.16.1]# chown nginx.nginx -R /apps/nginx/

备注:nginx完成安装以后,有四个主要的⽬录:
conf:该⽬录中保存了nginx所有的配置⽂件,其中nginx.conf是nginx服务器的最核⼼最主要的配置⽂件,其他 的.conf则是⽤来配置nginx相关的功能的,例如fastcgi功能使⽤的是fastcgi.conf和fastcgi_params两个⽂ 件,配置⽂件⼀般都有个样板配置⽂件,是⽂件名.default结尾,使⽤的使⽤将其复制为并将default去掉即可。

html:该⽬录中保存了nginx服务器的web⽂件,但是可以更改为其他⽬录保存web⽂件,另外还有⼀个50x的web⽂件是 默认的错误⻚⾯提⽰⻚⾯。

logs:该⽬录⽤来保存nginx服务器的访问⽇志错误⽇志等⽇志,logs⽬录可以放在其他路径,⽐ 如/var/logs/nginx⾥⾯。

sbin:该⽬录⽤来保存nginx⼆进制启动脚本,可以接受不同的参数以实现不同的功能。 验证版本及编译参数

root@ubuntu:/usr/local/src/nginx-1.16.1# nginx -V
nginx version: nginx/1.16.1
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/nginx-1.16.1/src/echo-nginx-module --with-file-aio --add-module=/usr/local/src/nginx-1.16.1/src/openssl-1.1.1d

三、创建Nginx⾃启动脚本:

root@ubuntu:/usr/local/src/nginx-1.16.1# cat /lib/systemd/system/nginx.service 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/bin/rm -f /apps/nginx/logs/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target

四、验证Nginx⾃启动脚本:

[root@s2 nginx-1.16.1]# systemctl daemon-reload
[root@s2 nginx-1.16.1]# systemctl start nginx
[root@s2 nginx-1.16.1]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.
[root@s2 nginx-1.16.1]# systemctl status nginx

五、查看帮助

[root@s1 ~]# nginx -h
nginx version: nginx/1.16.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/share/nginx/) #指定Nginx ⽬录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置⽂件路径
-g directives : set global directives out of configuration file #设置全局指令
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值