Created
June 27, 2016 00:18
-
-
Save alnutile/f8efe2793f7fafa25e373612cc3caa8b to your computer and use it in GitHub Desktop.
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
# | |
# REQUIRES: | |
# - server (the forge server instance) | |
# - site_name (the name of the site folder) | |
# - sudo_password (random password for sudo) | |
# - db_password (random password for database user) | |
# - event_id (the provisioning event name) | |
# - callback (the callback URL) | |
# | |
sudo sed -i "s/#precedence ::ffff:0:0\/96 100/precedence ::ffff:0:0\/96 100/" /etc/gai.conf | |
# Upgrade The Base Packages | |
apt-get update | |
apt-get upgrade -y | |
# Add A Few PPAs To Stay Current | |
apt-get install -y --force-yes software-properties-common | |
# apt-add-repository ppa:fkrull/deadsnakes-python2.7 -y | |
apt-add-repository ppa:nginx/development -y | |
apt-add-repository ppa:chris-lea/redis-server -y | |
apt-add-repository ppa:ondrej/apache2 -y | |
apt-add-repository ppa:ondrej/php -y | |
# Setup MySQL 5.7 Repositories | |
# Setup MariaDB Repositories | |
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 | |
sudo add-apt-repository 'deb [arch=amd64,i386] http://mirrors.accretive-networks.net/mariadb/repo/10.1/ubuntu xenial main' | |
# apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db | |
# add-apt-repository 'deb [arch=amd64,i386] http://ftp.osuosl.org/pub/mariadb/repo/10.1/ubuntu xenial main' | |
# Setup Postgres 9.4 Repositories | |
# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - | |
# sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" >> /etc/apt/sources.list.d/postgresql.list' | |
# Update Package Lists | |
apt-get update | |
# Base Packages | |
apt-get install -y --force-yes build-essential curl fail2ban gcc git libmcrypt4 libpcre3-dev \ | |
make python2.7 python-pip supervisor ufw unattended-upgrades unzip whois zsh | |
# Install Python Httpie | |
pip install httpie | |
# Disable Password Authentication Over SSH | |
sed -i "/PasswordAuthentication yes/d" /etc/ssh/sshd_config | |
echo "" | sudo tee -a /etc/ssh/sshd_config | |
echo "" | sudo tee -a /etc/ssh/sshd_config | |
echo "PasswordAuthentication no" | sudo tee -a /etc/ssh/sshd_config | |
# Restart SSH | |
ssh-keygen -A | |
service ssh restart | |
# Set The Hostname If Necessary | |
echo "deep-atoll" > /etc/hostname | |
sed -i 's/127\.0\.0\.1.*localhost/127.0.0.1 deep-atoll localhost/' /etc/hosts | |
hostname deep-atoll | |
# Set The Timezone | |
ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime | |
# Create The Root SSH Directory If Necessary | |
if [ ! -d /root/.ssh ] | |
then | |
mkdir -p /root/.ssh | |
touch /root/.ssh/authorized_keys | |
fi | |
# Setup Forge User | |
useradd forge | |
mkdir -p /home/forge/.ssh | |
mkdir -p /home/forge/.forge | |
adduser forge sudo | |
# Setup Bash For Forge User | |
chsh -s /bin/bash forge | |
cp /root/.profile /home/forge/.profile | |
cp /root/.bashrc /home/forge/.bashrc | |
# Set The Sudo Password For Forge | |
PASSWORD=$(mkpasswd XkjU2EDCJDUfaT1d8WEU) | |
usermod --password $PASSWORD forge | |
# Build Formatted Keys & Copy Keys To Forge | |
cat > /root/.ssh/authorized_keys << EOF | |
# Laravel Forge | |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDs/XPu7BxZGqprUaF2OPkYiSOo5Il6YfR6O5SxYlT+OD/VqAZCT0L6kUVq/tAU0bT77k9g39WvJ6tlvahloy8sjUVuB5NIol3f+eIFdYjWphh+EoZrkjw5ZYg3I7H9xeq1hAB41KHPX3OBbQyOI/cSzMmH2PVP9EwylVCIrNt9mQ== phpseclib-generated-key | |
EOF | |
cp /root/.ssh/authorized_keys /home/forge/.ssh/authorized_keys | |
# Create The Server SSH Key | |
ssh-keygen -f /home/forge/.ssh/id_rsa -t rsa -N '' | |
# Copy Github And Bitbucket Public Keys Into Known Hosts File | |
ssh-keyscan -H github.com >> /home/forge/.ssh/known_hosts | |
ssh-keyscan -H bitbucket.org >> /home/forge/.ssh/known_hosts | |
# Configure Git Settings | |
git config --global user.name "Alfred Nutile Inc" | |
git config --global user.email "[email protected]" | |
# Add The Reconnect Script Into Forge Directory | |
cat > /home/forge/.forge/reconnect << EOF | |
#!/usr/bin/env bash | |
echo "# Laravel Forge" | tee -a /home/forge/.ssh/authorized_keys > /dev/null | |
echo \$1 | tee -a /home/forge/.ssh/authorized_keys > /dev/null | |
echo "# Laravel Forge" | tee -a /root/.ssh/authorized_keys > /dev/null | |
echo \$1 | tee -a /root/.ssh/authorized_keys > /dev/null | |
echo "Keys Added!" | |
EOF | |
# Add The Environment Variables Scripts Into Forge Directory | |
cat > /home/forge/.forge/add-variable.php << EOF | |
<?php | |
// Get the script input... | |
\$input = array_values(array_slice(\$_SERVER['argv'], 1)); | |
// Get the path to the environment file... | |
\$path = getcwd().'/'.\$input[2]; | |
// Write a stub file if one doesn't exist... | |
if ( ! file_exists(\$path)) { | |
file_put_contents(\$path, '<?php return '.var_export([], true).';'); | |
} | |
// Set the new environment variable... | |
\$env = require \$path; | |
\$env[\$input[0]] = \$input[1]; | |
// Write the environment file to disk... | |
file_put_contents(\$path, '<?php return '.var_export(\$env, true).';'); | |
EOF | |
cat > /home/forge/.forge/remove-variable.php << EOF | |
<?php | |
// Get the script input... | |
\$input = array_values(array_slice(\$_SERVER['argv'], 1)); | |
// Get the path to the environment file... | |
\$path = getcwd().'/'.\$input[1]; | |
// Write a stub file if one doesn't exist... | |
if ( ! file_exists(\$path)) { | |
file_put_contents(\$path, '<?php return '.var_export([], true).';'); | |
} | |
// Remove the environment variable... | |
\$env = require \$path; | |
unset(\$env[\$input[0]]); | |
// Write the environment file to disk... | |
file_put_contents(\$path, '<?php return '.var_export(\$env, true).';'); | |
EOF | |
# Setup Site Directory Permissions | |
chown -R forge:forge /home/forge | |
chmod -R 755 /home/forge | |
chmod 700 /home/forge/.ssh/id_rsa | |
# Setup UFW Firewall | |
ufw allow 22 | |
ufw allow 80 | |
ufw allow 443 | |
ufw --force enable | |
# Allow FPM Restart | |
echo "forge ALL=NOPASSWD: /usr/sbin/service php7.0-fpm reload" > /etc/sudoers.d/php-fpm | |
echo "forge ALL=NOPASSWD: /usr/sbin/service php5-fpm reload" >> /etc/sudoers.d/php-fpm | |
# Install Base PHP Packages | |
apt-get install -y --force-yes php7.0-cli php7.0-dev \ | |
php-pgsql php-sqlite3 php-gd \ | |
php-curl php7.0-dev \ | |
php-imap php-mysql php-memcached php-mcrypt php-mbstring \ | |
php-xml php-imagick php7.0-zip php7.0-bcmath php-soap \ | |
php7.0-intl php7.0-readline | |
# Install Composer Package Manager | |
curl -sS https://getcomposer.org/installer | php | |
mv composer.phar /usr/local/bin/composer | |
# Misc. PHP CLI Configuration | |
sudo sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.0/cli/php.ini | |
sudo sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.0/cli/php.ini | |
sudo sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.0/cli/php.ini | |
sudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.0/cli/php.ini | |
# Configure Sessions Directory Permissions | |
chmod 733 /var/lib/php/sessions | |
chmod +t /var/lib/php/sessions | |
# | |
# REQUIRES: | |
# - server (the forge server instance) | |
# - site_name (the name of the site folder) | |
# | |
# Install Nginx & PHP-FPM | |
apt-get install -y --force-yes nginx php7.0-fpm | |
# Generate dhparam File | |
openssl dhparam -out /etc/nginx/dhparams.pem 2048 | |
# Disable The Default Nginx Site | |
rm /etc/nginx/sites-enabled/default | |
rm /etc/nginx/sites-available/default | |
service nginx restart | |
# Tweak Some PHP-FPM Settings | |
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.0/fpm/php.ini | |
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.0/fpm/php.ini | |
sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/7.0/fpm/php.ini | |
sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.0/fpm/php.ini | |
sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.0/fpm/php.ini | |
# Setup Session Save Path | |
sed -i "s/\;session.save_path = .*/session.save_path = \"\/var\/lib\/php5\/sessions\"/" /etc/php/7.0/fpm/php.ini | |
sed -i "s/php5\/sessions/php\/sessions/" /etc/php/7.0/fpm/php.ini | |
# Configure Nginx & PHP-FPM To Run As Forge | |
sed -i "s/user www-data;/user forge;/" /etc/nginx/nginx.conf | |
sed -i "s/# server_names_hash_bucket_size.*/server_names_hash_bucket_size 64;/" /etc/nginx/nginx.conf | |
sed -i "s/^user = www-data/user = forge/" /etc/php/7.0/fpm/pool.d/www.conf | |
sed -i "s/^group = www-data/group = forge/" /etc/php/7.0/fpm/pool.d/www.conf | |
sed -i "s/;listen\.owner.*/listen.owner = forge/" /etc/php/7.0/fpm/pool.d/www.conf | |
sed -i "s/;listen\.group.*/listen.group = forge/" /etc/php/7.0/fpm/pool.d/www.conf | |
sed -i "s/;listen\.mode.*/listen.mode = 0666/" /etc/php/7.0/fpm/pool.d/www.conf | |
# Configure A Few More Server Things | |
sed -i "s/;request_terminate_timeout.*/request_terminate_timeout = 60/" /etc/php/7.0/fpm/pool.d/www.conf | |
sed -i "s/worker_processes.*/worker_processes auto;/" /etc/nginx/nginx.conf | |
sed -i "s/# multi_accept.*/multi_accept on;/" /etc/nginx/nginx.conf | |
# Install A Catch All Server | |
cat > /etc/nginx/sites-available/catch-all << EOF | |
server { | |
return 404; | |
} | |
EOF | |
ln -s /etc/nginx/sites-available/catch-all /etc/nginx/sites-enabled/catch-all | |
# Restart Nginx & PHP-FPM Services | |
# Restart Nginx & PHP-FPM Services | |
if [ ! -z "\$(ps aux | grep php-fpm | grep -v grep)" ] | |
then | |
service php5-fpm restart | |
service php7.0-fpm restart | |
fi | |
service nginx restart | |
service nginx reload | |
# Add Forge User To www-data Group | |
usermod -a -G www-data forge | |
id forge | |
groups forge | |
# | |
# REQUIRES: | |
# - server (the forge server instance) | |
# | |
# Only Install PHP Extensions When Not On HHVM | |
curl --silent --location https://deb.nodesource.com/setup_6.x | bash - | |
apt-get update | |
sudo apt-get install -y --force-yes nodejs | |
npm install -g pm2 | |
npm install -g gulp | |
# | |
# REQUIRES: | |
# - server (the forge server instance) | |
# - db_password (random password for mysql user) | |
# | |
# Set The Automated Root Password | |
export DEBIAN_FRONTEND=noninteractive | |
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/data-dir select ''" | |
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password password 9koYO6UcaH21J2o5kMom" | |
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password_again password 9koYO6UcaH21J2o5kMom" | |
# Install MySQL | |
apt-get install -y mariadb-server | |
# Configure Password Expiration | |
# echo "default_password_lifetime = 0" >> /etc/mysql/my.cnf | |
# Configure Access Permissions For Root & Forge Users | |
sed -i '/^bind-address/s/bind-address.*=.*/bind-address = */' /etc/mysql/my.cnf | |
mysql --user="root" --password="9koYO6UcaH21J2o5kMom" -e "GRANT ALL ON *.* TO root@'54.84.51.50' IDENTIFIED BY '9koYO6UcaH21J2o5kMom';" | |
mysql --user="root" --password="9koYO6UcaH21J2o5kMom" -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '9koYO6UcaH21J2o5kMom';" | |
service mysql restart | |
mysql --user="root" --password="9koYO6UcaH21J2o5kMom" -e "CREATE USER 'forge'@'54.84.51.50' IDENTIFIED BY '9koYO6UcaH21J2o5kMom';" | |
mysql --user="root" --password="9koYO6UcaH21J2o5kMom" -e "GRANT ALL ON *.* TO 'forge'@'54.84.51.50' IDENTIFIED BY '9koYO6UcaH21J2o5kMom' WITH GRANT OPTION;" | |
mysql --user="root" --password="9koYO6UcaH21J2o5kMom" -e "GRANT ALL ON *.* TO 'forge'@'%' IDENTIFIED BY '9koYO6UcaH21J2o5kMom' WITH GRANT OPTION;" | |
mysql --user="root" --password="9koYO6UcaH21J2o5kMom" -e "FLUSH PRIVILEGES;" | |
# Set Character Set | |
echo "" >> /etc/mysql/my.cnf | |
echo "[mysqld]" >> /etc/mysql/my.cnf | |
echo "character-set-server = utf8" >> /etc/mysql/my.cnf | |
# Create The Initial Database If Specified | |
mysql --user="root" --password="9koYO6UcaH21J2o5kMom" -e "CREATE DATABASE forge;" | |
# | |
# REQUIRES: | |
# - server (the forge server instance) | |
# - db_password (random password for database user) | |
# | |
# Install Postgres | |
apt-get install -y --force-yes postgresql | |
# Configure Postgres For Remote Access | |
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /etc/postgresql/9.5/main/postgresql.conf | |
echo "host all all 0.0.0.0/0 md5" | tee -a /etc/postgresql/9.5/main/pg_hba.conf | |
sudo -u postgres psql -c "CREATE ROLE forge LOGIN UNENCRYPTED PASSWORD '9koYO6UcaH21J2o5kMom' SUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;" | |
service postgresql restart | |
# Create The Initial Database If Specified | |
sudo -u postgres /usr/bin/createdb --echo --owner=forge forge | |
# Install & Configure Redis Server | |
apt-get install -y redis-server | |
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /etc/redis/redis.conf | |
service redis-server restart | |
# Install & Configure Memcached | |
apt-get install -y memcached | |
sed -i 's/-l 127.0.0.1/-l 0.0.0.0/' /etc/memcached.conf | |
service memcached restart | |
# Install & Configure Beanstalk | |
apt-get install -y --force-yes beanstalkd | |
sed -i "s/BEANSTALKD_LISTEN_ADDR.*/BEANSTALKD_LISTEN_ADDR=0.0.0.0/" /etc/default/beanstalkd | |
sed -i "s/#START=yes/START=yes/" /etc/default/beanstalkd | |
/etc/init.d/beanstalkd start | |
# Configure Supervisor Autostart | |
systemctl enable supervisor.service | |
service supervisor start | |
# Configure Swap Disk | |
if [ -f /swapfile ]; then | |
echo "Swap exists." | |
else | |
fallocate -l 1G /swapfile | |
chmod 600 /swapfile | |
mkswap /swapfile | |
swapon /swapfile | |
echo "/swapfile none swap sw 0 0" >> /etc/fstab | |
echo "vm.swappiness=30" >> /etc/sysctl.conf | |
echo "vm.vfs_cache_pressure=50" >> /etc/sysctl.conf | |
fi | |
# Setup Unattended Security Upgrades | |
cat > /etc/apt/apt.conf.d/50unattended-upgrades << EOF | |
Unattended-Upgrade::Allowed-Origins { | |
"Ubuntu xenial-security"; | |
}; | |
Unattended-Upgrade::Package-Blacklist { | |
// | |
}; | |
EOF | |
cat > /etc/apt/apt.conf.d/10periodic << EOF | |
APT::Periodic::Update-Package-Lists "1"; | |
APT::Periodic::Download-Upgradeable-Packages "1"; | |
APT::Periodic::AutocleanInterval "7"; | |
APT::Periodic::Unattended-Upgrade "1"; | |
EOF | |
curl --insecure --data "event_id=5443591&server_id=86149&sudo_password=XkjU2EDCJDUfaT1d8WEU&db_password=9koYO6UcaH21J2o5kMom" https://forge.laravel.com/provisioning/callback/app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment