这里选用的是nginx-1.10.1稳定版,其基础依赖库有gcc、gcc-c++、pcre、zlib和openssl。nginx
pcre、zlib和openssl这三个依赖库在安装nginx时无需编译安装,下载源码包解压便可。c++
一、安装md5和sha1支持库centos
由于我选用的nginx配置里有sha和md5,须要openssl库,因此我要编译安装openssl。浏览器
1.一、先安装gccdom
yum install gcc.x86_64
1.二、再安装openssltcp
./config --prefix=/opt/openssl-1.0.2h --openssldir=/opt/openssl-1.0.2h/conf --shared make
make test make install
--openssldir是指定配置文件的目录,--shared是要求编译动态库。编译后若make test显示ALL TESTS SUCCESSFUL(通常在输出信息最后一页的最上边),说明生成的库正确,便可安装。测试
二、接下来安装nginx:atom
2.一、这个configure须要安装几个依赖库centos7
yum install gcc.x86_64 gcc-c++.x86_64 libxml2.x86_64 libxml2-devel.x86_64 libxslt.x86_64 libxslt-devel.x86_64 gd.x86_64 gd-devel.x86_64
2.二、接下来配置nginx的configurespa
./configure --prefix=/opt/nginx-1.13.6 \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module \
--with-http_perl_module=dynamic \
--with-mail \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-compat \
--with-pcre \
--with-pcre=/root/pcre-8.41 \
--with-zlib=/root/zlib-1.2.11 \
--with-libatomic \
--with-openssl=/root/openssl-1.0.2m
--with-openssl、--with-pcre和--with-zlib指定依赖库的源代码目录,nginx会根据其须要对他们进行编译。
--with-md5和--with-sha1指定须要的openssl库文件位置。
--with-threads使nginx使用线程池机制,--with-file-aio启用file aio支持。
若是不须要nginx特别处理不一样地域的访问,不建议安装--with-http_geoip_module,由于yum上没有geoip库,须要编译安装MaxMind的GeoIP库
配置完成后,会输出一下信息:
2.三、编译安装
make
make install
三、配置使用环境
3.一、确认版本号
/opt/nginx-1.10.1/sbin/nginx -V
显示版本信息和configure
3.二、启动nginx
若使用--user或--group参数,须要先添加相关用户才可以使用。不添加的话工做进程所属用户为nobody
#启动:
/opt/nginx-1.10.1/sbin/nginx
#重启
/opt/nginx-1.10.1/sbin/nginx -s reload
3.三、进程控制
#查询nginx主进程号 ps -ef | grep nginx #中止进程 kill -QUIT 主进程号 #快速中止 kill -TERM 主进程号 #强制中止 pkill -9 nginx
3.四、端口测试
netstat –na|grep 80
中止防火墙后,在其余机器浏览器中可见
3.五、配置防火墙
#centos6 vi /etc/sysconfig/iptables #在-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited以前,添加 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT service iptables restart #centos7 firewall-cmd --query-service http --permanent firewall-cmd --reload
3.六、开机启动
centos6.* :
echo "/opt/nginx-1.10.1/sbin/nginx">> /etc/rc.local
centos7.* :
自el7开始,redhat逐渐减小对rc.local的依赖。在centos7里最好使用systemctl将nginx加入系统服务
在/usr/lib/systemd/system/下新建一个nginx.service文件,文件内容为:
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart= /opt/nginx-1.10.1/sbin/nginx ExecReload= /opt/nginx-1.10.1/sbin/nginx -s restart ExecStop= /opt/nginx-1.10.1/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
Description 服务描述
After 须要预先开启哪些服务
Type 运行模式
ExeStart 开启
ExeReload 重载
ExeStop 中止
PrivateTmp 创建临时文件目录
systemctl daemon-reload #重载systemctl的守护进程
systemctl start nginx #开启nginx服务
systemctl restart nginx
systemctl stop nginx
systemctl enable nginx #nginx服务开机运行
systemctl status -l nginx #查询nginx服务状态
3.七、页面查看nginx运行状态
在/opt/nginx-1.10.1/conf/nginx.conf中,加入:
location /nginx_status { stub_status on; access_log off; #allow 127.0.0.1; #deny all; }
重启nginx,在浏览器打开ip/nginx_status
active connections – 活跃的链接数量
server accepts handled requests — 总共处理了5个链接 , 成功建立5次握手, 总共处理了60个请求
reading — 读取客户端的链接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的状况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留链接.
configure部分参数详细说明:http://blog.csdn.net/eric1012/article/details/6052154