Django + uWSGI + Nginx + https项目部署,并用OpenSSL 生成https证书

背景:最近突然接触到了OpenSSL,突然想到貌似还没写过Django + uWSGI + Nginx + https项目部署相关的内容,所以整理出来一份资料。

一、OpenSSL 安装

从OpenSSL官网下载最新的稳定版本,我下载的是2.0.16,官网:https://www.openssl.org/source/

  1. 安装依赖环境

    #检测是否安装gcc,如果有则不需要安装
    gcc -v
    
    #安装
    yum -y install gcc
    
    #检查是否已安装zlib库,如果有则不需要安装
    whereis zlib
    
    #安装
    yum -y install zlib
    
  2. 下载OpenSSL安装包

    wget https://www.openssl.org/source/openssl-fips-2.0.16.tar.gz
    
  3. 解压

    tar -xzf openssl-fips-2.0.16.tar.gz
    
  4. 进入目录

    cd openssl-fips-2.0.16/
    
  5. 设置安装路径

    ./config --prefix=/usr/local/openssl
    
  6. 编译安装

    make && make install
    
  7. 查看版本

    openssl version
    

二、生成证书

  1. 创建一个目录

    用于保存证书和私钥。(也可以放到其他目录下)

    mkdir /home/key_dir
    
  2. 进入目录

    cd /home/key_dir
    
  3. 创建服务器私钥

    注:长度1024位, des3加密算法的. (之后输入一个口令(两遍),需要记住)

    openssl genrsa -des3 -out server.key 1024
    
  4. 创建签名请求的证书CSR

    首先会输入密码(上边设置的),会要求你输入国别,比如CN,省份,城市,公司等等一系列信息,自己玩的可以随意输入

    最后输入的A challenge password []:An optional company name []:可以直接回车跳过

    openssl req -new -key server.key -out server.csr
    
  5. 在加载SSL支持的Nginx并使用上述私钥时除去必须的口令

    cp server.key server.key.org
    openssl rsa -in server.key.org -out server.key  #需要输入密码
    
  6. 标记证书使用上述私钥和CSR

    注:365是过期天数

    openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
    
  7. 查看证书和私钥是否创建成功

    [root@localhost key_dir]# ls
    server.crt  server.csr  server.key  server.key.org
    [root@localhost key_dir]# 
    

三、Nginx配置

因为我之前部署过项目,所以我只需要修改Nginx配置文件即可

Django + Nginx + uWSGI部署请参考我的上一篇文章:https://blog.csdn.net/weixin_44110998/article/details/104001529

  1. 修改nginx.conf

    其实就是在原配置的基础上,把原来监听的端口(我这是80)改为监听443,然后加入密钥相关配置

    http {
    	......
    	
    	server {
    	    listen 443;  #需要监控的端口
    	    
    	    server_name  localhost;
    	    
    	    ssl on;
        	ssl_certificate /home/key_dir/server.crt;
        	ssl_certificate_key /home/key_dir/server.key;
        	
    	    location / {
    	        include uwsgi_params;
    	        uwsgi_pass 127.0.0.1:8080; #与uwsgi中ip:端口相同
    	        uwsgi_send_timeout 600;
    	    }
    		#静态文件
    		location /static { 
    	        alias /home/nginx_test/static/; #静态文件路径
    	    }
    	}
    	......
    }
    
  2. 查看Nginx是否安装http_ssl_module

    /usr/local/nginx/sbin/nginx -V

    [root@localhost nginx-1.9.9]# /usr/local/nginx/sbin/nginx -V
    nginx version: nginx/1.9.9
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (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
    [root@localhost nginx-1.9.9]# 
    

    如果出现了上述代码最后一行的字样:--with-http_ssl_module,则已安装,下面的步骤可以跳过,直接进行修改修改nginx.conf的步骤

    去nginx解压目录下执行(如果删了就重新下载吧):

    ./configure --with-http_ssl_module
    

    如果报错./configure: error: SSL modules require the OpenSSL library.则执行下边的

    yum -y install openssl openssl-devel
    ./configure
    ./configure --with-http_ssl_module
    

    执行 make(切记不能 make install 会覆盖安装目录)

    make
    

    将原来 nginx 备份

    cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
    

    将新的 nginx 覆盖旧安装目录

    cp -rfp objs/nginx /usr/local/nginx/sbin/nginx
    

    测试 nginx 是否正确

    /usr/local/nginx/sbin/nginx -t
    

    错误运行结果:

    [root@localhost nginx-1.9.9]# /usr/local/nginx/sbin/nginx -t
    nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/nginx.conf:43
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
    [root@localhost nginx-1.9.9]# 
    

    正确运行结果:

    [root@localhost nginx-1.9.9]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@localhost nginx-1.9.9]# 
    
  3. 启动Nginx

    如果已启动就停止在启动(注:我测试的时候使用的是重启,没报错但是sll证书没生效,然后停止在启动就ok了)

    /usr/local/nginx/sbin/nginx            # 启动
    /usr/local/nginx/sbin/nginx -s stop    # 停止
    /usr/local/nginx/sbin/nginx -s reload  # 重启
    

四、访问测试

现在直接访问IP:端口应该是访问不了的,必须加上https://
在这里插入图片描述

欢迎关注同名微信公众号:程序猿杂记

程序猿杂记

技术|交流|福利
发布了63 篇原创文章 · 获赞 87 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_44110998/article/details/104021002
今日推荐