Académique Documents
Professionnel Documents
Culture Documents
/bin/bash
#loggingmethodfrom:
http://stackoverflow.com/questions/3173131/redirectcopy
ofstdouttologfilefromwithinbashscriptitself
#truncatelogfile
>logfile.txt
#redirectstdoutintoanamedpiperunning"tee"
exec>>(teelogfile.txt)
#redirectstderraswell
exec2>&1
#########################################################
###############
##
##VSPiInstallScript
##====================
##Thisshellscriptinstallsandconfiguresthesoftware
##anddependenciestoruntheVSPiWordpresson
Raspbian.
##
##Visithttp://github.com/villagescienceformore
information.
##
##Originallyfrom
https://github.com/lowendbox/lowendscript
##ModifiedbyNickWynjaon20131007underGPLv3
##
#########################################################
###############
##
##HELPERFUNCTIONS
##
functioncheck_install{
if[z"`which"$1"2>/dev/null`"]
then
executable=$1
shift
while[n"$1"]
do
DEBIAN_FRONTEND=noninteractiveaptgetqqyinstall
"$1"
print_info"$1installedfor$executable"
shift
done
else
print_warn"$2alreadyinstalled"
fi
functioncheck_remove{
if[n"`which"$1"2>/dev/null`"]
then
DEBIAN_FRONTEND=noninteractiveaptgetqyremove
purge"$2"
print_info"$2removed"
else
print_warn"$2isnotinstalled"
fi
functioncheck_sanity{
#Dosomesanitychecking.
if[$(/usr/bin/idu)!="0"]
then
die'Mustberunbyrootuser'
fi
if[!f/etc/debian_version]
then
die"Distributionisnotsupported"
fi
functiondie{
echo"ERROR:$1">/dev/null1>&2
exit1
functionget_domain_name(){
#Gettingridofthelowestpart.
domain=${1%.*}
lowest=`expr"$domain":'.*\.\([az][az]*\)'`
case"$lowest"in
com|net|org|gov|edu|co)
domain=${domain%.*}
;;
esac
lowest=`expr"$domain":'.*\.\([az][az]*\)'`
[z"$lowest"]&&echo"$domain"||echo"$lowest"
functionget_password(){
#Checkwhetherourlocalsaltispresent.
SALT=/var/lib/radom_salt
if[!f"$SALT"]
then
headc512/dev/urandom>"$SALT"
chmod400"$SALT"
fi
password=`(cat"$SALT";echo$1)|md5sum|base64`
echo${password:0:13}
functionprint_info{
echone'\e[1;36m'
echon$1
echoe'\e[0m'
functionprint_warn{
echone'\e[1;33m'
echon$1
echoe'\e[0m'
functionupdate_upgrade{
#Runthroughtheaptgetupdate/upgradefirst.This
shouldbedonebefore
#wetrytoinstallanypackage
print_info"Updatingpackages"
aptgetqqyupdate
aptgetqqyupgrade
functionremove_unneeded{
#SomeDebianhaveportmapinstalled.Wedon'tneedthat.
check_remove/sbin/portmapportmap
#Removersyslogd,whichallocates~30MBprivvmpageson
anOpenVZsystem,
#whichmightmakesomelowendVPSinoperatable.Wewill
dothiseven
#beforerunningaptgetupdate.
check_remove/usr/sbin/rsyslogdrsyslog
#Otherpackagesthatseemtobeprettycommonin
standardOpenVZ
#templates.
check_remove/usr/sbin/apache2'apache2*'
check_remove/usr/sbin/namedbind9
check_remove/usr/sbin/smbd'samba*'
check_remove/usr/sbin/nscdnscd
#Needtostopsendmailasremovingthepackagedoesnot
seemtostopit.
if[f/usr/lib/sm.bin/smtpd]
then
invokerc.dsendmailstop
check_remove/usr/lib/sm.bin/smtpd'sendmail*'
fi
##
##INSTALLANDCONFIGURE
##
functioninstall_vspi{
print_info"InstallingandconfiguringVSPi"
sudomvvspi/etc/vspi
sudochmoda+x/etc/vspi/vspi
sudolns/etc/vspi/vspi/usr/local/bin/
sudochmodR777/etc/vspi
echoe"1.0">/etc/vspi/version
sudochmod777/etc/vspi/version
functioninstall_mysql{
#InstalltheMySQLpackages
sudodebconfsetselections<<<'mysqlserver5.5mysql
server/root_passwordpasswordraspberry'
sudodebconfsetselections<<<'mysqlserver5.5mysql
server/root_password_againpasswordraspberry'
check_installmysqldmysqlserver5.5
#Installalowendcopyofthemy.cnftodisableInnoDB,
andthendelete
#alltherelatedfiles.
mkdirp/etc/mysql/conf.d/
echoe"[mysqld]\
key_buffer=8M\
query_cache_size=0\
skipinnodb">/etc/mysql/conf.d/lowendbox.cnf
echoe"[client]\nuser=root\npassword=raspberry"
>~/.my.cnf
chmod600~/.my.cnf
functioninstall_nginx{
check_installnginxnginx
#NeedtoincreasethebucketsizeforDebian5.
cat>/etc/nginx/conf.d/lowendbox.conf<<END
server_names_hash_bucket_size64;
END
invokerc.dnginxrestart
functioninstall_php{
sudoaptgetyqqinstallphp5php5fpmphppearphp5
mysql
functioninstall_syslogd{
#Wejustneedasimplevanillasyslogd.Alsothereisno
needtologto
#somanyfiles(wasteoffd).Justdumptheminto
#/var/log/(cron/mail/messages)
check_install/usr/sbin/syslogdinetutilssyslogd
invokerc.dinetutilssyslogdstop
forfilein/var/log/*.log/var/log/mail.*/var/log/debug
/var/log/syslog
do
[f"$file"]&&rmf"$file"
done
fordirinfscknews
do
[d"/var/log/$dir"]&&rmrf"/var/log/$dir"
done
cat>/etc/syslog.conf<<END
*.*;mail.none;cron.none/var/log/messages
cron.*/var/log/cron
mail.*/var/log/mail
END
[d/etc/logrotate.d]||mkdirp/etc/logrotate.d
cat>/etc/logrotate.d/inetutilssyslogd<<END
/var/log/cron
/var/log/mail
/var/log/messages{
rotate4
weekly
missingok
notifempty
compress
sharedscripts
postrotate
/etc/init.d/inetutilssyslogdreload>/dev/null
endscript
END
invokerc.dinetutilssyslogdstart
functioninstall_redis{
#redisisusedtocacheWordpresspagestospeedup
responsetime
sudoaptgetqqyinstallredisserver
functioninstall_fonts{
sudoaptgetqqyinstallfontskhmeros
sudoaptgetqqyinstallfontslao
sudoaptgetqqyinstallfontstlwggaruda
functioninstall_wordpress{
check_installwgetwget
sudogitclone
https://github.com/villagescience/wordpress.git/var/www/
$1
sudochownroot:rootR"/var/www/$1"
sudochmod777R"/var/www/$1/wpcontent"
#sudochmod666"/var/www/$1/.htaccess"
sudochmod666"/var/www/$1/wpconfig.php"
#SettinguptheMySQLdatabase
dbname=`echo$1|tr._`
userid=`get_domain_name$1`
#MySQLuseridcannotbemorethan15characterslong
userid="${userid:0:15}"
passwd=`get_password"$userid@mysql"`
sedi"s/database_name_here/$dbname/;s/username_here/
$userid/;s/password_here/$passwd/"\
"/var/www/$1/wpconfig.php"
mysqladmincreate"$dbname"
echo"GRANTALLPRIVILEGESON\`$dbname\`.*TO
\`$userid\`@localhostIDENTIFIEDBY'$passwd';"|\
mysql
rmr/etc/nginx/sitesavailable/default
#SettingupNginxmapping
cat>"/etc/nginx/sitesenabled/$1.conf"<<END
server{
listen80default_server;
server_namevspi;
root/var/www/$1;
location/index.php{
alias/var/www/$1/wpindexredis.php;
location/{
indexwpindexredis.php;
try_files\$uri\$uri//wpindexredis.php?\$args;
location/wpadmin/{
indexindex.php;
try_files\$uri\$uri//index.php\$args;
#Addtrailingslashto/wpadminrequests
rewrite/wpadmin\$\$scheme::/\$host\$uri/permanent;
gzipoff;
#Directivestosendexpiresheadersandturnoff404
errorlogging.
location~*\.(js|css|png|jpg|jpeg|gif|ico)${
expires24h;
log_not_foundoff;
#thispreventshiddenfiles(beginningwithaperiod)
frombeingserved
location~/\.{access_logoff;log_not_foundoff;deny
all;}
location~\.php${
client_max_body_size25M;
try_files\$uri=404;
fastcgi_passunix:/var/run/php5fpm.sock;
fastcgi_indexindex.php;
include/etc/nginx/fastcgi_params;
END
invokerc.dnginxreload
print_info"Issuingcurlcommand"
curld
"weblog_title=VSPi&user_name=admin&admin_password=raspber
ry&admin_password2=raspberry&admin_email=vspi@villagescie
nce.org"http://127.0.0.1/wpadmin/install.php?step=2
>/dev/null2>&1
functionconfig_network{
print_info"Installingnetworkpackages"
sudoaptgetqqyinstallbridgeutilshostapdavahi
daemondnsmasq
print_info"Configuringnetworksetup"
wgethttp://www.daveconroy.com/wp3/wp
content/uploads/2013/07/hostapd.zip
unziphostapd.zip
sudorm/usr/sbin/hostapd
sudomvhostapd/usr/sbin/hostapd.edimax
sudolnsf/usr/sbin/hostapd.edimax/usr/sbin/hostapd
sudochownroot.root/usr/sbin/hostapd
sudochmod755/usr/sbin/hostapd
cat>"/etc/network/interfaces"<<END
autolo
ifaceloinetloopback
ifaceeth0inetdhcp
ifacewlan0inetstatic
address10.0.10.1
netmask255.255.255.0
upiptablesrestore</etc/iptables.ipv4.nat
END
cat>"/etc/hostapd/hostapd.conf"<<END
#Opennetworksetup
interface=wlan0
driver=rtl871xdrv
ssid=VSPiConnect
#setsthemodeofwifi,dependsuponthedevicesyouwill
beusing.Itcanbea,b,g,n.Settingtogensures
backwardcompatiblity.
hw_mode=g
#Setthewifichannel:
channel=6
#Setsauthenticationalgorithm
#1onlyopensystemauthentication
auth_algs=1
wmm_enabled=0
END
#configureipforwading/masqueradefromwlanclientsto
theupstreameth0
#network,sothevspiservesasawirelessrouter,i.e.
clientscan
#connecttotheoutsideinternetaswellasthevspi
pages.
cat>"/etc/iptables.ipv4.nat"<<END
*filter
:INPUTACCEPT[149:13529]
:FORWARDACCEPT[0:0]
:OUTPUTACCEPT[22:2208]
AFORWARDieth0owlan0mstatestate
RELATED,ESTABLISHEDjACCEPT
AFORWARDiwlan0oeth0jACCEPT
COMMIT
*nat
:PREROUTINGACCEPT[75:5274]
:INPUTACCEPT[75:5274]
:OUTPUTACCEPT[3:268]
:POSTROUTINGACCEPT[0:0]
APOSTROUTINGoeth0jMASQUERADE
COMMIT
END
#fortheabovetowork,weneedtoenableipv4
forwardinginthekernel
echo"#allowvspitoactasarouter">>
/etc/sysctl.conf
echo"net.ipv4.ip_forward=1">>/etc/sysctl.conf
#setupdnsmasqasboththeDHCPserverforwlan0aswell
astheDNS
#server.Clientcomputerswhogetdnsserversthrough
DHCPwillsettheir
#DNSserverto10.0.10.1,allowingdnsmasqtoresolve
ourcustom*.vspi
#domainforthem.
#Note:iftheclientcomputerhastheirDNSsetup
manually,e.g.to
#google'sDNS,then*.vspidomainswon'twork.Thiscan
befixedby
#allowingDHCPtosetthem(sometimescalled"use
automaticDNSservers"
#innetworkoptions),ormanuallysettingtheDNSserver
to10.0.10.1.
sudorm/etc/dnsmasq.conf
cat>"/etc/dnsmasq.conf"<<END
interface=wlan0
dhcprange=10.0.10.10,10.0.10.200,12h
#resolvenamesin/etc/hostsunderthe"vspi"domain
domain=vspi
#expandunqualifieddomainstoit.vspi,e.g.
#typing'vspi'intoawebbrowserwillresolveto
#thesameipastypingin'vspi.vspi'
expandhosts
END
#rundnsmasqonstartup
sudoupdaterc.ddnsmasqenable
echoe"DAEMON_CONF='/etc/hostapd/hostapd.conf'">>
/etc/default/hostapd
sudoupdaterc.dhostapdenable
echoe"vspi">/etc/hostname
#resolvevspiasthewlan0router.Notethatdnsmasq
alsoreads/etc/hostsso
#thislinewillallow"vspi"fromaclienttoresolveto
thevspimachine.
echoe"10.0.10.1vspi">/etc/hosts
sudo/etc/init.d/hostname.sh
#########################################################
###############
#STARTOFPROGRAM
#########################################################
###############
exportPATH=/bin:/usr/bin:/sbin:/usr/sbin
check_sanity
update_upgrade
install_vspi
install_mysql
install_nginx
install_php
remove_unneeded
install_syslogd
install_redis
install_fonts
install_wordpressvspi.local
config_network
sudoreboot