NGINX [Http Server]


Posted by chenchih on 2025-09-07

用nginx 來架設HTTP server

  • OS: ubuntu 20.04

Ubuntu 安裝 NGINX

1. NGINX 基本設定

Step 1. 安裝 Nginx

sudo apt install nginx -y

Step 2. 檢查狀態

sudo systemctl status nginx

Step 3. 防火牆設定

sudo ufw allow 'Nginx HTTP'
sudo ufw reload

Step 4.測試看看,開啟瀏覽器

http://172.21.201.250
or 
http://127.0.0.1

Access Website default

Http 網頁改成檔案總管

  • 權限設定
    sudo chmod 644 /var/www/html/
    
  • ngnix 設定 加 autoindex on

    sudo nano /etc/nginx/sites-available/default


    location / {
        try_files $uri $uri/ =404;
        autoindex on; # <--- Add this line
    }
  • 測試config 有沒有問題 sudo nginx -t
  • reload and apply
    sudo systemctl reload nginx
    
  • .html網頁檔改掉其他檔名
sudo mv /var/www/html/index.nginx-debian.html /var/www/html/index.nginx-debian.html.bak
  • 開啟瀏覽器
    Website show file management

Docker 架 NGINX

安裝 Docker

  • 安裝和設定 docker

    sudo apt update
    sudo apt install docker.io -y
    sudo systemctl start docker
    sudo systemctl enable docker
    sudo usermod -aG docker chenchih # Add your user to the docker group
    # Remember to log out and log back in (or reboot) for the group change to take effect
    
  • 測試docker 沒問題

    docker run hello-world
    
  • 拉下ngnix image
    #Pull or Run Nginx in Docker
    docker pull nginx:latest
    
  • 跑 ngnix container
    docker run --name my-nginx -p 8080:80 -d nginx:latest
    

    Option設定:

    • --name my-nginx: Gives your container a memorable name.
    • -p 8080:80: Port mapping. Your Pi’s 8080 will point to the container’s 80.
    • -d: Detached mode (runs in the background).
    • nginx:latest: The image to use.

檢查Docker progress

  • docker有沒有跑

    docker ps -a # Lists all containers, including  stopped items
    

    process docker

  • 檢查Docker images 在本地

    docker images
    
  • 開啟瀏覽器
    開啟 http://<your_pi_ip_address>:8080 就可以看到如下:
    Access Website default

移除 Container

  • 停止和移除

    docker stop my-nginx # Stops the running container
    docker rm my-nginx # Removes the container instance
    

    可以再用docker ps -a 檢查
    remove docker

  • 移除image 在本地

    docker rmi nginx:latest
    

    remove image from local side


#ngnix #HTTP







Related Posts

[紀錄] Final Project 部署紀錄

[紀錄] Final Project 部署紀錄

使用 node.js 寫出串接 API 的程式

使用 node.js 寫出串接 API 的程式

Linux USB HID Device

Linux USB HID Device


Comments