1、官方下载nginx压缩包
官网下载Nginx压缩包 http://nginx.org/en/download.html
或者wget https://nginx.org/download/nginx-1.25.5.tar.gz
2、安装nginx
2.1、将下载的压缩包上传至linux服务器
2.2、安装 nginx 的相关依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.3、解压压缩包
tar -zxvf nginx-1.22.0.tar.gz
2.4、生成 Makefile 可编译文件
1、安装编译环境
yum -y install gcc gcc-c++
2、安装pcre软件包(使nginx支持http rewrite模块)
yum install -y pcre-devel
3、安装openssl-devel (使nginx支持ssl)
yum install -y openssl openssl-devel
4、安装zlib
yum install -y zilb zlib-devel
# 查看 nginx 安装的模块
/usr/local/nginx/sbin/nginx -V
# 模块参数具体功能
--with-cc-opt='-g -O2 -fPIE -fstack-protector //设置额外的参数将被添加到CFLAGS变量。(FreeBSD或者ubuntu使用)
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
--prefix=/usr/local/nginx //指向安装目录
--conf-path=/etc/nginx/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-ipv6 //启用ipv6支持
--with-http_ssl_module //启用ssl支持
--with-http_stub_status_module //获取nginx自上次启动以来的状态
--with-http_realip_module //允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module //实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_addition_module //作为一个输出过滤器,支持不完全缓冲,分部分响应请求
--with-http_dav_module //增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_geoip_module //使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
--with-http_gunzip_module //它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
--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支持启用外部模块支持
cd nginx-1.22.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module
2.5、编译安装
make && make install
3、启动nginx
3.1、进入安装目录
cd nginx 安装目录(默认 /usr/local/nginx/sbin)
3.2、执行启动命令
./nginx
3.3、查看是否启动成功
ps -ef | grep nginx
nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx:
nginx -s reload # 修改配置后重新加载生效
nginx -s reopen # 重新打开日志文件
nginx -s stop # 快速停止nginx
nginx -s quit # 完整有序的停止nginx
nginx -t # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf # 测试特定的nginx配置文件是否正确
whereis nginx # 查看安装路径
which nginx # 查询文件路径
ls -l /proc/xxx/exe # 获取配置文件
注意:
nginx -s reload 命令加载修改后的配置文件,命令下达后发生如下事件
1. Nginx的master进程检查配置文件的正确性,若是错误则返回错误信息,nginx继续采用原配置文件进行工作(因为worker未受到影响)
2. Nginx启动新的worker进程,采用新的配置文件
3. Nginx将新的请求分配新的worker进程
4. Nginx等待以前的worker进程的全部请求已经都返回后,关闭相关worker进程
5. 重复上面过程,直到全部旧的worker进程都被关闭掉
查看防火墙状态
systemctl status firewalld.service
关闭防火墙
systemctl stop firewalld.service
重启
systemctl restart firewalld.service
启动
systemctl start firewalld.service
禁止开机启动
systemctl disable firewalld.service
设置开机启用
systemctl enable firewalld.service
查看是否开机启动
systemctl is-enabled firewalld.service
查看已启动的服务列表
systemctl list-unit-files|grep enabled
查看已经开放的端口
firewall-cmd --list-ports
开启端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
4、设置反向代理
/usr/local/nginx/conf(默认安装路径)目录中的nginx.conf配置文件
将端口改为自己项目的端口
5、重启nginx
cd nginx 安装目录(默认 /usr/local/nginx/sbin)
./nginx -s reload
6、关闭nginx
方式一 ./nginx -s stop
方式二 ./nginx -s quit
这两个命令的区别在于nginx -s stop是快速停止Nginx,而nginx -s quit是有序的停止Nginx,前者可能会导致数据没有完全保存;
方式三 taskkill /F /IM nginx.exe > nul
这种方法可以直接在cmd命令面板上使用,不必跑到Nginx的安装包下运行,当前两种方法无法奏效时可以尝试使用此方法,前两种方法适用于大部分版本的Nginx,但是个别版本的可能不使用,使用这个基本上就能解决了~