Last active
March 8, 2022 20:28
-
-
Save susanBuck/0fa0ede85e1b6c0b68bfc9d24a43549a to your computer and use it in GitHub Desktop.
Server migration script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
main() { | |
do_swap | |
do_instructor_server_access | |
do_aliases | |
do_composer | |
do_github | |
do_modules | |
do_repository | |
do_laravel_app 'bookmark' | |
do_laravel_app 'p2' | |
do_laravel_app 'p3' | |
do_sites | |
do_done | |
} | |
# | |
# SWAP | |
# | |
do_swap() { | |
dump "SWAP" | |
NEEDLE='swapfile' | |
HAYSTACK=$(sudo swapon -s) | |
if grep -q $NEEDLE <<<$HAYSTACK; then | |
echo "Swap already enabled" | |
else | |
echo "Enabling swap" | |
sudo sh -c ' | |
fallocate -l 4G /swapfile | |
chmod 600 /swapfile | |
mkswap /swapfile | |
swapon /swapfile | |
' | |
ADD_LINE="/swapfile none swap sw 0 0" | |
grep -qxF "$ADD_LINE" /etc/fstab || echo $ADD_LINE >> /etc/fstab | |
fi | |
} | |
# | |
# INSTRUCTOR SERVER ACCESS | |
# | |
do_instructor_server_access() { | |
dump "INSTRUCTOR SERVER ACCESS" | |
NEEDLE="[email protected]" | |
HAYSTACK=$(cat ~/.ssh/authorized_keys) | |
if grep -q $NEEDLE <<<$HAYSTACK; then | |
echo "Instructor access already enabled (key found in ~/.ssh/authorized_keys)." | |
else | |
echo "Instructor key not found in ~/.ssh/authorized_keys; adding." | |
echo $KEY >> ~/.ssh/authorized_keys | |
fi | |
} | |
# | |
# ALIASES | |
# | |
do_aliases() { | |
dump "ALIASES" | |
if [ -s ~/.bash_aliases ]; then | |
echo "Aliases already added (~/.bash_aliases is not empty)" | |
else | |
cat > ~/.bash_aliases <<EOL | |
# Edit the bashrc file | |
alias configedit='code ~/.bashrc' | |
# Edit the alias config file | |
alias configalias='code ~/.bash_aliases' | |
# Recognize changes to bashrc config | |
# (which will also recognize changes to the alias config file) | |
alias configrefresh='source ~/.bashrc' | |
# Restart server and PHP | |
alias restart='systemctl restart nginx; systemctl reload php8.0-fpm' | |
# Open your e15 directory | |
alias e15='code -a /var/www/e15' | |
# Edit nginx sites config file for this course | |
alias sites='code /etc/nginx/sites-available/hes' | |
# Edit php.ini file | |
alias phpini='code /etc/php/8.0/fpm/php.ini' | |
# View server error log file | |
alias errorlog='code /var/log/nginx/error.log' | |
EOL | |
source ~/.bashrc | |
fi | |
} | |
# | |
# COMPOSER | |
# | |
do_composer() { | |
dump "COMPOSER" | |
if [ -s /usr/bin/composer ]; then | |
echo "Composer already installed." | |
else | |
cd /usr/bin | |
curl -sS https://getcomposer.org/installer | sudo php | |
sudo mv composer.phar composer | |
fi | |
} | |
# | |
# GITHUB | |
# | |
do_github() { | |
dump "GITHUB SSH KEYS" | |
NEEDLE="GitHub does not provide shell access" | |
HAYSTACK=$(ssh -T -o StrictHostKeyChecking=no [email protected] 2>&1) | |
if grep -q "$NEEDLE" <<<$HAYSTACK; then | |
echo 'Github SSH keys already set up.' | |
else | |
if [ ! -f ~/.ssh/id_rsa.pub ]; then | |
echo 'Generating SSH key' | |
ssh-keygen -q -t rsa -N '' <<< $'\ny' >/dev/null 2>&1 | |
# -N new_passphrase provides the new passphrase | |
# -q silence ssh-keygen | |
# -f filename specifies the filename of the key file | |
fi | |
echo "Please visit https://github.com/settings/keys and add the following key:" | |
echo "" | |
echo $(cat ~/.ssh/id_rsa.pub) | |
echo "" | |
read -p "Enter (y) once you have added the key: " | |
fi | |
} | |
# | |
# SERVER MODULES | |
# | |
do_modules() { | |
dump "MODULES" | |
# Assuming if xml module was installed, all others were installed | |
NEEDLE='xml' | |
HAYSTACK=$(dpkg --get-selections | grep -i php) | |
if grep -q $NEEDLE <<<$HAYSTACK; then | |
echo "Modules already enabled" | |
else | |
MAJOR=$(php -r 'echo PHP_MAJOR_VERSION;') | |
MINOR=$(php -r 'echo PHP_MINOR_VERSION;') | |
PHP_VERSION=${MAJOR}.${MINOR} | |
echo -e "\nUpdating modules for php $PHP_VERSION" | |
sudo sh -c ' | |
sudo add-apt-repository ppa:ondrej/php | |
sudo apt-get --assume-yes update | |
sudo apt-get --assume-yes install php${PHP_VERSION}-mbstring php${PHP_VERSION}-xml php${PHP_VERSION}-curl zip unzip | |
' | |
fi | |
} | |
# | |
# REPOSITORY | |
# | |
do_repository() { | |
dump "e15 REPOSITORY" | |
if [ -d /var/www/e15 ]; then | |
echo "/var/www/e15 directory/repository already exists" | |
else | |
echo "Going to clone your e15 repository into /var/www/" | |
cd "/var/www" | |
read -p "Enter your Github username: " USERNAME | |
git clone [email protected]:$USERNAME/e15.git | |
fi | |
} | |
# | |
# SETUP LARAVEL APPS | |
# | |
do_laravel_app() { | |
APP_NAME=$1 | |
dump "CHECKING FOR LARAVEL APP $APP_NAME" | |
if [ ! -d /var/www/e15/$APP_NAME ]; then | |
echo "$APP_NAME not found; skipping." | |
else | |
echo "Going to setup $APP_NAME Laravel app..." | |
cd /var/www/e15/$APP_NAME | |
echo "Adjusting permissions on 'storage' and 'bootstrap/cache'" | |
chown -R www-data storage | |
chown -R www-data bootstrap/cache | |
if [ -d /var/www/e15/$APP_NAME/vendor ]; then | |
echo "Detected vendor directory already exists; skipping running 'composer update'" | |
else | |
echo "Vendor directory does not exist; running 'composer update' to build" | |
composer update | |
fi | |
if [ -f /var/www/e15/$APP_NAME/.env ]; then | |
echo "Detected .env file already exists" | |
else | |
echo "Creating .env file" | |
cp .env.example .env | |
php artisan key:generate | |
fi | |
fi | |
} | |
# | |
# SITES | |
do_sites() { | |
dump "SETTING UP SITES CONFIG" | |
if [ -s /etc/nginx/sites-available/hes ]; then | |
echo "/etc/nginx/sites-available/hes already exists; skipping." | |
else | |
read -p "Enter the domain you’re using using for this course: " DOMAIN | |
cat > /etc/nginx/sites-available/hes <<EOL | |
server { | |
listen 80; | |
listen [::]:80; | |
server_name e15p2.$DOMAIN; | |
root /var/www/e15/bookmark/public; | |
add_header X-Frame-Options "SAMEORIGIN"; | |
add_header X-Content-Type-Options "nosniff"; | |
index index.php; | |
charset utf-8; | |
location / { | |
try_files $uri $uri/ /index.php?$query_string; | |
} | |
location = /favicon.ico { | |
access_log off; log_not_found off; | |
} | |
location = /robots.txt { | |
access_log off; log_not_found off; | |
} | |
error_page 404 /index.php; | |
location ~ \.php$ { | |
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
location ~ /\.(?!well-known).* { | |
deny all; | |
} | |
} | |
server { | |
listen 80; | |
listen [::]:80; | |
server_name bookmark.$DOMAIN; | |
root /var/www/e15/bookmark/public; | |
add_header X-Frame-Options "SAMEORIGIN"; | |
add_header X-Content-Type-Options "nosniff"; | |
index index.php; | |
charset utf-8; | |
location / { | |
try_files $uri $uri/ /index.php?$query_string; | |
} | |
location = /favicon.ico { | |
access_log off; log_not_found off; | |
} | |
location = /robots.txt { | |
access_log off; log_not_found off; | |
} | |
error_page 404 /index.php; | |
location ~ \.php$ { | |
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
location ~ /\.(?!well-known).* { | |
deny all; | |
} | |
} | |
server { | |
listen 80; | |
server_name e15practice.$DOMAIN; | |
root /var/www/e15/practice/; | |
index index.php index.html; | |
location ~ \.php$ { | |
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
} | |
server { | |
listen 80; | |
server_name e15p1.$DOMAIN; | |
root /var/www/e15/p1/; | |
index index.php index.html; | |
location ~ \.php$ { | |
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
} | |
EOL | |
ln -s /etc/nginx/sites-available/hes /etc/nginx/sites-enabled | |
systemctl restart nginx | |
echo "Created /etc/nginx/sites-available/hes and linked it to /etc/nginx/sites-enabled/hes" | |
echo "Restarted nginx" | |
fi | |
} | |
do_done() { | |
dump "DONE" | |
echo "The migration script has completed!" | |
echo "Here are manual steps you still need to complete:" | |
echo " 1. Update your server’s DNS settings so it points to your new server’s IP address" | |
echo " 2. Update the .env files in your Laravel apps with the appropriate values." | |
} | |
# | |
# DUMP HELPER | |
# | |
dump () { | |
echo "" | |
echo "===> $1" | |
} | |
main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment