install-debian-server.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "This script has been tested only on Linux Debian 7"
  10. _cwd="$(pwd)"
  11. echo "Installing harden"
  12. apt-get install harden
  13. echo "Installing ufw and setup firewall (allowing only ssh and http)"
  14. apt-get install ufw
  15. ufw allow ssh
  16. ufw allow http
  17. ufw enable
  18. ufw status verbose
  19. echo "Create new user (you will be asked a user name and a password)"
  20. read -p "Enter user name: " user
  21. # read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
  22. adduser "$user"
  23. echo "adding $user to admin group and limiting su to the admin group"
  24. groupadd admin
  25. usermod -a -G admin "$user"
  26. dpkg-statoverride --update --add root admin 4750 /bin/su
  27. echo "Securing ssh (disabling root login)"
  28. sed -i 's/PermitRootLogin\ yes/PermitRootLogin no/g' /etc/ssh/sshd_config
  29. sed -i 's/PermitEmptyPasswords\ yes/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
  30. sed -i 's/Protocol\ [0-9]/Protocol 2/g' /etc/ssh/sshd_config
  31. echo "Installing AMP web server"
  32. echo "Installing Apache2"
  33. apt-get install apache2
  34. a2enmod rewrite
  35. service apache2 restart
  36. echo "installing Mysql"
  37. apt-get install mysql-server
  38. mysql_secure_installation
  39. echo "Installing PHP"
  40. apt-get install php5 php-pear
  41. echo "Configuring PHP"
  42. cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.back
  43. sed -i "s/max_execution_time\ =\ [0-9]\+/max_execution_time = 60/g" /etc/php5/apache2/php.ini
  44. sed -i "s/max_input_time\ =\ [0-9]\+/max_input_time = 60/g" /etc/php5/apache2/php.ini
  45. sed -i "s/memory_limit\ =\ [0-9]\+M/memory_limit = 512M/g" /etc/php5/apache2/php.ini
  46. 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
  47. sed -i "s/;\?display_errors\ =\ On/display_errors = Off/g" /etc/php5/apache2/php.ini
  48. sed -i "s/;\?log_errors\ =\ Off/log_errors = On/g" /etc/php5/apache2/php.ini
  49. # following command doesn't work, make teh change manualy
  50. #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
  51. echo "register_globals = Off" >> /etc/php5/apache2/php.ini
  52. mkdir /var/log/php
  53. chown www-data /var/log/php
  54. apt-get install php5-mysql
  55. echo "installing vhost"
  56. read -p "hostname ? " _host_name
  57. cp "$_cwd"/example.org.conf /etc/apache2/sites-available/"$_host_name".conf
  58. sed -ir "s/example\.org/$_host_name/g" /etc/apache2/sites-available/"$_host_name".conf
  59. mkdir -p /srv/www/"$_host_name"/public_html
  60. mkdir /srv/www/"$_host_name"/logs
  61. #set proper right to user will handle the app
  62. chown -R root:admin /srv/www/"$_host_name"/
  63. chmod -R g+w /srv/www/"$_host_name"/
  64. chmod -R g+r /srv/www/"$_host_name"/
  65. # create a shortcut to the site
  66. mkdir /home/"$user"/www/
  67. chown "$user":admin /home/"$user"/www/
  68. ln -s /srv/www/"$_host_name" /home/"$user"/www/"$_host_name"
  69. #activate teh vhost
  70. a2ensite "$_host_name".conf
  71. #restart apache
  72. service apache2 restart
  73. #installing better prompt and some goodies for root
  74. echo "shell prompt"
  75. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd -
  76. # setup user environment
  77. echo "$user tasks"
  78. su $user
  79. cd ~
  80. echo "shell prompt"
  81. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd -
  82. cd ~
  83. source .bashrc
  84. # setup bare repositorie to push to
  85. echo "setup git repositories"
  86. mkdir ~/git-repositories
  87. mkdir ~/git-repositories/"$_host_name".git
  88. cd ~/git-repositories/"$_host_name".git
  89. git init --bare
  90. # setup git repo on site folder
  91. cd /srv/www/"$_host_name"/public_html/
  92. git init
  93. # link to the bare repo
  94. git remote add origin ~/git-repositories/"$_host_name".git
  95. # cerate hooks that will update the site repo
  96. cd ~
  97. cp "$_cwd"/git-pre-receive ~/git-repositories/"$_host_name".git/hooks/pre-receive
  98. cp "$_cwd"/git-post-receive ~/git-repositories/"$_host_name".git/hooks/post-receive
  99. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" ~/git-repositories/"$_host_name".git/hooks/pre-receive
  100. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" ~/git-repositories/"$_host_name".git/hooks/post-receive
  101. cd ~/git-repositories/"$_host_name".git/hooks/
  102. chmod +x post-receive pre-receive
  103. # done
  104. echo "install succeed"
  105. echo "your site stay now to ~/www/$_host_name"
  106. echo "you can push updates on prod branch throug $user@IP.IP.IP.IP:git-repositories/$_host_name.git"