本文主要講各主要linux發行版安裝nginx的方法,以及通用的編譯安裝方法。
關於nginx的配置,將放在以後的文章中。
文章參考nginx官方網站安裝教程,在翻譯的基礎上,添加個人的總結,以及一些常見問題的處理。
一、使用包管理器安裝
RedHat,CentOS發行版,yum包管理器安裝nginx
1 新建文件 /etc/yum.repos.d/nginx.repo,並寫入以下內容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
2 根據你使用的操作系統情況,將內容中的“OS” 替換為 “rhel” or “centos”,根據使用的系統版本,將 “OSRELEASE” 替換為 “6” or “7”。
Debian/Ubuntu發行版,apt包管理器安裝nginx
1 下載nginx軟件倉庫的密鑰文件
2 點此下載密鑰文件
3 將密鑰添加到系統中
sudo apt-key add nginx_signing.key
4 將倉庫信息寫入到/etc/apt/sources.lish文件
deb http://nginx.org/packages/OS/CODENAME nginx
deb-src http://nginx.org/packages/OS/ CODENAME nginx
根據使用的發行版信息,將OS修改為ubuntu或者debian
5 查詢文件下面的附錄一,將命令中的CODENAME,替換為你使用的系統版本的codename。
6 安裝nginx
sudo apt update
sudo apt install nginx
二、編譯安裝nginx
- 下載nginx源代碼
- 進入下面的地址,選擇自己想要安裝的版本即可,服務器請安裝stable版本。
- 下載地址nginx官方網站
1 解壓歸檔文件
- 在下載文件的保存位置,右鍵,選擇“在終端打開”,運行
tar -xvf nginx-1.15.5.tar.gz
解壓歸檔文件
2 切換到解壓出來的文件夾中
cd ./nginx-1.15.5
- configure
- configure的作用主要是檢查當前環境是否滿足安裝條件,並對即將安裝的軟件進行配置。
- 這裡可以直接運行
./configure
但是,我們可能不只需要nginx的基礎功能,或者需要對安裝進行一些配置。這時,我們需要添加一些編譯參數。具體的編譯參數可以通過:
./configure --help
來查看,具體各配置項目的意義,請查看附錄二。
一般可以這樣配置:
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
3 configure過程中可能會遇到的一些問題請查看附錄三。
成功示例圖
- 安裝軟件
make && make install
只要上一步的configure通過,這裡一般不會有問題。
- 運行nginx
/usr/local/nginx/sbin/nginx
運行成功無返回,如果運行失敗報錯,請查看文章底部。
三、附錄
附錄一 nginx二進制包支持的發行版
- RHEL和CentOS
- Version Supported Platforms
- 6.x x86_64, i386
- 7.4+ x86_64, ppc64le
- Debian
- Version Codename Supported Platforms
- 8.x jessie x86_64, i386
- 9.x stretch x86_64, i386
- Ubuntu
- Version Codename Supported Platforms
- 14.04 trusty x86_64, i386, aarch64/arm64
- 16.04 xenial x86_64, i386, ppc64el, aarch64/arm64
- 17.10 artful x86_64, i386
- 18.04 bionic x86_64
附錄二 nginx編譯參數含義
--help 顯示幫助信息
--prefix=PATH 設置安裝路徑
--sbin-path=PATH 啟動服務可執行文件路徑
--modules-path=PATH 模塊路徑
--conf-path=PATH 設置配置文件的路徑
--error-log-path=PATH 錯誤日誌的路徑
--pid-path=PATH 進程號文件保存路徑
--lock-path=PATH 鎖文件保存路徑
--user=USER 設置用來啟動nginx進程的用戶名
--group=GROUP 設置用來啟動nginx進程的用戶所屬的用戶組
--build=NAME 設置構建名稱
--builddir=DIR 設置構建目錄
--with-select_module 安裝指定模塊
--without-select_module 禁用指定模塊
--with-poll_module 安裝連接池模塊
--without-poll_module禁用連接池模塊
--with-threads 安裝線程池模塊
--with-file-aio 安裝AIO模塊,允許nginx異步操作文件
--with-http_ssl_module 安裝ssl模塊
--with-http_v2_module 安裝http v2模塊
--with-http_realip_module 將客戶端地址更改為在指定的頭字段中發送的地址
--with-http_addition_module 附加模塊
--with-http_xslt_module
--with-http_xslt_module=dynamic 可以構建使用一個或多個XSLT樣式錶轉換XML響應
--with-http_image_filter_module
--with-http_image_filter_module=dynamic 用於轉換JPEG,GIF,PNG和WebP的圖像的格式
--with-http_geoip_module
--with-http_geoip_module=dynamic 創建基於與MaxMind GeoIP二進制文件相配的客戶端IP地址的ngx_http_geoip_module變量
--with-http_sub_module 允許用一些其他文本替換nginx響應中的一些文本
--with-http_dav_module 增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法
--with-http_flv_module 尋求內存使用基於時間的偏移量文件
--with-http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module 隨機選擇該目錄下的一個文件顯示出來
--with-http_secure_link_module 防盜鏈模塊
--with-http_degradation_module 在內存不足的情況下返回204或444碼
--with-http_slice_module CDN系統中,向父層回源時,如果文件過大,使用slice可以讓用戶快速得到響應
--with-http_stub_status_module 查看Nginx的一些狀態信息
--without-http_charset_module 取消安裝charset模塊,該模塊添加特定字符集設置到客戶端相應的 “Content-Type”域中
--without-http_gzip_module 禁用gzip模塊,該模塊用來減輕服務器的帶寬問題
--without-http_ssi_module 禁用ssi模塊,該模塊可以使靜態網頁實現像動態網頁一樣 include 的功能
--without-http_userid_module 禁用userid模塊,該模塊可以標示客戶端設置合適的cookies
--without-http_access_module 禁用access模塊,該模塊可以實現基於IP的訪問控制
--without-http_auth_basic_module 禁用auth_basic模塊,該模塊可以實現基礎http認證
--without-http_mirror_module 把真實請求引流到測試環境
--without-http_autoindex_module 禁用autoindex模塊,該模塊允許列出目錄內容
--without-http_geo_module 禁用geo模塊,該模塊可以實現限速白名單的作用
--without-http_map_module 禁用模塊,該模塊可以創建變量,這些變量的值與另外的變量值相關聯
--without-http_split_clients_module 禁用split_clients模塊,該模塊可以實現負載均衡
--without-http_referer_module 禁用referer模塊,該模塊可根據header中的referer信息屏蔽某些請求對網站或應用的訪問
--without-http_rewrite_module 禁用rewrite模塊,該模塊可以實現url重寫
--without-http_proxy_module 禁用proxy模塊,該模塊可以實現代理
--without-http_fastcgi_module 禁用fastcgi模塊,該模塊允許將請求傳遞給另一臺服務器。
--without-http_uwsgi_module 禁用uwsgi模塊,該模塊允許將請求傳遞到uwsgi服務器
--without-http_scgi_module 禁用scgi模塊,該模塊允許nginx同Scgi協同工作
--without-http_grpc_module 禁用grpc模塊,該模塊可以代理gRPC TCP連接
--without-http_memcached_module 禁用memcached模塊,該模塊允許nginx直接操作memcached緩存
--without-http_limit_conn_module 禁用limit_conn模塊,該模塊用來限制連接數
--without-http_limit_req_module 禁用limit_req模塊,該模塊用來限制請求數
--without-http_empty_gif_module 禁用empty_gif模塊,該模塊可以返回一個保存在內存中的透明像素的GIF圖片
--without-http_browser_module 禁用browser模塊,該模塊創建變量,它們的值取決於 請求頭中“User-Agent”的值
--without-http_upstream_hash_module 禁用upstream_hash模塊,該模塊可以用來定義後端負載集群
--without-http_upstream_ip_hash_module 禁用upstream_ip_hash模塊,該模塊可以根據請求所屬的客戶端IP計算得到一個數值,然後把請求發往該數值對應的後端
--without-http_upstream_least_conn_module 禁用upstream_least_conn
--without-http_upstream_random_module 禁用upstream_random模塊
--without-http_upstream_keepalive_module 禁用upstream_keepalive模塊
--without-http_upstream_zone_module 禁用upstream_zone模塊
--with-http_perl_module 開啟perl模塊
--with-http_perl_module=dynamic 允許動態perl模塊
--with-perl_modules_path=PATH 指定perl模塊目錄位置
--with-perl=PATH 指定perl可執行文件位置
--http-log-path=PATH 指定日誌文件路徑
--http-client-body-temp-path=PATH 指定客戶端請求體臨時文件路徑
--http-proxy-temp-path=PATH 指定代理臨時文件路徑
--http-fastcgi-temp-path=PATH 指定fastcgi臨時文件路徑
--http-uwsgi-temp-path=PATH 指定uwsgi臨時文件路徑
--http-scgi-temp-path=PATH 指定http_scgi臨時文件路徑
--without-http 禁用http服務
--without-http-cache 禁用http緩存
郵件模塊
--with-mail 開啟POP3/IMAP4/SMTP模塊
--with-mail=dynamic 開啟動態POP3/IMAP4/SMTP模塊
--with-mail_ssl_module 開始加密mail模塊
--without-mail_pop3_module 禁用pop3模塊
--without-mail_imap_module 禁用imap模塊
--without-mail_smtp_module 禁用smtp模塊
stream模塊
--with-stream 開啟TCP/UDP策略模塊
--with-stream=dynamic 開啟動態TCP/UDP策略
--with-stream_ssl_module 為代理流服務器提供必要的SSL/TLS協議支持
以下為負載均衡各種算法的支持, 作用與http基本相同
--with-stream_realip_module
--with-stream_geoip_module
--with-stream_geoip_module=dynamic
--with-stream_ssl_preread_module
--without-stream_limit_conn_module
--without-stream_access_module
--without-stream_geo_module
--without-stream_map_module
--without-stream_split_clients_module
--without-stream_return_module
--without-stream_upstream_hash_module
--without-stream_upstream_least_conn_module
--without-stream_upstream_random_module
--without-stream_upstream_zone_module
--with-google_perftools_module 開啟google_perftools模塊,可以優化Nginx和MySQL的內存管
--with-cpp_test_module 添加cpp測試模塊
--add-module=PATH 允許擴展模塊
--add-dynamic-module=PATH 允許加載動態模塊的路徑
--with-compat 動態加載模塊
--with-cc=PATH 指定C編譯器的備用位置
--with-cpp=PATH 指定C預處理器的備用位置
--with-cc-opt=OPTIONS 要傳遞到C編譯器命令行的其他選項
--with-ld-opt=OPTIONS 要傳遞到C鏈接器命令行的其他選項
--with-cpu-opt=CPU 指定cpu體系結構,有效值:pentium, pentiumpro, pentium3, pentium4,athlon, opteron, sparc32, sparc64, ppc64
--without-pcre 禁用pcre庫
--with-pcre 強制使用pcre庫
--with-pcre=DIR pcre源文件路徑
--with-pcre-opt=OPTIONS 設置pcre安裝配置
--with-pcre-jit 構建帶有JIT編譯器的PCRE
--with-zlib=DIR 制定使用的zlib庫路徑
--with-zlib-opt=OPTIONS 為zlib安裝制定附加配置
--with-zlib-asm=CPU 為zlib庫指定彙編器源,有效的值:pentium, pentiumpro
--with-libatomic 使用系統默認libatomic庫
--with-libatomic=DIR 制定使用的libatomic庫路徑
--with-openssl=DIR 指定使用的openssl庫路徑
--with-openssl-opt=OPTIONS 設置openssl構建選項
--with-debug 開啟debug日誌
附錄三 nginx編譯過程中可能遇到的問題及解決方案
- ./configure: error: C compiler cc is not found.
- 這是沒有安裝c的編譯器,執行
yum install gcc
安裝gcc,中間交互一路y即可。
- ./configure: error: the HTTP rewrite module requires the PCRE library.
- You can either disable the module by using --without-http_rewrite_module
- option, or install the PCRE library into the system, or build the PCRE library
- statically from the source with nginx by using --with-pcre=
option. - 缺少pcre庫,執行
# 64位
yum install pcre-devel.x86_64
# 32位
yum install pcre-devel.i686
來安裝pcre庫
- ./configure: error: SSL modules require the OpenSSL library.
- You can either do not enable the modules, or install the OpenSSL library
- into the system, or build the OpenSSL library statically from the source
- with nginx by using --with-openssl=
option. - 缺少openssl庫,執行
# 64位
yum install openssl.x86_64
# 32位
yum install openssl.i686
- nginx: [emerg] getpwnam("nginx") failed
- 你指定了運行用戶,但是還沒有在系統中創建該用戶,執行
useradd nginx
- nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)
- 未創建該目錄,執行
mkdir -p /var/tmp/nginx/client/
如果大家還遇到了其他的問題,請在文章後面留言,持續更新,以幫助大家解決安裝上的問題。
閱讀更多 ian有話說 的文章