Sitemap

ติดตั้ง LEMP Stack บน Ubuntu 22.04 (VM) แถมติดตั้ง phpMyAdmin

5 min readJan 30, 2023
Press enter or click to view image in full size

ติดตั้งโปรแกรม
Oracle VM (Virtual Machine) VirtualBox https://www.virtualbox.org/wiki/Downloads

ดาวน์โหลด Ubuntu
https://ubuntu.com/download/desktop/thank-you?version=22.04.1&architecture=amd64

สร้าง VM Ubuntu

สามารถทำตามจาก clip นี้ได้ครับ

https://youtu.be/hTYzrvDL4lk
  1. Machine > New…
Press enter or click to view image in full size

2. ใส่ชื่อ (Name) และไฟล์ ISO ของ Ubuntu (ISO Image) และให้ติ๊ก Skip Unattended Installation ตามภาพ

Press enter or click to view image in full size

3. ระบุ RAM และ CPU ที่จะ allocate ให้กับ VM (ถ้ารันไม่ผ่านอาจจะเพิ่ม RAM และ CPU ได้) แนะนำเป็น RAM 4096MB และ 2 CPU

Press enter or click to view image in full size

4. ระบุ Hard disk ที่จะ allocate ให้ VM

Press enter or click to view image in full size

5. ทำการ Start VM ขึ้นมาใหม่ (ซึ่งจะเหมือนกับการเปิด server ที่ใส่ cd ตัวติดตั้ง ubuntu ไว้) ให้กด Enter เพื่อเริ่มการติดตั้ง

Press enter or click to view image in full size

6. กดเลือก Install Ubuntu

Press enter or click to view image in full size

7. เลือกตามภาพเพื่อให้การติดตั้งไม่นานมาก

Press enter or click to view image in full size

8. ใส่ชื่อ username, password

Press enter or click to view image in full size

9. กด Enter เพื่อเข้าสู่ Ubuntu ครั้งแรก

Press enter or click to view image in full size

10. เปิดหน้าจอไว้ซักพักจะมีให้ update ubuntu ให้กดเลือก Install Now ดังภาพ

Press enter or click to view image in full size

เซ็ตค่าเบื้องต้น Ubuntu และติดตั้ง Open SSH

  1. เปิด terminal ขึ้นมา
Press enter or click to view image in full size

2. ติดตั้งโปรแกรม net-tools โดยระบุสิทธิ์เป็น Super User (sudo)

sudo apt install net-tools

3. ตรวจสอบ ip ด้วยคำสั่ง

ifconfig
Press enter or click to view image in full size

4. ติดตั้งโปรแกรม SSH Server

sudo apt install openssh-server

5. ตรวจสอบสถานะของ SSH Server

sudo service ssh status
Press enter or click to view image in full size
กด q เพื่อออกจากการตรวจสอบสถานะ

ติดตั้ง LEMP Stack

สามารถทำตามจาก clip นี้ได้ครับ

LEMP Stack ประกอบไปด้วย
L — Linux
E — Nginx
M — MySQL
P — PHP
เอกสารอ้างอิง https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04#step-2-%E2%80%94-installing-mysql

  1. จากเครื่อง Host (Windows/Mac) เปิด Command Prompt/Terminal เพื่อ Secure Shell เข้าไปที่ VM
ssh u6090059@<<ip address>>
Press enter or click to view image in full size
  1. Update แหล่งติดตั้ง Package ต่าง ๆ ของเราก่อน
sudo apt update

2. ติดตั้ง Nginx

sudo apt install nginx

3. ลองเรียก web ผ่าน ip http://<<ip address>>/

Press enter or click to view image in full size

4. ตรวจสอบ folder html ของ nginx

Press enter or click to view image in full size

5. ดู permission ของไฟล์

Press enter or click to view image in full size

6. ไฟล์ html ของ Nginx จะเก็บไว้ที่ /var/www/html ให้ทำการแก้สิทธิของ folder ดังกล่าวด้วยคำสั่ง

sudo chown -R u6090059:u6090059 /var/www/html

5. ดู permission ของไฟล์อีกครั้ง

Press enter or click to view image in full size

6. ลองสร้างไฟล์ index.html ด้วย nano

nano index.html

7. ใส่ข้อมูลในไฟล์ตามภาพ (Ctrl + S เพื่อ save, Ctrl + X เพื่ออกจาก nano)

Press enter or click to view image in full size

8. ลองเรียก web ผ่าน ip http://<<ip address>>/

9. ติดตั้ง MySQL Server

sudo apt install mysql-server

10. เปิด MySQL CLI

sudo mysql

11. ใส่คำสั่ง SQL เพื่อสร้าง user สำหรับเชื่อมต่อ MySQL (รันทีละบรรทัด)

CREATE USER 'u6090059'@'%';
ALTER USER 'u6090059'@'%' IDENTIFIED WITH mysql_native_password BY 'P@ssword1234';
GRANT ALL PRIVILEGES ON * . * TO 'u6090059'@'%';
FLUSH PRIVILEGES;
SELECT User, Host FROM mysql.user;
exit
Press enter or click to view image in full size

12. ติดตั้ง PHP ด้วยคำสั่ง

sudo apt install php-fpm php-mysql

13. Config Nginx ให้รองรับ PHP

sudo nano /etc/nginx/sites-enabled/default
Press enter or click to view image in full size
Press enter or click to view image in full size

14. ตรวจสอบความถูกต้องของ config ใน nginx

sudo nginx -t
Press enter or click to view image in full size

15. Restart nginx

sudo service nginx restart

16. ทดสบว่า php ใช้งานได้บน Nginx โดยการเพิ่มไฟล์ใน html

cd /var/www/html
nano info.php
Press enter or click to view image in full size

17. ทดสอบเรียกไฟล์ info.php ผ่านหน้า web http://<<ip address>>/info.php

Press enter or click to view image in full size

(แถม) ติดตั้ง phpMyAdmin

อ้างอิงจาก https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-20-04-server

  1. ติดตั้ง phpMyAdmin ด้วยคำสั่ง
sudo apt install phpmyadmin
Press enter or click to view image in full size
ให้กด tab มาที่ Ok แล้ว Enter (ไม่ต้องเลือก)
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

2. ทำการ link folder /var/www/html/phpmyadmin ไปที่ folder ที่เก็บ code ของ phpmyadmin

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

3. ทดสอบเข้าใช้งาน phpMyAdmin ผ่าน url:
http://<<ip address>>/phpmyadmin

Press enter or click to view image in full size
Press enter or click to view image in full size

จบละครับ :D

บทความโดย อ.ผศ.ดร.กานต์ ยงศิริวิทย์
วิทยาลัยนวัตกรรมดิจิทัลเทคโนโลยี มหาวิทยาลัยรังสิต
https://www.youtube.com/@karnyong
https://www.facebook.com/melivecodepage

--

--

Karn Yongsiriwit
Karn Yongsiriwit

Written by Karn Yongsiriwit

Lecturer at Digital Innovation Technology, Rangsit University

No responses yet