SHELL 自动部署Nginx并设置虚拟主机
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SHELL 自动部署Nginx并设置虚拟主机相关的知识,希望对你有一定的参考价值。
楓城浪子原创,转载请标明出处!
更多技术博文请见个人博客:https://fengchenglangzi.000webhostapp.com
微信bh19890922
QQ445718526、490425557
#!/bin/bash #2017-8-29 16:07:07 #by fengchenglangzi #auto install nginx and vhost ################### #定义变量 SOFT_DIR="/usr/local/src" NGINX_DIR="/usr/local/nginx" NGINX_SOFT="nginx-1.12.0.tar.gz" NGINX_SOFT_NAME=$(echo $NGINX_SOFT|awk -F".tar" ‘{print $1}‘) YUM="yum install -y" PCRE_GCC="gcc gcc-c++ pcre pcre-devel openssl-devel" ################### #安装Nginx function Install_Nginx () { if [ ! -d $NGINX_DIR ];then $YUM $PCRE_GCC cd $SOFT_DIR if [ ! -d $SOFT_DIR/$NGINX_SOFT_NAME ];then wget -c http://nginx.org/download/$NGINX_SOFT tar -zxf $NGINX_SOFT fi cd $NGINX_SOFT_NAME sed -i -e ‘s/1.12.0//g‘ -e ‘s/nginx\//JWS/g‘ -e ‘s/"NGINX"/"JWS"/g‘ src/core/nginx.h useradd nginx;usermod -s /sbin/nologin nginx ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module && make -j8 && make install -j8 if [ $? -eq 0 ];then echo "Nginx安装完毕!!!" /usr/local/nginx/sbin/nginx fi else echo "Nginx已经安装!!!" fi } #################### #创建虚拟主机 function Create_Vhost () { if [ ! -d $NGINX_DIR ];then echo "当前系统未安装Nginx,请先安装Nginx!!!" else read -p "请输入新的虚拟主机域名:" input DOMAIN_NAME=($(echo $input)) i=0 while [[ $i < ${#DOMAIN_NAME[@]} ]] do if [ ! -d $NGINX_DIR/conf/vhost/${DOMAIN_NAME[i]} ];then mkdir -p $NGINX_DIR/conf/vhost/${DOMAIN_NAME[i]}/{logs,html} touch $NGINX_DIR/conf/vhost/${DOMAIN_NAME[i]}/nginx.conf sed -i "/include * mime.types/a\include $NGINX_DIR\/conf\/vhost\/${DOMAIN_NAME[i]}/nginx.conf;" $NGINX_DIR/conf/nginx.conf cat >> $NGINX_DIR/conf/vhost/${DOMAIN_NAME[i]}/nginx.conf << EOF server { listen 80; server_name ${DOMAIN_NAME[i]}; access_log $NGINX_DIR/conf/vhost/${DOMAIN_NAME[i]}/logs/access.log; location / { root $NGINX_DIR/conf/vhost/${DOMAIN_NAME[i]}/html/; index index.html index.htm; } } EOF cat >> $NGINX_DIR/conf/vhost/${DOMAIN_NAME[i]}/html/index.html << EOF <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>${DOMAIN_NAME[i]}</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> EOF echo "${DOMAIN_NAME[i]}创建完毕!!!" else echo "${DOMAIN_NAME[i]}已经创建,无需再次创建!!!" fi let "i++" done fi read -p "Nginx虚拟主机创建完毕,请确认是否重启Nginx(y/n):" yn if [ $yn == "y" ];then /usr/local/nginx/sbin/nginx -s reload elif [ $yn == "n" ];then break else echo "请正确输入选项(y/n)!!!" fi } PS3="请选择所需服务:" select i in INSTALL_NGINX CREATE_VHOST RELOAD_NGINX QUIT do case $i in INSTALL_NGINX) Install_Nginx ;; CREATE_VHOST) Create_Vhost ;; RELOAD_NGINX) /usr/local/nginx/sbin/nginx -s reload ;; QUIT) exit ;; *) echo "Usage: $0 {1|2|3|4}" esac done
本文出自 “楓城浪子” 博客,请务必保留此出处http://fengchenglangzi.blog.51cto.com/1322043/1961308
以上是关于SHELL 自动部署Nginx并设置虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章
Ansible + shell 实现部署fastdfs+nginx 实现图片服务器并提供动态缩放功能;