Skip to content

Instantly share code, notes, and snippets.

@daif
Created December 8, 2019 08:07
Show Gist options
  • Save daif/c50198735ca2383f14f9e075f7f36714 to your computer and use it in GitHub Desktop.
Save daif/c50198735ca2383f14f9e075f7f36714 to your computer and use it in GitHub Desktop.
LAMP installer
#!/bin/bash
# Copyright (C) 2019 Daif Alazmi
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
##################################################################
#
# Project: LAMP installer
# Version: 1.0.0
# Date: 2019-12-08
# Author: Daif Alazmi <[email protected]>
#
##################################################################
# Default configurations
TIMEZONE='Asia/Riyadh'
SITE_URL='localhost'
SITE_USR='developer'
MYSQL_PASS='toor'
SERVER_OS=`/usr/bin/lsb_release -ds | awk '{print $1}'`
SERVER_VER=`/usr/bin/lsb_release -ds | awk '{print $2}' | cut -d. -f1,2`
SITE_URL=`sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< "$SITE_URL"`
CHECK_USR=`id -u $SITE_USR 2>/dev/null || echo -1`
CHECK_HOST=`cat /etc/hosts | grep ${SITE_URL}`
# Exit if the current user is not root.
[[ $EUID -ne 0 ]] && echo -e "\033[0;31m \n>\n> Error: This script must be run as root! ... \n>\n\033[0m" && exit 1
# Exit if server is not Ubuntu 18.04
[[ $SERVER_OS != 'Ubuntu' || $SERVER_VER != '18.04' ]] && echo -e "\033[0;31m \n>\n> Error: This script required Ubuntu 18.04 ... \n>\n\033[0m" && exit 1
# Exit if user is not exists
[[ $SITE_USR == '' || $CHECK_USR == '-1' ]] && echo -e "\033[0;31m \n>\n> Error: User ${SITE_USR} is not exist ... \n>\n\033[0m" && exit 1
# Exit if mysql password is not set
[[ $MYSQL_PASS == '' ]] && echo -e "\033[0;31m \n>\n> Error: please set MYSQL_PASS ... \n>\n\033[0m" && exit 1
##################################################################
# 1 - Updating system
##################################################################
echo -e "\033[0;33m \n>\n> Updating system packages... \n>\n\033[0m"
apt -y update
apt -y upgrade
##################################################################
# 2 - Set timezone
##################################################################
echo -e "\033[0;33m \n>\n> Setting timezone to ${TIMEZONE}... \n>\n\033[0m"
timedatectl set-timezone ${TIMEZONE}
timedatectl
##################################################################
# 3 - Installing Apache2
##################################################################
echo -e "\033[0;33m \n>\n> Installing Apache2... \n>\n\033[0m"
apt -y install apache2 apache2-utils
##################################################################
# 4 - Configure Apache2
##################################################################
echo -e "\033[0;33m \n>\n> Configure Apache2... \n>\n\033[0m"
a2enmod actions alias rewrite proxy_fcgi fcgid setenvif headers
a2dismod -f status autoindex
cat <<EOT > /etc/apache2/conf-available/httpoxy.conf
<IfModule mod_headers.c>
RequestHeader unset Proxy early
</IfModule>
EOT
a2enconf httpoxy
systemctl restart apache2
##################################################################
# 5 - Installing MariaDB
##################################################################
echo -e "\033[0;33m \n>\n> Installing MariaDB... \n>\n\033[0m"
apt -y install mariadb-server mariadb-client libmysqlclient-dev
##################################################################
# 6 - Securing database
##################################################################
echo -e "\033[0;33m \n>\n> Securing database... \n>\n\033[0m"
mysql -e "DELETE FROM mysql.user WHERE User='';"
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
mysql -e "DROP DATABASE IF EXISTS test;"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -e "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root';"
mysql -e "UPDATE mysql.user SET Password=PASSWORD('${MYSQL_PASS}') WHERE User='root';"
mysql -e "FLUSH PRIVILEGES;"
echo -e "Database password = ${MYSQL_PASS} \n"
##################################################################
# 7 - Installing PHP7.2
##################################################################
echo -e "\033[0;33m \n>\n> Installing PHP... \n>\n\033[0m"
apt -y install php7.2-fpm php7.2-common php7.2-cli
##################################################################
# 8 - Installing PHP7.2 modules
##################################################################
echo -e "\033[0;33m \n>\n> Installing PHP modules... \n>\n\033[0m"
apt -y install php7.2-mysql php7.2-xml php7.2-xmlrpc php7.2-curl php7.2-gd php7.2-imagick php7.2-dev php7.2-imap php7.2-mbstring php7.2-soap php7.2-zip php7.2-bcmath
##################################################################
# 9 - Configure PHP
##################################################################
echo -e "\033[0;33m \n>\n> Configuring PHP... \n>\n\033[0m"
# enable PHP as fast CGI
a2enconf php7.2-fpm
# create new pool for the user
cat <<EOT > /etc/php/7.2/fpm/pool.d/${SITE_USR}.conf
[${SITE_USR}]
user = ${SITE_USR}
group = ${SITE_USR}
listen = /run/php/php7.2-fpm-${SITE_USR}.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
php_admin_value[memory_limit] = 2048M
php_admin_value[upload_max_filesize] = 64M
php_admin_value[max_file_uploads] = 64
php_admin_value[post_max_size] = 64M
php_admin_value[date.timezone] = ${TIMEZONE}
php_admin_value[error_log] = /home/${SITE_USR}/${SITE_URL}/logs/php7.2-fpm.log
php_admin_flag[log_errors] = on
pm.status_path = /status
ping.path = /ping
ping.response = pong
request_slowlog_timeout = 10s
slowlog = /home/${SITE_USR}/${SITE_URL}/logs/php7.2-fpm.log.slow
EOT
# make logs dir
mkdir -p /home/${SITE_USR}/${SITE_URL}/public/
mkdir -p /home/${SITE_USR}/${SITE_URL}/logs/
chown -R ${SITE_USR}:${SITE_USR} /home/${SITE_USR}/${SITE_URL}
# restart PHP service
systemctl restart php7.2-fpm
# create new site
cat <<EOT > /etc/apache2/sites-available/${SITE_URL}.conf
<VirtualHost ${SITE_URL}:80>
ServerName ${SITE_URL}
DocumentRoot /home/${SITE_USR}/${SITE_URL}/public/
<Directory /home/${SITE_USR}/${SITE_URL}/public>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch ".php$">
SetHandler "proxy:unix:/run/php/php7.2-fpm-${SITE_USR}.sock|fcgi://localhost/"
</FilesMatch>
ErrorLog /home/${SITE_USR}/${SITE_URL}/logs/error.log
CustomLog /home/${SITE_USR}/${SITE_URL}/logs/access.log combined
# Enable 'status' and 'ping' page
<LocationMatch "/(ping|status)">
SetHandler "proxy:unix:/run/php/php7.2-fpm-${SITE_USR}.sock|fcgi://localhost"
</LocationMatch>
<IfModule alias_module>
Alias /realtime-status "/usr/share/php/7.2/fpm/status.html"
</IfModule>
</VirtualHost>
EOT
# check if domain in hosts file
if [ $CHECK_HOST == '' ]; then
echo -e "127.0.0.1 ${SITE_URL}" >> /etc/hosts
fi
# enable the site
a2ensite ${SITE_URL}.conf
# restart Apache to apply changes
systemctl restart apache2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment