User Tools

Site Tools


Sidebar


Tags Cloud
create_site

Добавление вхоста в apache+nginx

FIXME Переписать этот ужас.

Только Debian. Пользователь, которому принадлежат сайты, должен быть предварительно создан.

#!/bin/bash

function read_domain
{
    if [[ -z $domain ]]; then
        echo -n "Provide domain to add: "
        read domain
    fi
}

function read_user
{
    if [[ -z $user ]]; then
        echo -n "Provide domain's owner login: "
        read user
    fi
}

function read_logs
{
    if [[ -z $logs ]]; then
        echo -n "Enable access/error logs? [y/N]: "
        read logs
    fi

    case $logs in
        y*|Y*)
            LOGS=1
        ;;
        *)
            unset $logs
        ;;
    esac
}

function init_vars
{
    #### BASE SETTINGS ####
    CLIENTS_HOME="/home/clients"
    DOMAIN_DIR="/home/clients/$user/domains/$domain"
    DOCROOT="$DOMAIN_DIR/html"
    LOGSDIR="$DOMAIN_DIR/logs"
    TMPCONFDIR="/opt/scripts/www_configs"

    # can be set as IP='111.111.111.111'
    IP=`ip route get 8.8.8.8 | head -1 | awk '{print $NF}'`

    if [[ ! -d $TMPCONFDIR ]]; then
        mkdir -p $TMPCONFDIR
    fi

    if [[ -z `id $user` ]]; then
        echo "You need to create user '$user' first!"
        exit 1
    fi

    if [[ ! -d $DOCROOT ]]; then
        mkdir -p $DOCROOT
        chown -R $user:$user $DOCROOT
    fi

		if [[ ! -d $LOGSDIR ]]; then
			mkdir -p $LOGSDIR
		fi
}

function generate_apache_vhost
{
    if [[ $logs ]]; then
        local log_block="LogLevel warn
    ErrorLog $LOGSDIR/apache_error_log
    CustomLog $LOGSDIR/apache_access_log combined"
    fi

    if [[ $domain ]] && [[ $user ]]; then

        cat > $TMPCONFDIR/apache.$domain << EOF
<VirtualHost 127.0.0.1:82>
    <IfModule mpm_itk_module>
        AssignUserId $user $user
    </IfModule>

    ServerName $domain
    ServerAlias www.$domain

    ServerAdmin $user@$domain
    DocumentRoot $DOCROOT
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory $DOCROOT>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ $DOCROOT/cgi-bin/
    <Directory "$DOCROOT/cgi-bin/">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    $log_block
</VirtualHost>

EOF
    fi
}

function generate_nginx_vhost
{
    if [[ $logs ]]; then
        local log_block="access_log $LOGSDIR/nginx_access_log;
      error_log $LOGSDIR/nginx_error_log;"
    fi

    if [[ $domain ]] && [[ $user ]]; then
        cat > $TMPCONFDIR/nginx.$domain << EOF
  server {
      listen      80;
      server_name $domain www.$domain;
      root $DOCROOT;
      location / {
          proxy_pass         http://127.0.0.1:82/;
          proxy_redirect     off;
          proxy_set_header   Host             \$host;
          proxy_set_header   X-Real-IP        \$remote_addr;
          proxy_set_header   X-Forwarded-For  \$proxy_add_x_forwarded_for;
          proxy_max_temp_file_size  0;
          client_max_body_size       10m;
          client_body_buffer_size    128k;
          proxy_connect_timeout      90;
          proxy_send_timeout         90;
          proxy_read_timeout         90;
          proxy_buffer_size          4k;
          proxy_buffers              4 32k;
          proxy_busy_buffers_size    64k;
          proxy_temp_file_write_size 64k;
      }

      location ~* ^.+.(jpg|gif|png|avi|mpg|zip|exe)$ {
        root $DOCROOT;
      }

      $log_block
   }
EOF
    fi
}

function generate_named_vhost
{
	serial=`/bin/date +"%Y%m%d%k"`

	cat >$TMPCONFDIR/named.$domain<< EOF
\$TTL 10800
@ IN SOA ns1.$domain. root.$domain. (
    $serial     ; serial
    10800       ; refresh
    3600        ; retry
    604800      ; expire
    3600 )      ; minimum
; NS
    IN  NS  ns1.$domain.
    IN  NS  ns2.$domain.
; MX
    IN  MX  10  mx1.$domain.
; records
ns1         IN  A   $IP
ns2         IN  A   $IP
mx1         IN  A   $IP
www         IN  A   $IP
$domain.    IN  A   $IP

EOF
}

function enable_apache_vhost
{
    if [[ -f "/etc/apache2/sites-available/${domain}.conf" ]]; then
        echo "Domain $domain already exists in /etc/apache2/sites-available/${domain}.conf"
        exit 1
    else
        cp $TMPCONFDIR/apache.$domain /etc/apache2/sites-available/${domain}.conf
        a2ensite $domain
        if [[ -z `apache2ctl configtest` ]]; then
            /etc/init.d/apache2 reload
            echo "$domain enabled!"
        else
            echo "Errors detected, please solve it manually!"
            exit 1
        fi

    fi
}

function enable_nginx_vhost
{
    if [[ -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "Domain $domain already exists in /etc/nginx/sites-available/$domain"
        exit 1
    else
        cp $TMPCONFDIR/nginx.$domain /etc/nginx/sites-available/$domain
        ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/$domain
        if [[ -z `nginx -t` ]]; then
            /etc/init.d/nginx reload
            echo "$domain enabled!"
        else
            echo "Errors detected, please solve it manually!"
            exit 1
        fi

    fi
}

function enable_named_vhost
{
	[[ -d "/etc/bind/zones" ]] || mkdir -p "/etc/bind/zones"

	if [[ -f "/etc/bind/zones/$domain.conf" ]]; then
		echo "Zone $domain already exists in /etc/bind/zones/$domain.conf"
		exit 1
  else
		cp $TMPCONFDIR/named.$domain /etc/bind/zones/$domain.conf
		
		cat >>/etc/bind/named.conf.local<< EOF
zone "$domain" {
    type master;
    file "/etc/bind/zones/$domain.conf";
};

EOF

		if [[ -z `/usr/sbin/named-checkconf` && -z `/usr/sbin/named-checkzone -q $domain /etc/bind/zones/$domain.conf` ]] ; then
			/usr/sbin/rndc reload
			echo "bind9: $domain enabled"
		else
			echo "Errors detected, please solve it manually!"
			exit 1
		fi
	fi
}

function enable_logrotate
{
    if [[ ! -d "/etc/logrotate.d/vhosts" ]]; then
        mkdir -p /etc/logrotate.d/vhosts
    fi

        cat > $TMPCONFDIR/logrotate.$domain << EOF
$LOGSDIR/*_log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 0644 root root
    sharedscripts
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \\
            run-parts /etc/logrotate.d/httpd-prerotate; \\
        fi; \\
    endscript
    postrotate
        /etc/init.d/apache2 reload > /dev/null
        [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
    endscript
}
EOF

    if [[ $logs ]]; then
        # install logrotate
        if [[ -f "/etc/logrotate.d/vhosts/$domain" ]]; then
            echo "Domain $domain already exists in /etc/logrotate.d/vhosts/$domain"
            exit 1
        else
            cp $TMPCONFDIR/logrotate.$domain /etc/logrotate.d/vhosts/$domain
        fi
    fi
}


read_domain
read_user
read_logs

init_vars

generate_apache_vhost
generate_nginx_vhost
generate_named_vhost

enable_apache_vhost
enable_nginx_vhost
enable_named_vhost

enable_logrotate

Простейший скрипт для добавления виртхоста и днс-записи на сервер с Debian, если отсутствует панель управления. (2009 год, возможно устарело)

Необходимо предварительно создать пользователя http

#!/bin/bash

echo -n "Введите имя сайта: "
read domain

# СОЗДАНИЕ ДИРЕКТОРИИ
mkdir -p /home/http/domains/$domain/html/cgi-bin
chown -R http:http /home/http/domains/$domain/html

# СОЗДАНИЕ КОНФИГА ВИРТХОСТА ДЛЯ АПАЧА
cat >/etc/apache2/sites-available/$domain << EOF
<VirtualHost *:80>
    ServerName $domain
    ServerAlias www.$domain
    DocumentRoot /home/http/domains/$domain/html
    ErrorLog /dev/null

    ScriptAlias /cgi-bin/ "/home/http/domains/$domain/html/cgi-bin/"

    <Directory "/cgi-bin/">
        AllowOverride None
        Options None +FollowSymLinks ExecCGI
        DirectoryIndex index.pl
    </Directory>

    <Directory />
        Options All -Indexes
        AllowOverride All
    </Directory>
</VirtualHost>
EOF

# ВКЛЮЧЕНИЕ ВИРТХОСТА
/usr/sbin/a2ensite $domain
/etc/init.d/apache2 reload

# СОЗДАНИЕ ФАЙЛА ДНС-ЗОНЫ
serial=`/bin/date +"%Y%m%d%k"`
cat >/etc/bind/zones/$domain<< EOF
\$TTL 10800
@ IN SOA ns1.$domain. root.$domain. (
    $serial     ; serial
    10800       ; refresh
    3600        ; retry
    604800      ; expire
    3600 )      ; minimum
; NS
    IN  NS  ns1.$domain.
    IN  NS  ns2.$domain.
; MX
    IN  MX  10  mx1.$domain.
; records
ns1         IN  A   95.169.190.217
ns2         IN  A   95.169.191.217
www         IN  A   95.169.191.217
$domain.    IN  A   95.169.191.217
EOF

# СОЗДАНИЕ ЗАПИСИ О ДНС-ЗОНЕ
cat >>/etc/bind/named.conf.local<< EOF
zone "$domain" {
    type master;
    file "/etc/bind/zones/$domain";
};
EOF

# ВКЛЮЧЕНИЕ ЗОНЫ
/usr/sbin/rndc reload
create_site.txt · Last modified: 2015/09/24 18:46 by kyxap