nginx 安装 SSL 证书

昨天在腾讯云那里申请了SSL证书,打算部署到服务器上,但是安装过程遇到了问题:

[root@izuf6hed2mdyv56471078kz sbin]# ./nginx -s reload
nginx: [emerg] unknown directive "ssl" in
/usr/local/nginx/conf/nginx.conf:91

于是查看了下nginx的安装信息,发现ssl模块并未被安装:

[root@izuf6hed2mdyv56471078kz sbin]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.11.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments:

原因大概是:在安装nginx的时候,没有将ssl模块编译进nginx

解决办法是:重装nginx,并在执行configure命令的时候加得上 –with-http_ssl_module 参数。

由于新的nginx版本已经出来了,就借此机会顺便升级一下吧。

步骤一、下载nginx解压

wget http://nginx.org/download/nginx-1.13.8.tar.gz

# 默认下载到root
cd /root

# 解压
tar xzvf nginx-1.13.8.tar.gz

# 进入解压后的目录
cd nginx-1.13.8

步骤二、安装nginx,并添加ssl模块

./configure --with-http_ssl_module

好吧,又报错了:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

提示我装openssl,好吧,我装。

步骤一点五、安装OpenSSL

这是centos 的安装方法:

yum -y install openssl openssl-devel

好,继续执行步骤二。

步骤二(续)、安装nginx,并添加ssl模块

cd /root/nginx-1.13.8
 ./configure --with-http_ssl_module

请先备份好nginx相关的文件!

请先备份好nginx相关的文件!

请先备份好nginx相关的文件!

然后是安装:

# 网上说这么做会覆盖之前的版本,我测试了下,似乎没有什么大问题...
# nginx.conf也没有变化,若心存疑虑,可以参考其他教程
make install

# 查看版本是否升级
/usr/local/nginx/sbin/nginx -V

步骤三、配置

编辑/usr/local/nginx/conf/nginx.conf 添加配置:

server {
        listen 443 ssl;
        server_name www.zxxblog.cn;
        ssl on;
        ssl_certificate /home/key/1_www.zxxblog.cn_bundle.crt;
        ssl_certificate_key /home/key/2_www.zxxblog.cn.key;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
        ssl_ciphers  HIGH:!aNULL:!MD5;
        #ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;   
        ssl_prefer_server_ciphers   on;   

        # !! 这下面的是我自己的配置,不同的人可能有所不同 !!
        location / {
            proxy_pass http://localhost:8080;
        }
}

步骤四、重启nginx

# 如果不成功,就kill掉nginx,然后再打开
/usr/local/nginx/sbin/nginx -s reload

然后,我在阿里云的安全组里添加了443端口,搞定!

效果:

未分类