Skip to content

Instantly share code, notes, and snippets.

@matteomattei
Last active August 29, 2015 14:03
Show Gist options
  • Save matteomattei/955ba06e091eef696868 to your computer and use it in GitHub Desktop.
Save matteomattei/955ba06e091eef696868 to your computer and use it in GitHub Desktop.
Script to install and configure NGNIX + PHP-FPM + MYSQL server on Debian
#!/bin/bash
usage()
{
cat << EOF >&2
Usage: ${0} [options]
-m [pwd] : set mysql root password
-d [domain] : setup a domain for ngnix
-h : print usage
EOF
exit 1
}
func_fail()
{
echo ${1}
exit 1
}
is_installed()
{
dpkg -l "${1}" > /dev/null 2>&1
return ${?}
}
setup_vhost()
{
DOMAIN="${1}"
[ -z "${DOMAIN}" ] && return 1
cat << EOF > /etc/nginx/sites-available/${DOMAIN}
server {
listen 80;
root /usr/share/nginx/${DOMAIN};
index index.php index.html index.htm;
server_name ${1};
server_name www.${1};
location / {
try_files \$uri \$uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location ~ \.php\$ {
try_files \$uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* .(jpg|jpeg|png|gif|ico|svg|css|js)\$ {
expires 7d;
}
}
EOF
ln -s /etc/nginx/sites-available/${DOMAIN} /etc/nginx/sites-enabled/${DOMAIN}
}
##############################
###### MAIN STARTS HERE ######
##############################
# VARIABLES
MYSQL_ROOT_PASSWORD=""
DOMAIN=""
while getopts 'm:d:h' opt "${@}"
do
case "${opt}" in
m)
MYSQL_ROOT_PASSWORD="${OPTARG}"
;;
d)
DOMAIN="${OPTARG}"
;;
h)
usage
;;
*)
usage
;;
esac
done
# SETUP APT SOURCE
sed -i "/non\-us\.debian\.org/d" /etc/apt/sources.list
# SETTING UP LOCALES
if [ -z "${LC_ALL}" ] || [ -z "${LANGUAGE}" ] || [ -z "${LANG}" ]
then
export LC_ALL="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"
export LANG="en_US.UTF-8"
sed -i "{s/^# en_US\.UTF\-8 UTF\-8/en_US.UTF-8 UTF-8/g}" /etc/locale.gen
update-locale LC_ALL=en_US.UTF-8
update-locale LANGUAGE=en_US.UTF-8
update-locale LANG=en_US.UTF-8
locale-gen en_US.UTF-8
. /etc/default/locale
fi
# UPDATE THE WHOLE SYSTEM
apt-get update
apt-get -y dist-upgrade
# MYSQL-SERVER
if ! $(is_installed mysql-server)
then
[ -z "${MYSQL_ROOT_PASSWORD}" ] && func_fail "mysql password not provided"
echo "mysql-server mysql-server/root_password password ${MYSQL_ROOT_PASSWORD}" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password ${MYSQL_ROOT_PASSWORD}" | debconf-set-selections
apt-get -y install mysql-server
else
echo "MYSQL server already installed"
fi
# MYSQL-CLIENT
if ! $(is_installed mysql-client)
then
apt-get -y install mysql-client
else
echo "MYSQL client already installed"
fi
# NGINX
if ! $(is_installed nginx)
then
apt-get -y install nginx
sed -i "{s/# gzip on;/gzip on;/g}" /etc/nginx/nginx.conf
sed -i "{s/# gzip_/gzip_/g}" /etc/nginx/nginx.conf
else
echo "NGINX already installed"
fi
# SETUP DOMAIN
if [ -n "${DOMAIN}" ]
then
setup_vhost ${DOMAIN}
mkdir -p /usr/share/nginx/${DOMAIN}
fi
# PHP-FPM
if ! $(is_installed php5-fpm)
then
apt-get -y install php5-fpm
sed -i "{s/^;cgi\.fix_pathinfo=1/cgi.fix_pathinfo=0/g}" /etc/php5/fpm/php.ini
fi
# PHP-MYSQL
if ! $(is_installed php5-mysql)
then
apt-get -y install php5-mysql
fi
service php5-fpm restart
service nginx restart
exit 0
@matteomattei
Copy link
Author

To use it, just login via SSH to a remote debian server and type:

wget -q -O - https://gist.github.com/matteomattei/955ba06e091eef696868/raw | bash /dev/stdin -m YOUR_MYSQL_PASSWORD -d YOUR_DOMAIN.COM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment