源码包安装nginx
环境:
CentOS 7
nginx:nginx-1.16.1
服务器 ip:192.168.184.137
1、配置基础环境
(1)关闭防火墙、selinux
[root@localhost ~]# systemctl stop firewalld # 关闭防火墙
[root@localhost ~]# systemctl disable firewalld # 开机自动关闭防火墙
[root@localhost ~]# getenforce #查看selinux的状态
enforcing
[root@localhost ~]# setenforce 0 # 临时关闭selinux
[root@localhost ~]# vim /etc/selinux/config #永久关闭selinux,但是要重启机器才会生效(reboot)
SELINUX=disabled #将enforcing修改为disabled
(2)源码编译环境的安装
[root@localhost ~]# yum -y install wget net-tools lsof vim #下载要用的工具
###下载源码编译工具
[root@localhost ~]# yum -y install gcc make zlib-devel pcre pcre-devel openssl-devel
[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz #下载nginx源码安装包
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.16.1.tar.gz #下载成功
2、解压并编译nginx
[root@localhost ~]# tar xf nginx-1.16.1
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.16.1 nginx-1.16.1.tar.gz
[root@localhost ~]# cd nginx-1.16.1
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
checkig for OS
...
[root@localhost nginx-1.16.1]# make #编译成二进制文件
[root@localhost nginx-1.16.1]# make install #编译安装
或者 两条命令可以一起执行
[root@localhost nginx-1.16.1]# make && make install
上述编译内容的解释:
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx的解析
--prefix=/usr/local/nginx #指定的nginx的安装目录
--user=nginx #指定用户
--group=nginx #指定组
除此以外还可以添加模块:例如常见的模块
--conf-path=/usr/local/nginx/conf/nginx.conf #指定配置文件
--http-log-path=/var/log/nginx/access.log #指定访问日志
--error-log-path=/var/log/nginx/error.log #指定错误日志
--lock-path=/var/lock/nginx.lock #指定lock文件
--pid-path=/run/nginx.pid #指定pid文件
--http-client-body-temp-path=/var/lib/nginx/body #设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi #设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy #设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi #设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi #设定http uwsgi临时文件路径
--with-debug #启用debug日志
--with-pcre-jit #编译PCRE包含“just-in-time compilation”
--with-http_ssl_module #启用ssl支持
--with-http_stub_status_module #获取nginx自上次启动以来的状态
--with-http_realip_module #允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module #实现基于一个子请求的结果的客户端授权
--with-http_addition_module #作为一个输出过滤器,支持不完全缓冲,分部分响应请求
--with-http_dav_module #增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_geoip_module #使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
--with-http_gzip_static_module #在线实时压缩输出数据流
--with-http_image_filter_module #传输JPEG/GIF/PNG 图片的一个过滤器(默认为不启用。gd库要用到)
--with-http_spdy_module #SPDY可以缩短网页的加载时间
--with-http_sub_module #允许用一些其他文本替换nginx响应中的一些文本
--with-http_xslt_module #过滤转换XML请求
--with-mail #启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module #启用ngx_mail_ssl_module支持启用外部模块支持
4、绝对路径启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx #会发现报错,是因为没有nginx用户
nginx:[emerg] getpwnam("nginx") failed
[root@localhost ~]# useradd nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# netstat -nltp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10968/nginx: master
或者用lsof查看端口
[root@localhost ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4259 root 6u IPv4 23870 0t0 TCP *:http (LISTEN)
nginx 4260 nginx 6u IPv4 23870 0t0 TCP *:http (LISTEN)
5、停止nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
6.测试页面
在浏览器访问服务器地址 — 192.168.184.137