這篇文章一步一步示範如何在 Hostmonster 上安裝 Python 2.7.9,大致步驟為:
- 認證 Hostmonster 帳號。
- 啟用 SSH Access 功能,產生 Keygen 並建立連線。
- 透過 SSH 於 Host 連線並安裝 Python。
目錄:
(零) 欲達成之目的
在 Hostmonster 的 Shared Hosting (最便宜的 Plan ) 上有隨 CentOS 附的 Python,但版本較舊,為了在 Host 上安裝 Djagon 等東西,最好手動安裝最新版的 Python。
要直接進入 Host 安裝 Python,最簡單又安全的方法是透過 SSH 連線下指令安裝,但是 Hostmonster 預設並不允許 SSH 連線,使用者必須手動開啟 SSH。
然而,Hostmonster 僅允許經過身分認證的使用者開啟 SSH,故第一步驟要先通過身分認證。
(一) 認證 Hostmonster 帳號
請參考官方的這篇文章: Verifying Your Account Purchase。
基本上顧客應該都是透過 paypal 付款的,就看 paypal 那部分就可以了。
完成後會收到 Verification has been completed for XXX 的認證信。
(二) 啟用 SSH 功能
1. 開啟 SSH Access
參考官方的這篇文章: SSH Access。
SSH/Shell is not enabled by default. To enable SSH we will need you to verify your hosting purchase, to do this please see: How to verify your hosting account team. Once this is done SSH access may be enabled in the cPanel.
SSH 預設是關閉的,要到 cPanel 的 SSH / Shell Access
手動開啟。
進入頁面後,Choose “SSH access enabled” from the drop-down menu.
如果出現下面的錯誤訊息,表示你尚未通過身分認證,請先去認證。
If you see the error “ERROR: You account must be verified before shell access can be enabled. Please contact our Verification Department to verify your account before continuing.” you will need to contact our Verification team to verify your account purchase.
2. 產生 SSH Keygen
參考官方的這篇文章: SSH Access - Generating a Public/Private Key。
Generating the Key
- Login to your HostMonster
Control Panel
- Choose
SSH / Shell Access
, in the Security section. - Click
Manage SSH Keys
- Choose to
Generate a new Key
- Enter a new
Key password
. - Choose DSA or
RSA
for the type. (It is recommend that you choose RSA.) - Choose a Key size, it is recommend to use
2048
or higher. - Click
Generate Key
- Click the
Go Back
button.
Authorize the Key
- Click
Manage Authorization
- Click the
Authorize
button for your newly created key. - Click
Go Back
to return to the main page.
Download Private Key
- To the right of your private key, click
View/Download
. - Click the
Download Key
button to download the private key. - If on Linux or Mac OS copy the id_dsa file to your
~/.ssh folder
. - On Windows you will want to move it some place safe.
作業系統是 Windows 的話還是用 Putty 連最方便。
作業系統是 OSX 的話,要注意的是 MAC finder 預設是不會顯示隱藏資料夾的,但可藉由
前往 -> 前往資料夾(Shift + command + G): ~/.ssh
或直接透過 terminal copy 到 ~/.ssh。
載下來的 Private Key 預設權限是 644,這會造成等等認證時產生問題。
用 finder 或到 terminal 用 chmod
將其權限改為 004。
3. 建立 SSH connection
參考官方的這篇文章: SSH Connections。
欲建立 SSH 連線,Windows 建議使用 Putty;UNIX 和 OSX 則可以直接用 Terminal 連。
ssh username@domain.com
Note: You would enter your cPanel username as the username for SSH. You can find the username in the Stats column on the left hand side of the cPanel.
這邊要注意的是,username 是您登入 Hostmonster 的 cPnael 時顯示的 username,domain 則是您註冊的網域。例如下圖。
如果剛剛沒有更改 Private Key 權限的話,會出現下面這種警告,無法使用該 Private Key 連線。
成功透過 SSH 連上 host 後,就可以設定自己習慣的環境了
例如在 .bashrc 設定 alias, export PATH,
或是設定 .vimrc 等等。
(三) 透過 SSH 於 Host 連線並安裝 Python。
連進 Host 後,預設已經裝上了隨 CentOS 附贈的 Python,但是版本較舊。
$ python --version
Python 2.6.6
參考官方的這篇文章: Install Python 2.7 to your account。
文章中指示的是 Python 2.7.2;
但我們可以到到 Python 官網的 Source 找尋最新的 Python 版本。
舉例來說,最新版的網址: Python 2.7.9。
下載:
mkdir ~/python
cd ~/python
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar zxfv Python-2.7.9.tgz
find ~/python -type d | xargs chmod 0755
cd Python-2.7.9
下載完後,安裝
./configure --prefix=$HOME/python
make
make install
完成之後如下圖,我們已經裝完 2.7.9 版本 python 了,但是 bash 還是使用預設的 2.6.6
For your local version of python to load you will need to add it to the .bashrc file.
我們要把剛剛安裝的 Python 路徑加入 $PATH
vim ~/.bashrc
export PATH=$HOME/python/Python-2.7.2/:$PATH
source ~/.bashrc
source 完後,可能需要重新登入才能生效。
$ python --version
Python 2.7.9
(四) Reference
- HostMonster Web Hosting Help: Verifying Your Account Purchase
- HostMonster Web Hosting Help: SSH Access
- HostMonster Web Hosting Help: SSH Access - Generating a Public/Private Key
- HostMonster Web Hosting Help: SSH Connections
- PuTTY
- HostMonster Web Hosting Help: Install Python 2.7 to your account
- Python Source Releases