Skip to content

Instantly share code, notes, and snippets.

@saosangmo
Forked from allebb/create_php_site.sh
Created January 30, 2020 14:54
Show Gist options
  • Save saosangmo/c39dbb1d7e87d80f765599c2d18fcd5d to your computer and use it in GitHub Desktop.
Save saosangmo/c39dbb1d7e87d80f765599c2d18fcd5d to your computer and use it in GitHub Desktop.
Nginx and PHP-FPM, bash script for creating new vhost’s under separate fpm pools
#!/bin/bash
# @author: Seb Dangerfield
# http://www.sebdangerfield.me.uk/?p=513
# Created: 11/08/2011
# Modified: 07/01/2012
# Modified: 17/05/2012
# Modify the following to match your system
NGINX_CONFIG='/etc/nginx/sites-available'
NGINX_SITES_ENABLED='/etc/nginx/sites-enabled'
PHP_INI_DIR='/etc/php5/fpm/pool.d'
WEB_SERVER_GROUP='www-data'
NGINX_INIT='/etc/init.d/nginx'
PHP_FPM_INIT='/etc/init.d/php5-fpm'
# --------------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-Z]|[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][-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
adduser $USERNAME
# -------
# CentOS:
# If you're using CentOS you will need to uncomment the next 3 lines!
# -------
#echo "Please enter a password for the user: $USERNAME"
#read -s PASS
#echo $PASS | passwd --stdin $USERNAME
echo "Would you like to change to web root directory (y/n)?"
read CHANGEROOT
if [ $CHANGEROOT == "y" ]; then
echo "Enter the new web root dir (after the public_html/)"
read DIR
PUBLIC_HTML_DIR='/public_html/'$DIR
else
PUBLIC_HTML_DIR='/public_html'
fi
# Now we need to copy the virtual host template
CONFIG=$NGINX_CONFIG/$DOMAIN.conf
cp $CURRENT_DIR/nginx.vhost.conf.template $CONFIG
$SED -i "s/@@HOSTNAME@@/$DOMAIN/g" $CONFIG
$SED -i "s#@@PATH@@#\/home\/"$USERNAME$PUBLIC_HTML_DIR"#g" $CONFIG
$SED -i "s/@@LOG_PATH@@/\/home\/$USERNAME\/_logs/g" $CONFIG
#Modified by Yong: instead of create pid file in /var/run/php-fpm/”$USERNAME”_fpm.sock,
#just put it in /var/run/”$USERNAME”_fpm.sock or other place that is not ephemeral by design,
#otherwise it will cause fpm startup problem ([ERROR] bind() for address
#‘/var/run/php-fpm/**_fpm.sock’ failed: No such file or directory) every time the server reboots.
#mkdir -p /var/run/php-fpm/
$SED -i "s#@@SOCKET@@#/var/run/"$USERNAME"_fpm.sock#g" $CONFIG
echo "How many FPM servers would you like by default:"
read FPM_SERVERS
echo "Min number of FPM servers would you like:"
read MIN_SERVERS
echo "Max number of FPM servers would you like:"
read MAX_SERVERS
# Now we need to create a new php fpm pool config
FPMCONF="$PHP_INI_DIR/$DOMAIN.pool.conf"
cp $CURRENT_DIR/pool.conf.template $FPMCONF
$SED -i "s/@@USER@@/$USERNAME/g" $FPMCONF
$SED -i "s/@@HOME_DIR@@/\/home\/$USERNAME/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$PUBLIC_HTML_DIR
mkdir /home/$HOME_DIR/_logs
mkdir /home/$HOME_DIR/_sessions
chmod 750 /home/$HOME_DIR -R
chmod 700 /home/$HOME_DIR/_sessions
chmod 770 /home/$HOME_DIR/_logs
chmod 750 /home/$HOME_DIR$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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment