Skip to content

Instantly share code, notes, and snippets.

@inderpreet
Last active Aug 3, 2021
Embed
What would you like to do?
#!/bin/bash
# @Brief Script to install mysql and python with apache2 and sample codes
# @author Inderpreet Singh(inderpreet.v1@gmail.com)
chek_sudo(){
if [ "$EUID" -ne 0 ]; then
echo "Please run with sudo"
exit
fi
}
detect_os(){
NAME=`uname -a`
if [[ "$NAME" == *"aspberry"* ]]; then
echo "Raspbian OS detected..."
return 0
elif [[ "$NAME" == *"buntu"* ]]; then
echo "Ubuntu Detected..."
return 1
fi
}
get_input(){
read -p "Do you want to continue?(y/n)" yn
if [[ "$yn" != "y" ]];then
exit 0
fi
}
purge_sys(){
read -p "Do you want me to remove the exisiting database system, apache2 ?(y/n)" yn
if [[ "$yn" == "y" ]]; then
echo "purging..."
sudo apt purge -y apache2 mariadb* mysql*
fi
}
update_install(){
read -p "Install updates, Apache 2 and Python 2?(y/n)" yn
if [[ "$yn" == "y" ]]; then
sudo apt update
sudo apt upgrade -y
sudo apt install -y apache2 python
fi
}
install_db(){
os=detect_os
if (( detect_os == 0 )); then
read -p "Installing MariaDB?(y/n)" yn
if [[ "$yn" == "y" ]]; then
sudo apt install mariadb-server
fi
elif (( detect_os == 1 )); then
read -p "Installing MySQL>(y/n)" yn
if [[ "$yn" == "y" ]]; then
sudo apt install mysql-server
fi
fi
}
create_apache2_conf(){
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2_bak.conf
sudo cat > /etc/apache2/apache2.conf <<-'EOF'
DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# Code added By Prof. Inderpreet Singh
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
AllowOverride None
Order allow,deny
Allow from all
AddHandler cgi-script .py .sh .cgi
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
EOF
}
apache_test_files(){
sudo cat > /var/www/html/test.htm <<-'EOF'
<!DOCTYPE HTML>
<html>
<body>
<h1>Test Page By Prof. Inderpreet Singh</h1>
<p>User Form Test.</p>
<p>This form will test the installation. Clicking the Submit button will execute a python script if everything is OK.</p>
<form action="/cgi-bin/test.py" method="POST">
<p>
LED State : <input type="checkbox" name="led" value="power_on"/>
</p>
<p> <input type="submit"></p> GP
</form>
</body>
</html>
EOF
sudo cat > /usr/lib/cgi-bin/test.py <<-'EOF'
#!/usr/bin/python
# By: Inderpreet Singh
import cgi, cgitb
import RPi.GPIO as gpio
LED = 7
gpio.setmode(gpio.BCM)
gpio.setup(LED, gpio.OUT)
form = cgi.FieldStorage()
userin = form.getvalue('led')
print "Content-type: text/html\r\n\r\n"
print "<html><body>"
if ( userin == "power_on"):
print "<h1> Checkbox was Checked. Click BACK<br></h1>"
else:
print "<h1> Checkbox was Unchecked. Click BACK<br></h1>"
print "</body></html>"
EOF
sudo chmod +x /usr/lib/cgi-bin/test.py
}
mn(){
chek_sudo
echo "Script to setup DB, Apache2 and python"
echo "--------------------------------------"
echo "Written By: Inderpreet Singh"
read -p "Press ENTER to continue..."
purge_sys
update_install
install_db
create_apache2_conf
sudo a2enmod cgid
systemctl restart apache2
apache_test_files
echo "open a browser and goto http://localhost/test.htm"
}
mn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment