【Nginx】CentOS服务器Lets Encrypt使用记录

发布于 2020-10-30  6 次阅读


Nginx

Nginx安装方式:
之前一直图方便,直接用yum install nginx进行安装,启动管理也很简单,就systemctl start nginx
后面使用了源码来进行安装, 可以更加自由地选择模块与配置。
例如如下的配置,可以选择gzipssl等。
我们可以通过nginx -V来查看编译的指令。

--prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --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_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=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

使用源码方式安装的nginx,目录一般在/usr/local/下面,启动方式也与yum安装的有所区别。

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

在nginx的启动状态可以使用如下语句来重新启动

nginx -s reload

Lets Encrypt

Lets Encrypt是一个CA证书机构,网上相关教程也比较多
这里使用github上一个开源项目 Certbot 来自动执行证书颁发和安装。

SSl 证书在/etc/letsencrypt/live/<域名>目录下。
然后在Nginx的<server>节点中直接添加SSL证书配置。

ssl_certificate      /etc/letsencrypt/live/xxx/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx/privkey.pem;

要刷新证书,直接使用如下命令完成续期。

certbot renew

提示证书更新成功后需要重启Nginx服务。

Nginx 重启异常处理

本次重启Nginx遇到一次异常报错,提示找不到pid文件

/run/nginx.pid not found

这个pid文件是nginx用来平滑重启使用的(也就是nginx -s reload命令),文件存放了nginx的master进程号。

Nginx 启动后会运行 Master进程和 Server进程

一般来说可能是被意外删除或者之前关闭nginx的方式不对,如果使用yum安装了Nginx并且使用systemctl来启动过nginx可能就会造成这样问题。

如果使用nginx -s stop来结束nginx进程,也会提示pid错误。而此时使用nginx -c nginx.conf的命令又会报错提示端口已被绑定。

因此需要手动干掉nginx的进程,使用killall -9 nginx关掉nginx(nginx不只一个进程)。

此后再使用nginx -c nginx.conf来重新启动Nginx一般就行了。
但是这里又遇到pid错误了,提示

/logs/nginx.pid not found

可以看到路径有些不同,这个是在nginx.conf中配置的问题

pid        logs/nginx.pid;

看了下发现/usr/local/nginx下没有logs目录,创建一个试试看。
再次运行,Nginx就正常启动了。

总体来说Nginx的配置还是比较重要的,还好不是什么业务量大的网站或者应用,不然服务只要停掉一会儿就算是灾难了。

Comments


来自像素世界的代码家,创造第九艺术!