Ubuntu 下安裝 Nginx
這一節(jié)學習在 Ubuntu 上搭建 Nginx 服務。本次實驗環(huán)境為 Ubuntu 18.04, Nginx 版本為 1.17.6(截止到 2019 年 12 月 12 日最新版本)。
1. 下載源碼包并解壓
打開終端并運行下面兩條命令即可下載并解壓 Nginx :
$ wget http://Nginx.org/download/nginx-1.17.6.tar.gz
$ tar -xzf Nginx-1.17.6.tar.gz
2. 預裝依賴包
Nginx 是完全用 c 語言編寫的,所以想要編譯 Nginx,必須要有 c 編譯器(gcc), 只要系統(tǒng)里有 gcc, Nginx 就可以編譯安裝。
但是往往我們會用的到 Nginx 的一些重要功能,比如壓縮和解壓縮功能,這時就必須需要依賴 zlib 庫,想在配置文件中使用正則表達式,就必須安裝 pcre 庫,最后想實現(xiàn) SSL/TLS 功能,必須安裝 openssl 庫。
無論是在 Ubuntu 還是 CentOS 系統(tǒng)中都大致如此,只不過管理軟件包的工具不一樣,依賴庫的名稱也不一樣。在 Ubuntu 系統(tǒng)下,在 Ubuntu 中執(zhí)行如下命令安裝依賴庫:
$ sudo apt-get update # 更新下apt源
$ sudo apt-get install gcc # Nginx必備
$ sudo apt-get install make # 編譯安裝需要make工具
$ sudo apt-get install libz-dev
$ sudo apt-get install libpcre3-dev
$ sudo apt-get install libssl-dev
3. 編譯并安裝
傻瓜式,使用默認安裝配置,以及默認安裝模塊。這樣 Nginx 將會安裝到默認的 /usr/local/nginx 目錄,可執(zhí)行文件是 /usr/local/nginx/sbin/nginx,默認的配置文件是 /usr/local/nginx/conf/nginx.conf。
$ cd nginx-1.17.6
$ ./configure # 編譯,檢查依賴是否正確
$ make && sudo make install # 安裝
如果想自定義比如安裝目錄、編譯新的模塊或者第三方模塊,需要使用相應的參數(shù)。
# 參數(shù)說明:
--prefix=PATH: #指定安裝目錄
--with-xxx_module: #添加某某模塊, xxx為對應的模塊名稱,比如--with-http_ssl_module等,這是針對沒有默認編譯進Nginx的模塊
--without-xxx_module: #禁止某模塊編譯進 Nginx , xxx 為對應模塊名稱,比如--without-stream_access_module 等,這是針對默認會編譯進 Nginx 的模塊
--add-module=PATH: #添加第三方 Nginx 模塊,指定模塊路徑即可,非常簡單
# 對于查看具體支持哪些參數(shù)選項,包括所有可以加入和禁止編譯進Nginx的模塊,
# 通過如下命令查詢:
$ ./configure --help
# 我們?yōu)楹罄m(xù)開展測試,多添加幾個模塊進Nginx,讓編譯出來的Nginx具備更多功能
$ ./configure --prefix=/root/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-stream \
--with-http_realip_module \
--with-http_flv_module \
--with-http_random_index_module \
--with-mail \
--with-pcre \
# 安裝
$ make && sudo make install
4. 啟動 Nginx 服務
安裝完成后,Nginx 的文件默認全在 --prefix 指定的目錄中,即 /root/nginx。編譯出的 Nginx 二進制文件在 /root/nginx/sbin 目錄下,默認的配置文件為 /root/nginx/conf/nginx.conf。我們可以直接啟動 Nginx:
$ cd /root/nginx/sbin
$ ./nginx
$ curl http://localhost # 測試 Nginx 服務是否啟動成功
如果最后一步測試,發(fā)現(xiàn)返回 403(權限拒絕)的結果,我們可以修改下 nginx.conf 的配置,將 nginx.conf 中第一行 user 指令參數(shù)設置為 root,然后在重啟或者熱加載 Nginx 并執(zhí)行 curl 請求,查看結果。
$ cd /root/nginx/conf
$ vim nginx.conf
$ cat nginx,conf
# 指定nginx以root用戶啟動
user root;
...
# 這次就能正確返回'Welcome to nginx'這樣的信息了
$ curl http://localhost
另外從瀏覽器上直接請求,也可以看到歡迎頁面。
Tips:對于百度、阿里、騰訊這樣的云主機,需要事先放通 80 端口,允許外面通過 80 端口訪問服務,以及數(shù)據(jù)從 80 端口出去。
5. Nginx 服務的操作
$ cd /root/nginx/sbin
# 查看版本信息
$ ./nginx -v
# 查看詳情,可以看到編譯進Nginx中的模塊
$ ./nginx -V
# stop|quit: 停止服務,區(qū)別和windows中的一致 reload:熱加載 reopen:重新打開日志文件
$ ./nginx -s stop|reload|reopen
# -c 指定配置文件,默認就是Nginx安裝目錄下的conf/nginx.conf文件
$ ./nginx -c /root/nginx/conf/nginx.conf
# -t 測試配置文件語法是否正常
$ ./nginx -tc /root/nginx/conf/nginx.conf
來看看在服務器上執(zhí)行 Nginx 命令的結果:
$ cd /root/nginx/sbin
$ ./nginx -t
nginx: the configuration file /root/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /root/nginx/conf/nginx.conf test is successful
$ ./nginx -V
nginx version: nginx/1.17.6
built by gcc 7.4.0 (Ubuntu 7.4.0-lubuntu1~18.04.1)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/root/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-http_realip_module --with-http_flv_module --with-http_random_index_module --with-mail --with-pcre
6. 安裝操作視頻演示
7. 小結
這里,我們講解了在 Ubuntu 系統(tǒng)上搭建 Nginx 服務的完整過程。相比其他互聯(lián)網(wǎng)組件而言,搭建 Nginx 的過程是非常順暢的,這極大地方便了學習 Nginx 的用戶,不用再被部署服務搞得焦頭爛額。此外,這里還簡單介紹了幾個 Nginx 命令行參數(shù),這些會在后面經(jīng)常用到。