The content includes scripts for both CentOS and Ubuntu systems, use as needed.Preparation before installationAfter CentOS stopped maintenance, the official related repositories have been closed, and you need to change the repository. Please refer to the following article.CentOS 7 stops online updates, yum changes to Vault repository.The article uses Tsinghua’s repository, but you can also change to Alibaba’s repository as needed. Below is the method to change to Alibaba’s repository; the original official repository is no longer maintained, just copy and paste to overwrite.
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache
Preparing the installation package<span>nginx </span><span>http://nginx.org/download/nginx-1.20.2.tar.gz</span>
Download the installation package to /usr/local/soft, this address is consistent with the address in the script file.
<span>This script supports online download,</span><span> if the installation package is not prepared, ensure that the installation machine is properly connected to the internet.</span>
Writing the script fileinstall_nginx.sh (CentOS version), below is the content of the script file, please copy and save it as a script file.
#! /usr/bin/bash
## Fully automated source code compilation and installation for any version
if [ ! -d "/usr/local/soft" ]; then
echo "Folder does not exist, please create a folder and place the installation package here"
exit
fi
#nginx
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /usr/local/soft
if [ ! -f nginx-1.20.2.tar.gz ]; then
wget http://nginx.org/download/nginx-1.20.2.tar.gz
fi
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_ssl_module --with-http_v2_module
make && make install
cd /lib/systemd/system
touch nginx.service
echo "[Unit]\nDescription=nginx service\nAfter=network.target\n[Service]\nType=forking\nExecStart=/usr/local/nginx/sbin/nginx\nExecReload=/usr/local/nginx/sbin/nginx -s reload\nExecStop=/usr/local/nginx/sbin/nginx -s stop\nPrivateTmp=true\n[Install]\nWantedBy=multi-user.target" >> nginx.service
systemctl daemon-reload
systemctl enable nginx.service
systemctl start nginx.service
Writing the script fileinstall_nginx.sh (Ubuntu version), below is the content of the script file, please copy and save it as a script file.
#! /usr/bin/bash
## Fully automated source code compilation and installation for any version
# ======= User Configuration ========
NGINX_DOWNLOAD_DIR="/usr/local"
NGINX_BASE_DIR="/usr/local/nginx"
NGINX_BIN="$NGINX_BASE_DIR/nginx"
NGINX_SERVER_DIR="$NGINX_BASE_DIR/sbin"
NGINX_CONFIG_DIR="$NGINX_BASE_DIR/nginx/config"
# ============================
# Check if root
if [ "[0;32m$(id -u)[0m" -ne 0 ]; then
echo "โ Please run this script as root user (sudo -i)"
exit 1
fi
echo "โ
Installing necessary dependencies..."
apt update && apt install -y wget gcc build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev libxml2 libxml2-dev libgd-dev libgeoip-dev libxslt1-dev
#nginx
echo "๐ Creating directories..."
mkdir -p "$NGINX_DOWNLOAD_DIR" "$NGINX_CONFIG_DIR"
chmod 750 "$NGINX_DOWNLOAD_DIR" "$NGINX_CONFIG_DIR"
# Download Nginx and verify
echo "โฌ๏ธ Downloading Nginx..."
wget --progress=bar:force http://nginx.org/download/nginx-1.20.2.tar.gz
echo "Granting execute permissions..."
chmod +x "$NGINX_BIN"
echo "โฌ๏ธ Extracting Nginx..."
tar -zxvf nginx-1.20.2.tar.gz
echo "โฌ๏ธ Compiling Nginx..."
cd nginx-1.20.2
./configure --prefix=$NGINX_BASE_DIR --with-http_gzip_static_module --with-http_ssl_module --with-http_v2_module
make && make install
echo "๐ Creating systemd service file..."
cat < /etc/systemd/system/nginx.service
[Unit]\nDescription=nginx service\nAfter=network.target\n[Service]\nType=forking\nExecStart=$NGINX_SERVER_DIR/nginx\nExecReload=$NGINX_SERVER_DIR/nginx -s reload\nExecStop=$NGINX_SERVER_DIR/nginx -s stop\nPrivateTmp=true\n[Install]\nWantedBy=multi-user.target
EOF
# Start service
echo "๐ Starting Nginx service..."
systemctl daemon-reload
systemctl enable nginx.service
systemctl start nginx.service
# Check service status
echo "๐งช Checking Nginx running status..."
if systemctl is-active --quiet nginx; then
echo "โ
Nginx started successfully!"
else
echo "โ Nginx failed to start, please execute journalctl -u nginx -e to check the logs"
fi
# Output installation information
echo ""
echo "============================================="
echo "๐ NGINX installation completed"
echo "๐ Installation directory: $NGINX_BASE_DIR"
echo "๐ Service directory: $NGINX_SERVER_DIR"
echo "โ๏ธ Configuration directory: $NGINX_CONFIG_DIR"
echo "============================================="
Technical accumulation, meeting is fate
