nginx-install/nginxinstall

103 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
########################################################################
# First Created: 17032021 Author: Allan Desc: Installs Nginx on Ubuntu #
########################################################################
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit 1; fi
#
# Are we in the right directory
#
scriptdir="nginx-install" && whereami=$(pwd |awk -F'/' '{print $NF}')
if [ $whereami != $scriptdir ]; then printf "\nWrong directory! Script must be run from $scriptdir\n\n" ; exit 1; fi
#
# Define variables and functions
#
line (){ for i in {1..50}; do echo -n "$1"; done && printf "\n"; }
#
# Function title
#
title () { printf "\nNginx install script V1.3\n\n"; }
#
# Function to check if a service is already running or not
#
serviceyes () { printf "\n$service is allready running cannot continue...\n\n"; }
servicealive () { status=$(systemctl is-active $service); if [[ "$status" == "active" ]]; then serviceyes ; exit; fi; }
#
# If Nginx is allready running then die
#
service="nginx" ; servicealive
#
# Display title
#
clear ; title
#
# Install and configure Nginx
#
apt install -y nginx
nginxfiles="/etc/nginx/nginxsnippets"
nginxconf="cfg"
rm /var/www/html/index.nginx-debian.html > /dev/null 2>&1
rm /etc/nginx/snippets/snakeoil.conf > /dev/null 2>&1
mkdir -p /var/cache/nginx/fcgi
#
# Copy Nginx snippets to Nginx directory i.e snippet code blocks and custom error pages
#
cp -R nginxsnippets $nginxfiles
#
# Install php php-fpm and configure nginx with standard config files
#
apt install -y php-fpm php-curl php-dom php-gd php-imagick php-ldap php-mbstring php-mysql php-pear php-soap php-xml php-zip
#
# Configure Nginx
#
phpver=$(php -v |head -1 |awk -F'.' '{print $1"."$2}' |sed 's/PHP //')
phpfpm="php$phpver-fpm" ; phpclidir="/etc/php/$phpver/cli" ; phpfpmdir="/etc/php/$phpver/fpm" ; socket="php$phpver"
#
# Adjusting a few nginx config files to match the installed php version before moving them to the right place
#
sed -i "s/VERSION/$phpver/" $nginxconf/nginx.conf
sed -i "s/VERSION/$phpver/" $nginxconf/nginx.conf.high.perf
sed -i "s/VERSION/$phpver/" $nginxfiles/wphardening.conf
cp $phpclidir/php.ini $phpclidir/php.ini.orig
cp $phpfpmdir/php.ini $phpfpmdir/php.ini.orig
cp $nginxconf/nginx.conf /etc/nginx
cp $nginxconf/nginx.conf.high.perf /etc/nginx
cp $nginxconf/default /etc/nginx/sites-available/default
#
# Creating a high performance www.conf file for optional later use
#
cp /etc/php/$phpver/fpm/pool.d/www.conf /etc/nginx/www.conf.high.perf
sed -i "s/pm.max_children = 5/pm.max_children = 50/" /etc/nginx/www.conf.high.perf
sed -i "s/pm.start_servers = 2/pm.start_servers = 15/" /etc/nginx/www.conf.high.perf
sed -i "s/pm.max_spare_servers = 3/pm.max_spare_servers = 10/" /etc/nginx/www.conf.high.perf
sed -i "s/;pm.process_idle_timeout = 10s/pm.process_idle_timeout = 10s/" /etc/nginx/www.conf.high.perf
sed -i "s/;pm.process_idle_timeout = 10s/pm.process_idle_timeout = 10s/" /etc/nginx/www.conf.high.perf
sed -i "s/;pm.max_requests = 500/pm.max_requests = 500/" /etc/nginx/www.conf.high.perf
#
# Adjusting php.ini with stuff I usually forget
#
sed -i 's/;max_input_vars = 1000/max_input_vars = 3000/' $phpclidir/php.ini
sed -i 's/;max_input_vars = 1000/max_input_vars = 3000/' $phpfpmdir/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 20M/' $phpfpmdir/php.ini
sed -i 's/;date.timezone =/date.timezone = Europe\/Copenhagen/' $phpclidir/php.ini
sed -i 's/;date.timezone =/date.timezone = Europe\/Copenhagen/' $phpfpmdir/php.ini
#
# Create directory for static websites that don't change much
#
mkdir /etc/nginx/static-sites
#
# Restart Nginx and Php-Fpm
#
systemctl restart $phpfpm && systemctl restart nginx
#
# All done
#
clear ; printf "\n" ; line '-' ; printf "All Done...\n" ; line '-' ; printf "\n"
#
# End of script
#