Skip to content

Instantly share code, notes, and snippets.

@DeRain
Forked from Nks/create_host_php.sh
Created September 16, 2016 20:25
Show Gist options
  • Save DeRain/028dd6e02992700e42d797cb816d9362 to your computer and use it in GitHub Desktop.
Save DeRain/028dd6e02992700e42d797cb816d9362 to your computer and use it in GitHub Desktop.
Automatically creating new virtual hosts for nginx with support of the php-fpm
#!/bin/bash
# @author: Seb Dangerfield
# http://www.sebdangerfield.me.uk/?p=513
# Created: 11/08/2011
# Modified: 07/01/2012
# Modified: 27/11/2012
# @author Zhdanov Vladislav
# https://github.com/nks
# Modified 16/09/2016
# Modify the following to match your system
NGINX_CONFIG='/etc/nginx/sites-available'
NGINX_SITES_ENABLED='/etc/nginx/sites-enabled'
NGINX_LISTEN_IP_PORT='192.168.0.100:80'
PHP_INI_DIR='/etc/php-fpm.d'
WEB_SERVER_GROUP='nginx'
NGINX_INIT='service nginx'
PHP_FPM_INIT='service php-fpm'
MOUNT_DIR='/mnt/websites/'
# --------------END
SED=`which sed`
CURRENT_DIR=`dirname $0`
if [ -z $1 ]; then
echo "No domain name given"
exit 1
fi
DOMAIN=$1
# check the domain is valid!
PATTERN="^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$";
if [[ "$DOMAIN" =~ $PATTERN ]]; then
DOMAIN=`echo $DOMAIN | tr '[A-Z]' '[a-z]'`
echo "Creating hosting for:" $DOMAIN
else
echo "invalid domain name"
exit 1
fi
# Create a new user!
echo "Please specify the username for this site?"
read USERNAME
HOME_DIR=$USERNAME
if [ -z $USERNAME ]; then
echo "Username required"
exit 1
fi
getent passwd $USERNAME > /dev/null 2>&1
USERNAME_EXISTS=$?;
echo $USERNAME_EXISTS
if [ $USERNAME_EXISTS -eq 0 ]; then
echo "User already exists."
else
echo "The user does not exist. Creating..."
adduser $USERNAME
echo "Please enter a password for the user: $USERNAME"
read -s PASS
echo $PASS | passwd --stdin $USERNAME
fi
echo "Would you like to change to web root directory (y/n)?"
read CHANGEROOT
if [ [$CHANGEROOT == "y"] ]; then
read -e -p "Enter the new web root dir (after the public_html/)" DIR
PUBLIC_HTML_DIR='/public_html/'$DIR
else
PUBLIC_HTML_DIR='/public_html'
fi
read -e -p "Listen IP for the nginx: " -i $NGINX_LISTEN_IP_PORT NGINX_LISTEN_IP_PORT
# Now we need to copy the virtual host template
CONFIG=$NGINX_CONFIG/$DOMAIN.conf
cp $NGINX_CONFIG/nginx.vhost.conf.template $CONFIG
$SED -i "s/@@LISTEN_IP_PORT@@/$NGINX_LISTEN_IP_PORT/g" $CONFIG
$SED -i "s/@@HOSTNAME@@/$DOMAIN/g" $CONFIG
$SED -i "s#@@PATH@@#\/home\/"$USERNAME"\/web\/"$DOMAIN$PUBLIC_HTML_DIR"#g" $CONFIG
$SED -i "s/@@LOG_PATH@@/\/home\/"$USERNAME"\/web\/"$DOMAIN"\/logs/g" $CONFIG
$SED -i "s#@@SOCKET@@#/var/run/php5-"$DOMAIN".sock#g" $CONFIG
read -e -p "How many FPM servers would you like by default:" -i 5 FPM_SERVERS
if [ -z $FPM_SERVERS ]; then
echo "Using default 5"
FPM_SERVERS=5
fi
read -e -p "Min number of FPM servers would you like:" -i 5 MIN_SERVERS
if [ -z $MIN_SERVERS ]; then
echo "Using default 5"
MIN_SERVERS=5
fi
read -e -p "Max number of FPM servers would you like:" -i 15 MAX_SERVERS
if [ -z $MAX_SERVERS ]; then
echo "Using default 15"
MAX_SERVERS=15
fi
# Now we need to create a new php fpm pool config
FPMCONF="$PHP_INI_DIR/$DOMAIN.pool.conf"
cp $PHP_INI_DIR/pool.conf.template $FPMCONF
$SED -i "s/@@DOMAIN@@/$DOMAIN/g" $FPMCONF
$SED -i "s/@@USER@@/$USERNAME/g" $FPMCONF
$SED -i "s/@@HOME_DIR@@/$(echo /home/"$USERNAME"/web/"$DOMAIN$PUBLIC_HTML_DIR" | sed -e 's/\\/\\\\/g; s/\//\\\//g; s/&/\\\&/g')/g" $FPMCONF
$SED -i "s/@@MOUNT_DIR@@/$(echo $MOUNT_DIR$HOME_DIR"/web/"$DOMAIN$PUBLIC_HTML_DIR | sed -e 's/\\/\\\\/g; s/\//\\\//g; s/&/\\\&/g')/g" $FPMCONF
$SED -i "s/@@START_SERVERS@@/$FPM_SERVERS/g" $FPMCONF
$SED -i "s/@@MIN_SERVERS@@/$MIN_SERVERS/g" $FPMCONF
$SED -i "s/@@MAX_SERVERS@@/$MAX_SERVERS/g" $FPMCONF
MAX_CHILDS=$((MAX_SERVERS+START_SERVERS))
$SED -i "s/@@MAX_CHILDS@@/$MAX_CHILDS/g" $FPMCONF
usermod -aG $USERNAME $WEB_SERVER_GROUP
chmod g+rx /home/$HOME_DIR
chmod 600 $CONFIG
ln -s $CONFIG $NGINX_SITES_ENABLED/$DOMAIN.conf
# set file perms and create required dirs!
mkdir -p /home/$HOME_DIR/web
mkdir -p $MOUNT_DIR$HOME_DIR/web/$DOMAIN/$PUBLIC_HTML_DIR
#mkdir -p /home/$HOME_DIR$PUBLIC_HTML_DIR
ln -s $MOUNT_DIR$HOME_DIR/web/$DOMAIN /home/$HOME_DIR/web/
chown -R $USERNAME:$USERNAME $MOUNT_DIR$HOME_DIR
mkdir /home/$HOME_DIR/web/$DOMAIN/logs
chmod 750 /home/$HOME_DIR -R
chmod 770 /home/$HOME_DIR/web/$DOMAIN/logs
chmod 750 /home/$HOME_DIR/web/$DOMAIN/$PUBLIC_HTML_DIR
chown $USERNAME:$USERNAME /home/$HOME_DIR/ -R
$NGINX_INIT reload
$PHP_FPM_INIT restart
echo -e "\nSite Created for $DOMAIN with PHP support"
server {
listen @@LISTEN_IP_PORT@@;
server_name @@HOSTNAME@@;
root "@@PATH@@";
index index.php index.html;
client_max_body_size 200m;
access_log @@LOG_PATH@@/access.log;
error_log @@LOG_PATH@@/error.log;
if ($http_user_agent ~* (Baiduspider|webalta|nikto|wkito|pikto|scan|acunetix|morfeus|webcollage|youdao) ) {
return 401;
}
if ($http_user_agent ~* (HTTrack|clshttp|archiver|loader|email|harvest|extract|grab|miner) ) {
return 401;
}
location / {
try_files $uri $uri/ /index.php$uri?$args;
}
location ~ "^(.+\.php)($|/)" {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SERVER_NAME $host;
if ($uri !~ "^/uploads/") {
fastcgi_pass unix:@@SOCKET@@;
}
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
location ~* \.(html|htm)$ {
expires 30m;
}
location ~* /\.(ht|git|svn) {
deny all;
}
}
[@@DOMAIN@@]
listen = /var/run/php5-@@DOMAIN@@.sock
listen.allowed_clients = 127.0.0.1
user = @@USER@@
group = @@USER@@
listen.owner = @@USER@@
listen.group = @@USER@@
;rlimit_core = unlimited
pm = dynamic
pm.max_children = @@MAX_CHILDS@@
pm.start_servers = @@START_SERVERS@@
pm.min_spare_servers = @@MIN_SERVERS@@
pm.max_spare_servers = @@MAX_SERVERS@@
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
php_admin_value[session.save_path] = "/dev/shm"
php_admin_value[open_basedir] = "@@HOME_DIR@@:@@MOUNT_DIR@@:/usr/share/pear:/usr/share/php:/tmp:/dev/shm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment