linux-tutorial/codes/linux/soft/nginx-install.sh

82 lines
2.5 KiB
Bash
Raw Normal View History

2019-05-07 15:39:11 +08:00
#!/usr/bin/env bash
2019-07-12 15:53:54 +08:00
###################################################################################
# 控制台颜色
BLACK="\033[1;30m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
CYAN="\033[1;36m"
RESET="$(tput sgr0)"
###################################################################################
2019-10-29 12:19:46 +08:00
printf "${BLUE}\n"
2019-05-07 15:39:11 +08:00
cat << EOF
###################################################################################
2019-10-29 12:19:46 +08:00
# 采用编译方式安装 Nginx, 并将其注册为 systemd 服务
# 默认下载安装 1.16.0 版本,安装路径为:/usr/local/nginx
# @system: 适用于 CentOS7+
2019-05-07 15:39:11 +08:00
# @author: Zhang Peng
###################################################################################
EOF
2019-10-29 12:19:46 +08:00
printf "${RESET}\n"
2019-07-12 15:53:54 +08:00
2019-10-29 12:19:46 +08:00
command -v yum > /dev/null 2>&1 || {
printf "${RED}Require yum but it's not installed.${RESET}\n";
exit 1;
}
2019-05-07 15:39:11 +08:00
2019-10-29 12:19:46 +08:00
printf "\n${GREEN}>>>>>>>> install nginx begin${RESET}\n"
2019-10-10 08:56:31 +08:00
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n"
2019-10-29 12:19:46 +08:00
printf "\t Usage: sh nginx-install.sh [version] \n"
printf "\t Default: sh nginx-install.sh 1.16.0 \n"
printf "\t Example: sh nginx-install.sh 1.16.0 \n"
2019-10-10 08:56:31 +08:00
printf "${RESET}\n"
2019-05-09 11:26:06 +08:00
fi
2019-10-29 12:19:46 +08:00
temp=/opt/nginx
2019-05-07 16:26:06 +08:00
version=1.16.0
2019-05-07 15:39:11 +08:00
if [[ -n $1 ]]; then
2019-10-10 08:56:31 +08:00
version=$1
2019-05-07 15:39:11 +08:00
fi
2019-07-12 15:53:54 +08:00
# install info
2019-10-29 12:19:46 +08:00
printf "${PURPLE}[Install Info]\n"
2019-07-12 15:53:54 +08:00
printf "\t version = ${version}\n"
printf "${RESET}\n"
2019-10-29 12:19:46 +08:00
printf "${CYAN}>>>> install required libs${RESET}\n"
2019-05-07 16:26:06 +08:00
yum install -y zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre
2019-05-07 15:39:11 +08:00
2019-07-12 15:53:54 +08:00
# download and decompression
2019-10-29 12:19:46 +08:00
printf "${CYAN}>>>> download nginx${RESET}\n"
mkdir -p ${temp}
curl -o ${temp}/nginx-${version}.tar.gz http://nginx.org/download/nginx-${version}.tar.gz
tar zxf ${temp}/nginx-${version}.tar.gz -C ${temp}
2019-05-07 15:39:11 +08:00
2019-07-12 15:53:54 +08:00
# configure and makefile
2019-10-29 12:19:46 +08:00
printf "${CYAN}>>>> compile nginx${RESET}\n"
cd ${temp}/nginx-${version}
2019-05-07 16:26:06 +08:00
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make && make install
2019-10-29 12:19:46 +08:00
rm -rf ${temp}
cd -
2019-05-15 22:43:15 +08:00
2019-10-29 12:19:46 +08:00
# setting systemd service
printf "${CYAN}>>>> set nginx as a systemd service${RESET}\n"
2019-05-16 11:49:19 +08:00
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/config/nginx/nginx.service -O /usr/lib/systemd/system/nginx.service
2019-05-15 22:43:15 +08:00
chmod +x /usr/lib/systemd/system/nginx.service
2019-10-29 12:19:46 +08:00
# boot nginx
printf "${CYAN}>>>> start nginx${RESET}\n"
2019-05-15 22:43:15 +08:00
systemctl enable nginx.service
systemctl start nginx.service
2019-10-29 12:19:46 +08:00
printf "\n${GREEN}<<<<<<<< install nginx end${RESET}\n"
printf "\n${PURPLE}nginx service status: ${RESET}\n"
systemctl status nginx