Skip to content

基础环境

# 安装gcc环境
gcc -v
yum install gcc-c++
# 安装pcre库,用户解析正则表达式
yum install pcre-devel
# 安装alib压缩解压缩依整
yum install zlib zlib-devel
# 安装openssl、SSL安全的加密的套接字协议层,用户HTTP安全传输,也就是https,如果需要指出SSL,才需要安装openssl
yum install openssl openssl-devel
## 如果没有wget命令需要安装
sudo apt-get install wget
# 下载源码包
wget http://nginx.org/download/nginx-1.10.2.tar.gz
# 下载后解压缩
tar -zxvf nginx-1.10.2.tar.gz

nginx 安装

# nginx安装
  # 0、进入nginx目录之后执行
./configure
  # 1、也可以指定安装目录,增加参数
--prefix=/user/nginx
  # 3、如果不指定路径,可以通过whereis nginx进行查询
whereis nginx
  # 4、默认安装在/usr/local/nginx

  # make: *** No rule to make target `build', needed by `default'. Stop.,
  # 报错安装:yum install -y openssl*    yum -y install ncurses-devel
  yum install -y openssl*    yum -y install ncurses-devel
  # 要重新跑一次./configure
  ./configure

  # 5、继续执行make
  make
  # 6、继续执行make install
  make install
  # https://juejin.cn/post/6844903893508292615

SSL 证书安装

  • 确认 nginx 已支持 SSL 确认是否安装了 SSL 模块,如果没有则需要重新编译安装。
# 查看 nginx 编译参数,确认是否有 --with-http_ssl_module (安装后的确认,并非安装前)
/usr/local/nginx/sbin/nginx -V

  • 安装 SSL 证书
./configure --prefix=/usr/local/nginx --with-http_ssl_module
# 4. 编译
make
# 5. 安装
make install
# 查看是否成功添加 SSL 模块(此时检查才是对的)
/usr/local/nginx/sbin/nginx -V

# 会提示有HTTPS模块
nginx version: nginx/1.10.2
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

安装 certbot 获取 SSL 证书

yum install epel-release -y
yum install certbot -y
# 停止 nginx,获取证书
/usr/local/nginx/sbin/nginx -s stop
certbot certonly --standalone -d pzblog.com -d www.pzblog.com

续签证书

# 续期所有证书
certbot renew

# 或者强制续期(不管是否到期)
certbot renew --force-renewal

扩展新证书

# 使用 --expand 参数扩展证书

# 停止 Nginx
/usr/local/nginx/sbin/nginx -s stop

# 扩展证书,添加 nginx.pzblog.com
sudo certbot certonly --standalone --expand -d pzblog.com -d www.pzblog.com -d nginx.pzblog.com

#不会重新申请全新的证书,而是在原有证书基础上添加新域名

添加了新域后的位置说明

#完整的目录结构
/etc/letsencrypt/
├── live/
│   └── pzblog.com/           # 证书目录(包含所有域名)
│       ├── cert.pem -> ../../archive/pzblog.com/cert1.pem
│       ├── chain.pem -> ../../archive/pzblog.com/chain1.pem
│       ├── fullchain.pem -> ../../archive/pzblog.com/fullchain1.pem
│       ├── privkey.pem -> ../../archive/pzblog.com/privkey1.pem
│       └── README
└── archive/
    └── pzblog.com/           # 实际证书文件(已更新)
        ├── cert1.pem         # 现在包含三个域名
        ├── chain1.pem
        ├── fullchain1.pem
        └── privkey1.pem

1、证书文件没变:还是 /etc/letsencrypt/live/pzblog.com/fullchain.pem 和 privkey.pem
2、证书内容变了:现在里面包含了 nginx.pzblog.com
3、Nginx 配置不用改:因为证书路径完全一样

测试是否生效:
curl -k https://nginx.pzblog.com

添加新域名的SSL标准流程(3步)

# 第一步停止 Nginx,因为安装时要占用80端口,如果不停会提示端口被占用,无法安装SSL:
/usr/local/nginx/sbin/nginx -s stop
# 第二步:
sudo certbot certonly --standalone --expand \
  -d pzblog.com \
  -d www.pzblog.com \
  -d nginx.pzblog.com \
  -d 新域名.pzblog.com
# 注意:必须把所有域名(包括原来的)都列出来,否则没列出的域名会从证书中移除,就算以前生效的如果这次没加,将会被视为不需要。

#第三步启动 Nginx,nginx是一个执行文件不是文件夹,执行如下即可:
/usr/local/nginx/sbin/nginx

启动、停止、重新加载

cd /usr/local/nginx/sbin
# 启动
./nginx
# 停止
./nginx -s stop
# 修改配置后重新加载
./nginx -s reload
# 重启
./nginx -s reopen
# 有序退出
./nginx -s quit

##查看 nginx 的版本

# 仅显示版本
./nginx -v
# 显示具体信息
./nginx -V

检查配置文件

# 检查配置文件是否正确
./nginx -t

显示帮助信息

端口占用方案

# 查看使用情况
netstat -nplt
# yum/apt-get -y install lsof
# https://www.gylmap.com/96906.html

nginx 删除干净

检查 nginx 服务是否运行,如果运行就关闭服务

ps -ef | grep nginx
/usr/local/nginx/sbin/nginx -s stop

查找并删除 nginx 相关文件

whereis nginx
find / -name nginx
rm -rf /usr/local/nginx

卸载 nginx 依赖

yum remove nginx
# 查看帮助文档信息
./nginx -h
# 或者
./nginx -?

配置 systemctl

netstat -tulnp | grep :80

[Unit]
Description=Nginx HTTP Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

配置文件软链接

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

查某个服务的进程

# https://blog.csdn.net/qq_43759478/article/details/131511449
ps aux | grep nginx | grep -v grep
sudo netstat -plnt | grep :80
sudo ss -plnt | grep :80
service nginx status