顯示具有 安裝教學 標籤的文章。 顯示所有文章
顯示具有 安裝教學 標籤的文章。 顯示所有文章

2020年11月25日 星期三

Ubuntu 安裝 web server Caddy 2


安裝

按照指令安裝 Caddy 2

echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" \ | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
sudo apt update
sudo apt install caddy

安裝完成後,執行 caddy 應該會跑出相關的指令提示

caddy



常用指令

執行 caddy

caddy run

等同於 caddy run,在背景程式運作

caddy start

將 Caddyfile 轉成 json config

caddy adapt
caddy adapt --config [PATH_TO_FILE]

重新載入 caddy,可使新的設定生效

caddy reload

關閉 caddy

caddy stop


2020年11月2日 星期一

[Vue.js] vue-i18n 實現多語系


快速使用

假設你已經安裝 Vue cli 3.x,你可以直接到專案裡執行:

vue add i18n

接著會詢問你預設的語言、備用語系(fallbackLocale)及字典檔的目錄,所謂的備用語系指的是當使用者選擇語言的字典檔有缺時,會採用哪個語言的資料替代。

若按照預設值執行的話,可以確認一下專案是否出現目錄 /[Your Project]/src/locales/ ,裡面有個 en.json 英文的字典檔。

這個方法相關的設定已經自動完成,可以直接在 template 上使用:

{{ $t('message') }}


安裝及設定

Step 1: 安裝套件

npm install vue-i18n

Step 2: 新建目錄及字典檔

mkdir locales
新增目錄 /[Your Project]/src/locales/ ,加入第一個字典檔 en.json
{
  "hello": "Hello!"
}
接著 index.js
import en from './en';

export const messages = {
    en,
};
你也可以在 locales/ 下繼續新增其他語系

Step 3: 設定

新增 i18n.js 設定 locale 預設語言及 fallbackLocale 備用語言等,messages 是所有語系的字典檔。
import Vue from 'vue';
import VueI18n from 'vue-i18n';
import {messages} from './locales';

Vue.use(VueI18n);

export default new VueI18n({
  locale: 'en', // set locale
  fallbackLocale: 'en',
  messages, // set locale messages
});
將設定加入 main.js
import Vue from 'vue'
import App from './App.vue'
import i18n from './i18n'

Vue.config.productionTip = false

new Vue({
  i18n,
  render: h => h(App),
}).$mount('#app')
試著在 template 上使用:

{{ $t('hello') }}

上述的檔案完成後,專案的架構如下列所示:

./src/
├── App.vue
├── i18n.js
├── locales
│   ├── en.json
│   └── index.js
└── main.js

更多的安裝資訊可以參考[文件]



進階使用

切換語系
在 root 更改設定值
$i18n.locale

備用語系
可依據各語言設定,可接受傳入字串、陣列、物件等
fallbackLocale


2020年10月16日 星期五

Ubuntu 20 安裝及建立 Angular 專案

請先安裝 node.js
sudo apt update
sudo apt install build-essential libssl-dev
curl https://raw.githubusercontent.com/creationix/nvm/v0.36.0/install.sh | sh
source ~/.profile
nvm install v12.19.0


安裝 Angular CLI 套件
npm install -g @angular/cli


建立專案
新增一個 my-app 的專案,執行指令後會詢問是否安裝 Angular routing 及樣式語言(stylesheet),不選擇的話可以直接按enter使用預設值。
ng new my-app
執行需要一點時間,完成後會在目前的目錄中出現一個 my-app/ 的資料夾。

執行
cd my-app
ng serve

請用瀏覽器開啟頁面
# run `ng serve --open` to open your browser automatically
http://localhost:4200/



相關文章:

2020年5月28日 星期四

靜態網頁部落格工具 Ruby + Jekyll 安裝教學


一、安裝Ruby
作業系統為Ubuntu 20,指令如下:
sudo apt update
sudo apt install ruby-full build-essential zlib1g-dev
設定用戶的環境變數,應避免用root安裝Ruby Gems。
echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
echo 'export GEM_HOME="$HOME/gems"' >> ~/.bashrc
echo 'export PATH="$HOME/gems/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

二、安裝Jekyll
Ruby中使用Gem作為套件管理的工具,就像是python和pip的關係。安裝套件的方式 gem install [packageName]
gem install jekyll bundler

三、建立新專案
新增一個 myblog 的專案
jekyll new myblog
在目錄下應該會發現新的資料夾 /myblog,大致的內容可以參考一下。
├── 404.html
├── about.markdown
├── _config.yml
├── Gemfile
├── Gemfile.lock
├── index.markdown
├── _posts
│   └── 2020-05-27-welcome-to-jekyll.markdown
└── _site
    ├── 404.html
    ├── about
    ├── assets
    ├── feed.xml
    ├── index.html
    └── jekyll

四、開啟服務
進入目錄後,啟動伺服器,預設主機名稱localhost、埠號4000,可以根據需求設定:
cd myblog
bundle exec jekyll serve

// or you can ...
bundle exec jekyll serve --host=[ip address] --port=[port number]
瀏覽器開啟 http://localhost:4000 ,就可以看到預設的首頁,安裝就告一段落,可以開始進行開發。

2020年5月26日 星期二

Asciidoctor + VS Code安裝外掛套件快速開發、預覽功能


一、安裝套件
方法一
(1) 按快捷鍵 [ctrl] + [P]
(2) 輸入下面的指令:
ext install joaompinto.asciidoctor-vscode
(3) 按 [Enter] 送出



方法二
(1) [View] → [Extensions]


(2) 搜尋「AsciiDoc joaompinto」,找到後按綠色按鈕 Install




二、使用方式
打開AsciiDoc的檔案(.adoc, .ad, .asc, .asciidoc),按下快捷鍵 [ctrl]+[shift]+[v]




三、輸出HTML
快捷鍵 [ctrl]+[alt]+[s],自動在同一個目錄下產生相同檔名的 html 檔案。

參考資料:https://github.com/asciidoctor/asciidoctor-vscode


2020年5月21日 星期四

Asciidoctor 安裝及入門範例


Asciidoctor 是使用純文字編輯的文檔格式,與 Markdown 用途相近(用法比較),利用特定的標記可以呈現為HTML、PDF等其他格式,可以根據排版的需求搭配css或模板進行輸出。上一篇「ubuntu18 + python2 文件編輯 AsciiDoc 安裝說明」提到的 AsciDoc 的進化版,AsciDoc 已經停止更新,目前 Asciidoctor 更活耀,常用在電子書或Git專案文件的撰寫。[使用手冊]

安裝
安裝 Asciidoctor(作業系統 Ubuntu 18)
sudo apt-get install -y asciidoctor

完成後可以確認安裝版本
asciidoctor --version
asciidoctor -V


簡易範例
1. 建立檔案
新增一個檔案 sample.adoc,內容:
.Sample document
====
Here's a sample AsciiDoc document:

[listing]
....
= Title of Document
Doc Writer
:toc:

This guide provides...
....

The document header is useful, but not required.
====

2. 輸出 html
asciidoctor sample.adoc
在同一個目錄下會產生同檔名的HTML檔案 sample.html

預設情況都是轉成html5,並搭配css3,如果想要轉成其他格式,可以加上 -b(--backend) 來指定:
// for *.xml
asciidoctor -b docbook sample.adoc


Custom Theme
預覽剛剛的 sample.html ,發現除了標題、內文等會自動加上顏色或背景,是預設套用的 css3成果:

可利用自訂的 css 來美化頁面,以 asciidoctor-skins 寫好的主題套用作為範例。下載:
git clone https://github.com/darshandsoni/asciidoctor-skins.git

重新輸出時,-a(--attribute) 加上 css 的路徑:
asciidoctor -a stylesheet=./asciidoctor-skins/css/notebook.css sample.adoc

// or
asciidoctor -a stylesheet=notebook.css -a stylesdir=./asciidoctor-skins/css sample.adoc

開啟畫面會套用新的 stylesheet,如下:


常用參數
除了上面提到的 -a-b ,其他常見的選項:
1. 輸出檔名 -o
// -o, --out-file
asciidoctor sample.adoc -o mypage.html

2. 輸出路徑 -D
// -D, --destination-dir
asciidoctor sample.adoc -D pages
輸出檔案會在資料夾 /pages 內。

3. 僅輸出內容 -s
// -s, --no-header-footer
asciidoctor sample.adoc -o mypage.html
只會輸出html中 <body> 標籤裡的內容,方便用於嵌入式頁面。

更多參數可參考:
https://asciidoctor.org/docs/user-manual/#document-conversion

2020年5月1日 星期五

ubuntu18 + python2 文件編輯 AsciiDoc 安裝說明


基本上 AsciiDoc 已經停止更新,2017年已經發布了最終版本,並表示不再更新,當然 asciidoc 仍可繼續使用,直到停止支援 python2。既然都明寫建議用戶轉移使用 Asciidoctor(底層是Ruby) 或支援 Python3 的 asciidoc-py3,以下安裝步驟僅作為紀錄。

安裝之前請先確保主機已經安裝了Python(版本2.4以上),因為安裝的腳本是用Python寫的,多數的linux系統都已經內建,所以可以直接開始按照指令安裝asciidoc。

以下操作環境為ubuntu 18.04(server),目前ubuntu 20.04 LTS (2020/04 - 2025/04)為最新 LTS 版,但不影響下面的操作:

方法一
Step1. 下載原始碼
透過git下載原始碼,選用的版本為8.6.9
cd /bin
git clone https://github.com/asciidoc/asciidoc asciidoc-8.6.9

cd asciidoc-8.6.9
git checkout 8.6.9

Step2. 綁定指令
哈囉
(A) 綁定指令
ln -s /bin/asciidoc-8.6.9/asciidoc.py /bin/asciidoc
ln -s /bin/asciidoc-8.6.9/a2x.py /bin/a2x
(B) 編譯
autoconf
./configure
make
sudo make install


方法二
更快速的方法,可直接透過AsciiDoc Debian package安裝,指令:
apt install asciidoc

Official Site: http://asciidoc.org/

2020年3月31日 星期二

[Git] 手動安裝 git 2.6 + 新指令 sparse-checkout


sparse checkout 的功能可以讓使用者追蹤儲存庫裡特定的資料夾或檔案,不需要clone整個專案。新版本 git 2.6 有新指令 sparse-checkout 讓操作更簡單方便,關於 sparse-checkout 的設定方法可以參考「[Git] 如何使用 sparse checkout 下載 repository 內特定資料夾或檔案」進行操作,以下主要介紹如何手動安裝 git以及常用的 sparse-checkout 指令。

安裝

1. 安裝相關套件
安裝編譯 Git 所需的函式庫:curl, zlib, openssl, expat 和 libiconv
sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
  libz-dev libssl-dev

2. 下載 git 原始碼
先下載 git 原始碼,接著解壓縮並進入目錄。
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.26.0.tar.gz
tar zxvf git-2.26.0.tar.gz
git-2.26.0
※如果需要其他 git 版本 [https://mirrors.edge.kernel.org/pub/software/scm/git/]

3. 編譯與安裝
編譯的時間稍微比較久
./configure --prefix=/usr \
            --with-gitconfig=/etc/gitconfig \
            --with-python=python3 && make
安裝
sudo make install

安裝後並沒有明顯的成功訊息,所以很難確定到底有沒有裝好,所以可以透過指令查看版本:
git --version
git version 2.26.0


指令說明

◆ sparse checkout 設定初始化
git sparse-checkout init
開啟設定之後,在目錄會自動新增檔案 sparse-checkout ,預設內容是:
/*
!/*/
代表根目錄內第一層內所有的檔案都列入追蹤;換句話說,根目錄裡任何資料夾內的檔案(包含資料夾)都會被忽略。

必須先確定檔案 .git/info/sparse-checkout 已經存在,使用下列的指令才不會報錯:
◆ 列出目前追蹤的檔案
git sparse-checkout list
◆ 新增指令資料夾或檔案
語法與 .gitignore 相似,最簡單是直接列出完整的檔案路徑,根據需求可以列出複雜的規則,像是追蹤特定的檔案附檔名,或是排除特定的項目。
git sparse-checkout add [folderPath/filePath]
◆ 更新及應用
將資料夾或檔案路徑規則寫入 sparse-checkout ,並馬上採用。
git sparse-checkout set [folderPath/filePath]
◆ 取消
git sparse-checkout disable

參考資料:
https://git-scm.com/download/linux
https://git-scm.com/book/zh-tw/v2/開始-Git-安裝教學
http://www.linuxfromscratch.org/blfs/view/svn/general/git.html

2020年2月27日 星期四

[JavaScript] 安裝Jest單元測試


Jest為javascript的前端測試工具,安裝快速且不需複雜的設定,執行速度快,所有測項可以同時進行讓效能最大化,並可以搭配用在vue.js、React等進行UI component的測試。

安裝指令

npm install --save-dev jest
npm的安裝教學可以參考「教學」。


實作

以簡單的JavaScript程式來測試執行,將傳入的陣列合併成字串回傳:
// combineArray.js
const combineArray = (array, separator = ',') => {
        if (!Array.isArray(array)) {
                return new Error('Invalid input array');
        }
        if (typeof separator !== 'string') {
                return new Error('Invalid input separator');
        }

        return array.join(separator)
};

module.exports = combineArray;
測試的副檔名 *.test.js
// combineArray.test.js
const combineArray = require('./combineArray');

test('Combine array: [1, 2]', () => {
        expect(combineArray([1,2])).toBe('1,2');
});
  • 每個一測試檔案都必須至少有一個test(),第一個參數是名稱;第二個參數是執行expect實際測試內容的function。[參考]
  • expect()常用的方式expect(A).toBe(B),也就是期望A會等於B。[參考]

package.json新增內容:
{
  "scripts": {
    "test": "jest"
  }
}

執行測試

npm run test
所有的*.test.js都會跑過一次,並顯示結果,包含有多少測試項目以及成功和失敗的數量。



根據程式碼的複雜程度可以寫出許多測試項目,為了方便辨識測試內容,describe()可以將測項分類、建立群組,.toThrow()判斷錯誤發生的情形等等:
const combineArray = require('./combineArray');

describe('Combine array', () => {
        const inputArray = [1, 2];

        test('Default', () => {
                expect(combineArray([])).toBe('');
                expect(combineArray(inputArray)).toBe('1,2');
                expect(combineArray(inputArray, undefined)).toBe('1,2');
        });
        test('With custom separator', () => {
                expect(combineArray(inputArray, ' ')).toBe('1 2');
                expect(combineArray(inputArray, '*')).toBe('1*2');
                expect(combineArray(inputArray, '-')).toBe('1-2');
        });

        describe('With invalid input', () => {
                test.each(
                        [undefined, null, 1, 'string', {}]
                )('Input array: %s', (item) => {
                        expect(() => {
                                combineArray(item);
                        }).toThrow(new Error('Invalid input array'));
                });
                test.each(
                        [null, 1, [], {}]
                )('Input separator: %s', (item) => {
                        expect(() => {
                                combineArray(inputArray, item);
                        }).toThrow(new Error('Invalid input separator'));
                });
        });
});
執行結果


若有任何不符合預期的結果,console上會寫出哪一個測項失敗,預期的輸出和輸入為何。測試項目寫得越完備越好,往後即使更新主程式,也可以再以指令執行測試,確保程式運作如你所預期。

更多的方法可以前往官網參考:
https://jestjs.io/

2019年9月23日 星期一

[Vue.js] storybook安裝方式與環境建置

storybook是UI的開發環境,可以讓開發人員獨立建立元件,並在隔離的開發環境中互動式地展示元件。

storybook在主程式以外運作,因此不需要擔心應用程式特定的依賴關係或需求,用戶可以獨立開發UI元件。

環境
  • Ubuntu
  • node.js v10.15.1

套件版本:
  • Vue 2.6.10
  • Storybook 5.2.1

第一步、安裝storybook及相關套件

安裝 @storybook/vue 套件
npm install @storybook/vue --save-dev
安裝其他相依套件,包含:vuevue-loadervue-template-compiler@babel/corebabel-loaderbabel-preset-vue
npm install vue --save
npm install vue-loader vue-template-compiler @babel/core babel-loader babel-preset-vue --save-dev


第二步、新增npm script

在檔案package.json新增script:
{
  "scripts": {
    "storybook": "start-storybook"
  }
  ...
}


第三步、新增config

在檔案.storybook/config.js新增config:
import { configure } from '@storybook/vue';

configure(require.context('../src/components/stories', true, /\.stories\.js$/), module);
代表會載入所有在資料夾 ../src/components/stories 內的符合 *.stories.js 的檔案。

第四步、新增stories

新增一個stories Button.stories.js
// src/components/stories/Button.stories.js
import Vue from 'vue';
import MyButton from '../Button.vue';

export default { title: 'Button' };

export const withText = () => '<my-button>with text</my-button>';

export const withEmoji = () => '<my-button>😀 😎 👍 💯</my-button>';

export const asAComponent = () => ({
  components: { MyButton },
  template: '<my-button :rounded="true">rounded</my-button>'
});


第五步、執行

npm run storybook
打開瀏覽器確認




參考專案架構:
./
├── .storybook
│   └── config.js
├── babel.config.js
├── package.json
├── package-lock.json
├── public
│   ├── favicon.ico
│   └── index.html
├── README.md
└── src
    ├── App.vue
    ├── assets
    │   └── logo.png
    ├── components
    │   ├── Button.vue
    │   └── stories
    │       └── Button.stories.js
    └── main.js

資料來源:https://storybook.js.org/docs/guides/guide-vue/

2019年7月26日 星期五

Ubuntu18+PHP7.2 安裝Laravel 5.8


安裝composer

Composer是PHP套件管理工具,請參考『[Symfony] ubuntu18安裝symfony 4.2教學』第二部分提供兩種安裝方式。[官方安裝說明]


建立專案

提供兩個方式來建立一個專案,命名為 blog
方法一、
composer create-project --prefer-dist laravel/laravel blog
指令執行後會自動 composer install 讀取 composer.json 安裝所需及相依的套件。

方法二、
composer global require "laravel/installer"
2-A. 把 $HOME/.composer/vendor/bin 路徑放置於環境變數 $PATH 裡。
export PATH="~/.config/composer/vendor/bin:$PATH"
laravel new blog
2-B. 如果沒有設定環境變數,可能會有錯誤訊息laravel: command not found,也可以用決定路徑來執行。
# "沒有"設定環境變數
.config/composer/vendor/bin/laravel new blog


兩個方法二擇一,成功後目錄應該會多一個 blog/ 的專案目錄,進入專案並執行:
php artisan key:generate

# Example oupout
Application key set successfully.
如果沒有執行這一段,執行專案可能會跳出錯誤訊息 No application encryption key has been specified.


執行專案

開發時可以執行下面的指令讓專案運作,預設路徑為http://localhost:8000
php artisan serve
也可以指定 HOSTPORT
php artisan serve --port=8080 --host=192.168.88.139


打開瀏覽器看到下面的畫面,就可以進入開發囉!



查詢Laravel安裝版本
php artisan -V
Laravel Framework 5.8.29


參考資料:
https://laravel.com/docs/5.8

2019年5月16日 星期四

[Vue.js] 升級vue-cli3建立Vue專案


之前安裝vue-cli2來建立vue專案 --『[Vue.js] 在ubuntu安裝 Vue.js』,有需要可以參考。

開始安裝vue-cli 3
npm install --global @vue/cli

完成安裝後可以用指令檢查版本
vue --version
// or
vue -V

vue cli3多了一個 create 功能來建專案:
vue create [APP_NAME]
你可以使用預設選項或根據需求選擇,結束後會自動幫你執行 npm install

整個專案目錄精簡了不少,少了 config/build/ 兩個目錄,然後 static/ 改為 public/
執行下面指令就可以進入開發囉
npm run serve
vue cli不再使用dev當作開發環境的指令,如果你習慣用dev,可以到 package.json 修改script。



因為移除了 config/ ,因此『[Vue.js] 開發用和正式環境的參數設定方式(.env)』的方式勢必就要調整了。
在專案的根目錄新增兩個檔案 .env.env.production,分別用在開發和正式環境。所有的參數名稱必須加上前綴 VUE_APP_ ,只有名稱 VUE_APP_* 的參數,才能在vue專案裡使用。
1. .env
VUE_APP_NODE_ENV: '"development"'
VUE_APP_ROOT_API: '"http://localhost/api"'
2. .env.production
VUE_APP_NODE_ENV: '"production"'
VUE_APP_ROOT_API: '"https://example.com/api"'

使用方式和之前相同
export default {
  mounted() {
    console.log(process.env.VUE_APP_ROOT_API);
  }
}

關於環境變量,參考下面連結了解更多。
https://cli.vuejs.org/guide/mode-and-env.html#environment-variables


2019年3月23日 星期六

[Vue.js] 安裝axios對後端傳送Request (GET/POST/PUT/DELETE)


安裝指令
npm install axios

使用方式
import axios from 'axios'
// use axios in your project
// ...
1. GET
[URL] 字串、API位址
[HEADER] dict,例如:{'Content-Type': 'multipart/form-data'}
[PARAM] dict,例如:{'id': '1'},等同於?id=1
axios.post('[URL]', {headers:[HEADER], params: [PARAM]})
    .then(response => {
        console.log(response);
    })
    .catch(err => {
        console.log(err.response);
    });
2. POST
[DATA] 資料
axios.get('[URL]', [DATA], {headers:[HEADER]})
    .then(response => {
        console.log(response);
    })
    .catch(err => {
        console.log(err.response);
    });
3. PUT
格式與post相同
axios.put('[URL]', [DATA], {headers:[HEADER]})
    .then(response => {
        console.log(response);
    })
    .catch(err => {
        console.log(err.response);
    });
4. DELETE
axios.delete('[URL]', {headers:[HEADER], data:[DATA]})
    .then(response => {
        console.log(response);
    })
    .catch(err => {
        console.log(err.response);
    });

vue-resource 比較不同的是error handling,API回應中通常包含http_statsu、body、data等,success handling回傳的就是標準的回應,但error handling放在response這個key裡面。舉例要取得http_status,在success handling裡是response[http_status],但error handling是err.response[http_status]


2019年2月10日 星期日

[Symfony] ubuntu18安裝symfony 4.2教學


上次寫的這篇「[Symfony] Ubuntu16+PHP7 安裝Synfony3.4」,是用指令 symfony 新建專案,例如 symfony new my_project 3.4 會建立一個版本3.4的專案,不過最新的4.2沒辦法用這個方法,因此誕生了這篇文章,另外作業系統也改用ubuntu18囉!

一、安裝php及相關套件
安裝php(>=7.1.3),ubuntu18內建為7.2。[版本需求]
sudo apt install php

這邊的套件則是symfony專案所需
php-xml php-curl php-mbstring php-zip unzip

二、安裝composer
安裝方法在這邊寫得很詳細,如果是作業系統是windows也有說明,下面則是整理了ubuntu的安裝方法,二擇一即可。(目前是v1.8.3)

[方法一]
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

WARNING: Please do not redistribute the install code. It will change with every version of the installer. Instead, please link to this page or check how to install Composer programmatically.
這一段話是要提醒[方法一]可能會因為版本更新而改變,如果想要安裝最新版本的composer的話,會建議直接到官方這個頁面查看指令,或者使用[方法二],也就是腳本(script)來執行。

[方法二]
開一個新的空白檔案install.sh,增加執行(x)權限。
touch install.sh
chmod +x install.sh

install.sh的檔案內容
#!/bin/sh

EXPECTED_SIGNATURE="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_SIGNATURE="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_SIGNATURE" != "$ACTUAL_SIGNATURE" ]
then
    >&2 echo 'ERROR: Invalid installer signature'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

接著只要執行就可以了,如果成功的話應該不會看到任何訊息,沒有消息就是好消息XD
./install.sh

這個方法可以直接安裝最新的composer,想參考更完整的說明可以看這邊

兩個方法二擇一完成之後,這時候目前的目錄下應該會有一個composer.phar的檔案,執行下面的指令,接著就可以直接在全域使用composer。
sudo cp composer.phar /bin/composer

三、新建symfony專案
建立一個名字為my-project的新專案,如果前面的步驟確實的話,會自動執行composer install完成所有的安裝。
composer create-project symfony/website-skeleton my-project

可以來看看專案囉!
cd my-project/
php bin/console server:run
預設為http://127.0.0.1:8000,打開瀏覽器看到畫面就算大功告成了!





如果安裝遇到問題,可以利用下面的方法確認。
composer require symfony/requirements-checker

這時候專案目錄public內會多一個check.php的檔案,可以打開http://127.0.0.1:8000/check.php確認可能的問題在哪,參考畫面如下:


當你排除上面所說的問題後,可以用指令來移除check.php等相關的檔案,以防透漏不必要或機密的資運給網站的訪客。
composer remove symfony/requirements-checker

參考資料:
https://symfony.com/doc/current/setup.html
https://symfony.com/doc/current/reference/requirements.html

2019年1月10日 星期四

[Django] Ubuntu+Apache2+mod_wsgi 部屬Django專案


在開發階段習慣用內建的runserver來啟動Django專案,但官方不建議在正式環境使用,以下介紹如何用ubuntu上安裝apache2和相關的套件來啟動專案,這樣最大的好處就是伺服器開啟時,可以由apache2自動將專案啟動。

Step1. 建立Django專案
※如果已經有現有專案,可以跳過這個步驟。
a. 建立一個獨立的虛擬環境(相關文章:[Python] Virtualenv基本操作)
$ sudo apt install python3-pip
$ sudo pip3 install virtualenv
$ mkdir web_project
$ cd web_project
$ virtualenv env --no-site-packages
b. 安裝Django、新增專案
$ source env/bin/activate
$ pip3 install django
$ django-admin.py startproject myproject .
c. 請在settings.py加上伺服器本身的IP,在開發階段可以用*代替。STATIC_ROOT這行本來在檔案裏面沒有,要手動自己加上去。
# myproject/settings.py
ALLOWED_HOSTS = ["*"]
...
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
d. 加入static的檔案
$ python3 manage.py collectstatic
成功之後資料夾內會多一個static的目錄。
※關於STATIC_ROOT的說明,可以參考『[Django] 如何設定static files(css, javascript, images)』。


Step2. 安裝Apache、mod_wsgi
python2和python3兩者擇一,請注意Django升級到2.0後就不再支援python2了,建議使用python3喔!
# python3
$ sudo apt update
$ sudo apt install apache2 libapache2-mod-wsgi-py3

# python2
$ sudo apt update
$ sudo apt install apache2 libapache2-mod-wsgi
如果希望重開伺服器時,apache2可以自動啟動,請執行下面指令。
$ sudo systemctl enable apache2
完成安裝後,可以在 http://localhost/ 看到apache預設的首頁。


Step3. 設定apache
請先參考一下目前的目錄架構

新建一個Apache的config檔,加入這一段:
# /etc/apache2/sites-available/django.conf
<VirtualHost *:80>
    DocumentRoot /var/www/web_project

    Alias /static /var/www/web_project/static
    <Directory /var/www/web_project/static>
        Require all granted
    </Directory>

    <Directory /var/www/web_project/myproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess myproject python-path=/var/www/web_project python-home=/var/www/web_project/env
    WSGIProcessGroup myproject
    WSGIScriptAlias / /var/www/web_project/myproject/wsgi.py

</VirtualHost>

預設只會讀取default內的設定,請記得啟用自訂的django.conf,並停用default的設定。
$ sudo a2ensite django.conf
$ sudo a2dissite 000-default.conf
$ sudo service apache2 reload
打開 http://localhost/ 就可以看到網站了。


參考資料:
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04

2019年1月6日 星期日

[Vue.js] 安裝vue-resource執行POST, GET


vue官方之前推薦使用vue-resource套件來執行HTTP Request,不過vue更新到2.0之後,作者就宣告不再更新vue-resource,目前漸漸被axios取代。所以這篇只是做為紀錄性質,寫一下使用vue-resource的方法。

請先用指令npm安裝vue-resource
$ npm install vue-resource

先在專案中加入這個套件
# src/main.js
import VueResource from 'vue-resource'
Vue.use(VueResource)

請根據需求設定{URL} {BODY} {HEADER}

1. GET
this.$http({
 url: {URL},
 method: 'GET',
 body: {},
 headers: {HEADER}
 }).then(function (response) {
  console.log('success');
 }, function (response) {
  console.log('fail');
});

2. POST
this.$http.post({URL}, {BODY}, {HEADER}).then(
 function (response) {
  console.log('success');
 }, function (response) {
  console.log('fail');
});
this.$http({
 url: {URL},
 method: 'POST',
 body: {BODY},
 headers: {HEADER}
 }).then(function (response) {
  console.log('success');
 }, function (response) {
  console.log('fail');
});

2018年11月16日 星期五

[Symfony] Ubuntu16+PHP7 安裝Synfony3.4


這次作業軟體是Ubuntu16.04,安裝的php是7.0。

首先請下載symfony指令,並移到指定資料夾方便全域使用。
sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

接著馬上就能新增一個專案,my_project可以替換成想要的專案名稱。
# create a symfony project
symfony new my_project 3.4

我第一次建完專案出現訊息提示"simplexml_import_dom() must be available",請指令安裝php-xml排除這個問題。
如果想確定環境是否符合symfony的執行需求,可用下面指令:
php my_project/bin/symfony_requirements

如果看到這樣的畫面,基本上就沒問題,下面還另外有一些建議,可以選擇性安裝!


將Symfony專案啟動的方式
# run application
cd my_project
php bin/console server:run

打開網頁 http://localhost:8080可以看到預設Symfony的頁面,收工囉~


2018年7月24日 星期二

安裝node js v8.9.4

前置作業
# install
apt-get update
sudo apt-get install build-essential libssl-dev
curl https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | sh
source ~/.profile
安裝
# install
nvm install v8.9.4
確認安裝版本
# check
node -v