天天看點

nginx的兩種安裝方式

我們知道nginx是非常火熱的一塊搭建web服務的軟體,它具有兩種不同的安裝方式。一種是軟體包管理工具直接安裝,一種是下載下傳源碼包進行編譯安裝。各有各的優缺點,下面就詳細介紹兩種不同安裝方式

安裝nginx

安裝nginx主要有兩種方式,一種是yum安裝,一種是源碼包安裝

  • yum安裝簡單,使用友善
  • 可以直接使用nginx指令等
  • 但檔案不在同一位置,解除安裝時不是特别友善
  • 源碼包安裝在同一位置,版本可以自己選擇
  • 容易解除安裝,并且可以自己指定使用第三方子產品,可以根據自己的需求標明子產品
  • 在自己的系統上進行編譯出來的軟體運作效率更高

yum安裝

# 1.下載下傳阿裡源的EPEL源倉庫
[[email protected] ~]# wget ‐O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel‐7.repo
# 2.安裝nginx
[[email protected] ~]# yum install nginx ‐y
[[email protected] ~]# nginx ‐v
nginx version: nginx/1.12.2

           
  • 配置檔案
/usr/sbin/nginx # 軟體目錄
/usr/share/nginx/html/index.html #網站預設目錄
/etc/nginx/nginx.conf # 主配置檔案
/var/log/nginx # 日志目錄
           
  • nginx.conf主配置檔案
[[email protected] html]# cat /etc/nginx/nginx.conf

user nginx; # 運作使用者
worker_processes auto; # 啟動程序數,通常和cpu核數一樣
error_log /var/log/nginx/error.log; # 錯誤日志
pid /run/nginx.pid; PID檔案

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024; # 單個程序的最大連接配接數
}
# http全局配置
http {
    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; # 允許sendfile方式傳輸檔案
    tcp_nopush          on; # 在sendfile方式啟動下,使用TCP_CORK套接字
    tcp_nodelay         on; # 連接配接保持活動狀态
    keepalive_timeout   65; # 連接配接逾時時間,預設為75s
    types_hash_max_size 2048; # 設定類型哈希表的最大大小

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _; # 域名
        root         /usr/share/nginx/html; # 網站根目錄

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html; # 錯誤頁
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

[[email protected] html]# 

           

編譯安裝nginx

  • 首先是下載下傳安裝包并解壓,源碼包可以去nginx.org官網下載下傳
[[email protected] nginx]# tar -xzf nginx-1.16.1.tar.gz 
[[email protected] nginx]# ll
總用量 1012
drwxr-xr-x. 8 1001 1001     158 8月  13 20:51 nginx-1.16.1
-rw-r--r--. 1 root root 1032630 8月  14 01:01 nginx-1.16.1.tar.gz
[[email protected] nginx]# cd nginx-1.16.1/
[[email protected] nginx-1.16.1]# ll
總用量 752
drwxr-xr-x. 6 1001 1001   4096 9月   4 15:04 auto
-rw-r--r--. 1 1001 1001 296463 8月  13 20:51 CHANGES
-rw-r--r--. 1 1001 1001 452171 8月  13 20:51 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 9月   4 15:04 conf
-rwxr-xr-x. 1 1001 1001   2502 8月  13 20:51 configure
drwxr-xr-x. 4 1001 1001     72 9月   4 15:04 contrib
drwxr-xr-x. 2 1001 1001     40 9月   4 15:04 html
-rw-r--r--. 1 1001 1001   1397 8月  13 20:51 LICENSE
drwxr-xr-x. 2 1001 1001     21 9月   4 15:04 man
-rw-r--r--. 1 1001 1001     49 8月  13 20:51 README
drwxr-xr-x. 9 1001 1001     91 9月   4 15:04 src
[[email protected] nginx-1.16.1]# 
           
  • nginx源碼包目錄解析
    • CHANGES 就是每個版本中提供了哪些功能以及bug的修複
    • CHANGES 因為作者是俄羅斯人,是以有一個俄羅斯語言的檔案版本
    • auto目錄中存放了大量腳本檔案,和configure腳本程式有關。
    • conf目錄中存放的是Nginx的配置檔案,包含基本配置檔案和對部分特性的配置檔案。
    • configure檔案是Nginx的自動腳本程式。
    • contrib 提供了兩個破腳本和vim工具
    • html 目錄存放了兩個字尾名為.html的靜态網頁檔案。
    • man 目錄中Nginx中存放的是所有幫助文檔,在安裝完成後用man指令來檢視幫助文檔,如man nginx
    • src 目錄中存放的是nginx軟體的所有源代碼
  • nginx.conf代碼高亮顯示
mkdir ~/.vim #建立.vim目錄(如果沒有的話)
cp -r contrib/vim/*  ~/.vim #在nginx目錄下執行
           
  • 編譯之前安裝依賴軟體gcc,然後編譯
[[email protected] nginx-1.16.1]# yum -y install gcc gcc-c++ autoconf automake make
# 編譯
[[email protected] nginx-1.16.1]# ./configure --prefix=/home/nginx/nginx/ --without-http_rewrite_module --without-http_gzip_module
!
!
Configuration summary
  + PCRE library is not used
  + OpenSSL library is not used
  + zlib library is not used

  nginx path prefix: "/home/nginx/nginx/"
  nginx binary file: "/home/nginx/nginx//sbin/nginx"
  nginx modules path: "/home/nginx/nginx//modules"
  nginx configuration prefix: "/home/nginx/nginx//conf"
  nginx configuration file: "/home/nginx/nginx//conf/nginx.conf"
  nginx pid file: "/home/nginx/nginx//logs/nginx.pid"
  nginx error log file: "/home/nginx/nginx//logs/error.log"
  nginx http access log file: "/home/nginx/nginx//logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[[email protected] nginx-1.16.1]# 

           

接下來便可以使用make 指令來根據 Makefile 檔案内容提供的合适規則編譯生成出真正可供使用者安裝服務程式的二進制可執行檔案了。

[email protected] frp 0.21.0]# make

運作二進制的服務程式安裝包

[[email protected] frp 0.21.0]# make install

清理源碼包臨時 檔案

[[email protected] frp 0.21.0]# make clean

最後防火牆記得放行80端口,運作./sbin/nginx 就可以開啟服務了。如果你在自己編譯安裝時出現錯誤,記得看報錯的提示。

繼續閱讀