服务启停
系统服务启停
#常规重启
systemctl enable nginx
systemctl stop nginx
systemctl start nginx
systemctl status nginx
systemctl reload nginx
systemctl restart nginx
systemctl disable nginx
#修改配置文件用户用nginx
sed -i '/^worker_processes/i\user nginx nginx;' /usr/local/nginx/conf/nginx.conf
#修改环境变量用nging启动
sed -i '/^export/i\PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin' ~/.bash_profile
source ~/.bash_profile
配置文件重启
#启动
cd /usr/local/nginx/sbin/ && ./nginx
#停止
ps -ef|grep nginx 杀死对应的进程号即可
[root@localhost sbin]# ./nginx -s stop #快速关闭
[root@localhost sbin]# ./nginx -s quit #优雅关闭,等待工作进程处理完成后关闭
ps -ef|grep nginx 杀死对应的进程号即可,简单粗暴
#重启
[root@localhost sbin]# ./nginx -s reload #热重启,重新加载配置文件
[root@localhost sbin]# ./nginx -s reopen #重启nginx
验证是否启动成功
#方式1 查看进程
[root@localhost sbin]# ps -ef|grep nginx | grep -v grep
root 3992 1 0 21:21 ? 00:00:00 nginx: master process ./nginx
nobody 3993 3992 0 21:21 ? 00:00:00 nginx: worker process
#方式2 浏览器直接访问 记得关闭防火墙
http://ip 直接访问即可,默认端口是80
#方式3 查看端口是否启动
[root@localhost ~]# netstat -nlput|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3992/nginx: master
rpm安装
下载地址
https://nginx.org/packages/centos/7/x86_64/RPMS/
安装
#直接采用rpm格式包安装,快捷方便
rpm -ivh nginx-1.24.0-1.el7.ngx.x86_64.rpm
rpm安装后的相关配置信息
/etc/nginx/nginx.conf 主配置文件,会默认把这个文件夹中所有子配置项都引入
/etc/nginx/conf.d/ 是子配置项存放处
/usr/share/nginx/html/ 静态文件都放在这个文件夹,也可以根据你自己的习惯放在其他地方
/etc/nginx/nginx.conf.default 默认配置文件
# 可执行程序文件
/usr/bin/nginx-upgrade
/usr/sbin/nginx
# nginx库文件
/usr/lib/systemd/system/nginx.service # 用于配置系统守护进程
/usr/lib64/nginx/modules # Nginx模块目录
# 帮助文档
/usr/share/doc/nginx-1.16.1
/usr/share/doc/nginx-1.16.1/CHANGES
/usr/share/doc/nginx-1.16.1/README
/usr/share/doc/nginx-1.16.1/README.dynamic
/usr/share/doc/nginx-1.16.1/UPGRADE-NOTES-1.6-to-1.10
# 静态资源目录
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
# 存放Nginx日志文件
/var/log/nginx
源码编译安装
下载对应的稳定版本
http://nginx.org/en/download.html
详情安装见一键安装脚本
[root@localhost ~]# curl -O https://nginx.org/download/nginx-1.26.2.tar.gz
[root@localhost ~]# curl -O https://nginx.org/download/nginx-1.24.0.tar.gz
[root@localhost ~]# curl -O https://nginx.org/download/nginx-1.22.1.tar.gz
模块安装
情况1:在最初安装的时候就提前准备好直接执行安装即可
[root@localhost nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
情况2:在初始安装的时候没有添加需要的模块,后续新增
- 在原来的程序包解压的目录下重新执行配置文件检查下,并带上需要的模块
[root@localhost nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
- 上一步没有报错后,在执行make进行编译,记住只需要编译即可
只需执行make即可 千万不要执行make install 不然就完蛋
[root@localhost nginx-1.22.1]# make
编译完成后,就把新的执行文件拷贝到安装目录下,要记得原来的可执行文件要备份一个
新编译的执行文件在/root/nginx-1.22.1/objs目录下
[root@localhost objs]# pwd
/root/nginx-1.22.1/objs
[root@localhost objs]# ls
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
先备份原来的
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# cp nginx nginx.bak
在拷贝新的到目录下
[root@localhost sbin]# cp /root/nginx-1.22.1/objs/nginx ./
cp:是否覆盖"./nginx"? y
[root@localhost sbin]#
- 验证新模块是否安装
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
模块的解释说明
--with-http_ssl_module HTTPS 的关键模块
--with-http_realip_module 用于获取客户端请求的真实IP等作用
--with-http_image_filter_module 图片处理,实现图片放大缩小裁切等功能
--with-http_geoip_module 用于 IP 访问控制,例如黑白名单
--with-http_sub_module 用于字符串替换
--with-http_flv_module 和 --with-http_mp4_module 流媒体处理模块
--with-http_gunzip_module 和 --with-http_gzip_static_module 资源压缩,静态资源压缩
--without-http_auth_basic_module 禁用用户认证模块,该模块可以用于网页登录验证
--with-http_auth_request_module 支持第三方认证
--with-http_stub_status_module nginx 状态
--with-stream TCP / UDP 代理模块
--with-pcre=DIR 指定 PCRE 目录
--with-zlib=DIR 指定 zlib 目录
--with-openssl=DIR 指定 openssl 目录
--with-http_addition_module 用于给响应的网站追加内容,比如追加 css / js
--with-http_random_index_module 从目录中随机挑选索引
--add-module=PATH 添加其他模块
已弃用的模块
--with-ipv6 nginx从1.11.5版本开始默认自动支持ipv6,所以不再需要--with-ipv6编译选项
相关命令操作
配置文件检查
[root@localhost sbin]# ./nginx -T #查看当前nginx最终配置
[root@localhost sbin]# ./nginx -t #检查当前配置文件是否修改正确
nginx: the configuration file /usr/local/nginx-1.24.0/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.24.0/conf/nginx.conf test is successful
版本及模块查看
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.22.1
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx
ssl证书配置
验证证书是否成功
[root@iZuf60yo2r6jhkx73c9owjZ ~]# echo | openssl s_client -connect 127.0.0.1:443 -servername wangjian.run 2>/dev/null
CONNECTED(00000003)
---
Certificate chain
0 s:/CN=wangjian.run
i:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=Encryption Everywhere DV TLS CA - G2
1 s:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=Encryption Everywhere DV TLS CA - G2
i:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert Global Root G2
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIGAjCCBOqgAwIBAgIQB6fp2B11iNaEoBVQ4yYo9jANBgkqhkiG9w0BAQsFADBu
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMS0wKwYDVQQDEyRFbmNyeXB0aW9uIEV2ZXJ5d2hlcmUg
RFYgVExTIENBIC0gRzIwHhcNMjMxMDI2MDAwMDAwWhcNMjQxMDI1MjM1OTU5WjAX
MRUwEwYDVQQDEwx3YW5namlhbi5ydW4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQC1W1f1FbUuomMKTrg/5Vn/jJhWAkiUYoNI2hnb+pC46X+lCQTEzDqF
Y7PuBFJP6h+ptdqW41rJ3wwzTyxJJbHoWFmb7ZH5fWlYBCWjSeUHrM6E0k7ALrFo
FM4TUiEBTfacTMwBynCkYsmLZUxWQSLI0h89Zl1boXCyrtIEe22WQ/FiY1g8efLG
******************************省略****************************
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容