centos7.2源码安装nginx
首先安装缺少的依赖包:
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
从https://nginx.org/en/download.html下载nginx-1.12.2.tar.gz,下载压缩包放在任意位置,建议放在/usr/local/src或者/root下面,/usr/local/src就是系统专门给用户存放源码的目录,/root/是默认操作的目录。所以也可以放在这里。
下载通过命令:
wget http://nginx.org/download/nginx-1.12.2.tar.gz
解压缩:
tar -zxvf nginx-1.12.2.tar.gz
解压后进入nginx-1.12.2目录下执行配置和make命令
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
nginx-1.10.1 make && make install
常用编译选项说明:
-
--prefix=PATH
: 指定nginx
的安装目录。默认/usr/local/nginx
,我的是/usr/local/webserver/nginx
-
--conf-path=PATH
: 设置nginx.conf
配置文件的路径。nginx
允许使用不同的配置文件启动,通过命令行中的-c
选项。默认为conf/nginx.conf
-
--user=name
: 设置nginx
工作进程的用户。安装完成后,可以随时在nginx.conf
配置文件更改user
指令。默认的用户名是nobody
。--group=name
类似 -
--with-pcre
: 设置PCRE库的源码路径,如果已通过yum方式安装,使用--with-pcre自动找到库文件。使用--with-pcre=PATH
时,需要从PCRE网站下载pcre库的源码(8.39)并解压,指定pcre
的源码路径 ,比如:--with-pcre=/root/pcre-8.39/
。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
时无需重新编译)
启动|停止nginx
/usr/local/webserver/nginx/sbin/nginx #启动nginx
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止nginx
安全停止 ./nginx -s quit 修改配置重新加载 ./nginx -s reload
再浏览器输入ip: