From 1ec71fb0d74292cb38191de7954c89d7615088d8 Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Mon, 5 Sep 2022 16:08:39 -0700 Subject: [PATCH 01/17] Adjust some mod_pagespeed settings to work with virtual dom apps better --- config/nginx/mod_pagespeed | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config/nginx/mod_pagespeed b/config/nginx/mod_pagespeed index f38be11..df0e6c9 100644 --- a/config/nginx/mod_pagespeed +++ b/config/nginx/mod_pagespeed @@ -2,6 +2,7 @@ pagespeed on; pagespeed RewriteLevel PassThrough; pagespeed FetchHttps enable; pagespeed EnableFilters add_head; +pagespeed CriticalImagesBeaconEnabled false; # pagespeed EnableFilters combine_css; # pagespeed EnableFilters rewrite_css; # pagespeed EnableFilters fallback_rewrite_css_urls; @@ -36,9 +37,9 @@ pagespeed EnableFilters rewrite_images; #pagespeed EnableFilters insert_image_dimensions; pagespeed EnableFilters inline_images; pagespeed EnableFilters recompress_images; -pagespeed EnableFilters recompress_jpeg; -pagespeed EnableFilters recompress_png; -pagespeed EnableFilters recompress_webp; +#pagespeed EnableFilters recompress_jpeg; +#pagespeed EnableFilters recompress_png; +#pagespeed EnableFilters recompress_webp; #pagespeed EnableFilters convert_gif_to_png; pagespeed EnableFilters strip_image_color_profile; pagespeed EnableFilters strip_image_meta_data; From f51cb53bfe05a5dff3c482fda6be2eda859246c0 Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Wed, 5 Oct 2022 17:18:45 -0700 Subject: [PATCH 02/17] Update mod_pagespeed to respect x-forwarded-proto, to support SSL offloading --- config/nginx/mod_pagespeed | 1 + 1 file changed, 1 insertion(+) diff --git a/config/nginx/mod_pagespeed b/config/nginx/mod_pagespeed index df0e6c9..5b777d0 100644 --- a/config/nginx/mod_pagespeed +++ b/config/nginx/mod_pagespeed @@ -3,6 +3,7 @@ pagespeed RewriteLevel PassThrough; pagespeed FetchHttps enable; pagespeed EnableFilters add_head; pagespeed CriticalImagesBeaconEnabled false; +pagespeed RespectXForwardedProto on; # pagespeed EnableFilters combine_css; # pagespeed EnableFilters rewrite_css; # pagespeed EnableFilters fallback_rewrite_css_urls; From eec3d2c840356efe86e99e12148459c9ed06ec63 Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Mon, 7 Aug 2023 11:51:00 -0700 Subject: [PATCH 03/17] Add PHP_VERSION handling for redis installation --- install/components/redis/install | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/install/components/redis/install b/install/components/redis/install index 8680b5d..c575ee8 100755 --- a/install/components/redis/install +++ b/install/components/redis/install @@ -4,6 +4,12 @@ REDIS_CONF=$( Date: Tue, 8 Aug 2023 18:05:05 -0700 Subject: [PATCH 04/17] Fix redis PHP_VERSION config path --- install/components/redis/install | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install/components/redis/install b/install/components/redis/install index c575ee8..95d6877 100755 --- a/install/components/redis/install +++ b/install/components/redis/install @@ -5,9 +5,9 @@ REDIS_CONF=$( Date: Tue, 8 Aug 2023 18:08:23 -0700 Subject: [PATCH 05/17] Fix redis PHP VERSION config path, harder. --- install/components/redis/install | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install/components/redis/install b/install/components/redis/install index 95d6877..f796db5 100755 --- a/install/components/redis/install +++ b/install/components/redis/install @@ -5,9 +5,9 @@ REDIS_CONF=$( Date: Mon, 18 Sep 2023 18:59:37 -0700 Subject: [PATCH 06/17] Ensure nginx web root paths work in setup-site and install-cert, with slashes too --- config/site.php-fpm.conf | 24 +++++++-------- scripts/install-cert.sh | 64 +++++++++++++++++++++++++++++++++++++++- scripts/setup-site | 33 ++++++++++++++++++++- 3 files changed, 107 insertions(+), 14 deletions(-) diff --git a/config/site.php-fpm.conf b/config/site.php-fpm.conf index 41aded1..3f7b493 100644 --- a/config/site.php-fpm.conf +++ b/config/site.php-fpm.conf @@ -7,12 +7,12 @@ group = www-data catch_workers_output = yes clear_env = no pm = static -pm.max_children = 20 -pm.start_servers = 20 -pm.min_spare_servers = 20 -pm.max_spare_servers = 20 -pm.max_requests = 1000 -request_terminate_timeout = 1200 +pm.max_children = 10 +pm.start_servers = 2 +pm.min_spare_servers = 2 +pm.max_spare_servers = 3 +pm.max_requests = 500 +request_terminate_timeout = 300 env[DB_HOST]="DATABASEHOST" env[DB_PORT]="DATABASEPORT" @@ -49,12 +49,12 @@ group = www-data catch_workers_output = yes clear_env = no pm = static -pm.max_children = 20 -pm.start_servers = 20 -pm.min_spare_servers = 20 -pm.max_spare_servers = 20 -pm.max_requests = 1000 -request_terminate_timeout = 1200 +pm.max_children = 10 +pm.start_servers = 2 +pm.min_spare_servers = 2 +pm.max_spare_servers = 3 +pm.max_requests = 500 +request_terminate_timeout = 300 env[DB_HOST]="DATABASEHOST" env[DB_PORT]="DATABASEPORT" diff --git a/scripts/install-cert.sh b/scripts/install-cert.sh index d581cb2..5632bfa 100755 --- a/scripts/install-cert.sh +++ b/scripts/install-cert.sh @@ -1,3 +1,58 @@ +#!/usr/bin/env bash + +# Define Funcs +extract_webroot_path() { + local domain="$1" + local nginx_conf_path="$2" + + # Ensure domain and nginx_conf_path are provided + if [[ -z "$domain" || -z "$nginx_conf_path" ]]; then + echo "Error: Either domain or nginx_conf_path is not provided." + return 1 + fi + + local result=$(grep -o "root /var/www/$domain[^;]*;" $nginx_conf_path) + + local webrootpath="${result#*"$domain"}" + webrootpath="${webrootpath%;}" + + # Check if webrootpath starts with /current and remove it if it does + if [[ $webrootpath == /current* ]]; then + webrootpath="${webrootpath#/current}" + fi + + # Print the webrootpath + echo "$webrootpath" +} + +fix_root_path() { + local domain="$1" + local nginx_conf_path="$2" + + # Ensure domain and nginx_conf_path are provided + if [[ -z "$domain" || -z "$nginx_conf_path" ]]; then + echo "Error: Either domain or nginx_conf_path is not provided." + return 1 + fi + + # Read the 'root' line from the provided nginx configuration file + local root_line=$(sed -n '/^[ \t]*root [^;]*;/p' "$nginx_conf_path") + + printf "========== ROOT LINE\n" + printf "$root_line\n" + + # Remove double slashes and trailing slash before the semicolon + local modified_line=$(echo "$root_line" | sed 's#//\+#/#g' | sed 's#/\(;\)$#\1#') + printf "========== MODIFIED ROOT LINE\n" + printf "$modified_line\n" + + # Use sed to replace root_line with modified_line in the provided nginx configuration file + sed -i "s#$(echo "$root_line" | sed 's#[\&/\.]#\\&#g')#$modified_line#g" "$nginx_conf_path" + + printf "========== AFTER SED\n" + cat "$nginx_conf_path" +} + # check for certbot if ! [ -x "$(command -v certbot)" ]; then sudo apt-get update @@ -95,6 +150,9 @@ then DOMAIN_COM=$(cat tempdomain.txt) rm tempdomain.txt + # Get Web Root Path for replacing in nginx config + WEBROOTPATH=$(extract_webroot_path "$DOMAIN" "/etc/nginx/sites-available/$DOMAIN.conf") + rm /etc/nginx/sites-available/"$DOMAIN.conf" # NGINX - Proxy or not to proxy? @@ -110,9 +168,10 @@ then fi cd /etc/nginx/sites-available/ + sed -i "s/SITEDOTCOM/${DOMAIN}/g;" "$DOMAIN.conf" sed -i "s/SITE_COM/${DOMAIN_COM}/g;" "$DOMAIN.conf" - sed -i "s/WEBROOTPATH/${WEBROOTPATH}/g;" "$DOMAIN.conf" + sed -i "s|WEBROOTPATH|${WEBROOTPATH}|g;" "$DOMAIN.conf" # nginx - handle current path stuff - # if /var/www/$DOMAIN/current exists, need to sed nginx config to swap paths @@ -121,6 +180,9 @@ then sed -i "/var\/www\/${DOMAIN}\/current/!s/var\/www\/${DOMAIN}\//var\/www\/${DOMAIN}\/current\//g" "$DOMAIN.conf" fi + # Fix possible issues in root path + fix_root_path "$DOMAIN" "$DOMAIN.conf" + # This may already exist... ln -s /etc/nginx/sites-available/"${DOMAIN}.conf" /etc/nginx/sites-enabled/ diff --git a/scripts/setup-site b/scripts/setup-site index aa78039..b487270 100755 --- a/scripts/setup-site +++ b/scripts/setup-site @@ -202,6 +202,34 @@ while [[ $# -gt 0 ]]; do esac done +fix_root_path() { + local domain="$1" + local nginx_conf_path="$2" + + # Ensure domain and nginx_conf_path are provided + if [[ -z "$domain" || -z "$nginx_conf_path" ]]; then + echo "Error: Either domain or nginx_conf_path is not provided." + return 1 + fi + + # Read the 'root' line from the provided nginx configuration file + local root_line=$(sed -n '/^[ \t]*root [^;]*;/p' "$nginx_conf_path") + + printf "========== ROOT LINE\n" + printf "$root_line\n" + + # Remove double slashes and trailing slash before the semicolon + local modified_line=$(echo "$root_line" | sed 's#//\+#/#g' | sed 's#/\(;\)$#\1#') + printf "========== MODIFIED ROOT LINE\n" + printf "$modified_line\n" + + # Use sed to replace root_line with modified_line in the provided nginx configuration file + sed -i "s#$(echo "$root_line" | sed 's#[\&/\.]#\\&#g')#$modified_line#g" "$nginx_conf_path" + + printf "========== AFTER SED\n" + cat "$nginx_conf_path" +} + ## SSH_PATH # if OWNER_USER is the same as whoami, then we can use the ssh keys in ${SSH_PATH} if [ "$OWNER_USER" == "$(whoami)" ]; then @@ -433,7 +461,7 @@ apply_nginx_config() { cd /etc/nginx/sites-available/ sed -i "s/SITEDOTCOM/${DOMAIN}/g" "$DOMAIN.conf" sed -i "s/SITE_COM/${DOMAIN_COM}/g" "$DOMAIN.conf" - sed -i "s/WEBROOTPATH/${WEB_ROOT_PATH}/g" "$DOMAIN.conf" + sed -i "s|WEBROOTPATH|${WEB_ROOT_PATH}|g" "$DOMAIN.conf" ln -s /etc/nginx/sites-available/"${DOMAIN}.conf" /etc/nginx/sites-enabled/ # if we have DEPLOY_SUBFOLDER and therefore a "current" directory - then we need to sed some stuff in nginx @@ -441,6 +469,9 @@ apply_nginx_config() { # For lines that do not contain '/var/www/${DOMAIN}/current', replace '/var/www/${DOMAIN}/' with '/var/www/${DOMAIN}/current/' sed -i "/var\/www\/${DOMAIN}\/current/!s/var\/www\/${DOMAIN}\//var\/www\/${DOMAIN}\/current\//g" "$DOMAIN.conf" fi + + # fix root path + fix_root_path "$DOMAIN" "$DOMAIN.conf" } # (only) if NGINX_SITE_CONF_PATH is empty, does NGINX_WITH_PHP matter in determining the default used: From b70359ff1c9b61cce231ebe7fa405dfc6678dcd4 Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Mon, 18 Sep 2023 20:06:47 -0700 Subject: [PATCH 07/17] Fix typo with setup-site referring to site.nginx.vueapp.conf incorrectly; remove debug outputs --- README.md | 4 ++-- scripts/install-cert.sh | 12 ++++++------ scripts/setup-site | 18 +++++++++--------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 88e292c..a700547 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ The following scripts are used "per site" that you want to setup on your server. --deploy-key-private-file=mysite-deploy-key --php-pools=true --nginx-with-php=true - --nginx-site-conf-path=/var/www/LEMP-setup-guide/config/site.nginx.conf (or site.vueapp.nginx.conf) + --nginx-site-conf-path=/var/www/LEMP-setup-guide/config/site.nginx.conf (or site.nginx.vueapp.conf) --php-with-mysql=true --php-site-conf-path=/var/www/LEMP-setup-guide/config/site.php-fpm.conf --mysql-create-db=true @@ -155,7 +155,7 @@ The following scripts are used "per site" that you want to setup on your server. | `--deploy-key-private-file=PATH`| Specify the path to the private deploy key file | None | | `--php-pools` | Specify if you want to set up PHP pools. Possible values: `true` or `false` | `false` | | `--nginx-with-php` | Specify if you want to set up Nginx with PHP upstreams. Possible values: `true` or `false` | `false` | - | `--nginx-site-conf-path=PATH` | Specify the path to the Nginx site conf file | `/var/www/LEMP-setup-guide/config/site.nginx.conf` (or `site.vueapp.nginx.conf`) | + | `--nginx-site-conf-path=PATH` | Specify the path to the Nginx site conf file | `/var/www/LEMP-setup-guide/config/site.nginx.conf` (or `site.nginx.vueapp.conf`) | | `--php-with-mysql` | Specify if you want to set up PHP with MySQL env vars. Possible values: `true` or `false` | `false` | | `--php-site-conf-path=PATH` | Specify the path to the PHP site conf file | `/var/www/LEMP-setup-guide/config/site.php-fpm.conf` | | `--mysql-create-db` | Specify if you want to set up a MySQL database. Possible values: `true` or `false` | `false` | diff --git a/scripts/install-cert.sh b/scripts/install-cert.sh index 5632bfa..b4f83d2 100755 --- a/scripts/install-cert.sh +++ b/scripts/install-cert.sh @@ -38,19 +38,19 @@ fix_root_path() { # Read the 'root' line from the provided nginx configuration file local root_line=$(sed -n '/^[ \t]*root [^;]*;/p' "$nginx_conf_path") - printf "========== ROOT LINE\n" - printf "$root_line\n" + # printf "========== ROOT LINE\n" + # printf "$root_line\n" # Remove double slashes and trailing slash before the semicolon local modified_line=$(echo "$root_line" | sed 's#//\+#/#g' | sed 's#/\(;\)$#\1#') - printf "========== MODIFIED ROOT LINE\n" - printf "$modified_line\n" + # printf "========== MODIFIED ROOT LINE\n" + # printf "$modified_line\n" # Use sed to replace root_line with modified_line in the provided nginx configuration file sed -i "s#$(echo "$root_line" | sed 's#[\&/\.]#\\&#g')#$modified_line#g" "$nginx_conf_path" - printf "========== AFTER SED\n" - cat "$nginx_conf_path" + # printf "========== AFTER SED\n" + # cat "$nginx_conf_path" } # check for certbot diff --git a/scripts/setup-site b/scripts/setup-site index b487270..d131479 100755 --- a/scripts/setup-site +++ b/scripts/setup-site @@ -27,7 +27,7 @@ ORIGINAL_ARG_COUNT=$# # --deploy-key-private-file=path-to-key \ # --php-pools=true \ # --nginx-with-php=true \ -# --nginx-site-conf-path=/var/www/LEMP-setup-guide/config/site.nginx.conf (or site.vueapp.nginx.conf) \ +# --nginx-site-conf-path=/var/www/LEMP-setup-guide/config/site.nginx.conf (or site.nginx.vueapp.conf) \ # --php-with-mysql=true \ # --php-site-conf-path=/var/www/LEMP-setup-guide/config/site.php-fpm.conf \ # --mysql-create-db=true \ @@ -75,7 +75,7 @@ help_message() { printf " --deploy-key-private-file=PATH Specify the path to the private deploy key file\n" printf " --php-pools=true|false Specify if you want to setup php pools; default is false\n" printf " --nginx-with-php=true|false Specify if you want to setup nginx with php upstreams; default is false\n" - printf " --nginx-site-conf-path=PATH Specify the path to the nginx site conf file; default is /var/www/LEMP-setup-guide/config/site.nginx.conf (or site.vueapp.nginx.conf)\n" + printf " --nginx-site-conf-path=PATH Specify the path to the nginx site conf file; default is /var/www/LEMP-setup-guide/config/site.nginx.conf (or site.nginx.vueapp.conf)\n" printf " --php-with-mysql=true|false Specify if you want to setup php with mysql env vars; default is false\n" printf " --php-site-conf-path=PATH Specify the path to the php site conf file; default is /var/www/LEMP-setup-guide/config/site.php-fpm.conf\n" printf " --mysql-create-db=true|false Specify if you want to setup mysql database; default is false\n" @@ -215,19 +215,19 @@ fix_root_path() { # Read the 'root' line from the provided nginx configuration file local root_line=$(sed -n '/^[ \t]*root [^;]*;/p' "$nginx_conf_path") - printf "========== ROOT LINE\n" - printf "$root_line\n" + # printf "========== ROOT LINE\n" + # printf "$root_line\n" # Remove double slashes and trailing slash before the semicolon local modified_line=$(echo "$root_line" | sed 's#//\+#/#g' | sed 's#/\(;\)$#\1#') - printf "========== MODIFIED ROOT LINE\n" - printf "$modified_line\n" + # printf "========== MODIFIED ROOT LINE\n" + # printf "$modified_line\n" # Use sed to replace root_line with modified_line in the provided nginx configuration file sed -i "s#$(echo "$root_line" | sed 's#[\&/\.]#\\&#g')#$modified_line#g" "$nginx_conf_path" - printf "========== AFTER SED\n" - cat "$nginx_conf_path" + # printf "========== AFTER SED\n" + # cat "$nginx_conf_path" } ## SSH_PATH @@ -481,7 +481,7 @@ if [ -z "$NGINX_SITE_CONF_PATH" ]; then NGINX_SITE_CONF_PATH="/var/www/LEMP-setup-guide/config/site.nginx.conf" else # use vueapp version - NGINX_SITE_CONF_PATH="/var/www/LEMP-setup-guide/config/site.vueapp.nginx.conf" + NGINX_SITE_CONF_PATH="/var/www/LEMP-setup-guide/config/site.nginx.vueapp.conf" fi fi From f902694b7b49a9541a23f37308631b57a46ce7e6 Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Tue, 19 Sep 2023 11:14:07 -0700 Subject: [PATCH 08/17] Ensure in setup-site that nginx w/ php streams is prompted for --- config/site.nginx.proxy.conf | 2 +- scripts/setup-site | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/config/site.nginx.proxy.conf b/config/site.nginx.proxy.conf index 0ea53fa..4aa4c8d 100644 --- a/config/site.nginx.proxy.conf +++ b/config/site.nginx.proxy.conf @@ -6,7 +6,7 @@ server { server { listen 80; - server_name SITEDOTCOM www.SITEDOTCOM; + server_name SITEDOTCOM; root /var/www/SITEDOTCOM/WEBROOTPATH; index index.php index.html; diff --git a/scripts/setup-site b/scripts/setup-site index d131479..4ba5d66 100755 --- a/scripts/setup-site +++ b/scripts/setup-site @@ -49,7 +49,7 @@ ORIGINAL_USER=$(whoami) DEPLOY_KEY_PUBLIC=false DEPLOY_KEY_PRIVATE=false PHP_POOLS=false -NGINX_WITH_PHP=false +NGINX_WITH_PHP="" NGINX_SITE_CONF_PATH="" PHP_WITH_MYSQL=false PHP_SITE_CONF_PATH="" @@ -143,6 +143,10 @@ while [[ $# -gt 0 ]]; do ;; --nginx-with-php=*) NGINX_WITH_PHP="${key#*=}" + # make sure it is either true or false if passed + if [ "$NGINX_WITH_PHP" != "true" ] && [ "$NGINX_WITH_PHP" != "false" ]; then + NGINX_WITH_PHP=false + fi shift ;; --nginx-site-conf-path=*) From 79a2ddd7bbc22722453953aace92aa30ac9f071d Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Tue, 19 Sep 2023 11:19:07 -0700 Subject: [PATCH 09/17] Fix typos in nginx-with-php variable setting from prompt --- scripts/setup-site | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/setup-site b/scripts/setup-site index 4ba5d66..6fe0317 100755 --- a/scripts/setup-site +++ b/scripts/setup-site @@ -453,9 +453,9 @@ if [ -z "$NGINX_WITH_PHP" ]; then read -p "Do you need to setup nginx with upstreams and php?" -n 1 -r echo # (optional) move to a new line if [[ $REPLY =~ ^[Yy]$ ]]; then - $NGINX_WITH_PHP = true + NGINX_WITH_PHP=true else - $NGINX_WITH_PHP = false + NGINX_WITH_PHP=false fi fi From f09302572c523cc2e3ed6f83e78cbdbb55cda87d Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Tue, 19 Sep 2023 11:22:16 -0700 Subject: [PATCH 10/17] Ensure in setup-site that php pools is prompted for and fix typo with setting them --- scripts/setup-site | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/scripts/setup-site b/scripts/setup-site index 6fe0317..66c5eaf 100755 --- a/scripts/setup-site +++ b/scripts/setup-site @@ -48,7 +48,7 @@ OWNER_USER=$(whoami) ORIGINAL_USER=$(whoami) DEPLOY_KEY_PUBLIC=false DEPLOY_KEY_PRIVATE=false -PHP_POOLS=false +PHP_POOLS="" NGINX_WITH_PHP="" NGINX_SITE_CONF_PATH="" PHP_WITH_MYSQL=false @@ -139,6 +139,10 @@ while [[ $# -gt 0 ]]; do ;; --php-pools=*) PHP_POOLS="${key#*=}" + # make sure it is either true or false if passed + if [ "$PHP_POOLS" != "true" ] && [ "$PHP_POOLS" != "false" ]; then + PHP_POOLS=false + fi shift ;; --nginx-with-php=*) @@ -505,9 +509,9 @@ if [ -z "$PHP_POOLS" ]; then read -p "Do you need to set up PHP FPM pools?" -n 1 -r echo # (optional) move to a new line if [[ $REPLY =~ ^[Yy]$ ]]; then - $PHP_POOLS = true + PHP_POOLS=true else - $PHP_POOLS = false + PHP_POOLS=false fi fi From 0ca925a4fe83f286dd0b9701342d1dbb0f64821c Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Tue, 19 Sep 2023 11:25:31 -0700 Subject: [PATCH 11/17] Ensure setup-site prompts for php-mysql and create-mysql-db and fixes typos --- scripts/setup-site | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/setup-site b/scripts/setup-site index 66c5eaf..5dcca69 100755 --- a/scripts/setup-site +++ b/scripts/setup-site @@ -51,9 +51,9 @@ DEPLOY_KEY_PRIVATE=false PHP_POOLS="" NGINX_WITH_PHP="" NGINX_SITE_CONF_PATH="" -PHP_WITH_MYSQL=false +PHP_WITH_MYSQL="" PHP_SITE_CONF_PATH="" -MYSQL_CREATE_DB=false +MYSQL_CREATE_DB="" MYSQL_ROOT_USER="" MYSQL_ROOT_PASS="" DATABASE_NAME="" @@ -159,6 +159,10 @@ while [[ $# -gt 0 ]]; do ;; --php-with-mysql=*) PHP_WITH_MYSQL="${key#*=}" + # make sure it is either true or false if passed + if [ "$PHP_WITH_MYSQL" != "true" ] && [ "$PHP_WITH_MYSQL" != "false" ]; then + PHP_WITH_MYSQL=false + fi shift ;; --php-site-conf-path=*) @@ -167,6 +171,10 @@ while [[ $# -gt 0 ]]; do ;; --mysql-create-db=*) MYSQL_CREATE_DB="${key#*=}" + # make sure it is either true or false if passed + if [ "$MYSQL_CREATE_DB" != "true" ] && [ "$MYSQL_CREATE_DB" != "false" ]; then + MYSQL_CREATE_DB=false + fi shift ;; --mysql-root-user=*) @@ -543,9 +551,9 @@ if [ -z "$PHP_WITH_MYSQL" ]; then read -p "Do you want to setup PHP environmental variables for a mysql database? " -n 1 -r echo # (optional) move to a new line if [[ $REPLY =~ ^[Yy]$ ]]; then - $PHP_WITH_MYSQL = true + PHP_WITH_MYSQL=true else - $PHP_WITH_MYSQL = false + PHP_WITH_MYSQL=false fi fi @@ -594,9 +602,9 @@ then read -p "Do you want to setup a local mysql database using the info for php env vars above? " -n 1 -r echo # (optional) move to a new line if [[ $REPLY =~ ^[Yy]$ ]]; then - $MYSQL_CREATE_DB = true + MYSQL_CREATE_DB=true else - $MYSQL_CREATE_DB = false + MYSQL_CREATE_DB=false fi fi From b4a0de17f01a4297d09b3a37e775aaa066e8a014 Mon Sep 17 00:00:00 2001 From: Angela Murrell Date: Wed, 4 Oct 2023 11:44:24 -0700 Subject: [PATCH 12/17] Fix issue in setup-site script with dots in repo names --- scripts/setup-site | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/setup-site b/scripts/setup-site index 5dcca69..0c5b2a1 100755 --- a/scripts/setup-site +++ b/scripts/setup-site @@ -317,7 +317,7 @@ fi # Extract repo name from the github URL get_repo_name() { local url="$1" - echo "$url" | cut -d'/' -f 2 | cut -d'.' -f 1 + echo "$url" | awk -F/ '{print $2}' | sed 's/\.git$//' } # Add to ssh config file to ensure this host uses the correct key for the repo From c7dfd6fbbab29b52a0155c86609e3654e39dfc6e Mon Sep 17 00:00:00 2001 From: Grey Vugrin Date: Tue, 28 Nov 2023 12:16:10 -0800 Subject: [PATCH 13/17] Added PHP upgrade script and README --- .../upgrade-php/UPGRADE_PHP_README.md | 49 +++++ install/components/upgrade-php/upgrade-php.sh | 193 ++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 install/components/upgrade-php/UPGRADE_PHP_README.md create mode 100755 install/components/upgrade-php/upgrade-php.sh diff --git a/install/components/upgrade-php/UPGRADE_PHP_README.md b/install/components/upgrade-php/UPGRADE_PHP_README.md new file mode 100644 index 0000000..6b394c2 --- /dev/null +++ b/install/components/upgrade-php/UPGRADE_PHP_README.md @@ -0,0 +1,49 @@ +# Upgrade PHP + +## Overview + +This script will upgrade PHP from one version to another, and make all changes that LEMP setup guide makes to the PHP and PHP-FPM configs as part of the initial/per-site setup. + +> Note: +> - The script must be run as root (`sudo su` first if you're not already root) +> - It will exit on the first error it encounters +> - It assumes that at least 1 site has already been set up. You can run the LEMP setup guide to set up a site. + +It will do the following: + +1. Install new versions of PHP and php-fpm if they don't already exist +1. Install all php packages +1. Enable all php extensions +1. Make edits to PHP.ini file +1. Make edits to www.conf file if needed +1. Copy all php-fpm configs but www.conf over to the new version +1. Loop through each file in /etc/nginx/sites-enabled. Per site: + - Update php-fpm config + - Test php-fpm config + - Check that new php-fpm sockets have been created + - Update nginx site conf to point to new socket + - Test nginx config +1. Update nginx fastcgicache conf to point to a new global socket +1. Test nginx config and reload (this sets the new php version live) +1. Update default PHP version +1. Output next steps + +## Usage + +```sh +upgrade-php.sh OLD_VERSION NEW_VERSION +``` + +Example: upgrade-php.sh 8.0 8.2 + +> Note: +> - Set $DEBUG for a verbose output - $DEBUG=true ./upgrade-php.sh [options] +> - You can also run it without args to see the usage info: ./upgrade-php.sh + +## Troubleshooting + +- If you're having issues with the script, try running it with the $DEBUG variable set to true. This will output more verbose information about what the script is doing. + +```sh +$DEBUG=true ./upgrade-php.sh OLD_VERSION NEW_VERSION +``` diff --git a/install/components/upgrade-php/upgrade-php.sh b/install/components/upgrade-php/upgrade-php.sh new file mode 100755 index 0000000..35082ac --- /dev/null +++ b/install/components/upgrade-php/upgrade-php.sh @@ -0,0 +1,193 @@ +set -e + +# function to script_echo something in purple +function script_echo() { + echo -e "\033[35m$1\033[0m" +} + +# check that user is root +if [ ! $EUID = 0 ]; then + script_echo "Please run this script as root!" + exit 1 +fi + + +# If no args were passed, script_echo out some helpful info +if [ $# -eq 0 ]; then + script_echo "Usage: upgrade-php.sh OLD_VERSION NEW_VERSION" + script_echo "Example: upgrade-php.sh 8.0 8.2" + script_echo "Notes:" + script_echo '- Set $DEBUG for a verbose output - $DEBUG=true ./upgrade-php.sh [options]' + exit 1 +fi + +# For debugging the script +# set -v if +if [ ! -z "$DEBUG" ]; then + set -v +fi + +# Validate that the user passed at least two args +if [ $# -lt 2 ]; then + script_echo "Please pass the version args (OLD_VERSION NEW_VERSION)" + script_echo "Example: upgrade-php.sh 8.0 8.2" + exit 1 +fi + +# Validate that the version args are valid +# they should be numbers with a decimal point +if [[ ! $1 =~ ^[0-9]+(\.[0-9]+)?$ ]] || [[ ! $2 =~ ^[0-9]+(\.[0-9]+)?$ ]]; then + script_echo "Please pass valid version args" + script_echo "Example: upgrade-php.sh 8.0 8.2" + exit 1 +fi + +OLD_PHP_VERSION=$1 +NEW_PHP_VERSION=$2 + +# Install new versions of PHP and php-fpm but only if they don't already exist +if [ -f /usr/bin/php${NEW_PHP_VERSION} ]; then + script_echo "â„šī¸ php${NEW_PHP_VERSION} already exists, skipping install" +else + script_echo "đŸ“Ļ Installing php${NEW_PHP_VERSION}" + sudo apt-get update + sudo apt-get -y --no-install-recommends install php${NEW_PHP_VERSION} +fi + +if [ -f /usr/sbin/php-fpm${NEW_PHP_VERSION} ]; then + script_echo "â„šī¸ php-fpm${NEW_PHP_VERSION} already exists, skipping install" +else + script_echo "đŸ“Ļ Installing php-fpm${NEW_PHP_VERSION}" + sudo apt-get update + sudo apt-get -y --no-install-recommends install php${NEW_PHP_VERSION}-fpm +fi + +# Install all php packages if they don't already exist +script_echo "đŸ“Ļ Installing php packages" +sudo apt-get update && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-curl && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-mysql && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-dev && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-zip && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-mbstring && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-memcached && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-pgsql && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-xml && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-intl && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-redis && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-bcmath && \ + sudo apt-get -y install php${NEW_PHP_VERSION}-gd + +# make edits to PHP.ini file +script_echo "🖊 Editing php.ini" +sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/${NEW_PHP_VERSION}/fpm/php.ini + +# Enable all php extensions +script_echo "đŸ“Ļ Enabling all php extensions" +phpenmod -v $NEW_PHP_VERSION mbstring +phpenmod -v $NEW_PHP_VERSION curl +phpenmod -v $NEW_PHP_VERSION xml +phpenmod -v $NEW_PHP_VERSION xmlreader +phpenmod -v $NEW_PHP_VERSION simplexml +phpenmod -v $NEW_PHP_VERSION gd + +# TODO: Why are there not more of these to enable? Compared to number that we installed + +# make edits to www.conf file if needed +PHP_WWW_CONF=/etc/php/${NEW_PHP_VERSION}/fpm/pool.d/www.conf +if [ ! -f $PHP_WWW_CONF ]; then + script_echo "â„šī¸ www.conf not found, skipping edits" +else + script_echo "🖊 Editing www.conf" + sed -i "s/^;listen.owner = www-data/listen.owner = www-data/g" $PHP_WWW_CONF + sed -i "s/^;listen.group = www-data/listen.group = www-data/g" $PHP_WWW_CONF + sed -i "s/^;listen.mode = 0660/listen.mode = 0660/" $PHP_WWW_CONF +fi + +# Copy all php-fpm configs but www.conf over to the new version +ls /etc/php/${OLD_PHP_VERSION}/fpm/pool.d/ | grep -v www.conf | while read -r line; do + # Copy the file to the new version + script_echo "đŸ“Ļ Copying php-fpm configs for site pool: ${line}" + sudo cp /etc/php/${OLD_PHP_VERSION}/fpm/pool.d/${line} /etc/php/${NEW_PHP_VERSION}/fpm/pool.d/${line} +done + +# loop through each file in sites-enabled +ls /etc/nginx/sites-enabled/ | while read -r line; do + NGINX_SITE_CONF=${line} + + # Remove the .conf from the end of the file name and save to SITE_NAME + SITE_NAME=${line%.conf} + + # script_echo which file we're on + script_echo "đŸĻž Processing site: ${SITE_NAME}" + + # Get the socket name from inside the nginx file. use that to replace the socket name in the php-fpm file + # This is the old socket name (get everything between unix: and .sock), and add .sock to the end + OLD_SOCKET_PREFIX=$(cat /etc/nginx/sites-enabled/${NGINX_SITE_CONF} | grep "unix:" | head -n 1 | grep -oP '(?<=unix:).*(?=sock)')sock + # replace / with \/ to get a sed friendly string + OLD_SOCKET_GLOB=$(echo ${OLD_SOCKET_PREFIX} | sed 's/\//\\\//g') + + NEW_SOCKET_PREFIX="/var/run/php${NEW_PHP_VERSION}-fpm.${SITE_NAME}.sock" + # replace / with \/ to get a sed friendly string + NEW_SOCKET_GLOB=$(echo ${NEW_SOCKET_PREFIX} | sed 's/\//\\\//g') + + # Do a recursive grep in pool.d to find all the files that have the old socket name + grep -rl ${OLD_SOCKET_GLOB} /etc/php/${NEW_PHP_VERSION}/fpm/pool.d/ | while read -r phpconfig; do + # Update php-fpm config + script_echo "🖊 Editing php-fpm config for site php-fpm config: ${phpconfig}" + sudo sed -i "s/^listen = ${OLD_SOCKET_GLOB}/listen = ${NEW_SOCKET_GLOB}/g" ${phpconfig} + done + + # Test php-fpm config and restart + script_echo "đŸ•ĩī¸â€â™€ī¸ Testing php-fpm config and restarting" + sudo php-fpm${NEW_PHP_VERSION} -t && sudo service php${NEW_PHP_VERSION}-fpm restart + + # check that sockets have been created, exit if not + # there should be 2, sock1 and sock2 + # pattern is /var/run/php${NEW_PHP_VERSION}-fpm.${SITE_NAME}.sock(1 or 2) + # the -S flag checks if the file exists and is a socket. -f fails bc it's not a regular file + script_echo "đŸ•ĩī¸â€â™€ī¸ Checking for php-fpm sockets" + if [ ! -S ${NEW_SOCKET_PREFIX}1 ] || [ ! -S ${NEW_SOCKET_PREFIX}2 ]; then + script_echo "Missing php-fpm sockets" + script_echo "Expected: ${NEW_SOCKET_PREFIX}1" + script_echo "Expected: ${NEW_SOCKET_PREFIX}2" + exit 1 + fi + + # update nginx site conf to point to new socket + # Note! We always want to edit sites-available, not sites-enabled. + # sites-enabled containts symlinks to sites-available. Making changes there would break the symlink + script_echo "🖊 Editing nginx site conf" + sudo sed -i "s/unix:${OLD_SOCKET_GLOB}/unix:${NEW_SOCKET_GLOB}/g" /etc/nginx/sites-available/${NGINX_SITE_CONF} + + # test nginx config + script_echo "đŸ•ĩī¸â€â™€ī¸ Testing nginx config" + sudo nginx -t +done + +# update nginx fastcgicache conf to point to a new global socket +# replace any socket that starts with /var/run/php*.sock +# with /var/run/php${NEW_PHP_VERSION}-fpm.sock +if [ ! -f /etc/nginx/conf/fastcgicache.conf ]; then + script_echo "â„šī¸ fastcgicache.conf not found, skipping edits" +else + script_echo "🖊 Editing nginx fastcgicache conf" + # sudo sed -i "s/unix:${OLD_SOCKET_GLOB}/unix:${NEW_SOCKET_GLOB}/g" /etc/nginx/conf/fastcgicache.conf + sudo sed -i "s/\/var\/run\/php.*\.sock/\/var\/run\/php${NEW_PHP_VERSION}-fpm.sock/g" /etc/nginx/conf/fastcgicache.conf +fi + +# test nginx config and reload +script_echo "đŸ•ĩī¸â€â™€ī¸ Testing nginx config and reloading" +sudo nginx -t && sudo service nginx reload + +# # Set default PHP version +script_echo "đŸ“Ļ Setting default php version to ${NEW_PHP_VERSION}" +sudo update-alternatives --set php /usr/bin/php${NEW_PHP_VERSION} + +script_echo " +✅ All done! +-------------------- +📝 Next steps: +- Restart any processes running the old php version, ex: pm2. Check with: \`ps aux | grep php${OLD_PHP_VERSION}\` +- Shut down the old php-fpm service: \`sudo service php${OLD_PHP_VERSION}-fpm stop\` +" From 791221eec19e0637c68efdfb8cd378640b5880c2 Mon Sep 17 00:00:00 2001 From: Grey Vugrin Date: Tue, 28 Nov 2023 12:24:43 -0800 Subject: [PATCH 14/17] Added helpful commands to upgrade php readme --- .../upgrade-php/UPGRADE_PHP_README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/install/components/upgrade-php/UPGRADE_PHP_README.md b/install/components/upgrade-php/UPGRADE_PHP_README.md index 6b394c2..cb21ccd 100644 --- a/install/components/upgrade-php/UPGRADE_PHP_README.md +++ b/install/components/upgrade-php/UPGRADE_PHP_README.md @@ -47,3 +47,31 @@ Example: upgrade-php.sh 8.0 8.2 ```sh $DEBUG=true ./upgrade-php.sh OLD_VERSION NEW_VERSION ``` + +### Helpful commands + + +- Get php version +```sh +php -v +``` + +- Check pool.d configs (this will show the old and new versions) +```sh +grep -R "listen = /var/run/php" /etc/php/*/fpm/pool.d/ +``` + +- Look for sockets +```sh +ls -al /var/run/ | grep php +``` + +- Check nginx site configs +```sh +grep -R "unix:/var/run/php" /etc/nginx/sites-enabled/ +``` + +- Find running php-fpm processes +```sh +ps aux | grep php-fpm +``` From dc7e58eec392b29bf2f859dfd1778d8cbb4d3ea5 Mon Sep 17 00:00:00 2001 From: Grey Vugrin Date: Tue, 28 Nov 2023 12:25:15 -0800 Subject: [PATCH 15/17] Added link to upgrade php readme in main one --- README.md | 1 + install/components/upgrade-php/upgrade-php.sh | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index a700547..d2ed090 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ There are also **components** in the `install` folder, which allow you to instal - redis - postgressql - custom +- upgrade-php (see [Upgrade PHP Readme for help](./install/components/upgrade-php/UPGRADE_PHP_README.md)) ### Custom Scripts diff --git a/install/components/upgrade-php/upgrade-php.sh b/install/components/upgrade-php/upgrade-php.sh index 35082ac..79139d7 100755 --- a/install/components/upgrade-php/upgrade-php.sh +++ b/install/components/upgrade-php/upgrade-php.sh @@ -91,8 +91,6 @@ phpenmod -v $NEW_PHP_VERSION xmlreader phpenmod -v $NEW_PHP_VERSION simplexml phpenmod -v $NEW_PHP_VERSION gd -# TODO: Why are there not more of these to enable? Compared to number that we installed - # make edits to www.conf file if needed PHP_WWW_CONF=/etc/php/${NEW_PHP_VERSION}/fpm/pool.d/www.conf if [ ! -f $PHP_WWW_CONF ]; then From dd2de20686579772c998bba4ae7d78f03cde3a0b Mon Sep 17 00:00:00 2001 From: Grey Vugrin Date: Fri, 19 Jan 2024 12:03:57 -0800 Subject: [PATCH 16/17] Update logrotate templates to chown files after rotating --- scripts/logrotate/TEMPLATE | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/logrotate/TEMPLATE b/scripts/logrotate/TEMPLATE index 38e0329..7a645ea 100644 --- a/scripts/logrotate/TEMPLATE +++ b/scripts/logrotate/TEMPLATE @@ -9,5 +9,6 @@ LOG_PATH { postrotate systemctl reload PHP_FPM systemctl reload nginx + chown www-data:www-data LOG_PATH endscript } \ No newline at end of file From 693c688c7b7e970a5576208bb96bf615453aca09 Mon Sep 17 00:00:00 2001 From: Grey Vugrin Date: Mon, 22 Jan 2024 12:02:02 -0800 Subject: [PATCH 17/17] Update logrotate permission handling --- scripts/logrotate/TEMPLATE | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/logrotate/TEMPLATE b/scripts/logrotate/TEMPLATE index 7a645ea..f8e08f3 100644 --- a/scripts/logrotate/TEMPLATE +++ b/scripts/logrotate/TEMPLATE @@ -1,4 +1,5 @@ LOG_PATH { + su www-data www-data daily missingok rotate 14 @@ -7,8 +8,7 @@ LOG_PATH { create 0640 www-data www-data sharedscripts postrotate - systemctl reload PHP_FPM - systemctl reload nginx - chown www-data:www-data LOG_PATH + sudo systemctl reload PHP_FPM + sudo systemctl reload nginx endscript } \ No newline at end of file