Skip to content

Instantly share code, notes, and snippets.

@SamJUK
Last active November 17, 2024 19:05
Show Gist options
  • Select an option

  • Save SamJUK/b3becaf6723acf4208eb5b8d92ef24f4 to your computer and use it in GitHub Desktop.

Select an option

Save SamJUK/b3becaf6723acf4208eb5b8d92ef24f4 to your computer and use it in GitHub Desktop.
Warden.DEV SPX Installation for versions `0.14.3` and below.
#!/usr/bin/env sh
#
# A fairly simple shell script to install and configure PHP-SPX within a Warden.dev Environment
#
# @NOTE: This approach only works for versions upto `0.14.3` and below. You can check this with `warden version`
# For versions 0.15.0 and above, track this discussion: https://github.com/orgs/wardenenv/discussions/719
# Or, use @bgorski implementation instead https://gist.github.com/SamJUK/b3becaf6723acf4208eb5b8d92ef24f4?permalink_comment_id=5288398#gistcomment-5288398
#
# Usage:
# - Download the script: `curl https://... > ~/warden-install-spx.sh`
# - Set Permissions on the script `chmod +x ~/warden-install-spx.sh`
# - cd to your warden project `cd ~/Projects/magento.test`
# - Run the downloaded script `sh ~/warden-install-spx.sh`
#
# - Navigate to the SPX control panel `https://app.mywebsite.test/?SPX_KEY=dev&SPX_UI_URI=/` and enable profiling
# - Hit the pages you want to profile
# - Navigate back to the SPX control panel to view the traces
#
set -e
echo "-------------------------"
echo " Warden SPX Installation "
echo "-------------------------"
echo "[i] Checking if this is a Warden Project"
warden env config >/dev/null
echo "[i] Checking if SPX is already installed"
RC=$(warden shell -c "php -m | grep SPX; echo $?")
[ "$RC" != "0" ] && exit 255
echo "[i] Installing Dependencies"
warden shell -c "sudo yum install -y php-devel"
echo "[i] Downloading SPX Source Code"
warden shell -c "rm -rf /tmp/spx; git clone https://github.com/NoiseByNorthwest/php-spx.git /tmp/spx"
echo "[i] Building SPX Extension"
warden shell -c "cd /tmp/spx && phpize && ./configure && make && sudo make install"
echo "[i] Writing SPX Configuration"
warden shell -c "
cat - <<EOF | sudo tee -a /etc/php.d/99-spx.ini
extension=spx.so
spx.debug=1
spx.http_enabled=1
spx.http_ip_whitelist=*
spx.http_key=dev
spx.http_trusted_proxies=REMOTE_ADDR
EOF
"
echo "[i] Add Varnish Cache Bypass"
warden env exec varnish sh -c "sed -i '#^.*SPX_ENABLED.*$#d' /etc/varnish/default.vcl"
warden env exec varnish sh -c "sed -i 's#sub vcl_recv {#sub vcl_recv {\nif (req.url ~ \"SPX_UI_URI|SPX_KEY\" || req.http.Cookie ~ \"SPX_ENABLED\") { return (pass); }#g' /etc/varnish/default.vcl"
echo "[i] Reloading Varnish Config"
T=$(date +%s)
warden env exec varnish sh -c "varnishadm vcl.load reload$T /etc/varnish/default.vcl; varnishadm vcl.use reload$T;"
echo "[i] Restarting PHP-FPM Container"
warden env restart php-fpm
@bgorski
Copy link
Copy Markdown

bgorski commented Nov 17, 2024

@SamJUK just to let you know, this will no longer work on the latest Warden version (or actually on the latest version of some containers). Right now we're in a temporary state where some foundations for more streamlined SPX support are already included (e.g. in nginx config) and it renders the old way of adding SPX support obsolete.

For anyone interested, to make SPX in Warden work now, you can add or modify the .warden/warden-env.yml file in your project and add the following things:

x-volumes: &volumes
  - ${WARDEN_SSL_DIR}/rootca/certs:/etc/ssl/warden-rootca-cert:ro
  - ${WARDEN_COMPOSER_DIR}:/home/www-data/.composer:cached
  - .${WARDEN_WEB_ROOT:-}/:/var/www/html:cached
  - bashhistory:/bash_history
  - sshdirectory:/home/www-data/.ssh

x-extra_hosts: &extra_hosts
  - ${TRAEFIK_DOMAIN}:${TRAEFIK_ADDRESS:-0.0.0.0}
  - ${TRAEFIK_SUBDOMAIN:-app}.${TRAEFIK_DOMAIN}:${TRAEFIK_ADDRESS:-0.0.0.0}

services:
  php-spx:
    hostname: "${WARDEN_ENV_NAME}-php-spx"
    image: ${WARDEN_IMAGE_REPOSITORY}/php-fpm${WARDEN_SVC_PHP_IMAGE_SUFFIX:-}:${PHP_VERSION:-7.4}-spx
    environment:
      - TRAEFIK_DOMAIN
      - TRAEFIK_SUBDOMAIN
      - SSH_AUTH_SOCK=${SSH_AUTH_SOCK_PATH_ENV:-/tmp/ssh-auth.sock}
      - NODE_VERSION=${NODE_VERSION:-12}
      - COMPOSER_VERSION=${COMPOSER_VERSION:-1}
      - COMPOSER_MEMORY_LIMIT=-1
      - HISTFILE=/bash_history/.bash_history
      - CHOWN_DIR_LIST=${CHOWN_DIR_LIST:-}
    volumes: *volumes
    extra_hosts: *extra_hosts

where x-volumes and x-extra_hosts are taken from environments/includes/php-fpm.base.yml in your warden installation (the ones above work for 0.15.0) and php-spx is a new container that will handle all requests that have SPX enabled.

In order to enable SPX in your requests, add the following cookies or URL params:

  • SPX_ENABLED with a value of 1
  • SPX_KEY with "warden" (without quotation marks) as the value.

To go to your SPX web UI, just use the following URL: https://yourproject.test/?SPX_UI_URI=/ where yourproject.test matches the domain you use for your project.

I'm not sure how long it will work this way, since I guess that some easier method of enabling SPX will get added to Warden in the future, but hopefully it will save someone some time setting this up for as long as there's no better way.

@SamJUK
Copy link
Copy Markdown
Author

SamJUK commented Nov 17, 2024

Thanks for the heads up @bgorski, I'll update the gist to include a disclaimer it does not work with 0.15.0 or above.

I'll look at getting a PR submitted to finish the implementation, if your not working on anything yourself, as its definitely going to catch me out in the future :^)

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