CentOS 7 Install Nginx - IT 484

CentOS 7 Install Nginx – IT 484

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

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

功能簡介

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

安裝套件

CentOS 7 Install Nginx

新建檔案,建立套件庫資訊。

vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

建立套件庫後,使用 yum 命令安裝。

yum 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 CentOS 啟動服務後,使用 systemctl status 命令來啟查看服務狀況。

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-02-06 21:06:03 CST; 3 weeks 3 days ago
 Main PID: 1193 (nginx)
    Tasks: 5
   Memory: 9.0M
   CGroup: /system.slice/nginx.service
           ├─1193 nginx: master process /usr/sbin/nginx
           ├─6273 nginx: worker process
           ├─6274 nginx: worker process
           ├─6275 nginx: worker process
           └─6276 nginx: worker process

心得分享

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

發佈留言