Building nginx from Sources

Onur BOLATOĞLU
3 min readDec 16, 2022

--

Nginx derleyerek kurmak ve 3. parti modülleri yüklemek

sudo apt-get update && sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev
wget  http://nginx.org/download/nginx-1.22.1.tar.gz

tar -zxvf nginx-1.22.1.tar.gz

cd nginx-1.22.1

./configure --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module
  • Diğer yapılandırma seçeneklerine nginx dökümanlarından ulaşabilirsiniz;
make && make install
  • Yapılandırma tamamlandı. systemd dosyasını oluşturuyoruz.
nano /lib/systemd/system/nginx.service
  • Aşağıdaki yapılandırmayı yapıştırıyoruz.
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl restart nginx
systemctl enable nginx
systemctl status nginx
  • Nginx kurulumu tamamlandı.

Yeni Modül Ekleme

Eğer daha sonradan nginx ’e farklı bir modül eklemek istersek. Modülleri aşağıdaki linkten repolarına gidip, inceleyebiliriz.

  • İşlemlerimize geçelim. Bu örneğimizde headers-more-nginx-module modülünü kuracağız.

Not: Bu modül nginx’de headerlar konusunda geniş yapılandırma seçenekleri sunuyor. Biz bu örnekte, Server headerının değerini silip, (web sunucu teknolojisini ve nginx versiyonunu gösterir default değeri). bunun yerine kendimiz custom bir değer vereceğiz.

Server: nginx/1.22.1 (Ubuntu)

Server headerının default değeri yukarıdaki gibidir.

  • Modülümüzü github reposundan sunucumuza indiriyoruz;
git clone https://github.com/openresty/headers-more-nginx-module.git
cd nginx-1.22.1

./configure --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module --add-dynamic-module=/home/onur/headers-more-nginx-module

make && make install
  • Yukarıdaki komut ile nginx yeniden yapılandırıyoruz. Yeni ekleyeceğimiz modülün path bilgisini ./configure komutunun en sonuna ekliyoruz.
nginx -V

nginx version: nginx/1.22.1
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module --add-dynamic-module=/home/onur/headers-more-nginx-module
  • Yukarıdaki komut ile modülün kurulmuş olduğunu görebilirsiniz.
  • Modül dosyası /etc/nginx/modules dizinine kopyalanmamış olabilir. Bunun için bu dizini kontrol ediyoruz. Eğer modül dosyası mevcut değil ise, kopyalamamız gerekiyor.
sudo cp objs/ngx_http_headers_more_filter_module.so /etc/nginx/modules
  • Modülü Kullanmak için, nginx.conf dosyasına aşağıdaki satırları ekliyoruz.
load_module /etc/nginx/modules/ngx_http_headers_more_filter_module.so;

http {
server_tokens off;
more_set_headers 'Server: Onur'
  • Servisi yeniden başlatıyoruz.
systemctl restart nginx
  • Ardından curl sorgusu gönderiyoruz:
root@ubuntu-nginx:/home/onur/nginx-1.22.1# curl -I localhost
HTTP/1.1 200 OK
Date: Fri, 16 Dec 2022 11:59:51 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Dec 2022 11:17:49 GMT
Connection: keep-alive
ETag: "639c53dd-267"
Server: Onur
Accept-Ranges: byte

--

--