Skip to content

Instantly share code, notes, and snippets.

@mikebronner
Created February 21, 2019 00:28
Show Gist options
  • Save mikebronner/a4bf3530c5d3466f2bf0b128d7f38e6a to your computer and use it in GitHub Desktop.
Save mikebronner/a4bf3530c5d3466f2bf0b128d7f38e6a to your computer and use it in GitHub Desktop.
Nginx Amplify Scripts
#!/bin/bash
if [ `id -u` -ne 0 ]
then
echo "You must be root!" >&2
exit 1
fi
amplify_api_key="$AMPLIFY_KEY"
db_password="$DATABASE_PASSWORD"
# Install Amplify Agent
wget -O amplify_install https://github.com/nginxinc/nginx-amplify-agent/raw/master/packages/install.sh
chmod a+x amplify_install
API_KEY="$amplify_api_key" ./amplify_install
rm -v amplify_install
# Configure nginx, if it is installed
if dpkg-query -W --showformat='${Status}' nginx | grep -q 'install ok installed'
then
cat > /etc/nginx/conf.d/nginx-amplify.conf <<- EOF
server {
listen 127.0.0.1:80;
server_name 127.0.0.1;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
EOF
sed -ir 's/^([[:space:]]*)((access|error)_log[[:space:]].*)$/\1#\2/' /etc/nginx/nginx.conf
cat > /etc/nginx/conf.d/logging.conf <<- EOF
log_format main_ext '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$host" sn="$server_name" rt=$request_time ua="$upstream_addr" us="$upstream_status" ut="$upstream_response_time" ul="$upstream_response_length" cs=$upstream_cache_status';
access_log /var/log/nginx/access.log main_ext;
error_log /var/log/nginx/error.log warn;
EOF
service nginx reload
fi
# Configure PHP-FPM, if it is installed
if dpkg-query -W --showformat='${Status}' 'php[0-9]*-fpm' | grep -q 'install ok installed'
then
php_version=`php -r 'echo phpversion();' | egrep -o '^[[:digit:]]+\.[[:digit:]]+'`
cat > /etc/php/$php_version/fpm/pool.d/amplify.conf <<- EOF
[www]
pm.status_path = /status
EOF
sed -ir 's/^[[:space:]]*phpfpm[[:space:]]*=.*$/phpfpm = True/' /etc/amplify-agent/agent.conf
service php$php_version-fpm reload
else
sed -ir 's/^[[:space:]]*phpfpm[[:space:]]*=.*$/phpfpm = False/' /etc/amplify-agent/agent.conf
fi
# Configure mysql, if it is installed
if dpkg-query -W --showformat='${Status}' mysql-server | grep -q 'install ok installed'
then
sed -ir \
-e 's/^[[:space:]]*mysql[[:space:]]*=.*$/mysql = True/' \
-e 's/^[[:space:]]*user[[:space:]]*=.*$/user = codepier/' \
-e "s/^[[:space:]]*password[[:space:]]*=.*$/password = $db_password/" \
/etc/amplify-agent/agent.conf
else
sed -ir 's/^[[:space:]]*mysql[[:space:]]*=.*$/mysql = False/' /etc/amplify-agent/agent.conf
fi
# Finish
service amplify-agent restart
#!/bin/bash
if [ `id -u` -ne 0 ]
then
echo "You must be root!" >&2
exit 1
fi
# Deconfigure nginx
rm -vf /etc/nginx/conf.d/nginx-amplify.conf
rm -vf /etc/nginx/conf.d/logging.conf
sed -ir 's/^([[:space:]]*)#((access|error)_log[[:space:]].*)$/\1\2/' /etc/nginx/nginx.conf 2> /dev/null
service nginx reload 2> /dev/null
# Deconfigure PHP-FPM
rm -vf /etc/php/*/fpm/pool.d/amplify.conf
service php$php_version-fpm reload 2> /dev/null
apt purge -y nginx-amplify-agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment