Last active
November 24, 2023 10:08
-
-
Save joseederangojr/cb8c01ed3d83e94c7cb9856ae04a05d1 to your computer and use it in GitHub Desktop.
bash script to automate fresh installation for laravel (nginx,php,composer,redis,mysql,supervisor,certbot, nodejs)
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 | |
# Original script https://raw.githubusercontent.com/vitodeploy/vito/main/install/install.sh | |
export DEBIAN_FRONTEND=noninteractive | |
export NEEDRESTART_MODE=a | |
echo "Enter username" | |
read USERNAME | |
echo "Enter domain" | |
read DOMAIN | |
echo "Enter project absolute path" | |
read PROJECT_PATH | |
if [[ -z "${USERNAME}" ]]; then | |
echo "Error: USERNAME environment variable is not set." | |
exit 1 | |
fi | |
if [[ -z "${DOMAIN}" ]]; then | |
echo "Error: DOMAIN environment variable is not set." | |
exit 1 | |
fi | |
if [[ -z "${PROJECT_PATH}" ]]; then | |
echo "Error: PROJECT_PATH environment variable is not set." | |
exit 1 | |
fi | |
if [[ -e "${PROJECT_PATH}/.install" ]]; then | |
echo "Error: Project already installed" | |
exit 1 | |
fi | |
apt remove needrestart -y | |
# upgrade | |
apt clean | |
apt update | |
apt upgrade -y | |
apt autoremove -y | |
# requirements | |
apt install -y software-properties-common curl zip unzip git gcc | |
# nodejs | |
curl -fsSL https://deb.nodesource.com/setup_lts.x | -E bash - | |
apt update | |
apt install nodejs -y | |
# certbot | |
apt install certbot python3-certbot-nginx -y | |
# nginx | |
export NGINX_CONFIG=" | |
user www-data; | |
worker_processes auto; | |
pid /run/nginx.pid; | |
include /etc/nginx/modules-enabled/*.conf; | |
events { | |
worker_connections 768; | |
} | |
http { | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 2048; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE | |
ssl_prefer_server_ciphers on; | |
access_log /var/log/nginx/access.log; | |
error_log /var/log/nginx/error.log; | |
gzip on; | |
include /etc/nginx/conf.d/*.conf; | |
include /etc/nginx/sites-enabled/*; | |
} | |
" | |
echo "Configuring Nginx" | |
if command -v nginx &> /dev/null; then | |
echo "Nginx is already installed." | |
else | |
# Install Nginx | |
apt install nginx -y | |
if [ $? -eq 0 ]; then | |
echo "Nginx has been successfully installed." | |
else | |
echo "Error: Unable to install Nginx. Please check your internet connection and try again." | |
exit 1 | |
fi | |
if ! echo "${NGINX_CONFIG}" | tee /etc/nginx/nginx.conf; then | |
echo "Can't configure nginx!" && exit 1 | |
fi | |
service nginx start | |
fi | |
echo "Configuring Redis" | |
# redis | |
if command -v redis-server &> /dev/null; then | |
echo "Redis is already installed." | |
else | |
# Install Redis | |
apt install redis-server -y | |
if [ $? -eq 0 ]; then | |
echo "Redis has been successfully installed." | |
else | |
echo "Error: Unable to install Redis. Please check your internet connection and try again." | |
exit 1 | |
fi | |
fi | |
echo "Configuring PHP" | |
export PHP_VERSION="8.2" | |
# Check if PHP is installed and if it meets the desired version | |
if php -v | grep "PHP ${PHP_VERSION}"; then | |
echo "PHP ${desired_php_version} is already installed." | |
else | |
# Install PHP | |
add-apt-repository ppa:ondrej/php -y | |
apt update | |
apt install -y php${PHP_VERSION} php${PHP_VERSION}-fpm php${PHP_VERSION}-mbstring php${PHP_VERSION}-mysql php${PHP_VERSION}-mcrypt php${PHP_VERSION}-gd php${PHP_VERSION}-xml php${PHP_VERSION}-curl php${PHP_VERSION}-gettext php${PHP_VERSION}-zip php${PHP_VERSION}-bcmath php${PHP_VERSION}-soap php${PHP_VERSION}-redis | |
service php${PHP_VERSION}-fpm enable | |
service php${PHP_VERSION}-fpm start | |
apt install -y php${PHP_VERSION}-ssh2 | |
service php${PHP_VERSION}-fpm restart | |
# composer | |
curl -sS https://getcomposer.org/installer -o composer-setup.php | |
php composer-setup.php --install-dir=/usr/local/bin --filename=composer | |
# Check if installation was successful | |
if [ $? -eq 0 ]; then | |
echo "PHP ${PHP_VERSION} has been successfully installed." | |
else | |
echo "Error: Unable to install PHP ${PHP_VERSION}. Please check your internet connection and try again." | |
exit 1 | |
fi | |
fi | |
# database | |
echo "Configuring database..." | |
export DB_PASS=$(openssl rand -base64 12) | |
if command -v mysql &> /dev/null; then | |
echo "MySQL is already installed." | |
else | |
echo "Enter database user" | |
read DB_USER | |
echo "Enter database name" | |
read DB_NAME | |
if [[ -z "${DB_USER}" ]]; then | |
echo "Error: DB_USER environment variable is not set." | |
exit 1 | |
fi | |
if [[ -z "${DB_NAME}" ]]; then | |
echo "Error: DB_NAME environment variable is not set." | |
exit 1 | |
fi | |
wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb | |
dpkg -i mysql-apt-config_0.8.22-1_all.deb | |
apt update | |
apt install mysql-server -y | |
# Check if installation was successful | |
if [ $? -eq 0 ]; then | |
echo "MySQL has been successfully installed." | |
else | |
echo "Error: Unable to install MySQL. Please check your internet connection and try again." | |
exit 1 | |
fi | |
service mysql enable | |
service mysql start | |
mysql -e "CREATE DATABASE IF NOT EXISTS ${DB_NAME} CHARACTER SET utf8 COLLATE utf8_general_ci" | |
mysql -e "CREATE USER IF NOT EXISTS '${DB_USER}'@'*' IDENTIFIED BY '${DB_PASS}'" | |
mysql -e "FLUSH PRIVILEGES" | |
mysql -e "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'*'" | |
mysql -e "FLUSH PRIVILEGES" | |
fi | |
# setup website | |
export SSL=${SSL:-1} | |
export COMPOSER_ALLOW_SUPERUSER=1 | |
export VHOST_CONFIG=" | |
server { | |
server_name ${DOMAIN}; | |
root ${PROJECT_PATH}/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/php${PHP_VERSION}-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
location ~ /\.(?!well-known).* { | |
deny all; | |
} | |
} | |
" | |
echo "Configuring website" | |
export REPO=https://github.com/laravel/laravel.git | |
export COMPOSER_ALLOW_SUPERUSER=1 | |
rm -rf ${PROJECT_PATH} | |
mkdir ${PROJECT_PATH} | |
chown -R ${USERNAME}:${USERNAME} ${PROJECT_PATH} | |
chmod -R 755 ${PROJECT_PATH} | |
echo "${VHOST_CONFIG}" | tee /etc/nginx/sites-available/${DOMAIN} | |
ln -s /etc/nginx/sites-available/${DOMAIN} /etc/nginx/sites-enabled/ | |
service nginx restart | |
rm -rf ${PROJECT_PATH} | |
git config --global core.fileMode false | |
git clone ${REPO} ${PROJECT_PATH} | |
find ${PROJECT_PATH} -type d -exec chmod 755 {} \; | |
find ${PROJECT_PATH} -type f -exec chmod 644 {} \; | |
cd ${PROJECT_PATH} && git config core.fileMode false | |
cd ${PROJECT_PATH} && composer install --no-dev | |
cp .env.example .env | |
export URL="https://${DOMAIN}" | |
sed -i "s|APP_URL=.*|APP_URL=${URL}|" ${PROJECT_PATH}/.env | |
sed -i "s|DB_DATABASE=.*|DB_DATABASE=${DB_NAME}|" ${PROJECT_PATH}/.env | |
sed -i "s|DB_USERNAME=.*|DB_USERNAME=${DB_USER}|" ${PROJECT_PATH}/.env | |
sed -i "s|DB_PASSWORD=.*|DB_PASSWORD=${DB_PASS}|" ${PROJECT_PATH}/.env | |
php artisan key:generate | |
php artisan storage:link | |
# setup supervisor | |
echo "Configuring supervisor" | |
export WORKER_CONFIG=" | |
[program:worker] | |
process_name=%(program_name)s_%(process_num)02d | |
command=php ${PROJECT_PATH}/artisan queue:work --sleep=3 --backoff=0 --timeout=3600 --tries=1 | |
autostart=1 | |
autorestart=1 | |
user=${USERNAME} | |
redirect_stderr=true | |
stdout_logfile=/home/${USERNAME}/.logs/workers/worker.log | |
stopwaitsecs=3600 | |
" | |
apt-get install supervisor -y | |
service supervisor enable | |
service supervisor start | |
mkdir -p /home/${USERNAME}/.logs | |
mkdir -p /home/${USERNAME}/.logs/workers | |
touch /home/${USERNAME}/.logs/workers/worker.log | |
echo "${WORKER_CONFIG}" | tee /etc/supervisor/conf.d/worker.conf | |
supervisorctl reread | |
supervisorctl update | |
supervisorctl start worker:* | |
# certbot | |
echo "Setup certbot" | |
printf "2\n" | certbot --nginx -d ${DOMAIN} | |
echo "π Congratulations!" | |
echo "β DB Name: ${DB_NAME}" | |
echo "β DB Username: ${DB_USER}" | |
echo "β DB Password: ${DB_PASS}" | |
echo "β URL: https://${DOMAIN}" | |
touch ${PROJECT_PATH}/.install |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment