给服务器上的tomcat添加https协议

生成SSL证书

笔者这里用的是开源的Let's Encrypt的ssl证书是免费而且好用的

1.下载certbot

# wget  https://dl.eff.org/certbot-auto

2.把文件移动到/usr/local/bin目录下,并赋予执行权限

# mv certbot-auto /usr/local/bin/
# chmod a+x /usr/local/bin/certbot-auto

3.执行下面的命令生成证书

# certbot-auto certonly --webroot -w /home/xx/tomcat6/webapps -d test.omgdba.com -m [email protected]

命令解释:

–webroot 指定网站根目录,certbot会在该目录下生成一个文件以确认域名对网站的所有权

-d 指定要生成证书的域名,如有多个域名,可使用-d xxx.com -d ex.xxx.com来指定

-m 设置通知邮箱,证书生成后会向该邮箱发送一封邮件

证书生成后有以下提示

IMPORTANT NOTES:
– Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/test.omgdba.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/test.omgdba.com/privkey.pem
Your cert will expire on 2018-07-16. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
“certbot-auto renew”
– If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let’s Encrypt:     https://letsencrypt.org/donate
Donating to EFF:                                 https://eff.org/donate-le


证书生成成功后,会存在于/etc/letsencrypt/live/你的域名,这个目录下,内容有


下面是在tomcat中配置ssl证书

1.tomcat配置pem证书,需要apr支持

//配置gcc依赖  
# yum install gcc  
  
//安装openssl  
# wget  https://www.openssl.org/source/openssl-1.1.0f.tar.gz  
# tar -zxvf openssl-1.1.0f.tar.gz  
# cd openssl-1.1.0f  
# ./config --prefix=/usr/local/openssl  
# make && make install  
     
     
    
     
   //安装apr  
# cd ..  
# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.2.tar.gz  
# tar -zxvf apr-1.6.2.tar.gz  
# cd apr-1.6.2  
# ./configure --prefix=/usr/local/apr  
# make && make install  
  
//安装apr-util  
# cd ..  
# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.0.tar.gz  
# tar -zxvf apr-util-1.6.0.tar.gz  
# cd apr-util-1.6.0  
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  
# make && make install  
  
//安装tomcat-native  
# cd ..  
# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-connectors/native/1.2.12/source/tomcat-native-1.2.12-src.tar.gz  
# tar -zxvf tomcat-native-1.2.12-src.tar.gz  
# cd tomcat-native-1.2.12-src/native  
# ./configure --with-apr=/usr/local/apr --with-ssl=/usr/local/openssl  
# make && make install  
  
  
//配置环境变量  
# vi /etc/profile  
//在末尾添加  
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib  


使profile配置文件生效
# source /etc/profile  

2.tomcat的配置

更改tomcat下conf目录下的server.xml(红色的是修改部分

(1)该文件的第一处更改(这个80,其实是8080,这样是为了省略端口号):

<Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443" />
    <!-- A "Connector" using the shared thread pool-->

(2)该文件的第二处修改

<Connector port="443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true" URIEncoding="UTF-8">
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="/etc/letsencrypt/live/你的域名/privkey.pem"
                         certificateFile="/etc/letsencrypt/live/你的域名/cert.pem"
                         certificateChainFile="/etc/letsencrypt/live/你的域名/chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>

(3)该文件的第三处修改

<!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="443" />
注意:SSL证书需要用到443端口,要保证443端口没有被占用


3.将http强制调转到https

在tomcat的conf文件下的web.xml配置文件末尾的地方添加下面的代码

        <security-constraint>  
	    <!-- Authorization setting for SSL -->  
	    <web-resource-collection >  
	        <web-resource-name >SSL</web-resource-name>  
	        <url-pattern>/*</url-pattern>  
	    </web-resource-collection>  
	    <user-data-constraint>  
	        <transport-guarantee>CONFIDENTIAL</transport-guarantee>  
	    </user-data-constraint>  
	</security-constraint>

注意:如果证书生成失败,则有可能是因为python的版本过低,ssl证书生成需要的python版本至少是2.7以上

下载&编译&安装新版本的python

1.首先通过下面这个命令检查python版本(注意V是大写的

# python -V
2.下载python版本,这个是下载地址:https://www.python.org/ftp/python/
# wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

3.下载完解压缩

# tar -zxvf Python-3.5.2.tgz

4.进入解压缩后的目录,安装配置

# cd Python-3.5.2/
# ./configure 
注意:

执行 ./configure 时,如果报错:

configure: error: no acceptable C compiler found in $PATH

说明没有安装合适的编译器。这时,需要安装/升级 gcc 及其它依赖包。

# yum install make gcc gcc-c++ 

完成之后,重新执行:

# ./configure 

5.配置完成后,就可以进行编译了

# make 

6.最后就是安装了

# make install 

安装成功以后,就可以查看 Python 的版本了:

# python -V
Python 2.7.5
# python3 -V
Python 3.5.2

你会发现有俩个python版本,

设置 3.x 为默认版本

查看 Python 的路径,在 /usr/bin 下面。可以看到 python 链接的是 python 2.6,所以,执行 python 就相当于执行 python 2.6。

# ls -al /usr/bin | grep python
-rwxr-xr-x.  1 root root      11216 12月  1 2015 abrt-action-analyze-python
lrwxrwxrwx.  1 root root          7 8月  30 12:11 python -> python2
lrwxrwxrwx.  1 root root          9 8月  30 12:11 python2 -> python2.6
-rwxr-xr-x.  1 root root       7136 11月 20 2015 python2.6

将原来 python 的软链接重命名:

# mv /usr/bin/python /usr/bin/python.bak

将 python 链接至 python3:

# ln -s /usr/local/bin/python3 /usr/bin/python

这时,再查看 Python 的版本:

# python -V
Python 3.5.2

输出的是 3.x,说明已经使用的是 python3了。

配置yum

升级 Python 之后,由于将默认的 python 指向了 python3,yum 不能正常使用,需要编辑 yum 的配置文件:

# vi /usr/bin/yum

同时修改:(如果没有,下面那个文件,就不用修改)

# vi /usr/libexec/urlgrabber-ext-down

将 #!/usr/bin/python 改为 #!/usr/bin/python2.6,保存退出即可。






猜你喜欢

转载自blog.csdn.net/Little1Pudding/article/details/80008971