2024-06-12 13:02:54 +02:00

240 lines
9.2 KiB
Bash

#!/usr/bin/env bash
##################################
# Usage: #
# sudo ./bin/homestead/wsl-init #
##################################
export DEBIAN_FRONTEND=noninteractive
echo "What is your WSL user name?"
read WSL_USER_NAME
echo "What is your WSL user group? (Same as username if you're unsure)"
read WSL_USER_GROUP
# Update Package List
apt-get update
# Update System Packages
apt-get upgrade -y
# Force Locale
echo "LC_ALL=en_US.UTF-8" >> /etc/default/locale
locale-gen en_US.UTF-8
apt-get install -y software-properties-common curl gnupg debian-keyring debian-archive-keyring apt-transport-https \
ca-certificates
# Install Some PPAs
apt-add-repository ppa:ondrej/php -y
# Prepare keyrings directory
sudo mkdir -p /etc/apt/keyrings
# NodeJS
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
# PostgreSQL
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/keyrings/postgresql.gpg
sudo sh -c 'echo "deb [signed-by=/etc/apt/keyrings/postgresql.gpg] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
## Update Package Lists
apt-get update -y
# Install Some Basic Packages
apt-get install -y build-essential dos2unix gcc git git-lfs libmcrypt4 libpcre3-dev libpng-dev chrony unzip make pv \
python3-pip re2c supervisor unattended-upgrades whois vim cifs-utils bash-completion zsh graphviz avahi-daemon tshark
# Install Generic PHP packages
apt-get install -y --allow-change-held-packages \
php-imagick php-memcached php-redis php-xdebug php-dev php-swoole
# PHP 8.3
apt-get install -y --allow-change-held-packages \
php8.3 php8.3-bcmath php8.3-bz2 php8.3-cgi php8.3-cli php8.3-common php8.3-curl php8.3-dba php8.3-dev \
php8.3-enchant php8.3-fpm php8.3-gd php8.3-gmp php8.3-imap php8.3-interbase php8.3-intl php8.3-ldap \
php8.3-mbstring php8.3-mysql php8.3-odbc php8.3-opcache php8.3-pgsql php8.3-phpdbg php8.3-pspell php8.3-readline \
php8.3-snmp php8.3-soap php8.3-sqlite3 php8.3-sybase php8.3-tidy php8.3-xml php8.3-xsl \
php8.3-zip php8.3-imagick php8.3-memcached php8.3-redis php8.3-xmlrpc php8.3-xdebug
# Configure php.ini for CLI
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/8.3/cli/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/8.3/cli/php.ini
sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/8.3/cli/php.ini
sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/8.3/cli/php.ini
# Configure Xdebug
echo "xdebug.mode = debug" >> /etc/php/8.3/mods-available/xdebug.ini
echo "xdebug.discover_client_host = true" >> /etc/php/8.3/mods-available/xdebug.ini
echo "xdebug.client_port = 9003" >> /etc/php/8.3/mods-available/xdebug.ini
echo "xdebug.max_nesting_level = 512" >> /etc/php/8.3/mods-available/xdebug.ini
echo "opcache.revalidate_freq = 0" >> /etc/php/8.3/mods-available/opcache.ini
# Fixed php fpm bind listening socket - no such file issue.
mkdir -p /run/php
touch /run/php/php8.3-fpm.sock
# Install Composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chown -R vagrant:vagrant /home/vagrant/.config
# Install Global Packages
sudo su vagrant <<'EOF'
/usr/local/bin/composer global require "laravel/envoy=^2.0"
/usr/local/bin/composer global require "laravel/installer=^5.0"
/usr/local/bin/composer global config --no-plugins allow-plugins.slince/composer-registry-manager true
/usr/local/bin/composer global require "slince/composer-registry-manager=^2.0"
EOF
# Install Nginx
apt-get install -y --allow-downgrades --allow-remove-essential --allow-change-held-packages nginx
rm /etc/nginx/sites-enabled/default
rm /etc/nginx/sites-available/default
# Create a configuration file for Nginx overrides.
mkdir -p /home/$WSL_USER_NAME/.config/nginx
chown -R $WSL_USER_NAME:$WSL_USER_GROUP /home/$WSL_USER_NAME
touch /home/$WSL_USER_NAME/.config/nginx/nginx.conf
ln -sf /home/$WSL_USER_NAME/.config/nginx/nginx.conf /etc/nginx/conf.d/nginx.conf
# Configure php.ini for FPM
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/8.3/fpm/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/8.3/fpm/php.ini
sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/8.3/fpm/php.ini
sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/8.3/fpm/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 100M/" /etc/php/8.3/fpm/php.ini
sed -i "s/post_max_size = .*/post_max_size = 100M/" /etc/php/8.3/fpm/php.ini
sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/8.3/fpm/php.ini
printf "[openssl]\n" | tee -a /etc/php/8.3/fpm/php.ini
printf "openssl.cainfo = /etc/ssl/certs/ca-certificates.crt\n" | tee -a /etc/php/8.3/fpm/php.ini
printf "[curl]\n" | tee -a /etc/php/8.3/fpm/php.ini
printf "curl.cainfo = /etc/ssl/certs/ca-certificates.crt\n" | tee -a /etc/php/8.3/fpm/php.ini
# Set The Nginx & PHP-FPM User
sed -i "s/user www-data;/user $WSL_USER_NAME;/" /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 = $WSL_USER_NAME/" /etc/php/8.3/fpm/pool.d/www.conf
sed -i "s/group = www-data/group = $WSL_USER_NAME/" /etc/php/8.3/fpm/pool.d/www.conf
sed -i "s/listen\.owner.*/listen.owner = $WSL_USER_NAME/" /etc/php/8.3/fpm/pool.d/www.conf
sed -i "s/listen\.group.*/listen.group = $WSL_USER_NAME/" /etc/php/8.3/fpm/pool.d/www.conf
sed -i "s/;listen\.mode.*/listen.mode = 0666/" /etc/php/8.3/fpm/pool.d/www.conf
# Disable XDebug On The CLI
sudo phpdismod -s cli xdebug
service nginx restart
service php8.3-fpm restart
# Add $WSL_USER_NAME User To WWW-Data
usermod -a -G www-data $WSL_USER_NAME
id $WSL_USER_NAME
groups $WSL_USER_GROUP
# Install Node
apt-get install -y nodejs
/usr/bin/npm install -g npm
# Install SQLite
apt-get install -y sqlite3 libsqlite3-dev
# Disable Apparmor
# See https://github.com/laravel/homestead/issues/629#issue-247524528
service apparmor stop
update-rc.d -f apparmor remove
# Remove MySQL
apt-get remove -y --purge mysql-server mysql-client mysql-common
apt-get autoremove -y
apt-get autoclean
rm -rf /var/lib/mysql/*
rm -rf /var/log/mysql
rm -rf /etc/mysql
# Add Maria PPA
curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
echo "mariadb-server mysql-server/data-dir select ''" | debconf-set-selections
echo "mariadb-server mysql-server/root_password password secret" | debconf-set-selections
echo "mariadb-server mysql-server/root_password_again password secret" | debconf-set-selections
mkdir /etc/mysql
touch /etc/mysql/debian.cnf
# Install MariaDB
apt-get install -y mariadb-server mariadb-client
# Configure Maria Remote Access and ignore db dirs
sed -i "s/bind-address = 127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/mariadb.conf.d/50-server.cnf
cat > /etc/mysql/mariadb.conf.d/50-server.cnf << EOF
[mysqld]
bind-address = 0.0.0.0
ignore-db-dir = lost+found
#general_log
#general_log_file=/var/log/mysql/mariadb.log
EOF
export MYSQL_PWD=secret
mysql --user="root" -e "GRANT ALL ON *.* TO root@'0.0.0.0' IDENTIFIED BY 'secret' WITH GRANT OPTION;"
service mysql restart
mysql --user="root" -e "CREATE USER IF NOT EXISTS 'homestead'@'0.0.0.0' IDENTIFIED BY 'secret';"
mysql --user="root" -e "GRANT ALL ON *.* TO 'homestead'@'0.0.0.0' IDENTIFIED BY 'secret' WITH GRANT OPTION;"
mysql --user="root" -e "GRANT ALL ON *.* TO 'homestead'@'%' IDENTIFIED BY 'secret' WITH GRANT OPTION;"
mysql --user="root" -e "FLUSH PRIVILEGES;"
service mysql restart
mysql_upgrade --user="root" --verbose --force
service mysql restart
unset MYSQL_PWD
# Install Postgres 15
apt-get install -y postgresql-15 postgresql-server-dev-15 postgresql-15-postgis-3 postgresql-15-postgis-3-scripts
# Configure Postgres Users
sudo -u postgres psql -c "CREATE ROLE homestead LOGIN PASSWORD 'secret' SUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;"
# Configure Postgres Remote Access
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /etc/postgresql/15/main/postgresql.conf
echo "host all all 10.0.2.2/32 md5" | tee -a /etc/postgresql/15/main/pg_hba.conf
sudo -u postgres /usr/bin/createdb --echo --owner=homestead homestead
service postgresql restart
# Disable to lower initial overhead
systemctl disable postgresql
# Install Redis, Memcached, & Beanstalk
apt-get install -y redis-server memcached
systemctl enable redis-server
service redis-server start
# One last upgrade check
apt-get upgrade -y
# Clean Up
apt -y autoremove
apt -y clean
chown -R $WSL_USER_NAME:$WSL_USER_GROUP /home/$WSL_USER_NAME
chown -R $WSL_USER_NAME:$WSL_USER_GROUP /usr/local/bin
# Add Composer Global Bin To Path
printf "\nPATH=\"$(sudo su - $WSL_USER_NAME -c 'composer config -g home 2>/dev/null')/vendor/bin:\$PATH\"\n" | tee -a /home/$WSL_USER_NAME/.profile
apt-get -y autoremove;
apt-get -y clean;
# What are you doing Ubuntu?
# https://askubuntu.com/questions/1250974/user-root-cant-write-to-file-in-tmp-owned-by-someone-else-in-20-04-but-can-in
sysctl fs.protected_regular=0
# Setup Homestead repo
su $WSL_USER_NAME -c 'composer install'
su $WSL_USER_NAME -c 'bash init.sh'