DEV Community

Damrongsak Reetanon
Damrongsak Reetanon

Posted on

3

ติดตั้ง NGINX แบบขี้เกียจ

หลังจากที่ไม่ได้จับ NGINX มาสักพัก มัวแต่ไปนั่งเล่นอย่างอื่นอยู่ แต่พอดีว่าช่วงนี้งานเข้า NGINX รัว ๆ ก็เลยต้อง setup ระบบไว้เตรียมบรรยาย ... แต่มาคราวนี้รู้สึกขี้เกียจ ไม่อยากทำอะไรยุ่งยาก เก็บพลังงานไว้ดีกว่า ... ด้วยความที่เพิ่งไปสอน Ansible มา เห็นว่า NGINX ทำ collection ใหม่ไว้หลายอย่างเลย ลองแวะไปดูให้ละเอียด ... มี Ansible playbook สำหรับติดตั้ง NGINX นินา ก็เลยบันทึกประสบการณ์แบบขี้เกียจ ๆ มาให้ลองอ่านดูครับ

สิ่งที่ต้องเตรียม

  • server Ubuntu Server 20.04.2 LTS

ติดตั้ง ansible และส่วนอื่น ๆ ที่จำเป็น

$ sudo apt -y update
$ sudo apt install -y ansible
$ ansible-galaxy collection install nginxinc.nginx_core
$ git clone https://github.com/nginxinc/ansible-collection-nginx.git
Enter fullscreen mode Exit fullscreen mode

แก้ไข deploy-nginx.yml

$ cd ansible-collection-nginx/playbooks/
$ vi deploy-nginx.yml
---
- hosts: all
  become: yes
  become_user: root
  become_method: sudo
  connection: local
  collections:
    - nginxinc.nginx_core
  roles:
    - role: nginx
Enter fullscreen mode Exit fullscreen mode

เรียกใช้งาน ansible playbook เพื่อติดตั้ง NGINX

$ ansible-playbook -i "localhost, " deploy-nginx.yml

PLAY [all] *************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************
ok: [localhost]

TASK [nginx : Check whether you are using a supported NGINX distribution] **********************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "Your OS, Ubuntu is supported by NGINX Open Source"
}
[...]
TASK [nginx : Debug NGINX output] **************************************************************************************************************************
skipping: [localhost]

TASK [nginx : Configure logrotate for NGINX] ***************************************************************************************************************
skipping: [localhost]

TASK [nginx : Install NGINX Amplify] ***********************************************************************************************************************
skipping: [localhost]

PLAY RECAP *************************************************************************************************************************************************
localhost                  : ok=14   changed=5    unreachable=0    failed=0    skipped=22   rescued=0    ignored=0
Enter fullscreen mode Exit fullscreen mode

ทดสอบการทำงานของ NGINX

$ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
มูลค่าความสุข

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay