Ubuntu 18.04 Install Nginx - IT 484

Ubuntu 18.04 Install Nginx – IT 484

  • Post author:
  • Post category:IT / Nginx
  • Post comments:0 Comments
  • Post last modified:2023-01-13

相較於其它的 Web Server ,安裝非常簡單,可以大幅地節省工程師開發與部署的時間,實作架設 Server,可以使用 apt-get 來達成,這是最快速的方法。

簡介

Nginx 是一個異步框架的網頁伺服器,用於處理併發請求,整體是採用模組化設計,組態檔案也非常簡潔容易設定,同時也提供了反向代理、負載平衡器、HTTP 快取等功能,相較於 Apache、lighttpd 具有占有內存少,穩定性高等優勢,能保持低資源低消耗高性能,在網際網路項目中廣泛應用。

安裝套件

Ubuntu 18.04 Install Nginx

建立套件庫資訊。

cd /tmp/ && wget http://nginx.org/keys/nginx_signing.key
sh -c "echo 'deb http://nginx.org/packages/mainline/ubuntu/ '$(lsb_release -cs)' nginx' > /etc/apt/sources.list.d/Nginx.list"
apt-get update

建立套件庫後,使用 apt-get 命令安裝。

apt-get install nginx

組態設定

這是主要的組態檔,參考此檔案的內容,視需求而調整設定。

worker_processes # 執行緒數量
events.worker_connections # 連線數
http.include # 引用其他組態檔

vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

運行管理

啟動服務

安裝完成後,並不會自動啟動服務,使用 systemctl start命令來啟動服務。

systemctl start nginx

開機啟動

實現主機重開時,可以自動啟動服務,使用 systemctl enable 命令來啟用開機啟動。

systemctl enable nginx

停止服務

啟動服務後,使用 systemctl stop 命令終止服務運行。

systemctl stop nginx

狀態驗證

Install Nginx Ubuntu 啟動服務後,使用 systemctl status 命令來啟查看服務狀況。

systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor prese                                                              t: disabled)
   Active: active (running) since Sun 2022-02-06 21:06:03 CST; 3 weeks 3 days ag                                                              o
     Docs: http://nginx.org/en/docs/
 Main PID: 685 (nginx)
    Tasks: 2
   Memory: 1.4M
   CGroup: /system.slice/nginx.service
           ├─ 685 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.c...
           └─7385 nginx: worker process

心得分享

Ubuntu Install Nginx 快速安裝後,便於個人電腦、筆電在開發研究或測試時使用,省略複雜的設定,所以沒有參數需要調整,簡單快速建置就能使用服務,除此之外還有其他平台:

發佈留言