CentOS 6.8部署Google Firebase Hosting
Google Firebase推出至今也好幾年,據說可以直接使用這個模式使用建置在世界各地的Google CDN加快存取速度,現在來用用看他們的Firebase Hosting功能吧!
本次使用此功能的作業系統就是CentOS6.8,不過我覺得CentOS7應該也可以照做唷!~
Step 1
安裝nodejs
1 |
[Andy@www ~]$ sudo yum install -y nodejs --enablerepo=epel |
測試一下有沒有安裝好npm,若有就會出現下列版本訊息!
1 2 |
[Andy@www ~]$ npm -v 5.6.0 |
若沒有,才需要另外安裝npm:『sudo yum install npm』
Step 2
安裝Firebase-tools
1 |
[Andy@www ~]$ sudo npm install --unsafe-perm -g firebase-tools |
指令中一定要加上『–ussafe-perm』,否則在安裝的過程中就會不斷出現權限不足的錯誤!
1 gyp WARN EACCES user "root" does not have permission to access the dev dir "/usr/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp/8.9.4"
切換到想當成Host的目錄下,建立要拿來放Firebase project的目錄!
1 2 3 |
[Andy@www ~]$ mkdir ~/myFirebaseProj [Andy@www ~]$ cd ~/myFirebaseProj [Andy@www myFirebaseProj]$ |
Step 3
登入Firebase帳號
請使用下方第5行的網址登入帳號!登入程式會等待你驗證~
登入驗證身份成功之後,程式會跳出第9行驗證成功字樣,而最後是你登入的帳號!
1 2 3 4 5 6 7 8 9 |
[Andy@www myFirebaseProj]$ firebase login ? Allow Firebase to collect anonymous CLI usage and error reporting information? No Visit this URL on any device to log in: https://accounts.google.com/o/oauth2/auth?client_id=xxxxxx9527520xxxxxx.apps.googleusercontent.com&scope=email%20openid%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloudplatformprojects.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Ffirebase%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&response_type=code&state=xxxxxx&redirect_uri=http%3A%2F%2Flocalhost%3Axxxx Waiting for authentication... ✔ Success! Logged in as xxxx@gmail.com |
Step 4
初始化專案(firebase init)
贊助廣告
在你的專案目錄下執行初始化指令。
Firebase會問你一些關於專案的設定,就用上下鍵選擇一下!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[Andy@www myFirebaseProj]$ firebase init ######## #### ######## ######## ######## ### ###### ######## ## ## ## ## ## ## ## ## ## ## ## ###### ## ######## ###### ######## ######### ###### ###### ## ## ## ## ## ## ## ## ## ## ## ## #### ## ## ######## ######## ## ## ###### ######## You're about to initialize a Firebase project in this directory: /home/andy/myFirebaseProj ? Which Firebase CLI features do you want to setup for this folder? Press Space to select features, then Enter to confirm your choices. ⚠ You have not selected any features. Continuing will simply associate this folder with a Firebase project. Press Ctrl + C if you want to start over. === Project Setup First, let's associate this project directory with a Firebase project. You can create multiple project aliases by running firebase use --add, but for now we'll just set up a default project. ? Select a default Firebase project for this directory: [create a new project] i Writing configuration info to firebase.json... i Writing project information to .firebaserc... ✔ Firebase initialization complete! Project creation is only available from the Firebase Console Please visit https://console.firebase.google.com to create a new project, then run firebase use --add |
Step 5
指定Firebase專案
選擇你要用Firebase上的哪一個專案對應到現在這個目錄!
1 2 3 4 5 6 |
[Andy@www myFirebaseProj]$ firebase use --add ? Which project do you want to add? xxxxx ? What alias do you want to use for this project? (e.g. staging) xxxxx Created alias xxxxx for xxxxx. Now using alias xxxxx (xxxxx) |
Step 6
建立public資料夾,存放你的網站檔案!
1 |
[Andy@www myFirebaseProj]$ mkdir public |
Step 7
編輯Firebase設定檔!改成下方設定值並且存檔!
1 |
[Andy@www myFirebaseProj]$ vim firebase.json |
1 2 3 4 5 |
{ "hosting": { "public": "public" } } |
Step 8
到public資料夾下建立一個測試index.html
1 2 |
[Andy@www myFirebaseProj]$ cd public [Andy@www public]$ vim index.html |
1 2 3 4 5 6 7 |
<html> <meta charset='utf-8'/> <title>Firebase Testing</title> <body> <h1>安安你好</h1> </body> </html> |
Step 9
部署Firebase
最後!將我們的目錄部署到Firebase!
1 2 3 4 5 6 7 8 9 10 11 12 |
[Andy@www public]$ firebase deploy === Deploying to 'xxxxx'... i deploying hosting i hosting: preparing public directory for upload... ✔ hosting: 1 files uploaded successfully ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/xxxxx/overview Hosting URL: https://xxxxx.firebaseapp.com |