CentOS部署项目Nginx+uwsgi+Django+HTTPS

CentOS部署项目Nginx+uwsgi+Django+HTTPS

项目需求,部署需要把HTTP转成HTTPS,研究了一下,写个文档

一、安装OpenSSL

1.安装环境依赖
先检测是否安装了gcc

gcc -v
#如果没有安装,则安装
yum -y install gcc

检查是否安装了zilb

whereis zlib
# 安装
yum -y install zlib

2.安装OpenSSL

#下载安装包
wget https://www.openssl.org/source/openssl-fips-2.0.16.tar.gz
#解压
tar -xzf openssl-fips-2.0.16.tar.gz
#进入目录
cd openssl-fips-2.0.16/
#设置安装路径
./config --prefix=/usr/local/openssl
#编译安装
make && make install
#查看版本
openssl version

二、生成证书

1.创建目录

mkdir /home/key_dir #用于保存密钥和证书

2.进入目录进行操作

cd /home/key_dir

3.创建服务器私钥

openssl genrsa -des3 -out server.key 1024
#注意,长度1024位, des3加密算法的. (之后输入一个口令(两遍),需要记住)

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

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
#过期时间事365

7.查看证书和私钥是否创建成功

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

三、Nginx的配置

我项目本身已经部署过一个了,所以在这里我直接在nginx配置文件中进行配置了
我的上一篇部署文档地址:nginx+uwsgi+django
1.修改nginx.conf
也就是在原来的配置文件上添加密钥和证书

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

[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: 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]# 

启动Nginx

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

到这就完成了,再次访问的时候只能使用https了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值