CentOS一鍵編譯安裝nginx,帶http2/brotli/zlib/pcre/ssl,非root監聽80端口

系統環境與依賴:

腳本示範的是CentOS 7.6。Ubuntu 18也是可以的,把腳本里的yum改成apt即可。
各依賴庫的版本請看url裏的數字。有升級的話,改一下版本數字即可繼續用這個腳本。

腳本

說明在註釋裏:

# 這一步需要root用戶來執行
# 需要git來從GitHub上clone brotli。gcc-c++是pcre需要的,不然configure會報錯。
yum install -y wget git gcc-c++

# 以下的步驟不需要root權限
# pcre是支持location的正則表達式的。nginx官網文檔說了只支持pcre,不支持pcre2
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar zxvf pcre-8.43.tar.gz

wget http://zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz

wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar zxvf nginx-1.16.0.tar.gz

wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar zxvf openssl-1.0.2s.tar.gz

# brotli是google發明的數據壓縮算法。它有依賴子庫
git clone --depth=1 https://github.com/google/ngx_brotli.git
cd ngx_brotli && git submodule update --init && cd ..

cd nginx-1.16.0
# prefix是把nginx安裝到哪個目錄
./configure \
    --prefix=../nginx \
    --with-pcre=../pcre-8.43 \
    --with-zlib=../zlib-1.2.11 \
    --with-openssl=../openssl-1.0.2s \
    --add-module=../ngx_brotli \
    --user=root \
    --group=root \
    --with-file-aio \
    --with-http_v2_module \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_sub_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module
make install

運行$/nginx/sbin/nginx即可啓動了。實際啓用gzip、http2、brotli、ssl還需修改nginx.conf哦。簡單示例:

http {
  ...
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/javascript application/json application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  gzip_vary off;

  brotli on;
  brotli_types text/plain application/javascript application/json application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

  server {
    ...
    listen 443 ssl http2;

    ssl_certificate /home/fm/local/nginx/ssl/server.crt;
    ssl_certificate_key /home/fm/local/nginx/ssl/server.key;
    ssl_session_timeout  5m;
    ...
  }
  ...
}

非root用戶監聽80和443端口

非root用戶運行nginx不能監聽1024以下的端口號。如果不想用root賬號來啓動,可以先這樣設置:

# 需要root用戶cd到sbin目錄中設權限
chown root:root nginx
chmod 755 nginx
chmod u+s nginx

然後非root用戶也能啓動nginx並監聽80端口了。

參考文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章