現在是 2017-12-31,此方法依舊可成功。
流程如下:
- 安裝 Xcode command line tools
- 安裝 Homebrew
- 安裝 Golang
- 設定 GOPATH 環境變數
- 安裝 Visual Studio Code 及開發環境套件
忙碌人生
現在是 2017-12-31,此方法依舊可成功。
流程如下:
先裝好以下套件:
Homebrew(http://brew.sh/index_zh-tw.html)
X Code Command Line Tool(自己去App Store抓)
brew update brew install openssl ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/ ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
下載pkg來安裝:
https://www.microsoft.com/net/download
處理一下預設的路徑即可:
ln -s /usr/local/share/dotnet/dotnet /usr/local/bin
dotnet new dotnet restore dotnet run
Hello World 就出現拉~
然後就安裝Visual Studio Code,測試一下那IDE的自動完成功能。
其實VSC很聰明,基於ATOM,裝什麼就去商店抓,相依還會自己檢查,我後來想,會用這個工具主要是Debug,可以設中斷點、看堆疊,不然其實Build在CMD處理即可,PHP這種Debug可以硬幹,直接顯示在網頁上,但是我想現在應該不能這樣了,現在可是後端工程師XD!
::安裝C#套件並啟用
這個微軟的套件其實是 C# for Visual Studio Code (powered by OmniSharp)
http://www.omnisharp.net/
也會有一些相依性的東西在默默安裝
不過目前Debug區出現了一些警告,我還沒研究這是什麼…
—— 參考文獻 ——
[1] .NET – Powerful Open Source Cross Platform Development : https://www.microsoft.com/net/
[2] core/known-issues.md at master · dotnet/core · GitHub – https://github.com/dotnet/core/blob/master/cli/known-issues.md
從PHP轉戰golang,超級不習慣的,因為之前也沒有MVC的概念,學習之路更是艱辛
於是我出了個題目給自己:
Heroku + Golang + Git command deploy
我,三種全部都不會!!
沒關係,這就是學習的好機會,來吧!
*環境:Mac OS X El Capitan 10.11.6
先把golang環境架起來吧!
到官網下載golang,我習慣自己處理套件,不裝包好的pkg
sudo tar -C /usr/local -xzf go1.7.3.darwin-amd64.tar.gz
那個版本之類的要記得自己調整啊,我打這篇的時候版本是1.7.3
接著是設定環境變數
nano ~/.bash_profile
加入這幾行
#Go Lang export PATH=$PATH:/usr/local/go/bin export GOPATH=$HOME/Desktop/Trees/Go export GOBIN=$GOPATH/bin export PATH=$GOPATH:$GOBIN:$PATH
我解說一下,第一個export把go本身執行檔、編譯之類的弄進環境變數,第二個是GOPATH,是整個Workspace的位置,go跟某些語言一樣有自動套件相依檢查機制,所以整個Workspace就是你的套件庫。第三跟第四個是export我們利用go get從repository抓下來的工具。
然後你就可以挑一個你喜歡的editor來玩囉,我是開始學習使用Visual Studio Code來玩golang拉,那GUI的特性帶來的許多方便,雖然我還是覺得他比Sublime Text 3肥了些。
這段就直接安裝Heroku包好的pkg了
https://devcenter.heroku.com/articles/heroku-command-line
然後,別跟我說你沒裝git…
首先介紹幾個heroku常用指令:
heroku login : 登入heroku
heroku ps : 目前使用中的Process,也會顯示剩餘的dyno hour(heroku的計價單位)
heroku create : 創造空的app,create後面可以帶參數,會成為app的name
heroku logs : 查看log檔
假設我們已經申請好Heroku帳號,下一步就直接登入了:
heroku login
登入之後,我們可以先從遠端拉個Hello World來玩玩看Heroku的部署,輸入以下指令:
go get github.com/heroku/go-getting-started
你會發現,有一包資料夾叫做go-getting-started,出現在你設定的GOPATH裡的src資料夾,沒錯,你已經學會利用go get抓git上面的repository了!
我們切換到該reposiory中,準備部署至heroku上看看
cd $GOPATH/src/github.com/heroku/go-getting-started heroku create git push heroku master
這邊講解一下,我們切換到repository之後在做heroku create的動作,目的是因為heroku他會自動針對此repository,將遠端分支”heroku”設定為heroku即將產生的app git位置,在鍵入heroku create之後,他會給你兩個網址:
獨立網址,例如:https://fierce-beyond-60579.herokuapp.com/
Git Repository,例如:https://git.heroku.com/fierce-beyond-60579.git
獨立網址就是此app的唯一位置,他的git就是你之後deploy的對象。
是的就是這麼簡單,你已經部署了hello world上去heroku囉!…什麼?你說你失敗了?
我猜你一定沒有把SSH KEY給heroku!
ssh-keygen -t rsa heroku keys:add
是的,跟github一樣,要把ssh加到網站上面才能開始一切操作喔!
Heroku也提供本地測試,只需要輸入:
heroku local web
他自己就會監聽5000 port了!
如果是自製的Project,遇到heroku local不能跑的時候,記得檢查有沒有產生Procfile檔案(請參考heroku文件,他很簡單的就只是告訴heroku你的app在process裡叫做什麼名字),或是檢查你的自製Project有沒有執行過 go install -v
相信你已經準備好自己的Hello World專案準備弄上這個免費雲端,但是!部署前要先知道遊戲規則,要在Heroku上面部署自己的app的話,你需要裝官方認得的相依性套件管理工具,我們會以godep來做範例。
Godep相當的方便,跟go get差不多,但是它是把所有跟你的專案有關的套件都抓到你的app資料夾裡,可以獨立運作也方便管理,同時也會自動產生一個相依列表”Godeps.json”,Heroku就是以這個檔案來當作識別,抓取你app的相關資料。
安裝指令:
go get github.com/tools/godep
使用方式:
godep save ./
好的,到這裡為止,應該準備的差不多了,不過我這邊要特別註解一下關於git的部分,因為小弟平常用git都是GUI操作,只知道幾個關鍵名詞但是對command line操作git完全沒概念,所以就來記錄一下!
我們先介紹幾個常用的指令:
git init : 初始化你的project,主要是產生 .git 資料夾
git remote add <remote> <url> : 新增遠端git位置
git remote remove <remote> : 移除遠端git位置
git remote -v : 查看遠端詳細位置
git add -A . : 一次新增所有檔案,準備commit
git commit -m <title> -m <message> : commit 加上訊息 git status : 顯示目前repo狀態
git push : PUSH到指定的遠端與分支
我們假設你已經寫好你的Hello World了…什麼?你還沒寫?好吧,我的借你抄一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package main import ( "fmt" "net/http" "os" ) func main() { http.HandleFunc("/", hello) fmt.Println("listening...") err := http.ListenAndServe(":"+os.Getenv("PORT"), nil) if err != nil { panic(err) } } func hello(res http.ResponseWriter, req *http.Request) { fmt.Fprintln(res, "hello, world") } |
稍微說一下,一般我們在處理Listen Port時會直接寫上例如:5000這樣,但是在Heroku線上,他只開80 Port給你,並且寫好了環境變數,使用方式就如我的範例那樣,os.Getenv(“PORT”)就可以拿到80這個數字,不包含冒號喔!
跑完godep、跑完git init,準備好Procfile檔案(反正就是在專案根目錄生一個叫做Procfile的檔案,裡面寫著 web: go-getting-started 即可,詳情請看https://devcenter.heroku.com/articles/getting-started-with-go#define-a-procfile),就可以準備部署了,我們可以先檢查一下自己的git remote 分支 heroku 目前被指向誰,輸入 git remote -v:
請注意看我的heroku目前被指向 https://git.heroku.com/young-ridge-89894.git ,這代表我如果在這個專案進行部署,會被部署到heroku上的young-ridge-89894這支app,如果你確定這是你的目標,那接下來就好處理了,但是如果不是,記得鍵入以下指令:
git remote remove heroku
並加入遠端目標,例如:
git remote add heroku https://git.heroku.com/polar-sierra-53616.git
如果你是想要部署在新的app上面,只要在remove之後直接heroku create即可唷!
如果你的遠端目前已經被某個別的project部署過,只要在部署時加上 -f 就可以強制部署了!
部屬就跟git command操作一樣,先add、再commit、再push到遠端
範例指令:
git add -A . git commit -m "Test" git push heroku master
登登登登~完成了!
我覺得應該可以開一篇關於go framework的東西XD
在範例專案 github.com/heroku/go-getting-started 中,他是使用了gin-gonic這個framework來開發的網頁,包含了html template的使用方法在裡面,感覺很好玩!
還有在利用Visual Studio Code的時候,發現了Git History這個套件(https://github.com/DonJayamanne/gitHistoryVSCode),他可以直接在Visual Studio Code中看到Git Log的Graph唷!使用方式就是 CMD + shift + P ,選擇 git view history (log) 。
阿對了,如果你是從Sublime Text轉來使用的人,可以打開一個功能,絕對會讓你有回到家的感覺,於選單的「使用者設定」中,插入以下:
"editor.renderIndentGuides": true
瞧瞧他打開了什麼?是垂直整理線!
——參考文獻——
[1] Getting Started on Heroku with Go – https://devcenter.heroku.com/articles/getting-started-with-go
[2] Go Dependencies via Godep – https://devcenter.heroku.com/articles/go-dependencies-via-godep
[3] Heroku Local – https://devcenter.heroku.com/articles/heroku-local
[4] Godep – https://github.com/tools/godep
[5] Managing Your SSH Keys – https://devcenter.heroku.com/articles/keys
[6] Deploying a Golang Project to Heroku – http://www.zhubert.com/blog/2014/03/08/deploying-golang-to-heroku/
[7] Manage Dependencies With GODEP – https://www.goinggo.net/2013/10/manage-dependencies-with-godep.html
[8] Git 教學(1) : Git 的基本使用 – http://gogojimmy.net/2012/01/17/how-to-use-git-1-git-basic/
[9] Getting Started with Go on Heroku – https://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html
[10] [NodeJS] 將程式部署到 Heroku 上 – NodeJS on Heroku – http://cire.pixnet.net/blog/post/37369672-%5Bnodejs%5D-%E5%B0%87%E7%A8%8B%E5%BC%8F%E9%83%A8%E7%BD%B2%E5%88%B0-heroku-%E4%B8%8A—nodejs-on–heroku
[11] Gin Gonic – https://gin-gonic.github.io/gin/
[12] How to attach my repo to heroku app – http://stackoverflow.com/questions/6877915/how-to-attach-my-repo-to-heroku-app
[13] Overwriting an existing Heroku app – http://stackoverflow.com/questions/14437903/overwriting-an-existing-heroku-app
[14] 在Heroku部署Rails專案結合AWS S3存放上傳圖片 – http://springok-blog.logdown.com/posts/2015/10/29/heroku-deploy-a-rails-project-with-aws-s3-storage-upload-pictures
快把以下這段加到你的 functions.php 裡,立馬見效!
1 2 3 4 5 6 7 8 9 10 11 | function restrict_media_library( $wp_query_obj ) { global $current_user, $pagenow; if( !is_a( $current_user, 'WP_User') ) return; if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' ) return; if( !current_user_can('manage_media_library') ) $wp_query_obj->set('author', $current_user->ID ); return; } add_action('pre_get_posts','restrict_media_library'); |
以下包含暫時解法,因為是直接動WordPress Kernel
1. WordPress 網址無法轉址(Permalinks)
要修改 web.config 檔
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="wordpress" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
2. WordPress 中文網址無法正常轉址
修改 wp-includes/ 底下的 class-wp.php
$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
改成
$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? mb_convert_encoding( $_SERVER['PATH_INFO'], 'UTF-8', 'Big5') : '';
然後
list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
改成
list( $req_uri ) = explode( '?', mb_convert_encoding( $_SERVER['REQUEST_URI'], 'UTF-8', 'Big5' ) );
3. WordPress 中文檔案上傳錯誤
修改 wp-admin/includes/ 底下的 file.php
$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
改成
$fileTypeNameArr = explode( '.', $filename ); $countNum = count( $fileTypeNameArr ) - 1; $fileExt = $fileTypeNameArr[$countNum]; $filename = time() . '-' . rand( 0,999999999 ) . '.' . $fileExt;
—-參考文獻—-
[1] Using Permalinks – https://codex.wordpress.org/Using_Permalinks
[2] [WordPress]中文網址,發生404錯誤,教你如何解決? – http://dev.brucelulu.com/topics/180
[3] How to Setup WordPress Permalinks – https://gist.github.com/jonahvsweb/5479580
[4] IIS permalinks compatibility – web.config not working? – http://stackoverflow.com/questions/27988710/iis-permalinks-compatibility-web-config-not-working
[5] 解決 WordPress 在 IIS 中無法使用固定網址的問題 – http://www.minitw.com/archives/server/solved-can-not-be-used-in-iis-wordpress-permalink-issue.htm?variant=zh-tw
[6] WordPress小技巧(1):解決中文檔名圖片無法上傳問題 – http://www.cc.net.tw/2013/07/434/
直接貼入程式碼使用即可
<?php // Print last SQL query string echo 'Last Query : ' . $wpdb->last_query . '<br>'; // Print last SQL query result echo 'Last Result : '; var_dump( $wpdb->last_result ); // Print last SQL query error echo 'Last Error : ' . $wpdb->last_error . '<br>'; ?>
最近在做網站時,都是在host以測試網址先架好wordpress,資料也都上架後才買網址然後更換,就會有很多文章的圖片無法正常顯示,因為主要網址已經換掉了,即使在wordpress後台設定改了網址,也只會影響到wordpress預設欄位的網址,很多theme的post meta不會自動更新網址,只好手動去phpMyAdmin敲一些SQL指令了:
在SQL指令那邊複製貼上以下文字(記得把網址換成自己的來源與目標網址)
1 2 3 4 | UPDATE wp_options SET option_value = replace(option_value, 'http://舊網址', 'http://新網址') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'http://舊網址','http://新網址'); UPDATE wp_posts SET post_content = replace(post_content, 'http://舊網址', 'http://新網址'); UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://舊網址','http://新網址'); |