在企業環境中,除了使用套件管理員安裝,有時也需要自行編譯 Nginx,以便指定模組、路徑與參數。以下以 Nginx 1.4.7 為例。
yum groupinstall -y "Development Tools" "Server Platform Development" yum install -y openssl-devel pcre-devel
為了安全,通常會讓 Nginx 以專用帳號運行:
groupadd -r nginx useradd -r -g nginx nginx
yum install wget wget https://nginx.org/download/nginx-1.4.7.tar.gz tar xf nginx-1.4.7.tar.gz cd nginx-1.4.7
./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/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_flv_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 make make install
Nginx 可搭配 tcmalloc 加速記憶體配置,但需要事先安裝 gperftools,並在編譯時加入對應模組。
若需要 Perl 模組,可加入 --with-http_perl_module,但此模組仍屬實驗性質,正式環境應謹慎評估。
在 Red Hat 系列中,可提供 SysV init 腳本,讓 Nginx 能透過 service 或 chkconfig 管理。完成後記得賦予執行權限:
chmod +x /etc/rc.d/init.d/nginx chkconfig --add nginx chkconfig nginx on
最後啟動服務並測試:
service nginx start