#!/bin/bash
#Nifty: Install nginx with PHP-FPM 7.4 from Sury repository

curl -s https://nifty.sh/lib/universal/niftylib.sh > /tmp/lib.nifty.sh
. /tmp/lib.nifty.sh
rm -f /tmp/lib.nifty.sh

# Requirements
require_root
require_64bit
require_os debian10

ce "Updating system"
apt-get update 
apt-get dist-upgrade -y
apt-get -y install apt-transport-https lsb-release ca-certificates curl gnupg2


ce "Installing nginx repo"
echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list
curl -fsSL https://nginx.org/keys/nginx_signing.key | apt-key add -

ce "Installing Sury PHP repo"
curl -sSL -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list

apt-get update


ce "Installing required packages"
apt-get install -y nginx php7.4-cli php7.4-opcache php7.4-xml php7.4-gd php7.4-mbstring php7.4-soap php7.4-mysql php7.4-bcmath php7.4-fpm

curl vim.dk >> /etc/vim/vimrc

ce "Enabling nginx on startup"
systemctl enable nginx

ce "Enabling PHP on startup"
systemctl enable php7.4-fpm

ce "Applying php.ini BP"
cp /etc/php/7.4/fpm/php.ini /etc/php.ini
rm /etc/php/7.4/fpm/php.ini /etc/php/7.4/cli/php.ini
ln -s /etc/php.ini /etc/php/7.4/fpm/php.ini
ln -s /etc/php.ini /etc/php/7.4/cli/php.ini

ce "Doing php.ini customization"
sed -i '/^;/d' /etc/php.ini
sed -i '/^$/d' /etc/php.ini
sed -i 's/memory_limit = 128M/memory_limit = 512M/g' /etc/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 600/g' /etc/php.ini
sed -i 's/max_input_time = 60/max_input_time = 600/g' /etc/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 256M/g' /etc/php.ini
sed -i '3idate.timezone = "Europe/Copenhagen"' /etc/php.ini
sed -i '/disable_functions/d' /etc/php.ini

ce "Set up PHP-FPM"
ln -s /etc/php/7.4/fpm/pool.d /etc/php-fpm.d
cat <<EOF > /etc/php-fpm.d/www.conf
; Note! If you do not intend on using this file, then just empty it, do not delete it.
; otherwise yum will generate it again and php-fpm will break.
[www-data]
;listen = 127.0.0.1:9000
;listen.allowed_clients = 127.0.0.1

listen = /run/php-fpm-\$pool.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

user = \$pool
group = \$pool

pm = dynamic
pm.max_children = 500
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 150
pm.max_requests = 200
listen.backlog = -1
pm.status_path = /status
request_terminate_timeout = 2720s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
EOF


ce "Starting PHP-FPM"
systemctl restart php7.4-fpm

ce "Configuring nginx"
mkdir /etc/nginx/vhosts.d/

cat <<EOF > /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for" \$request_time \$upstream_response_time';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    ##
    # Gzip Settings
    ##
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 5;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript
    text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/vhosts.d/*.conf;

}
EOF

cat <<EOF > /etc/nginx/vhosts.d/0-default.conf
# redirect non-www to www.
#server {
#          listen 80;
#          server_name domain;
#          return       301 http://www.\$host\$request_uri;
#}

server {
        listen 80 default_server;
        # listen 443 ssl http2;
        server_name $(hostname);
        root /var/www/default;

        #include /etc/nginx/letsencrypt.common.conf;
        #ssl_certificate /etc/letsencrypt/live/www.domain.tld/fullchain.pem;
        #ssl_certificate_key /etc/letsencrypt/live/www.domain.tld/privkey.pem;
        #include /etc/nginx/ssl.common.conf;


        index index.html index.htm index.php;
        client_max_body_size 50m;
        proxy_connect_timeout  60s;
        proxy_send_timeout  60s;
        proxy_read_timeout  60s;
        fastcgi_send_timeout 60s;
        fastcgi_read_timeout 60s;

        location / {
                fastcgi_read_timeout 60s;
                proxy_read_timeout 150;
                try_files \$uri \$uri/ /index.php?\$args;
        }

        location ~ \.php\$ {
                try_files \$uri =404;
                include fastcgi_params;
                expires off;
                fastcgi_split_path_info ^(.+\.php)(/.+)\$;
                fastcgi_pass unix:/var/run/php-fpm-www-data.sock;
                fastcgi_pass_request_headers on;
                fastcgi_keep_conn off;
                fastcgi_param SCRIPT_FILENAME \$document_root\${fastcgi_script_name};
        }
}
EOF

mkdir -p /var/www/default
chown -R www-data: /var/www/default

ce "All done!

NOTE: If you need mysql, use the mysql installation script
"


