一、下載的壓縮包放在根目錄html
wget http://www.openssl.org/source/openssl-1.0.2j.tar.gz
二、在文件夾下解壓縮,獲得openssl-1.0.2j文件夾python
tar -xzf openssl-1.0.2j.tar.gz
三、進入解壓的目錄:linux
cd openssl-1.0.2j
四、設定Openssl 安裝,( --prefix )參數爲欲安裝之目錄,也就是安裝後的檔案會出如今該目錄下:c++
./config --prefix=/usr/local/openssl
五、執行命令sql
./config -t
6.執行make,編譯Opensslvim
make
這裏若是出現以下錯誤bash
make[1]: gcc: Command not found網絡
上網查才發現我安裝的CentOS7中沒有GCC編譯器python2.7
保證系統網絡暢通之後,執行命令 yum -y install gcc 安裝GCCsocket
七、安裝 Openssl:
make install
八、查看版本
openssl version
一、安裝python3.6可能使用的依賴
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel
二、到python官網找到下載路徑, 用wget下載
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
3 解壓tgz包
tar -xf Python-3.6.5.tgz
4 進入python目錄
cd /Python-3.6.5/
五、編譯,安裝
5.1 提示找不到SSL模塊
python安裝完畢後,提示找不到ssl模塊:
import _ssl # if we can't import it, let the error propagate ImportError: No module named _ssl
5.2 從新編譯python(去掉最後4行的註釋)
#修改Setup文件,在解壓文件夾下的Modules文件夾下的Setup.dist vim Modules/Setup.dist #修改結果以下:去掉最後4行註釋,獲得以下的結果 # Socket module helper for socket(2) _socket socketmodule.c timemodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto
5.3 編譯安裝
#這裏要先把目錄切回到解壓文件夾下
./configure --prefix=/usr/local/python3 make make install
5.4 測試,已可正常使用
[root@localhost ~]# python Python 3.6.4 (default, May 28 2018, 23:45:10) [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>>
注:如需保留舊版本的就不須要執行 6 .7兩部
六、重命名舊版本的python依賴
ll /usr/bin | grep python
mv /usr/bin/python /usr/bin/python2.7
七、刪除舊的軟連接,建立新的軟連接到最新的python
rm -rf /usr/bin/python ln -s /usr/local/bin/python3.6 /usr/bin/python python -V
使用yum命令報錯File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
問題出現緣由:
yum包管理是使用python2.x寫的,將python2.x升級到python3.1.3之後,因爲python版本語法兼容性致使問題出現
解決辦法:
修改yum配置文件,將python版本指向之前的舊版本
# vi /usr/bin/yum #!/usr/bin/python2.7
修改urlgrabber-ext-down文件,更改python版本
# vi /usr/libexec/urlgrabber-ext-down #!/usr/bin/python2.7
vim ~/.bash_profile
添加下面這條參數
export PATH=/usr/local/python/bin:$PATH
保存
source ~/.bash_profile
vim install_python.sh
#!/bin/bash echo "正在安裝相關組件" yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc-c++ gcc openssl-devel echo "下載安裝包" wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz echo "正在解壓安裝包" tar -xf Python-3.6.5.tgz -C /root/ && cd /root/Python-3.6.5/ echo "添加ssl支持" cat >> /root/Python-3.6.5/Modules/Setup.dist <<"EOF" _socket socketmodule.c SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto EOF echo "正在編譯安裝Python" ./configure --prefix=/usr/local/python && make && make install cd /root echo "刪除安裝包" rm -rf /root/Python-3.6.5.tgz && rm -rf /root/Python-3.6.5 echo "正在添加環境變量" echo "export PATH=/usr/local/python/bin:$PATH">> ~/.bash_profile source ~/.bash_profile echo "安裝完成,請執行python3進行測試"
參考文章:https://blog.csdn.net/ikownyou/article/details/53021686