install-debian-server.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "\033[35;1mThis script has been tested only on Linux Debian 7 \033[0m"
  10. echo "Please run this script as root"
  11. echo -n "Should we start? [Y|n] "
  12. read yn
  13. yn=${yn:-y}
  14. if [ "$yn" != "y" ]; then
  15. echo "aborting script!"
  16. exit
  17. fi
  18. echo "* * *"
  19. apt-get update
  20. apt-get upgrade
  21. # get the current position
  22. _cwd="$(pwd)"
  23. echo "\033[35;1mInstalling harden \033[0m"
  24. sleep 5
  25. apt-get install harden
  26. echo "Harden instaled"
  27. echo "* * *"
  28. echo "\033[35;1mInstalling ufw and setup firewall (allowing only ssh and http) \033[0m"
  29. sleep 5
  30. apt-get install ufw
  31. ufw allow ssh
  32. ufw allow http
  33. ufw enable
  34. ufw status verbose
  35. echo "ufw installed and firwall configured"
  36. echo "* * *"
  37. echo "\033[35;1mCreate new user (you will be asked a user name and a password) \033[0m"
  38. sleep 5
  39. echo -n "Enter user name: "
  40. read user
  41. # read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
  42. adduser "$user"
  43. echo "adding $user to admin group and limiting su to the admin group"
  44. groupadd admin
  45. usermod -a -G admin "$user"
  46. dpkg-statoverride --update --add root admin 4750 /bin/su
  47. echo "user $user configured"
  48. echo "* * *"
  49. while [ "$securssh" != "y" ] && [ "$securssh" != "n" ]
  50. do
  51. echo -n "Securing ssh (disabling root login)? [y|n] "
  52. read securssh
  53. # securssh=${securssh:-y}
  54. done
  55. if [ "$securssh" = "y" ]; then
  56. sed -i 's/PermitRootLogin\ yes/PermitRootLogin no/g' /etc/ssh/sshd_config
  57. sed -i 's/PermitEmptyPasswords\ yes/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
  58. sed -i 's/Protocol\ [0-9]/Protocol 2/g' /etc/ssh/sshd_config
  59. echo "SSH secured"
  60. else
  61. echo 'root user can stile coonect through ssh'
  62. fi
  63. echo "* * *"
  64. echo "\033[35;1mInstalling AMP web server \033[0m"
  65. echo "\033[35;1mInstalling Apache2 \033[0m"
  66. sleep 5
  67. apt-get install apache2
  68. a2enmod rewrite
  69. service apache2 restart
  70. echo "Apache2 installed"
  71. echo "* * *"
  72. echo "\033[35;1minstalling Mysql \033[0m"
  73. sleep 5
  74. apt-get install mysql-server
  75. mysql_secure_installation
  76. echo "mysql installed"
  77. echo "* * *"
  78. echo "\033[35;1mInstalling PHP \033[0m"
  79. sleep 5
  80. apt-get install php5 php-pear php5-gd
  81. echo "Configuring PHP"
  82. cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.back
  83. sed -i "s/max_execution_time\ =\ [0-9]\+/max_execution_time = 60/g" /etc/php5/apache2/php.ini
  84. sed -i "s/max_input_time\ =\ [0-9]\+/max_input_time = 60/g" /etc/php5/apache2/php.ini
  85. sed -i "s/memory_limit\ =\ [0-9]\+M/memory_limit = 512M/g" /etc/php5/apache2/php.ini
  86. 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
  87. sed -i "s/;\?display_errors\ =\ On/display_errors = Off/g" /etc/php5/apache2/php.ini
  88. sed -i "s/;\?log_errors\ =\ Off/log_errors = On/g" /etc/php5/apache2/php.ini
  89. # following command doesn't work, make teh change manualy
  90. #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
  91. echo "register_globals = Off" >> /etc/php5/apache2/php.ini
  92. mkdir /var/log/php
  93. chown www-data /var/log/php
  94. apt-get install php5-mysql
  95. echo "php installed"
  96. echo "* * *"
  97. echo "\033[35;1mInstalling Awstat \033[0m"
  98. sleep 5
  99. apt-get install awstats
  100. echo "Awstat installed"
  101. echo "* * *"
  102. echo "\033[35;1mVHOST install \033[0m"
  103. while [ "$vh" != "y" ] && [ "$vh" != "n" ]
  104. do
  105. echo -n "Should we install a vhost? [y|n] "
  106. read vh
  107. # vh=${vh:-y}
  108. done
  109. if [ "$vh" = "y" ]; then
  110. while [ "$_host_name" = "" ]
  111. do
  112. read -p "enter a hostname ? " _host_name
  113. if [ "$_host_name" != "" ]; then
  114. read -p "is hostname $_host_name correcte [y|n] " validated
  115. if [ "$validated" = "y" ]; then
  116. break
  117. else
  118. _host_name=""
  119. fi
  120. fi
  121. done
  122. cp "$_cwd"/assets/example.org.conf /etc/apache2/sites-available/"$_host_name".conf
  123. sed -ir "s/example\.org/$_host_name/g" /etc/apache2/sites-available/"$_host_name".conf
  124. mkdir -p /srv/www/"$_host_name"/public_html
  125. mkdir /srv/www/"$_host_name"/logs
  126. #set proper right to user will handle the app
  127. chown -R root:admin /srv/www/"$_host_name"/
  128. chmod -R g+w /srv/www/"$_host_name"/
  129. chmod -R g+r /srv/www/"$_host_name"/
  130. # create a shortcut to the site
  131. mkdir /home/"$user"/www/
  132. chown "$user":admin /home/"$user"/www/
  133. ln -s /srv/www/"$_host_name" /home/"$user"/www/"$_host_name"
  134. #activate the vhost
  135. a2ensite "$_host_name".conf
  136. #restart apache
  137. service apache2 restart
  138. echo "vhost $_host_name configured"
  139. else
  140. echo "Vhost installation aborted"
  141. fi
  142. echo "* * *"
  143. #installing better prompt and some goodies for root
  144. echo "\033[35;1mInstalling shell prompt for root \033[0m"
  145. sleep 5
  146. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd ~
  147. source ~/.bashrc
  148. echo "done"
  149. echo "* * *"
  150. # __ _______ __________
  151. # / / / / ___// ____/ __ \
  152. # / / / /\__ \/ __/ / /_/ /
  153. # / /_/ /___/ / /___/ _, _/
  154. # \____//____/_____/_/ |_|
  155. # setup user environment
  156. echo "\033[35;1mInstalling shell prompt for $user \033[0m"
  157. sleep 5
  158. sudo -u $user -H sh -c "cd ~; git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd ~"
  159. echo "done"
  160. echo "* * *"
  161. # setup bare repositorie to push to
  162. echo "\033[35;1msetup git repositorie \033[0m"
  163. while [ "$gr" != "y" ] && [ "$gr" != "n" ]
  164. do
  165. echo -n "Should we install a git repos for $_host_name in $user home? [y|n] "
  166. read gr
  167. done
  168. sudo -u $user -H sh -c "mkdir ~/git-repositories; mkdir ~/git-repositories/$_host_name.git; cd ~/git-repositories/$_host_name.git; git init --bare"
  169. # setup git repo on site folder
  170. cd /srv/www/"$_host_name"/public_html/
  171. git init
  172. # link to the bare repo
  173. git remote add origin /home/"$user"/git-repositories/"$_host_name".git
  174. # create hooks that will update the site repo
  175. cd ~
  176. cp "$_cwd"/assets/git-pre-receive /home/"$user"/git-repositories/"$_host_name".git/hooks/pre-receive
  177. cp "$_cwd"/assets/git-post-receive /home/"$user"/git-repositories/"$_host_name".git/hooks/post-receive
  178. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" /home/"$user"/git-repositories/"$_host_name".git/hooks/pre-receive
  179. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" /home/"$user"/git-repositories/"$_host_name".git/hooks/post-receive
  180. cd /home/"$user"/git-repositories/"$_host_name".git/hooks/
  181. chmod +x post-receive pre-receive
  182. # done
  183. echo "git repos for $_host_name install succeed"
  184. echo "your site stay now to /home/$user/www/$_host_name"
  185. echo "you can push updates on prod branch through $user@IP.IP.IP.IP:git-repositories/$_host_name.git"
  186. echo "* * *"