install-debian-server.sh 6.5 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 -e "\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 -e "\033[35;1mInstalling harden \033[0m"
  24. sleep 5
  25. apt-get install harden
  26. echo "Harden instaled"
  27. echo "* * *"
  28. echo -e "\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 -e "\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 -e "\033[35;1mInstalling AMP web server \033[0m"
  65. echo -e "\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 -e "\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 -e "\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 -e "\033[35;1mInstalling Awstat \033[0m"
  98. sleep 5
  99. apt-get install awstats
  100. echo "Awstat installed"
  101. echo "* * *"
  102. while [ "$vh" != "y" ] && [ "$vh" != "n" ]
  103. do
  104. echo -n "Should we install a vhost? [y:n] "
  105. read vh
  106. # vh=${vh:-y}
  107. done
  108. if [ "$vh" = "y" ]; then
  109. while [ "$_host_name" = "" ]
  110. do
  111. read -p "enter a hostname ? " _host_name
  112. if [ "$_host_name" != "" ]; then
  113. read -p "is hostname $_host_name correcte [y:n] " validated
  114. if [ "$validated" = "y" ]; then
  115. break
  116. else
  117. _host_name=""
  118. fi
  119. fi
  120. done
  121. cp "$_cwd"/assets/example.org.conf /etc/apache2/sites-available/"$_host_name".conf
  122. sed -ir "s/example\.org/$_host_name/g" /etc/apache2/sites-available/"$_host_name".conf
  123. mkdir -p /srv/www/"$_host_name"/public_html
  124. mkdir /srv/www/"$_host_name"/logs
  125. #set proper right to user will handle the app
  126. chown -R root:admin /srv/www/"$_host_name"/
  127. chmod -R g+w /srv/www/"$_host_name"/
  128. chmod -R g+r /srv/www/"$_host_name"/
  129. # create a shortcut to the site
  130. mkdir /home/"$user"/www/
  131. chown "$user":admin /home/"$user"/www/
  132. ln -s /srv/www/"$_host_name" /home/"$user"/www/"$_host_name"
  133. #activate the vhost
  134. a2ensite "$_host_name".conf
  135. #restart apache
  136. service apache2 restart
  137. echo "vhost $_host_name configured"
  138. else
  139. echo "Vhost installation aborted"
  140. fi
  141. echo "* * *"
  142. #installing better prompt and some goodies for root
  143. echo -e "\033[35;1mInstalling shell prompt for root \033[0m"
  144. sleep 5
  145. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd -
  146. source ~/.bashrc
  147. echo "done"
  148. echo "* * *"
  149. # __ _______ __________
  150. # / / / / ___// ____/ __ \
  151. # / / / /\__ \/ __/ / /_/ /
  152. # / /_/ /___/ / /___/ _, _/
  153. # \____//____/_____/_/ |_|
  154. # setup user environment
  155. echo -e "\033[35;1mInstalling shell prompt for $user \033[0m"
  156. sleep 5
  157. cd ~
  158. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd -
  159. cd ~
  160. source .bashrc
  161. echo "done"
  162. echo "* * *"
  163. # setup bare repositorie to push to
  164. echo -e "\033[35;1msetup git repositories for $_host_name \033[0m"
  165. sleep 5
  166. mkdir ~/git-repositories
  167. mkdir ~/git-repositories/"$_host_name".git
  168. cd ~/git-repositories/"$_host_name".git
  169. git init --bare
  170. # setup git repo on site folder
  171. cd /srv/www/"$_host_name"/public_html/
  172. git init
  173. # link to the bare repo
  174. git remote add origin ~/git-repositories/"$_host_name".git
  175. # create hooks that will update the site repo
  176. cd ~
  177. cp "$_cwd"/assets/git-pre-receive ~/git-repositories/"$_host_name".git/hooks/pre-receive
  178. cp "$_cwd"/assets/git-post-receive ~/git-repositories/"$_host_name".git/hooks/post-receive
  179. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" ~/git-repositories/"$_host_name".git/hooks/pre-receive
  180. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" ~/git-repositories/"$_host_name".git/hooks/post-receive
  181. cd ~/git-repositories/"$_host_name".git/hooks/
  182. chmod +x post-receive pre-receive
  183. # done
  184. echo "git repos for $_host_name install succeed"
  185. echo "your site stay now to ~/www/$_host_name"
  186. echo "you can push updates on prod branch through $user@IP.IP.IP.IP:git-repositories/$_host_name.git"
  187. echo "* * *"