Motivation
- 想要自己編譯 Nginx,並且加入一些模組
- 新開了一個 Mail Server,想要使用 Nginx 作為 Mail Proxy Server
Evnironment
- Ubuntu 22.04
Procedure
-
安裝編譯 Nginx 所需的套件
1 2sudo apt update sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -
安裝 PCRE (依照自身需求修改版本號)
1 2 3 4 5 6wget github.com/PCRE2Project/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.gz tar -zxf pcre2-10.42.tar.gz cd pcre2-10.42 ./configure make sudo make install -
安裝 zlib (依照自身需求修改版本號)
1 2 3 4 5 6wget http://zlib.net/zlib-1.2.13.tar.gz tar -zxf zlib-1.2.13.tar.gz cd zlib-1.2.13 ./configure make sudo make install -
安裝 nginx (依照自身需求修改版本號)
1 2 3wget https://nginx.org/download/nginx-1.24.0.tar.gz tar zxf nginx-1.24.0.tar.gz cd nginx-1.24.0 -
設定模組 (以下指令必須為單行)
1 2 3 4 5 6 7 8 9 10./configure \ --with-pcre=../pcre2-10.42 \ --with-zlib=../zlib-1.3.1 \ --with-http_ssl_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_ssl_module \ --with-http_v2_module \ --with-http_v3_module -
編譯並安裝
1 2make sudo make install -
安裝完成的 Nginx 位於
/usr/local/nginx/,設定檔位於/usr/local/nginx/conf/nginx.conf -
Nginx 執行檔位於
/usr/local/nginx/sbin/nginx,啟動 Nginx1sudo /usr/local/nginx/sbin/nginx -
把 Nginx 執行檔加入系統路徑
1sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx -
設定 Nginx 服務
1sudo vim /etc/systemd/system/nginx.service -
nginx.service 內容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15[Unit] Description=Nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit ExecStartPost=/bin/sleep 0.1 PIDFile=/usr/local/nginx/logs/nginx.pid [Install] WantedBy=multi-user.target -
Reload systemd
1sudo systemctl daemon-reload -
啟動 Nginx
1sudo systemctl start nginx -
查看 Nginx 狀態
1sudo systemctl status nginx