Nginx安装记录

注意:如果用源码安装,nginx配置时需要指定--with-pcer对应的压缩包路径,如果使用二进制安装不需要指定

依赖包使用二进制yum一键安装:yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

一键安装开发工具包:yum -y groupinstall "Development Tools" "Development Libraries"

下载Nginx

wget -c -P /opt/tmp/ http://nginx.org/download/nginx-1.15.6.tar.gz

Nginx安装所需依赖包

1、rewrite模块需要pcre库(下载: http://www.pcre.org)    支持nginx伪静态

wget -c -P /opt/tmp/ https://ftp.pcre.org/pub/pcre/pcre-8.39.tar.gz

2、ssl模块需要openssl库(下载: http://www.openssl.org)    nginx扩展

wget -c -P /opt/tmp/ https://www.openssl.org/source/openssl-1.1.1.tar.gz

3、gzip模块需要zlib库(下载:http://www.zlib.net)      nginx扩展

wget -c -P /opt/tmp/ http://www.zlib.net/zlib-1.2.11.tar.gz


编译安装Nginx所需依赖包


pcre:

tar zxvf pcre-8.39.tar.gz

cd pcre-8.39/

./configure --prefix=/usr/local/pcre 

make

make install 


openssl:

tar zxvf openssl-1.1.1.tar.gz

cd openssl-1.1.1/

*注意此处

./config --prefix=/usr/local/openssl

make&&make install


tar zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11/

./configure --prefix=/usr/local/zlib

make&&make install


安装Nginx

groupadd -r nginx

useradd -r -g nginx -s /bin/false -M nginx

tar zxvf nginx-1.15.6.tar.gz 

cd nginx-1.15.6/

 ./configure --prefix=/usr/local/nginx \   set installation prefix   

--without-http_memcached_module \        disable ngx_http_memcached_module

--user=nginx \                     set non-privileged user for worker processes

--group=nginx \               set non-privileged group for worker processes

--with-http_stub_status_module \         取得一些nginx的运行状态

--with-http_ssl_module \             开启HTTP SSL模块,以支持HTTPS请求。

--with-http_gzip_static_module \         预压缩文件传输前检查,防止文件被重复压缩

--with-pcre=/opt/tmp/pcre-8.39 \        *路径指向解压源码所在的目录

--with-openssl=/opt/tmp/openssl-1.1.1 \    *路径指向解压源码所在的目录

--with-zlib=/opt/tmp/zlib-1.2.11         *路径指向解压源码所在的目录

make

make install

*注:编译好后可通过/usr/local/nginx/sbin/nginx -V (Nginx安装的路径)查看编译时候的参数

启动Nginx服务 

/usr/local/nginx/sbin/nginx 

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

参数“-c”指定了Nginx配置文件的路径,如果不加“-c”参数,Nginx会默认加载其安装目录的conf子目录中的nginx.conf文件.


查看监听端口netstat -nptl

nginx启动、重启、关闭
一、启动  
cd usr/local/nginx/sbin
./nginx
二、重启
  更改配置重启nginx  
kill -HUP 主进程号或进程号文件路径
或者使用
cd /usr/local/nginx/sbin
./nginx -s reload
    判断配置文件是否正确 
nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
cd  /usr/local/nginx/sbin
./nginx -t
三、关闭
  查询nginx主进程号
  ps -ef | grep nginx
  从容停止   kill -QUIT 主进程号
  快速停止   kill -TERM 主进程号
  强制停止   kill -9 nginx
  若nginx.conf配置了pid文件路径,如果没有,则在logs目录下
  kill -信号类型 '/usr/local/nginx/logs/nginx.pid'
四、升级
  1、先用新程序替换旧程序文件
  2、kill -USR2 旧版程序的主进程号或者进程文件名
    此时旧的nginx主进程会把自己的进程文件改名为.oldbin,然后执行新版nginx,此时新旧版本同时运行
  3、kill -WINCH 旧版本主进程号
  4、不重载配置启动新/旧工作进程
    kill -HUP 旧/新版本主进程号
    从容关闭旧/新进程
    kill -QUIT 旧/新进程号
    快速关闭旧/新进程
    kill -TERM 旧/新进程号

Nginx关闭版本信息显示

nginx出错会在http头显示醒目的版本号提示,为了安全需要关闭这些信息。

方法很简单,只需在nginx.conf的http中加入server_tokens参数

http {

    include       mime.types;

    default_type  application/octet-stream;

    server_tokens off;

}


防火墙

Centos 7默认启用Firewalld

访问Nginx需要开放80端口

开启服务

firewall-cmd --permanent --add-service=http

开启端口
firewall-cmd --permanent --zone=trusted --add-port=80/tcp

命令含义:

--permanent    #使命令永久生效 无此参数重启后失效

--zone  #作用域

--add-service=http    #添加服务

--add-port=80/tcp    #添加端口 格式为:端口号/通讯协议

查看已经开放的端口:

firewall-cmd --list-ports

查看已开发的服务

firewall-cmd --list-services

重启防火墙

firewall-cmd --reload

或/bin/systemctl restart firewalld.service

查看防火墙状态

firewall-cmd --state