Last active
May 6, 2022 01:16
-
-
Save fengjijiao/50e88189e5e1fbf5c86ba324b0b369db to your computer and use it in GitHub Desktop.
simply to install nginx for centos7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bash | |
| NGINX_VERSION=1.20.2 | |
| NGINX_DOWNLOAD_URL="http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" | |
| NGINX_SAVED_COMPRESSED_FILENAME=nginx.tar.gz | |
| NGINX_COMPILE_DIR=/usr/local/nginx_compile | |
| NGINX_INSTALL_DIR=/usr/local/nginx | |
| NGINX_BIN=${NGINX_INSTALL_DIR}/bin/nginx | |
| NGINX_LOG_DIR=/var/log/nginx | |
| NGINX_RUN_DIR=/var/run/nginx | |
| NGINX_PID=$NGINX_RUN_DIR/nginx.pid | |
| REQUIRED_SOFTWARE_LIST=( wget tar sed ) | |
| WWW_DIR=/var/www/html | |
| function check_software() { | |
| check_software_bool $1 | |
| if [ $? == 1 ]; then | |
| echo "${1} installed" | |
| else | |
| echo "${1} not installed" | |
| echo "start to install ${1}" | |
| yum install $1 | |
| fi | |
| } | |
| function check_software_bool() { | |
| RESULT=`whereis $1` | |
| RESULT_LENGTH=`echo -n $RESULT | wc -c` | |
| PACK_LENGTH=`echo -n $1 | wc -c` | |
| PACK_LENGTH=`expr $PACK_LENGTH + 1` | |
| if [ $RESULT_LENGTH -gt $PACK_LENGTH ]; then | |
| return 1 | |
| else | |
| return 0 | |
| fi | |
| } | |
| function required_software_check() { | |
| for i in "${REQUIRED_SOFTWARE_LIST[@]}" | |
| do | |
| check_software $i | |
| done | |
| } | |
| function install() { | |
| yum update -y | |
| required_software_check | |
| wget -O $NGINX_SAVED_COMPRESSED_FILENAME $NGINX_DOWNLOAD_URL | |
| mkdir -p $NGINX_COMPILE_DIR | |
| current_dir=$(pwd) | |
| tar -zxf $NGINX_SAVED_COMPRESSED_FILENAME --strip-components 1 -C $NGINX_COMPILE_DIR | |
| mkdir -p ${NGINX_INSTALL_DIR} | |
| mkdir ${NGINX_INSTALL_DIR}/bin | |
| mkdir ${NGINX_INSTALL_DIR}/module | |
| mkdir -p ${NGINX_LOG_DIR} | |
| mkdir -p ${NGINX_RUN_DIR} | |
| yum -y groupinstall "Development Tools" | |
| #require pcre pcre-devel zlib openssl openssl-devel | |
| yum -y install pcre pcre-devel zlib openssl openssl-devel gd gd-devel | |
| cd $NGINX_COMPILE_DIR | |
| ./configure --prefix=${WWW_DIR} --sbin-path=${NGINX_BIN} --conf-path=${NGINX_INSTALL_DIR}/nginx/nginx.conf --http-log-path=${NGINX_LOG_DIR}/access.log --error-log-path=${NGINX_LOG_DIR}/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=${NGINX_PID} --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=${NGINX_INSTALL_DIR}/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module | |
| make | |
| make install | |
| groupadd nginx | |
| useradd -r -g nginx nginx | |
| chown -R nginx:nginx $NGINX_LOG_DIR | |
| chown -R nginx:nginx $NGINX_RUN_DIR | |
| chown -R nginx:nginx $NGINX_INSTALL_DIR | |
| chown -R nginx:nginx $WWW_DIR | |
| sed -i '1 i\user nginx nginx;' ${NGINX_INSTALL_DIR}/nginx/nginx.conf | |
| sed -i '1 i\error_log '${NGINX_LOG_DIR}'/\error.log;' ${NGINX_INSTALL_DIR}/nginx/nginx.conf | |
| sed -i '/pid/d' ${NGINX_INSTALL_DIR}/nginx/nginx.conf | |
| sed -i '1 i\pid '${NGINX_PID}';' ${NGINX_INSTALL_DIR}/nginx/nginx.conf | |
| cat << EOT > /lib/systemd/system/nginx.service | |
| [Unit] | |
| Description=The NGINX HTTP and reverse proxy server | |
| After=syslog.target network-online.target remote-fs.target nss-lookup.target | |
| Wants=network-online.target | |
| [Service] | |
| User=root | |
| Type=forking | |
| PIDFile=${NGINX_PID} | |
| ExecStartPre=${NGINX_INSTALL_DIR}/bin/nginx -t | |
| ExecStart=${NGINX_INSTALL_DIR}/bin/nginx | |
| ExecReload=${NGINX_INSTALL_DIR}/bin/nginx -s reload | |
| ExecStop=/bin/kill -s QUIT $MAINPID | |
| PrivateTmp=true | |
| [Install] | |
| WantedBy=multi-user.target | |
| EOT | |
| setcap cap_net_bind_service=+ep $NGINX_BIN | |
| systemctl daemon-reload | |
| systemctl enable --now nginx | |
| cd ${current_dir} | |
| rm -rf $NGINX_SAVED_COMPRESSED_FILENAME | |
| cat << EOF | |
| nginx install finished! | |
| configure: ${NGINX_INSTALL_DIR}/nginx/nginx.conf | |
| www dir: ${WWW_DIR} | |
| EOF | |
| } | |
| function remove() { | |
| systemctl disable nginx | |
| systemctl stop nginx | |
| rm -rf $NGINX_INSTALL_DIR | |
| rm -rf $NGINX_LOG_DIR | |
| rm -rf $NGINX_RUN_DIR | |
| rm -rf /lib/systemd/system/nginx.service | |
| systemctl daemon-reload | |
| userdel nginx > /dev/null | |
| groupdel nginx > /dev/null | |
| } | |
| function help() { | |
| cat << EOF | |
| Nginx install helper!!! | |
| usage: | |
| ./nginx.sh menu | |
| install: install nginx | |
| remove: remove nginx | |
| EOF | |
| } | |
| function main() { | |
| if [ $# -lt 1 ]; then | |
| help | |
| else | |
| case $1 in | |
| install) | |
| install | |
| ;; | |
| remove) | |
| remove | |
| ;; | |
| *) | |
| echo 'command not found!' | |
| help | |
| ;; | |
| esac | |
| fi | |
| } | |
| main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment