做開發有些時間了,一直希望從高標準嚴格自己,也希望有個特色風格的技術博客,看了喵神的博客,我很喜歡,于是就仿照做了一個~
1.購買域名
域名也就是網址,可以到萬網、Godaddy等域名供應商購買,我的域名lansefund.com是在萬網購買,如下:
當然域名的后綴有很多種.com 、.cn 、.me 、.net等,根據自己的偏好購買就可以了,如果想購買的域名被注冊了可以選擇其他的后綴!
2.域名解析
所謂解析就是域名綁定服務器IP地址即可,像這種輕量級的網站服務器可以選擇云虛擬主機或阿里云服務器ECS,我的是在在阿里云服務器ECS_Ubun系統上安裝Ghost,然后根據阿里云的教程一步步解析就好了。如果選擇云服務器ECS創建實例后會給一個彈性公用IP地址和私有IP地址,解析域名在IP地址里寫彈性公用IP地址。由于我用的是Mac,所以教程也是基于Mac OS的系統,Windows的步驟也差不多,遇到問題多利用搜索引擎就好了!
域名解析好后,可以打開終端 輸入ping lansefund.com (ping空格+域名),如果出現下圖樣式,解析就成功了,(括號里面123.56.16.58是我的服務器IP地址)
3.配置服務器
打開終端,輸入命令行
ssh root@你的IP地址
回車,輸入你的服務器密碼
這段命令行用來連接服務器,出現下面的結果表示連接成功
3.1安裝node.js
- 1.安裝程序
Node.js是一個可以快速構建網絡服務及應用的 平臺,基于Chrome's JavaScript runtime,即Google V8引擎,是一款高性能的服務架構平臺,Ubuntu系統上執行以下代碼即可安裝Node.js
sudo apt-get update
sudo apt-get install -y python-software-properties python g++ make
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
- 2.查看是否安裝成功
node -v
v0.10.37
npm -v
1.4.28
3.2 安裝Nginx
輸入安裝命令
sudo apt-get install nginx
3.3安裝MySQL
Ghost 默認采用 Sqlite3 數據庫, 安裝MySQL執行如下命令:
sudo apt-get install mysql-server mysql-client
安裝過程中,系統會提示你給 root 用戶(這里的 root 是 MySQL 數據庫的管理賬號) 設置個密碼,建議設置的復雜些,更加安全些。如下圖:
安裝成功后,為確保安全性執行以下指令加強 MySQL 的安全設置:
sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
//輸入安裝 MySQL 時為 root 賬戶設置的密碼
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
//是否修改 root 賬戶的密碼?
Change the root password? [Y/n] n
... skipping.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
//是否刪除匿名用戶?
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
//是否禁止 root 賬戶遠程登錄?
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
//是否刪除 MySQL 默認創建的 test 數據庫,并刪除所有對 test 數據庫的權限設置?
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
//是否重新加載權限表?
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
OK!MySQL 到此就安裝好了!
下面,我們為為 MySQL 設置默認字符集。將數據庫、htnl頁面、源碼文件都設置為統一的字符集會減少很多麻煩,當然,utf8 是最好的選擇。
執行如下命令:
cd /etc/mysql
然后輸入
vi my.cnf
這條命令的作用是用 Vim編輯器打開 /etc/mysql 目錄下的 my.cnf 文件,在終端對文件進行修改,將光標定位到 [mysqld] 位置,按 "i" ,添加如下紅線框內設置,如下:
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
修改完成之后,按esc退出編輯,輸入:wq,退出MySQL。
4.安裝Ghost
4.1創建數據庫
輸入指令
mysql -uroot -p -e 'create database ghost;'
根據系統提示,輸入MySQL數據庫的root 的密碼(上一步驟剛設置過的).指令執行之后就創建了一個叫做 ghost 的數據庫。
4.2 配置Nginx
我們利用 Nginx 做 Ghost 的前端代理服務 輸入:
cd /etc/nginx/sites-available/
sudo touch ghost.conf
sudo vi ghost.conf
就可以用vim 編輯器打開 ghost.conf 文件進行編輯,如下
然后按esc :wq 退出編輯
4.3 建立軟連接
為 ghost.conf 文件做一個軟鏈接到 /etc/nginx/sites-enabled/ 目錄下:
sudo ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghost.conf
4.4 安裝forever
如果是通過npm start 啟動 Ghost 的話,只要關閉了遠程連接,Ghost 也就停了,為了使ghost永遠運行,使用 forever 工具解決這個問題。接下來執行以下指令來安裝 forever,執行如下命令:
sudo npm install forever -g
4.5 開始安裝Ghost
- 4.5.1.下載Ghost
先安裝curl,輸入
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
Ghost 安裝包是經過壓縮的,在 Linux上需要用 unzip
工具對其解壓,先要安裝 unzip工具:
sudo apt-get install unzip
然后下載Ghost 安裝包:
cd /srv/
sudo curl -L http://dl.ghostchina.com/Ghost-0.7.4.zip -o ghost.zip
網址的版本號可以根據ghostchina.com 上的最新版本修改,我寫這篇文章的最新版本是0.7.4,所以我輸入的是0.7.4。
解壓縮:
sudo unzip ghost.zip -d ghost
- 4.5.2 修改Ghost配置文件
進入 Ghost 系統目錄,為 Ghost 增加配置文件并配置數據庫
cd /srv/ghost/
sudo cp config.example.js config.js
sudo vi config.js
修改production 的配置信息,修改為如下
完成之后按esc :wq 退出編輯。
- 4.5.3 安裝 Ghost 依賴庫
打開 Ghost 系統的目錄下面的 package.json 文件,將 "sqlite3": "x.x.0", 這一行刪除掉。
接下來,進入存放 Ghost 系統的目錄并安裝 Ghost 所依賴的npm包:
cd /srv/ghost/
sudo npm install --production
所有依賴包就安裝好了,當前目錄下會多出一個node_modules目錄。
- 4.5.4 啟動 Ghost
執行如下指令重啟 Nginx、啟動 Ghost:
sudo service nginx restart
cd /srv/ghost
sudo NODE_ENV=production forever start index.js
所有的操作都做完了,待DNS生效后,打開域名,就可以看到Ghost博客已經成功運行了。