Install the latest version of Nginx from GitHub on Ubuntu |

Install the latest version of Nginx from GitHub on Ubuntu

Posted on Sep 20, 2023

Installing software from source provides a level of control and flexibility that packaged versions might not offer. However, this also means you’ll have to manage dependencies and updates manually.

Here’s how you can install the latest version of NGINX from its GitHub source on an Ubuntu system:

1. Install Required Dependencies

First, update the package list and install the required packages to build NGINX from source:

sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev

2. Clone the NGINX Repository from GitHub

To get the latest version of NGINX from GitHub, you’d first clone the repository:

git clone https://github.com/nginx/nginx.git
cd nginx

3. Configure the Build

Next, you’ll need to configure the build. This step prepares the source code for the compilation. You can specify various modules and compile-time options with this command:

./auto/configure --prefix=/etc/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=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --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_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

Note: The above command is just an example. You might want to adjust it depending on which modules and configurations you want to include or exclude.

4. Compile and Install NGINX

Now you can compile the source code and install it:

make
sudo make install

5. Create Systemd Service File (Optional)

To manage NGINX using systemctl, you can create a systemd service file:

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

Paste the following into the editor:

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=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 -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

Save and exit the editor.

Now, reload the systemd configuration:

sudo systemctl daemon-reload

6. Start and Enable NGINX

sudo mkdir -p /var/cache/nginx/client_temp
sudo systemctl start nginx
sudo systemctl enable nginx
sudo nginx -t

And that’s it! You should now have the latest version of NGINX installed from its GitHub source. Remember that when you install from source, you’re responsible for tracking updates and recompiling as necessary.

Here is fully automated script

#!/bin/bash

# Exit on any error
set -e

# Constants
NGINX_GIT_REPO="https://github.com/nginx/nginx.git"
NGINX_DIR="/tmp/nginx"
SYSTEMD_SERVICE_FILE="/etc/systemd/system/nginx.service"

# Step 1: Install Required Dependencies
sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev git

# Step 2: Clone NGINX Repo
git clone $NGINX_GIT_REPO $NGINX_DIR
cd $NGINX_DIR

# Step 3: Configure the Build
./auto/configure \
  --prefix=/etc/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=/var/run/nginx.pid \
  --lock-path=/var/run/nginx.lock \
  --http-client-body-temp-path=/var/cache/nginx/client_temp \
  --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
  --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
  --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
  --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
  --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_secure_link_module \
  --with-http_slice_module \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-http_sub_module \
  --with-http_v2_module \
  --with-mail \
  --with-mail_ssl_module \
  --with-stream \
  --with-stream_realip_module \
  --with-stream_ssl_module \
  --with-stream_ssl_preread_module

# Step 4: Compile and Install
make
sudo make install

# Step 5: Create Systemd Service File (Optional)
echo "[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=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 -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID

[Install]
WantedBy=multi-user.target
" | sudo tee $SYSTEMD_SERVICE_FILE

# Step 6: Enable and Start Service
sudo mkdir -p /var/cache/nginx/client_temp
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
sudo nginx -t

echo "NGINX installed and running!"