Operations 6 min read

Automated Nginx Installation and Configuration Script for CentOS/Ubuntu

This guide provides a fully automated Bash script that downloads, verifies, compiles, installs, and configures Nginx as a systemd service on CentOS and Ubuntu systems, including dependency installation, service setup, and common management commands.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Automated Nginx Installation and Configuration Script for CentOS/Ubuntu

Nginx (engine x) is a high‑performance HTTP and reverse‑proxy web server widely used in production environments. The following Bash script performs a completely automated download, verification, compilation, installation, and systemd service configuration of Nginx, supporting both CentOS‑based and Ubuntu‑based distributions.

The script defines helper functions to check command exit status, download the source tarball, install required dependencies, configure, compile, and install Nginx, and finally create and enable a systemd service unit.

#!/bin/bash
ck_ok() {
    if [ $? -ne 0 ]; then
        echo "$1 error."
        exit 1
    fi
}

download_ng() {
    cd /usr/local/src
    if [ -f nginx-1.23.0.tar.gz ]; then
        echo "当前目录已经存在nginx-1.23.0.tar.gz"
        echo "检测md5"
        ng_md5=`md5sum nginx-1.23.0.tar.gz|awk '{print $1}'`
        if [ ${ng_md5} == 'e8768e388f26fb3d56a3c88055345219' ]; then
            return 0
        else
            sudo /bin/mv nginx-1.23.0.tar.gz nginx-1.23.0.tar.gz.old
        fi
    fi
    sudo curl -O http://nginx.org/download/nginx-1.23.0.tar.gz
    ck_ok "下载Nginx"
}

install_ng() {
    cd /usr/local/src
    echo "解压Nginx"
    sudo tar zxf nginx-1.23.0.tar.gz
    ck_ok "解压Nginx"
    cd nginx-1.23.0

    echo "安装依赖"
    if which yum >/dev/null 2>&1; then
        for pkg in gcc make pcre-devel zlib-devel openssl-devel; do
            if ! rpm -q $pkg >/dev/null 2>&1; then
                sudo yum install -y $pkg
                ck_ok "yum 安装${pkg}"
            else
                echo "${pkg}已经安装"
            fi
        done
    fi
    if which apt >/dev/null 2>&1; then
        for pkg in make libpcre++-dev libssl-dev zlib1g-dev; do
            if ! dpkg -l $pkg >/dev/null 2>&1; then
                sudo apt install -y $pkg
                ck_ok "apt 安装${pkg}"
            else
                echo "${pkg}已经安装"
            fi
        done
    fi

    echo "configure Nginx"
    sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module
    ck_ok "Configure Nginx"

    echo "编译和安装"
    sudo make && sudo make install
    ck_ok "编译和安装"

    echo "编辑systemd服务管理脚本"
    cat > /tmp/nginx.service <

Save the script as nginx_install.sh and run it with:

chmod +x nginx_install.sh && ./nginx_install.sh

After the script finishes, you can access the server’s IP address to verify that Nginx is serving pages. Common Nginx management commands are provided below:

# 启动nginx
systemctl start nginx
# 停止nginx
systemctl stop nginx
# 设置开机启动
systemctl enable nginx
# 取消开机启动
systemctl disable nginx

For a one‑click installation, you can also execute the following command, which fetches and runs the script directly:

bash -c "$(curl -L s.aaa.al/nginx_install.sh)"
automationLinuxNginxInstallationbashsystemd
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.