#!/bin/bash
#Nifty: Install nginx with PHP-FPM 8.0 from Remi repository. CentOS 7

###

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_os centos7

NUMCPU=`cat /proc/cpuinfo  |grep "cpu MHz" |wc -l`
ce "Updating the system packages..."
yum update -y 

ce "Installing EPEL"
yum install epel-release yum-utils -y

ce "Installing remi"
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm

ce "Enabling PHP 8.0 repo"
yum-config-manager --enable remi-php80

ce "Installing required packages"
yum -y install php php-cli php-pdo php-opcache php-xml php-gd php-mbstring php-mcrypt php-soap php-mysqlnd php-bcmath php-pecl-zip vim php-fpm nginx

curl vim.dk >> /etc/vimrc

ce "Enabling nginx on startup"
systemctl enable nginx

ce "Enabling PHP on startup"
systemctl enable php-fpm

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


ce "Set up PHP-FPM"
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]
listen = 127.0.0.1:9000
;listen.owner = apache
;listen.group = apache
;listen.mode = 0660
user = apache
group = apache
listen.allowed_clients = 127.0.0.1
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 php-fpm


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

cat <<EOF > /etc/nginx/nginx.conf
user  nginx;
worker_processes  5;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
}


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

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

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

    server_tokens off;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    ##
    # 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;

    index   index.html index.htm;

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

    server {
        listen       80 default_server;
        server_name  localhost;
        root         /var/www/default;

        #charset koi8-r;

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

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        # redirect server error pages to the static page /40x.html
        error_page  404              /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }
    }
}
EOF

cat <<EOF > /etc/nginx/vhosts.d/domain.tld
upstream phpfpm {
  server 127.0.0.1:9000;
}

# redirect non-www to www.
#server {
#          listen 80;
#          server_name domain;
#          return       301 http://www.\$host\$request_uri;
#}

server {
        listen 80;
        server_name www.domain.tld;
        root /var/www/www.domain.tld/public_html;

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

        location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm|tar)\$ {
                expires 1y;
                access_log off;
                add_header Cache-Control "public";
        }

        location /
        {
                fastcgi_read_timeout 2600s;
                proxy_read_timeout 150;
                try_files \$uri \$uri/ /index.php?\$args;
                access_log off;
                expires max;
        }

        location ~* \.(jpeg\|jpg\|gif\|png\|css\|js\|ico\|swf)\$ {
                try_files \$uri \$uri/ @proxy;
                expires max;
                access_log off;
        }

        location @proxy {
                fastcgi_pass phpfpm;
        }

        location ~ \.php\$ {
                try_files \$uri =404;
                expires off;
                fastcgi_split_path_info ^(.+\.php)(/.+)\$;
                proxy_connect_timeout  2600s;
                proxy_send_timeout  2600s;
                proxy_read_timeout  2600s;
                fastcgi_send_timeout 2600s;
                fastcgi_read_timeout 2600s;
                fastcgi_pass phpfpm;
                fastcgi_pass_request_headers on;
                fastcgi_keep_conn off;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME \$document_root\${fastcgi_script_name};
        }
}
EOF


SIP=`hostname -I |sed 's/ /\n/g' |grep -v 127.0.0.1 |head -n1`

echo -en "\n\n\n"
ce "All done!

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











