nginx ubuntu site enable

Just create this script /usr/bin/nginx_modsite and make it executable.

#!/bin/bash

##
#  File:
#    nginx_modsite
#  Description:
#    Provides a basic script to automate enabling and disabling websites found
#    in the default configuration directories:
#      /etc/nginx/sites-available and /etc/nginx/sites-enabled
#    For easy access to this script, copy it into the directory:
#      /usr/local/sbin
#    Run this script without any arguments or with -h or --help to see a basic
#    help dialog displaying all options.
##

# Copyright (C) 2010 Michael Lustfield <[email protected]>

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS‘‘ AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

##
# Default Settings
##

NGINX_CONF_FILE="$(awk -F= -v RS=‘ ‘ ‘/conf-path/ {print $2}‘ <<< $(nginx -V 2>&1))"
NGINX_CONF_DIR="${NGINX_CONF_FILE%/*}"
NGINX_SITES_AVAILABLE="$NGINX_CONF_DIR/sites-available"
NGINX_SITES_ENABLED="$NGINX_CONF_DIR/sites-enabled"
SELECTED_SITE="$2"

##
# Script Functions
##

ngx_enable_site() {
    [[ ! "$SELECTED_SITE" ]] &&
        ngx_select_site "not_enabled"

    [[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] && 
        ngx_error "Site does not appear to exist."
    [[ -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
        ngx_error "Site appears to already be enabled"

    ln -sf "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" -T "$NGINX_SITES_ENABLED/$SELECTED_SITE"
    ngx_reload
}

ngx_disable_site() {
    [[ ! "$SELECTED_SITE" ]] &&
        ngx_select_site "is_enabled"

    [[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
        ngx_error "Site does not appear to be \‘available\‘. - Not Removing"
    [[ ! -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
        ngx_error "Site does not appear to be enabled."

    rm -f "$NGINX_SITES_ENABLED/$SELECTED_SITE"
    ngx_reload
}

ngx_list_site() {
    echo "Available sites:"
    ngx_sites "available"
    echo "Enabled Sites"
    ngx_sites "enabled"
}

##
# Helper Functions
##

ngx_select_site() {
    sites_avail=($NGINX_SITES_AVAILABLE/*)
    sa="${sites_avail[@]##*/}"
    sites_en=($NGINX_SITES_ENABLED/*)
    se="${sites_en[@]##*/}"

    case "$1" in
        not_enabled) sites=$(comm -13 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
        is_enabled) sites=$(comm -12 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
    esac

    ngx_prompt "$sites"
}

ngx_prompt() {
    sites=($1)
    i=0

    echo "SELECT A WEBSITE:"
    for site in ${sites[@]}; do
        echo -e "$i:\t${sites[$i]}"
        ((i++))
    done

    read -p "Enter number for website: " i
    SELECTED_SITE="${sites[$i]}"
}

ngx_sites() {
    case "$1" in
        available) dir="$NGINX_SITES_AVAILABLE";;
        enabled) dir="$NGINX_SITES_ENABLED";;
    esac

    for file in $dir/*; do
        echo -e "\t${file#*$dir/}"
    done
}

ngx_reload() {
    read -p "Would you like to reload the Nginx configuration now? (Y/n) " reload
    [[ "$reload" != "n" && "$reload" != "N" ]] && invoke-rc.d nginx reload
}

ngx_error() {
    echo -e "${0##*/}: ERROR: $1"
    [[ "$2" ]] && ngx_help
    exit 1
}

ngx_help() {
    echo "Usage: ${0##*/} [options]"
    echo "Options:"
    echo -e "\t<-e|--enable> <site>\tEnable site"
    echo -e "\t<-d|--disable> <site>\tDisable site"
    echo -e "\t<-l|--list>\t\tList sites"
    echo -e "\t<-h|--help>\t\tDisplay help"
    echo -e "\n\tIf <site> is left out a selection of options will be presented."
    echo -e "\tIt is assumed you are using the default sites-enabled and"
    echo -e "\tsites-disabled located at $NGINX_CONF_DIR."
}

##
# Core Piece
##

case "$1" in
    -e|--enable)    ngx_enable_site;;
    -d|--disable)   ngx_disable_site;;
    -l|--list)  ngx_list_site;;
    -h|--help)  ngx_help;;
    *)      ngx_error "No Options Selected" 1; ngx_help;;
esac

How it works:

To list all the sites

$ sudo nginx_modsite -l

To enable site "test_website"

$ sudo nginx_modsite -e test_website

To disable site "test_website"

$ sudo nginx_modsite -d test_website
时间: 2024-08-08 12:44:09

nginx ubuntu site enable的相关文章

Flask+uwsgi+Nginx+Ubuntu部署

学了一段时间flask,可是一直没有做过部署, 于是想着怎么部署呢, 想想,先吧服务给搞通吧,于是呢 就先想着去吧服务给搞起来,这里选择的是Flask+uwsgi+Nginx+Ubuntu, Python选择的是2.7.2这个是Ubuntu系统自带的学起来感觉还是简单的 不用去软连,目前自己的flask是python3写的 ,慢慢去过渡,先吧这个给搞通了,那么在优化也是很顺手的.其实对于很多的原理自己也是一知半解,先吧这个给搭起来,慢慢去了解里面的逻辑什么的. Nginx Nginx 是高效的

Flask+uwsgi+Nginx+Ubuntu部署教程

学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的过程就会重复一次.不知道多少人的膝盖中箭了呢?我也这样干过,这么做确实很蠢,所以我决定写一篇 Flask+uwsgi+Nginx+Ubuntu 的部署教程,解答一些我自己在这个过程中的疑问,从原理到方案,以一个小白的角度,总结一下部署.运维这件事,应该对初学 Flask 需要部署的同学有些帮助. 环

Nginx: ubuntu系统上如何判断是否安装了Nginx?

问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何查看Nginx版本号? 使用命令:nginx -v   //显示Nginx版本号 nginx -V  // 显示Nginx版本号,编译器版本号,配置信息 2. master process:主进程 worker process:工作进程 参考:https://zhidao.baidu.com/que

Ubuntu下安装Nginx,PHP5(及PHP-FPM),MySQL

Ubuntu 下 nginx , php , mysql 和 golang 的简单安装 我是搞php出身,自然安装lnmp是常规技能.以前的手段还是lnmp安装包,比如军哥的lnmp1.0.随着php和mysql的更新,大多数一键安装都开始版本老化,更新困难的问题.因此,重新研究了一下Ubuntu下lnmp的安装,发现现在简单的多,记录一下. 另外最近在学习golang,Ubuntu下安装自然也是必须的过程.不过golang的安装也有一些奥妙.当然,不是源码安装的啦. Nginx Stable/

ubuntu 16 安装django nginx uWSGI

参考 https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04 1.安装pip sudo apt-get update sudo apt-get install python-pip 如果您使用python3 sudo apt-get update sudo apt-get install python3-pip 2.

Ubuntu中Nginx的安装与配置全过程

1. 在终端运行命令:$sudo apt-get install nginx ubuntu安装Nginx之后的文件结构大致为: 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下 启动程序文件在/usr/sbin/nginx 日志放在了/var/log/nginx中,分别是access.log和error.log 并已经在/etc/init.d/下创建了启动脚本nginx 默认的虚拟主机的目录设置在了/usr/share/

转- 在ubuntu下安装Nginx

一. 安装包安装 1.1 安装Nginx $sudo apt-get install nginx Ubuntu安装之后的文件结构大致为: 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下 程序文件在/usr/sbin/nginx 日志放在了/var/log/nginx中 并已经在/etc/init.d/下创建了启动脚本nginx 默认的虚拟主机的目录设置在了/var/www/nginx-default (有的版本 默认的

Nginx + spawn-fcgi- Ubuntu中文

Nginx - Ubuntu中文 页面 讨论 查看源代码 历史 导航 首页 最近更改 随机页面 页面分类 帮助 编辑 编辑指南 沙盒 新闻动态 字词处理 工具 链入页面 相关更改 特殊页面 打印版本 固定链接 页面信息 Nginx 目录 1 安装nginx 2 启动nginx 3 配置php和mysql 3.1 安装Php和mysql 3.2 安装FastCgi 3.3 配置 nginx 4 安装nginx +uwsgi+ Django 5 配置 nginx uwsgi django 6 uws

Installing Ubuntu on a Pre-Installed Windows 8 (64-bit) System (UEFI Supported)

http://askubuntu.com/questions/221835/installing-ubuntu-on-a-pre-installed-windows-8-64-bit-system-uefi-supported If you are using Ubuntu 15.04+, many issues are now solved, so there is no need to follow this guide except if you are using any Ubuntu