1. 程式人生 > 其它 >Nginx 編譯安裝

Nginx 編譯安裝

本文介紹了手動編譯安裝 nginx 的具體步驟。

安裝依賴包

RedHat 系

$ yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel

Debian 系

$ sudo apt install libpcre3 libpcre3-dev libssl-dev zlib1g-dev zlib1g

編譯

$ ./configure --prefix=/etc/nginx  
      --sbin-path=/etc/nginx/sbin/nginx 
      --conf-path=/etc/nginx/nginx.conf 
      --user=nginx 
      --group=nginx 
      --with-http_ssl_module 
      --with-http_realip_module 
      --with-http_addition_module 
      --with-http_sub_module 
      --with-http_dav_module 
      --with-http_flv_module 
      --with-http_mp4_module 
      --with-http_gunzip_module 
      --with-http_gzip_static_module 
      --with-http_random_index_module 
      --with-http_secure_link_module 
      --with-http_stub_status_module 
      --with-http_auth_request_module 
      --with-threads 
      --with-stream 
      --with-stream_ssl_module 
      --with-http_slice_module 
      --with-mail 
      --with-mail_ssl_module 
      --with-file-aio 
      --with-http_v2_module

錯誤排查

錯誤1

checking for OS Linux 3.10.0-327.36.1.el7.x86_64 x86_64 checking for C compiler … not found ./configure: error: C compiler cc is not found

$ yum install -y gcc gcc-c++

錯誤2

./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using –without-http_rewrite_moduleoption, or install the PCRE library into the system, or build the PCRE librarystatically from the source with nginx by using –with-pcre=option.

$ yum install pcre-devel
$ sudo apt install libpcre3 libpcre3-dev

錯誤3

./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL libraryinto the system, or build the OpenSSL library statically from the sourcewith nginx by using –with-openssl=option.

$ yum install openssl-devel
$ sudo apt install libssl-dev

錯誤4

./configure: error: the HTTP gzip module requires the zlib library.

$ yum install zlib-devel
$ sudo apt install zlib1g-dev zlib1g

安裝

$ make

$ groupadd -r nginx 
  && useradd -r -g nginx -s /bin/false -M nginx

$ sudo make install

加入環境變數

編輯 ~/.bash_profile

export PATH=/etc/nginx/sbin:$PATH

編輯 /etc/sudoers

注意:必須編輯此檔案,否則 sudo 會找不到命令。

Defaults	secure_path="/etc/nginx/sbin:..."

systemd

/etc/systemd/system/ 下增加 nginx.service 檔案,以下路徑根據實際自己修改。

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/etc/nginx/run/nginx.pid
ExecStartPre=/etc/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/etc/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

啟動

$ sudo nginx -t

$ sudo nginx

相關連結