Last active
November 16, 2021 03:27
-
-
Save ctudorache87/e4bcdc9133e804767764f20b9b37b60a to your computer and use it in GitHub Desktop.
Provisioning script for PHP & Node development environment on WSL
This file contains 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 | |
# Provisioning script for PHP & Node development environment on WSL | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run this script as root" | |
exit | |
fi | |
# create work dir | |
mkdir ~/work | |
chown -R $(id -u):$(id -g) ~/work | |
# add repositories and update packages | |
add-apt-repository -y ppa:ondrej/php | |
apt-get update && apt-get -y upgrade | |
# install cURL | |
echo "Installing cURL..." | |
if [ -x /usr/bin/curl ]; then | |
echo "Curl is already installed!"; | |
elif [ ! -x /usr/bin/curl ]; then | |
apt-get install -y curl | |
fi | |
# install git | |
echo "Installing Git..." | |
git --version 2>&1 >/dev/null | |
GIT_IS_AVAILABLE=$? | |
if [ $GIT_IS_AVAILABLE -eq 0 ]; then | |
echo "Git is already installed!" | |
elif [ ! $GIT_IS_AVAILABLE -eq 0 ]; then | |
apt install -y git | |
read -p "Enter your Git username: " USERNAME | |
read -p "Enter your Git email: " EMAIL | |
git config --global user.name "$USERNAME" | |
git config --global user.email $EMAIL | |
fi | |
# install PHP | |
echo "Installing PHP versions 7.1, 7.2 and 7.3..." | |
git --version 2>&1 >/dev/null | |
PHP_IS_AVAILABLE=$? | |
if [ $PHP_IS_AVAILABLE -eq 0 ]; then | |
echo "PHP is already installed!" | |
elif [ ! $PHP_IS_AVAILABLE -eq 0 ]; then | |
apt install php7.1 php7.1-common php7.1-opcache php7.1-mcrypt php7.1-cli php7.1-gd php7.1-curl php7.1-mysql php7.1-mbstring php7.1-xml php7.1-dev php7.1-xdebug php7.1-bcmath | |
apt install php7.2 php7.2-common php7.2-opcache php7.2-mcrypt php7.2-cli php7.2-gd php7.2-curl php7.2-mysql php7.2-mbstring php7.2-xml php7.2-dev php7.2-xdebug php7.2-bcmath | |
apt install php7.3 php7.3-common php7.3-opcache php7.3-mcrypt php7.3-cli php7.3-gd php7.3-curl php7.3-mysql php7.3-mbstring php7.3-xml php7.3-dev php7.3-xdebug php7.3-bcmath | |
# TODO: configure xdebug | |
fi | |
# install composer | |
echo "Installing Composer..." | |
apt install -y composer | |
# install mysql-server | |
echo "Installing MySQL Server..." | |
debconf-set-selections <<< "mysql-server mysql-server/data-dir select ''" | |
debconf-set-selections <<< "mysql-server mysql-server/root_password password secret" | |
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password secret" | |
apt-get install -y mysql-server | |
# configure mysql | |
echo "bind-address = 0.0.0.0" | tee -a /etc/mysql/conf.d/mysql.cnf | |
echo -e "[mysqld]\ndefault_authentication_plugin = mysql_native_password" | tee -a /etc/mysql/conf.d/mysql.cnf | |
service mysql restart | |
# add root and wsl users | |
mysql --user="root" --password="secret" -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;" | |
mysql --user="root" --password="secret" -e "CREATE USER 'wsl'@'0.0.0.0' IDENTIFIED BY 'secret';" | |
mysql --user="root" --password="secret" -e "CREATE USER 'wsl'@'%' IDENTIFIED BY 'secret';" | |
mysql --user="root" --password="secret" -e "GRANT ALL PRIVILEGES ON *.* TO 'wsl'@'0.0.0.0' WITH GRANT OPTION;" | |
mysql --user="root" --password="secret" -e "GRANT ALL PRIVILEGES ON *.* TO 'wsl'@'%' WITH GRANT OPTION;" | |
mysql --user="root" --password="secret" -e "FLUSH PRIVILEGES;" | |
service mysql restart | |
read -p "Do you want to add a new user and database? [Y/N] " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
read -p "Enter the name of the database: " DB_NAME | |
# TODO: ask user to add a new database and user | |
mysql --user="root" --password="secret" -e "CREATE DATABASE $DB_NAME;" | |
echo "A new $DB_NAME database added!" | |
fi | |
# TODO: ask for input if we should stop mysql from running at startup | |
# systemctl disable mysql | |
# start/stop mysql service | |
# systemctl stop mysql.service | |
# systemctl start mysql.service | |
# install nodejs via nvm | |
echo "Installing Node Version Manager..." | |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash | |
chown -R $(id -u):$(id -g) ~/.nvm/ | |
# source node to access the nvm command | |
. ~/.nvm/nvm.sh | |
echo "Installing latest version of Node.js..." | |
nvm install node | |
# clean stuff up | |
apt -y autoremove && apt clean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment