Friday, April 8, 2022

[SOLVED] how to install Nginx on CentOs7 without internet connection with root permission?

Issue

I need to install Nginx on my target which there is no internet connection, how can I install Nginx with all dependencies in an offline mode?? thanks in advance for your answers.


Solution

I have recently gone through this procedure and this is what worked for me on centos7:

You need an online Linux server to download dependencies. You can use virtual machines or anything else. On your online server create a .sh file and copy script below in it. (I named it download_dependencies)

#!/bin/bash
# This script is used to fetch external packages that are not available in standard Linux distribution

# Example: ./fetch-external-dependencies ubuntu18.04
# Script will create nms-dependencies-ubuntu18.04.tar.gz in local directory which can be copied
# into target machine and packages inside can be installed manually

set -eo pipefail

# current dir
PACKAGE_PATH="."

mkdir -p $PACKAGE_PATH

declare -A CLICKHOUSE_REPO
CLICKHOUSE_REPO['ubuntu18.04']="https://repo.clickhouse.tech/deb/lts/main"
CLICKHOUSE_REPO['ubuntu20.04']="https://repo.clickhouse.tech/deb/lts/main"
CLICKHOUSE_REPO['centos7']="https://repo.clickhouse.tech/rpm/lts/x86_64"
CLICKHOUSE_REPO['centos8']="https://repo.clickhouse.tech/rpm/lts/x86_64"
CLICKHOUSE_REPO['rhel7']="https://repo.clickhouse.tech/rpm/lts/x86_64"
CLICKHOUSE_REPO['rhel8']="https://repo.clickhouse.tech/rpm/lts/x86_64"

declare -A NGINX_REPO
NGINX_REPO['ubuntu18.04']="https://nginx.org/packages/mainline/ubuntu/pool/nginx/n/nginx/"
NGINX_REPO['ubuntu20.04']="https://nginx.org/packages/mainline/ubuntu/pool/nginx/n/nginx/"
NGINX_REPO['centos7']="https://nginx.org/packages/mainline/centos/7/x86_64/RPMS/"
NGINX_REPO['centos8']="https://nginx.org/packages/mainline/centos/8/x86_64/RPMS/"
NGINX_REPO['rhel7']="https://nginx.org/packages/mainline/rhel/7/x86_64/RPMS/"
NGINX_REPO['rhel8']="https://nginx.org/packages/mainline/rhel/8/x86_64/RPMS/"

CLICKHOUSE_KEY="https://repo.clickhouse.com/CLICKHOUSE-KEY.GPG"
NGINX_KEY="https://nginx.org/keys/nginx_signing.key"

declare -A CLICKHOUSE_PACKAGES
# for Clickhouse package names are static between distributions
# we use ubuntu/centos entries as placeholders
CLICKHOUSE_PACKAGES['ubuntu']="
clickhouse-server_21.3.10.1_all.deb
clickhouse-common-static_21.3.10.1_amd64.deb"

CLICKHOUSE_PACKAGES['centos']="
clickhouse-server-21.3.10.1-2.noarch.rpm
clickhouse-common-static-21.3.10.1-2.x86_64.rpm"

CLICKHOUSE_PACKAGES['ubuntu18.04']=${CLICKHOUSE_PACKAGES['ubuntu']}
CLICKHOUSE_PACKAGES['ubuntu20.04']=${CLICKHOUSE_PACKAGES['ubuntu']}
CLICKHOUSE_PACKAGES['centos7']=${CLICKHOUSE_PACKAGES['centos']}
CLICKHOUSE_PACKAGES['centos8']=${CLICKHOUSE_PACKAGES['centos']}
CLICKHOUSE_PACKAGES['rhel7']=${CLICKHOUSE_PACKAGES['centos']}
CLICKHOUSE_PACKAGES['rhel8']=${CLICKHOUSE_PACKAGES['centos']}

declare -A NGINX_PACKAGES
NGINX_PACKAGES['ubuntu18.04']="nginx_1.21.3-1~bionic_amd64.deb"
NGINX_PACKAGES['ubuntu20.04']="nginx_1.21.2-1~focal_amd64.deb"
NGINX_PACKAGES['centos7']="nginx-1.21.4-1.el7.ngx.x86_64.rpm"
NGINX_PACKAGES['centos8']="nginx-1.21.4-1.el8.ngx.x86_64.rpm"
NGINX_PACKAGES['rhel7']="nginx-1.21.4-1.el7.ngx.x86_64.rpm"
NGINX_PACKAGES['rhel8']="nginx-1.21.4-1.el8.ngx.x86_64.rpm"

download_packages() {
    local target_distribution=$1
    if [ -z $target_distribution ]; then
        echo "$0 - no target distribution specified"
        exit 1
    fi

    mkdir -p "${PACKAGE_PATH}/${target_distribution}"
    # just in case delete all files in target dir
    rm -f "${PACKAGE_PATH}/${target_distribution}/*"

    readarray -t clickhouse_files <<<"${CLICKHOUSE_PACKAGES[${target_distribution}]}"
    readarray -t nginx_files <<<"${NGINX_PACKAGES[${target_distribution}]}"

    echo "Downloading Clickhouse signing keys"
    curl -fs ${CLICKHOUSE_KEY} --output "${PACKAGE_PATH}/${target_distribution}/clickhouse-key.gpg"
    echo "Downloading Nginx signing keys"
    curl -fs ${NGINX_KEY} --output "${PACKAGE_PATH}/${target_distribution}/nginx-key.gpg"

    for package_file in "${clickhouse_files[@]}"; do
        if [ -z $package_file ]; then
            continue
        fi
        file_url="${CLICKHOUSE_REPO[$target_distribution]}/$package_file"
        save_file="${PACKAGE_PATH}/${target_distribution}/$package_file"
        echo "Fetching $file_url"
        curl -fs $file_url --output $save_file
    done

    for package_file in "${nginx_files[@]}"; do
        if [ -z $package_file ]; then
            continue
        fi
        file_url="${NGINX_REPO[$target_distribution]}/$package_file"
        save_file="${PACKAGE_PATH}/${target_distribution}/$package_file"
        echo "Fetching $file_url"
        curl -fs $file_url --output $save_file
    done

    bundle_file="${PACKAGE_PATH}/nms-dependencies-${target_distribution}.tar.gz"
    tar -zcf $bundle_file -C "${PACKAGE_PATH}/${target_distribution}" .
    echo "Bundle file saved as $bundle_file"

}

target_distribution=$1

if [ -z $target_distribution ]; then
    echo "Usage: $0 target_distribution"
    echo "Supported target distributions: ${!CLICKHOUSE_REPO[@]}"
    exit 1
fi

# check if target distribution is supported

if [ -z ${CLICKHOUSE_REPO[$target_distribution]} ]; then
    echo "Target distribution is not supported."
    echo "Supported distributions: ${!CLICKHOUSE_REPO[@]}"
    exit 1
fi

download_packages "${target_distribution}"

Then on the same directory that contains download_dependencies.sh run command below:

download_dependencies.sh <your linux version> 

In my case, I ran code below (leave it blank to see options):

download_dependencies.sh centos7

It should start to download and when it finished you should see nms-dependencies-rhel7.tar.gz in your directory. Copy that file(.tar.gz) to your offline target.

Now on your target machine, go to directory which you copied your file and run the code below:

tar -zxvf nms-dependencies-rhel7.tar.gz
sudo yum install *.rpm

After installation you can start nginx using systemctl:

sudo systemctl start clickhouse-server
sudo systemctl start nginx

Your nginx service must be running now!



Answered By - Aysan Eshraghi
Answer Checked By - David Marino (WPSolving Volunteer)