YiHeng0221
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 「程式筆記」部署 AWS EC2 雲端主機 + LAMP Server + phpMyAdmin / 更新 phpMyAdmin root 密碼 ## 前言 重新部署 AWS EC2 雲端主機,原本已經部署完了,健忘如我卻因為忘記 phpMyadmin 的密碼,在腦鬆的狀態下的跟著 stackoverflow 想試著重新設定密碼,卻整個爆掉,藉這個機會重新部署並做個紀錄,並藉此解決重設密碼的問題(本文最下方)。 本文部署方式參考 [部署 AWS EC2 遠端主機 + Ubuntu LAMP 環境 + phpmyadmin](https://github.com/Lidemy/mentor-program-2nd-yuchun33/issues/15) 的方式部署,內容已經很詳盡,感謝 yuchun33 大大。 ## 設定遠端主機 ### Step 1 建立帳號 首先先至 AWS 官方網站建立帳號,需要簡訊驗證,在此發現台灣之星似乎收不到 amazon 的驗證簡訊,上網爬了一下文有發現[\[問題\] 台灣之星註冊amazon帳號收不到驗証碼](https://www.ptt.cc/bbs/MobileComm/M.1594860626.A.14C.html) 這篇文章,解決方式是將 amazon 網站語言切換成英文,就會成功收到,但是我仍然沒有收到,之後我選擇直接請官方客服協助進行手動驗證,也就是他會直接打電話過來,但聯絡完之後需要至多 48 小時的時間。 ### Step 2 選擇方案 完成驗證後進入 [AWS 管理控制台](https://us-east-2.console.aws.amazon.com/console/home?region=us-east-2) 於建制解決方案點選啟動虛擬主機,如附圖: ![](https://i.imgur.com/1rmMTC5.jpg) ### Step 3 設定 AMI 接下來會進到選擇 Amazon Machine Image 的步驟,直接選擇你需要的,本文是使用 Ubuntu Server 18.04,可直接在搜尋欄中搜尋並點擊 Select: ![](https://i.imgur.com/zBE9HDo.jpg) :::info Amazon Machine Image 是包含軟體組態 (包括作業系統) 的範本。可以從 AMI 啟動執行個體,執行個體是 AMI 的複本,在雲端中以虛擬伺服器的形式執行。 [執行個體與 AMI](https://docs.aws.amazon.com/zh_tw/AWSEC2/latest/UserGuide/ec2-instances-and-amis.html) ::: ### Step 4 選擇執行個體類型 選免費的那個: ![](https://i.imgur.com/JA6QpxA.jpg) ### Step 5 一直按 Next 接下來只要一直點擊 next 直到步驟六,中間步驟三是設定執行個體的詳細資訊;步驟四是根據需要修改現有的項目。針對每個要加入的執行個體存放磁碟區;步驟五是新增管理標籤,用來分類 AWS 的資源。 ### Step 6 設定安全群組 點擊 Add Rule 並新增 port 80 和 port 443,用來與 http/https 連線: ![](https://i.imgur.com/BTXwHTE.jpg) ### Step 7 Review 確認一下你所選的東西,然後就可以下一步。 ![](https://i.imgur.com/EzmJO7M.jpg) ### Step 8 新增及下載金鑰對 依照需求選擇要新增一個新的金鑰對或是選擇已有的金鑰對,下載下來然後收好。 再來點擊 launch instances 來啟動。 ![](https://i.imgur.com/146Dhll.jpg) ### Step 10 個體啟動成功 會出現「 Your instances are now launching 」的提示,點擊右下角的「 View Instances 」進入管理介面就可以發現 Server 已經啟動了, 找到「IPv4 Public IP」並把它記下來,這就是之後網站的 IP。 ![](https://i.imgur.com/MfMiHgK.jpg) ### Step 11 連線至執行個體 點擊上面的連線(Connect)進行連線: ![](https://i.imgur.com/Y8RAAVg.jpg) 接下來會進入連線至執行個體的頁面,CLI 進入金鑰對的資料夾中,再將裡面的兩行程式碼複製並在 CLI 上輸入: ![](https://i.imgur.com/1vGoGHc.jpg) 接下來就連線到了! ![](https://i.imgur.com/R1eBryh.jpg) ### 卡關時間 首先我在連線至執行個體的頁面中點選 Session Manager 上發現了一個奇怪的警告訊息: ![](https://i.imgur.com/IJE1Yom.jpg) 把警告餵了狗: > Session Manager 是 System Manager 服務中的其中一項功能,能夠讓使用者透過主控台或指令迅速的與 EC2 主機進行連線。 不確定是不是太重要,於是我先沒有理他,繼續在 CLI 輸入: ` ssh -i "<金鑰對名稱>" ubuntu@ec2-<IP4>.us-east-2.compute.amazonaws.com` 再來卻出現了這個錯誤訊息: `The authenticity of host '<IP4>' can't be established.` 但後面還有詢問我是否要繼續連線 `Are you sure you want to continue connecting (yes/no)? `,按照 yuchun33 大大的步驟也有看到該訊息,因此繼續輸入 `yes`。 之後出現了警告: `Warning: Permanently added '<IP4>' (ECDSA) to the list of known hosts. Connection closed by <IP4> port 22` `Connection closed by <IP4> port 22`? なに?沒有連上! 重新啟動CLI 再輸入一次: ` ssh -i "<金鑰對名稱>" ubuntu@ec2-<IP4>.us-east-2.compute.amazonaws.com` 就直接成功連上了,沒有出現任何錯誤訊息,到底是發生什麼事...? ## 設定 LAMP LAMP 就是 Linux + apache + mysql + php。 ### Step 1 更新 ubuntu 在 CLI 中進入虛擬主機之後,先更新 ubuntu 的系統: `$ sudo apt update && sudo apt upgrade && sudo apt dist-upgrade` CLI 會詢問是否要繼續,輸入 `$ y` yuchun33 大大的步驟中表示會出現三次設定框,我的電腦中沒有出現,但並未影響後續安裝。 ### Step 2 安裝 Tasksel Tasksel 是一款可以讓我們快速安裝 dns server、lamp,等等應用的軟體包。 輸入:`$ sudo apt install tasksel` CLI 會詢問是否要繼續,輸入 `$ y` ### Step 3 使用 Tasksel 下載 LAMP-Server 輸入:`$ sudo tasksel install lamp-server` 跳入安裝畫面: ![](https://i.imgur.com/abGLEHf.jpg) 不用害怕,等他跑完。 ### Step 4 下載 phpmyadmin 輸入:`$ sudo apt install phpmyadmin` 來下載 phpmyadmin。 CLI 會詢問是否要繼續,輸入 `$ y` ### Step 5 連接 apache2 接下來會跳入連接 apache2 的畫面: ![](https://i.imgur.com/MUTSemJ.jpg) 按下空白鍵,讓 apache2 顯示米字號 `*` 再按下 Enter。 接下來會詢問你是否要設定 dbconfig-common,直接選擇 yes: ![](https://i.imgur.com/dh9xNfF.jpg) 再來設定密碼即可。 ### Step 6 改變 phpmyadmin 登入設定 改成可以使用密碼登入: `$ sudo mysql -u root mysql` 再來會要輸入 SQL 指令: `$ UPDATE user SET plugin='mysql_native_password' WHERE User='root';` 刷新MySQL的系統權限相關表: `$ FLUSH PRIVILEGES;` 離開 sql 指令: `$ exit` 設定 root 密碼: `$ sudo mysql_secure_installation` 出現訊息後輸入 y/Y: ![](https://i.imgur.com/rrqFwg0.jpg) 會需要選擇密碼強度,按照個人習慣來選擇: ![](https://i.imgur.com/YOMq9FD.jpg) :::warning 我就是因為在這裡輸入了 1,然後我就忘記我密碼中的 special characters 是哪個了...... ::: 輸入密碼、確認密碼後接下來會出現幾個問題: * Do you wish to continue with the password provided? * Remove anonymous users? * Disallow root login remotely? * Remove test database and access to it? * Reload privilege tables now? 都選擇 yes 直到出現 All Done! 即可。 最後進入瀏覽器輸入`<IP4>/phpmyadmin`就可以看到登入畫面,帳號為`root`,密碼就是剛設定的。 ## 放置檔案 ### Step 1 進入/var/www/html 輸入 `$ cd /var/www/html` ### Step 2 改變權限 `$ sudo chown ubuntu /var/www/html` 這樣才能去 github clone 自己的檔案。 ### Step 3 clone `$ git clone 網址` ### 看看你的成果 在網址輸入`<ip4>/<folderName>/<fileName>` 就可以看到放入的檔案內容! ## 卡關時間 -- 更改 phpMyadmin root 密碼 如果健忘如我,忘了 phpMyadmin root 的密碼,該如何重新設定呢? ### 確認 MySQL 版本 首先,一樣使用 CLI 進入你的虛擬主機。 輸入`$ mysql -V` 確認版本。 ### 繞過帳號權限與密碼認證重新開啟 MySQL 先停止 MySQL 服務,輸入: `$ sudo /etc/init.d/mysql stop` 再確認你有一個 mysqlid folder 在 /var/run/ 中,若沒有就創一個: `$ sudo mkdir /var/run/mysqld` 並修改權限: `$ sudo chown mysql /var/run/mysqld` 輸入此指令以繞過帳號權限與密碼認證開啟 MySQL: `$ sudo mysqld_safe --skip-grant-tables&` :::warning 這個指令只有在本機端可以使用。 ::: 輸入後你會看到類似下圖這樣的訊息: ![](https://i.imgur.com/uShcDbm.jpg) 再來按下 enter。 接下來就可以繞過帳號權限與密碼認證開啟 MySQL: `$ sudo mysql --user=root mysql` 成功登入之後就會看到 `mysql>` 以輸入 SQL 指令。 ### 更改密碼 這裡以版本 5.7 為例,參考文章中有 MySQL 8 的更改指令。 > 參考文章 [How to Reset the MySQL Root Password on Ubuntu](https://devanswers.co/how-to-reset-mysql-root-password-ubuntu/) 依序輸入以下指令以更新密碼: `mysql> update user set authentication_string=PASSWORD('<你的新密碼>') where user='root';` `mysql> update user set plugin="mysql_native_password" where User='root';` 刷新MySQL的系統權限相關表: `mysql> flush privileges;` 退出 MySQL: `mysql> exit;` ### 測試新密碼 再重新開啟 MySQL 服務之前,要先確認所有 MySQL processes 都已經結束: `$ sudo killall -u mysql` 輸入後會看到類似以下文字的訊息 `2020-12-12T13:39:51.856700Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended` 開啟 MySQL 服務: `$ sudo /etc/init.d/mysql start` 用新密碼登入 MySQL: `$ sudo /etc/init.d/mysql start` 接下來看到 Welcome 的訊息就代表你更新密碼成功了!

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully