Build and install Nginx 1.25.3 under Ubuntu 22.04.3 LTS

Make working folder

mkdir -p ~/nginx && cd ~/nginx

Build and install libmaxminddb, this is to show GeoIP

git clone --recursive https://github.com/maxmind/libmaxminddb
cd ~/libmaxminddb/
./bootstrap
./configure
make
make check
sudo make install
sudo ldconfig
cd ~/nginx

Install modules as required

cd ~/nginx
wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.bz2 && tar xvf pcre-8.45.tar.bz2
wget http://zlib.net/zlib-1.3.tar.xz && tar -xf zlib-1.3.tar.xz
wget http://www.openssl.org/source/openssl-3.0.7.tar.gz && tar -zxf openssl-3.0.7.tar.gz
git clone https://github.com/arut/nginx-dav-ext-module
git clone https://github.com/arut/nginx-rtmp-module
git clone https://github.com/vozlt/nginx-module-vts
git clone https://github.com/kaltura/nginx-vod-module
git clone https://github.com/aperezdc/ngx-fancyindex
git clone https://github.com/leev/ngx_http_geoip2_module.git
git clone https://github.com/kaltura/nginx-srt-module.git

Download Nginx

cd ~/nginx
wget https://nginx.org/download/nginx-1.25.3.tar.gz && tar zxf nginx-1.25.3.tar.gz
cd nginx-1.25.3

Build and install

sudo ./configure --prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=www-data \
--group=www-data \
--build=Ubuntu \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-openssl=../openssl-3.0.7 \
--with-openssl-opt=enable-ec_nistp_64_gcc_128 \
--with-openssl-opt=no-nextprotoneg \
--with-openssl-opt=no-weak-ssl-ciphers \
--with-openssl-opt=no-ssl3 \
--with-pcre=../pcre-8.45 \
--with-pcre-jit \
--with-zlib=../zlib-1.3 \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_secure_link_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_geoip_module \
--with-debug \
--with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' \
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' \
--add-module=../nginx-rtmp-module \
--add-module=../nginx-dav-ext-module \
--add-module=../ngx-fancyindex \
--add-module=../ngx_http_geoip2_module \
--add-module=../nginx-vod-module --with-file-aio \
--add-module=../nginx-srt-module \
--add-module=../nginx-module-vts --with-cc-opt="-Wimplicit-fallthrough=0" && sudo make -j$(nproc)
sudo make install

Configure Nginx as a service

sudo mkdir -p /var/lib/nginx

Create service file

sudo vim /etc/systemd/system/nginx.service

Copy follow into the file

[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

Start Nginx service and enable it so that it starts after system boots


sudo systemctl start nginx.service && sudo systemctl enable nginx.service

Check to see if service is running

sudo systemctl status nginx.service

Then you should be able to see http server is up by

curl -I 127.0.0.1

Next, you might want log rotates, create file “/etc/logrotate.d/nginx” and copy follow into it

sudo vim /etc/logrotate.d/nginx
"/var/log/nginx/error.log" /var/log/nginx/access.log {
rotate 4
weekly
size 1000k
dateext
dateformat -%Y-%m-%d
missingok
compress
sharedscripts
postrotate
test -r /var/run/nginx.pid && kill -USR1 cat /var/run/nginx.pid
endscript
}

execute this command to see if rotation works

sudo logrotate -v -f /etc/logrotate.d/nginx