install-debian-server.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/bin/sh
  2. # bachir soussi chiadmi
  3. #
  4. # http://www.debian.org/doc/manuals/securing-debian-howto/
  5. # https://www.thefanclub.co.za/how-to/how-secure-ubuntu-1204-lts-server-part-1-basics
  6. # https://www.linode.com/docs/websites/lamp/lamp-server-on-debian-7-wheezy/
  7. # http://web-74.com/blog/reseaux/gerer-le-deploiement-facilement-avec-git/
  8. #
  9. echo '
  10. ____ __ _ _____
  11. / __ \___ / /_ (_)___ _____ / ___/___ ______ _____ _____
  12. / / / / _ \/ __ \/ / __ `/ __ \ \__ \/ _ \/ ___/ | / / _ \/ ___/
  13. / /_/ / __/ /_/ / / /_/ / / / / ___/ / __/ / | |/ / __/ /
  14. /_____/\___/_.___/_/\__,_/_/ /_/ /____/\___/_/ |___/\___/_/
  15. '
  16. echo "\033[35;1mThis script has been tested only on Linux Debian 7 \033[0m"
  17. echo "Please run this script as root"
  18. echo -n "Should we start? [Y|n] "
  19. read yn
  20. yn=${yn:-y}
  21. if [ "$yn" != "y" ]; then
  22. echo "aborting script!"
  23. exit
  24. fi
  25. echo '
  26. __ ______ __________ ___ ____ ______
  27. / / / / __ \/ ____/ __ \/ | / __ \/ ____/
  28. / / / / /_/ / / __/ /_/ / /| | / / / / __/
  29. / /_/ / ____/ /_/ / _, _/ ___ |/ /_/ / /___
  30. \____/_/ \____/_/ |_/_/ |_/_____/_____/
  31. '
  32. apt-get update
  33. apt-get upgrade
  34. # get the current position
  35. _cwd="$(pwd)"
  36. echo '
  37. __ _____ ____ ____ _______ __
  38. / / / / | / __ \/ __ \/ ____/ | / /
  39. / /_/ / /| | / /_/ / / / / __/ / |/ /
  40. / __ / ___ |/ _, _/ /_/ / /___/ /| /
  41. /_/ /_/_/ |_/_/ |_/_____/_____/_/ |_/
  42. '
  43. echo "\033[35;1mInstalling harden \033[0m"
  44. sleep 3
  45. apt-get install harden
  46. echo "Harden instaled"
  47. echo "033[92;1m* * *033[Om"
  48. echo '
  49. ______________ _______ _____ __ __
  50. / ____/ _/ __ \/ ____/ | / / | / / / /
  51. / /_ / // /_/ / __/ | | /| / / /| | / / / /
  52. / __/ _/ // _, _/ /___ | |/ |/ / ___ |/ /___/ /___
  53. /_/ /___/_/ |_/_____/ |__/|__/_/ |_/_____/_____/
  54. '
  55. echo "\033[35;1mInstalling ufw and setup firewall (allowing only ssh and http) \033[0m"
  56. sleep 3
  57. apt-get install ufw
  58. ufw allow ssh
  59. ufw allow http
  60. ufw enable
  61. ufw status verbose
  62. echo "ufw installed and firwall configured"
  63. echo "033[92;1m* * *033[Om"
  64. echo '
  65. __ _______ __________
  66. / / / / ___// ____/ __ \
  67. / / / /\__ \/ __/ / /_/ /
  68. / /_/ /___/ / /___/ _, _/
  69. \____//____/_____/_/ |_|
  70. '
  71. echo "\033[35;1mCreate new user (you will be asked a user name and a password) \033[0m"
  72. sleep 3
  73. echo -n "Enter user name: "
  74. read user
  75. # read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
  76. adduser "$user"
  77. echo "adding $user to admin group and limiting su to the admin group"
  78. groupadd admin
  79. usermod -a -G admin "$user"
  80. dpkg-statoverride --update --add root admin 4750 /bin/su
  81. echo "user $user configured"
  82. echo "033[92;1m* * *033[Om"
  83. echo '
  84. __________ __ __
  85. / ___/ ___// / / /
  86. \__ \\__ \/ /_/ /
  87. ___/ /__/ / __ /
  88. /____/____/_/ /_/
  89. '
  90. while [ "$securssh" != "y" ] && [ "$securssh" != "n" ]
  91. do
  92. echo -n "Securing ssh (disabling root login)? [y|n] "
  93. read securssh
  94. # securssh=${securssh:-y}
  95. done
  96. if [ "$securssh" = "y" ]; then
  97. sed -i 's/PermitRootLogin\ yes/PermitRootLogin no/g' /etc/ssh/sshd_config
  98. sed -i 's/PermitEmptyPasswords\ yes/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
  99. sed -i 's/Protocol\ [0-9]/Protocol 2/g' /etc/ssh/sshd_config
  100. service ssh reload
  101. echo "SSH secured"
  102. else
  103. echo 'root user can stile coonect through ssh'
  104. fi
  105. echo "033[92;1m* * *033[Om"
  106. echo "\033[35;1mInstalling AMP web server \033[0m"
  107. echo '
  108. ___ __ ___
  109. / | ____ ____ ______/ /_ ___ |__ \
  110. / /| | / __ \/ __ `/ ___/ __ \/ _ \__/ /
  111. / ___ |/ /_/ / /_/ / /__/ / / / __/ __/
  112. /_/ |_/ .___/\__,_/\___/_/ /_/\___/____/
  113. /_/
  114. '
  115. echo "\033[35;1mInstalling Apache2 \033[0m"
  116. sleep 3
  117. apt-get install apache2
  118. a2enmod rewrite
  119. cat "$_cwd"/assets/apache2.conf > /etc/apache2/apache2.conf
  120. # Change logrotate for Apache2 log files to keep 10 days worth of logs
  121. sed -i 's/\tweekly/\tdaily/' /etc/logrotate.d/apache2
  122. sed -i 's/\trotate .*/\trotate 10/' /etc/logrotate.d/apache2
  123. # Remove Apache server information from headers.
  124. sed -i 's/ServerTokens .*/ServerTokens Prod/' /etc/apache2/conf.d/security
  125. sed -i 's/ServerSignature .*/ServerSignature Off/' /etc/apache2/conf.d/security
  126. service apache2 restart
  127. echo "Apache2 installed"
  128. echo "033[92;1m* * *033[Om"
  129. echo '
  130. __ ___ __
  131. / |/ /_ ___________ _/ /
  132. / /|_/ / / / / ___/ __ `/ /
  133. / / / / /_/ (__ ) /_/ / /
  134. /_/ /_/\__, /____/\__, /_/
  135. /____/ /_/
  136. '
  137. echo "\033[35;1minstalling Mysql \033[0m"
  138. sleep 3
  139. apt-get install mysql-server
  140. mysql_secure_installation
  141. echo "mysql installed"
  142. echo "033[92;1m* * *033[Om"
  143. echo '
  144. ____ __ ______
  145. / __ \/ / / / __ \
  146. / /_/ / /_/ / /_/ /
  147. / ____/ __ / ____/
  148. /_/ /_/ /_/_/
  149. '
  150. echo "\033[35;1mInstalling PHP \033[0m"
  151. sleep 3
  152. apt-get install php5 php-pear php5-gd
  153. echo "Configuring PHP"
  154. cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.back
  155. sed -i "s/max_execution_time\ =\ [0-9]\+/max_execution_time = 60/g" /etc/php5/apache2/php.ini
  156. sed -i "s/max_input_time\ =\ [0-9]\+/max_input_time = 60/g" /etc/php5/apache2/php.ini
  157. sed -i "s/memory_limit\ =\ [0-9]\+M/memory_limit = 512M/g" /etc/php5/apache2/php.ini
  158. sed -i "s/;\?error_reporting\ =\ [^\n]\+/error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR/g" /etc/php5/apache2/php.ini
  159. sed -i "s/;\?display_errors\ =\ On/display_errors = Off/g" /etc/php5/apache2/php.ini
  160. sed -i "s/;\?log_errors\ =\ Off/log_errors = On/g" /etc/php5/apache2/php.ini
  161. # following command doesn't work, make teh change manualy
  162. #sed -ri ":a;$!{N;ba};s/;\?\ \?error_log\ =\ [^\n]\+([^\n]*\n(\n|$))/error_log = \/var\/log\/php\/error.log\1/g" /etc/php5/apache2/php.ini
  163. echo "register_globals = Off" >> /etc/php5/apache2/php.ini
  164. mkdir /var/log/php
  165. chown www-data /var/log/php
  166. apt-get install php5-mysql
  167. echo "php installed"
  168. echo "033[92;1m* * *033[Om"
  169. echo '
  170. __ __ ___ ___ __ _
  171. ____ / /_ ____ / |/ /_ __/ | ____/ /___ ___ (_)___
  172. / __ \/ __ \/ __ \/ /|_/ / / / / /| |/ __ / __ `__ \/ / __ \
  173. / /_/ / / / / /_/ / / / / /_/ / ___ / /_/ / / / / / / / / / /
  174. / .___/_/ /_/ .___/_/ /_/\__, /_/ |_\__,_/_/ /_/ /_/_/_/ /_/
  175. /_/ /_/ /____/
  176. '
  177. echo "\033[35;1mInstalling phpMyAdmin \033[0m"
  178. apt-get install phpmyadmin
  179. echo "phpMyAdmin installed"
  180. echo "033[92;1m* * *033[Om"
  181. echo '
  182. __ __
  183. _ __/ /_ ____ _____/ /_
  184. | | / / __ \/ __ \/ ___/ __/
  185. | |/ / / / / /_/ (__ ) /_
  186. |___/_/ /_/\____/____/\__/
  187. '
  188. echo "\033[35;1mVHOST install \033[0m"
  189. while [ "$vh" != "y" ] && [ "$vh" != "n" ]
  190. do
  191. echo -n "Should we install a vhost? [y|n] "
  192. read vh
  193. # vh=${vh:-y}
  194. done
  195. if [ "$vh" = "y" ]; then
  196. while [ "$_host_name" = "" ]
  197. do
  198. read -p "enter a hostname ? " _host_name
  199. if [ "$_host_name" != "" ]; then
  200. read -p "is hostname $_host_name correcte [y|n] " validated
  201. if [ "$validated" = "y" ]; then
  202. break
  203. else
  204. _host_name=""
  205. fi
  206. fi
  207. done
  208. cp "$_cwd"/assets/example.org.conf /etc/apache2/sites-available/"$_host_name".conf
  209. sed -ir "s/example\.org/$_host_name/g" /etc/apache2/sites-available/"$_host_name".conf
  210. mkdir -p /srv/www/"$_host_name"/public_html
  211. mkdir /srv/www/"$_host_name"/logs
  212. #set proper right to user will handle the app
  213. chown -R root:admin /srv/www/"$_host_name"/
  214. chmod -R g+w /srv/www/"$_host_name"/
  215. chmod -R g+r /srv/www/"$_host_name"/
  216. # create a shortcut to the site
  217. mkdir /home/"$user"/www/
  218. chown "$user":admin /home/"$user"/www/
  219. ln -s /srv/www/"$_host_name" /home/"$user"/www/"$_host_name"
  220. #activate the vhost
  221. a2ensite "$_host_name".conf
  222. #restart apache
  223. service apache2 restart
  224. echo "vhost $_host_name configured"
  225. else
  226. echo "Vhost installation aborted"
  227. fi
  228. echo "033[92;1m* * *033[Om"
  229. echo '
  230. ___ __ __
  231. / |_ _______/ /_____ _/ /_
  232. / /| | | /| / / ___/ __/ __ `/ __/
  233. / ___ | |/ |/ (__ ) /_/ /_/ / /_
  234. /_/ |_|__/|__/____/\__/\__,_/\__/
  235. '
  236. echo "\033[35;1mInstalling Awstat \033[0m"
  237. sleep 3
  238. apt-get install awstats
  239. # Configure AWStats
  240. temp=`grep -i sitedomain /etc/awstats/awstats.conf.local | wc -l`
  241. if [ $temp -lt 1 ]; then
  242. echo SiteDomain="$_host_name" >> /etc/awstats/awstats.conf.local
  243. fi
  244. # Disable Awstats from executing every 10 minutes. Put a hash in front of any line.
  245. sed -i 's/^[^#]/#&/' /etc/cron.d/awstats
  246. echo "Awstat installed"
  247. echo "033[92;1m* * *033[Om"
  248. # echo '
  249. # ______________ _______
  250. # /_ __/ ____/ |/ / __ \
  251. # / / / __/ / /|_/ / /_/ /
  252. # / / / /___/ / / / ____/
  253. # /_/ /_____/_/ /_/_/
  254. # '
  255. # function check_tmp_secured {
  256. # temp1=`grep -w "/var/tempFS /tmp ext3 loop,nosuid,noexec,rw 0 0" /etc/fstab | wc -l`
  257. # temp2=`grep -w "tmpfs /tmp tmpfs rw,noexec,nosuid 0 0" /etc/fstab | wc -l`
  258. # if [ $temp1 -gt 0 ] || [ $temp2 -gt 0 ]; then
  259. # return 1
  260. # else
  261. # return 0
  262. # fi
  263. # } # End function check_tmp_secured
  264. # function secure_tmp_tmpfs {
  265. # cp /etc/fstab /etc/fstab.bak
  266. # # Backup /tmp
  267. # cp -Rpf /tmp /tmpbackup
  268. # rm -rf /tmp
  269. # mkdir /tmp
  270. # mount -t tmpfs -o rw,noexec,nosuid tmpfs /tmp
  271. # chmod 1777 /tmp
  272. # echo "tmpfs /tmp tmpfs rw,noexec,nosuid 0 0" >> /etc/fstab
  273. # # Restore /tmp
  274. # cp -Rpf /tmpbackup/* /tmp/ >/dev/null 2>&1
  275. # #Remove old tmp dir
  276. # rm -rf /tmpbackup
  277. # # Backup /var/tmp and link it to /tmp
  278. # mv /var/tmp /var/tmpbackup
  279. # ln -s /tmp /var/tmp
  280. # # Copy the old data back
  281. # cp -Rpf /var/tmpold/* /tmp/ >/dev/null 2>&1
  282. # # Remove old tmp dir
  283. # rm -rf /var/tmpbackup
  284. # echo -e "\033[35;1m /tmp and /var/tmp secured using tmpfs. \033[0m"
  285. # } # End function secure_tmp_tmpfs
  286. # check_tmp_secured
  287. # if [ $? = 0 ]; then
  288. # secure_tmp_tmpfs
  289. # else
  290. # echo -e "\033[35;1mFunction canceled. /tmp already secured. \033[0m"
  291. # fi
  292. echo '
  293. ____ __
  294. / __ \_________ ____ ___ ____ / /_
  295. / /_/ / ___/ __ \/ __ `__ \/ __ \/ __/
  296. / ____/ / / /_/ / / / / / / /_/ / /_
  297. /_/ /_/ \____/_/ /_/ /_/ .___/\__/
  298. /_/
  299. '
  300. #installing better prompt and some goodies for root
  301. echo "\033[35;1mInstalling shell prompt for root \033[0m"
  302. sleep 3
  303. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd ~
  304. source ~/.bashrc
  305. echo "done"
  306. echo "033[92;1m* * *033[Om"
  307. echo '
  308. __
  309. ___ ____ ____/ /
  310. / _ \/ __ \/ __ /
  311. / __/ / / / /_/ /
  312. \___/_/ /_/\__,_/
  313. '
  314. echo "\033[35;1m* * script done * * \033[0m"