409 lines
11 KiB
Bash
Executable file
409 lines
11 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
# Ce petit script customise une VZ Debian 7
|
|
# pour préparer l'arrivée d'un Drupal :
|
|
# - install Apache/PHP
|
|
# - install mysql ou postgresql
|
|
# - install éventuelle de PHPMyAdmin/PHPPgAdmin
|
|
|
|
TYPE_BDD=""
|
|
INSTALL_PHPBDDADMIN=""
|
|
INSTALL_DRUSH=""
|
|
BDD_CONNEXIONS_OPEN_BAR=""
|
|
NOM_UTILISATEUR="deploy"
|
|
|
|
# Arrêt à la première erreur non-catchée
|
|
set -e
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
$0 [{ -m | -p } [ -l ] [ -a ]] nom_site
|
|
$0 -h
|
|
|
|
-m : site avec MySQL
|
|
-p : site avec PostgreSQL
|
|
-a : ajout de PHPMyAdmin / PHPPgAdmin
|
|
-l : ouverture des connexions à la BDD depuis le LAN
|
|
-d : install drush via php-pear
|
|
nom_site : pseudo du site à créer (nom du répertoire et du fichier du vhost)
|
|
|
|
-h : ce message d'aide
|
|
EOF
|
|
}
|
|
|
|
patch_apache_default_vhost() {
|
|
cat <<EOF
|
|
diff --git a/apache2/sites-available/default b/apache2/sites-available/default
|
|
index b0703f5..3efc3dd 100644
|
|
--- a/apache2/sites-available/default
|
|
+++ b/apache2/sites-available/default
|
|
@@ -1,26 +1,18 @@
|
|
<VirtualHost *:80>
|
|
ServerAdmin webmaster@localhost
|
|
|
|
- DocumentRoot /var/www
|
|
+ DocumentRoot /var/www/default
|
|
<Directory />
|
|
Options FollowSymLinks
|
|
AllowOverride None
|
|
</Directory>
|
|
- <Directory /var/www/>
|
|
+ <Directory /var/www/default/>
|
|
Options Indexes FollowSymLinks MultiViews
|
|
AllowOverride None
|
|
Order allow,deny
|
|
allow from all
|
|
</Directory>
|
|
|
|
- ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
|
|
- <Directory "/usr/lib/cgi-bin">
|
|
- AllowOverride None
|
|
- Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
|
|
- Order allow,deny
|
|
- Allow from all
|
|
- </Directory>
|
|
-
|
|
ErrorLog \${APACHE_LOG_DIR}/error.log
|
|
|
|
# Possible values include: debug, info, notice, warn, error, crit,
|
|
diff --git a/apache2/sites-available/default-ssl b/apache2/sites-available/default-ssl
|
|
index ea454b8..cca8e63 100644
|
|
--- a/apache2/sites-available/default-ssl
|
|
+++ b/apache2/sites-available/default-ssl
|
|
@@ -2,26 +2,18 @@
|
|
<VirtualHost _default_:443>
|
|
ServerAdmin webmaster@localhost
|
|
|
|
- DocumentRoot /var/www
|
|
+ DocumentRoot /var/www/default
|
|
<Directory />
|
|
Options FollowSymLinks
|
|
AllowOverride None
|
|
</Directory>
|
|
- <Directory /var/www/>
|
|
+ <Directory /var/www/default/>
|
|
Options Indexes FollowSymLinks MultiViews
|
|
AllowOverride None
|
|
Order allow,deny
|
|
allow from all
|
|
</Directory>
|
|
|
|
- ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
|
|
- <Directory "/usr/lib/cgi-bin">
|
|
- AllowOverride None
|
|
- Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
|
|
- Order allow,deny
|
|
- Allow from all
|
|
- </Directory>
|
|
-
|
|
ErrorLog \${APACHE_LOG_DIR}/error.log
|
|
|
|
# Possible values include: debug, info, notice, warn, error, crit,
|
|
EOF
|
|
}
|
|
|
|
patch_mysql_access_from_lan() {
|
|
cat <<EOF
|
|
diff --git a/mysql/my.cnf b/mysql/my.cnf
|
|
index 1f96915..94ea010 100644
|
|
--- a/mysql/my.cnf
|
|
+++ b/mysql/my.cnf
|
|
@@ -44,7 +44,7 @@ skip-external-locking
|
|
#
|
|
# Instead of skip-networking the default is now to listen only on
|
|
# localhost which is more compatible and is not less secure.
|
|
-bind-address = 127.0.0.1
|
|
+#bind-address = 127.0.0.1
|
|
#
|
|
# * Fine Tuning
|
|
#
|
|
EOF
|
|
}
|
|
|
|
patch_phpmyadmin_access_from_lan() {
|
|
cat <<EOF
|
|
diff --git a/phpmyadmin/apache.conf b/phpmyadmin/apache.conf
|
|
index 415acb9..7f274a3 100644
|
|
--- a/phpmyadmin/apache.conf
|
|
+++ b/phpmyadmin/apache.conf
|
|
@@ -1,11 +1,14 @@
|
|
# phpMyAdmin default Apache configuration
|
|
|
|
-Alias /phpmyadmin /usr/share/phpmyadmin
|
|
+#Alias /phpmyadmin /usr/share/phpmyadmin
|
|
|
|
<Directory /usr/share/phpmyadmin>
|
|
Options FollowSymLinks
|
|
DirectoryIndex index.php
|
|
|
|
+ Order allow,deny
|
|
+ Allow from 172.20.0.0/16
|
|
+
|
|
<IfModule mod_php5.c>
|
|
AddType application/x-httpd-php .php
|
|
|
|
diff --git a/apache2/sites-available/phpmyadmin b/apache2/sites-available/phpmyadmin
|
|
new file mode 100644
|
|
index 0000000..8d81d37
|
|
--- /dev/null
|
|
+++ b/apache2/sites-available/phpmyadmin
|
|
@@ -0,0 +1,23 @@
|
|
+<VirtualHost *:80>
|
|
+ ServerAdmin webmaster@localhost
|
|
+
|
|
+ DocumentRoot /usr/share/phpmyadmin
|
|
+
|
|
+ Include /etc/apache2/conf.d/phpmyadmin.conf
|
|
+
|
|
+ ServerName phpmyadmin
|
|
+ ServerAlias phpmyadmin.*
|
|
+
|
|
+ <Directory />
|
|
+ Options FollowSymLinks
|
|
+ AllowOverride None
|
|
+ </Directory>
|
|
+
|
|
+ ErrorLog \${APACHE_LOG_DIR}/phpmyadmin_error.log
|
|
+
|
|
+ # Possible values include: debug, info, notice, warn, error, crit,
|
|
+ # alert, emerg.
|
|
+ LogLevel warn
|
|
+
|
|
+ CustomLog \${APACHE_LOG_DIR}/phpmyadmin_access.log combined
|
|
+</VirtualHost>
|
|
EOF
|
|
}
|
|
|
|
# Début du code
|
|
# gestion des options de lancement
|
|
while getopts almpdh f; do
|
|
case $f in
|
|
'a')
|
|
INSTALL_PHPBDDADMIN="true"
|
|
;;
|
|
|
|
'l')
|
|
BDD_CONNEXIONS_OPEN_BAR="yes"
|
|
;;
|
|
|
|
'm')
|
|
TYPE_BDD="mysql"
|
|
;;
|
|
|
|
'p')
|
|
TYPE_BDD="postgresql"
|
|
;;
|
|
|
|
'd')
|
|
INSTALL_DRUSH="true"
|
|
;;
|
|
|
|
'h')
|
|
usage
|
|
exit 0
|
|
;;
|
|
|
|
\?)
|
|
usage >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
#(code inutile, mais que je garde parce qu'on ne sait jamais)
|
|
shift $( expr $OPTIND - 1 )
|
|
NOM_SITE="$1"
|
|
|
|
# On part dans le répertoire de travail
|
|
cd /etc
|
|
|
|
# Petites vérifications
|
|
if [ -z "$NOM_SITE" ]; then
|
|
echo "ERREUR: veuillez donner un identifiant au site." >&2
|
|
exit 1
|
|
fi
|
|
if [ ! -z "$INSTALL_PHPBDDADMIN" ] && [ -z "$TYPE_BDD" ]; then
|
|
echo "ERREUR: veuillez choisir PostgreSQL/MySQL." >&2
|
|
exit 1
|
|
fi
|
|
if ! git status >/dev/null 2>&1 || [ $( git status --porcelain | wc -l ) -gt 0 ]; then
|
|
echo "ERREUR: travaux non-commités dans /etc." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Installation des paquets communs
|
|
# (note: php5-mcrypt est une dépendance de phpmyadmin. On le met par défaut pour éviter
|
|
# des disparités dans les install dev/prod)
|
|
echo "INFO: aptitude update..."
|
|
aptitude update
|
|
echo "INFO: aptitude install..."
|
|
aptitude install libapache2-mod-php5 php5-gd php-apc php5-mcrypt
|
|
|
|
git add .
|
|
git commit -a -m "Installation Apache/PHP"
|
|
|
|
# Petite config. optimisée de php-apc
|
|
if [ -f "/etc/php5/mods-available/apc.ini" ]; then
|
|
echo "apc.rfc1867 = 1" >>/etc/php5/mods-available/apc.ini
|
|
echo "apc.shm_size = 128M" >>/etc/php5/mods-available/apc.ini
|
|
git commit -a -m "PHP/APC: configuration pour Drupal"
|
|
fi
|
|
|
|
echo "INFO: modification du default vhost..."
|
|
mkdir /var/www/default
|
|
mv /var/www/index.html /var/www/default/
|
|
patch_apache_default_vhost | git apply -
|
|
git commit -a -m "Apache: default vhost dans sous-rep."
|
|
|
|
case "$TYPE_BDD" in
|
|
'mysql')
|
|
echo "INFO: aptitude install php5-mysql mysql-server ..."
|
|
echo "NOTICE: laisser le mot de passe vide."
|
|
aptitude install php5-mysql mysql-server
|
|
git add .
|
|
git commit -a -m "Install MySQL"
|
|
|
|
if [ -n "$BDD_CONNEXIONS_OPEN_BAR" ]; then
|
|
patch_mysql_access_from_lan | git apply -
|
|
git commit -a -m "MySQL: accès depuis le réseau"
|
|
fi
|
|
|
|
# Génération d'un mot de passe aléatoire
|
|
MYSQL_ADMIN_PASSWORD=$( dd if=/dev/random 2>/dev/null bs=1 count=10 status=noxfer | base64 | sed 's#[/=]##g' )
|
|
echo "UPDATE mysql.user SET Password = PASSWORD('$MYSQL_ADMIN_PASSWORD') where User = 'root'" | mysql
|
|
echo "FLUSH PRIVILEGES" | mysql
|
|
|
|
# mise en place de la sauvegarde
|
|
printf "[client]\npassword=\"%s\"\n" "$MYSQL_ADMIN_PASSWORD" >~/.my_sauvegarde.cnf
|
|
RANDOM_NUMBER=$( hexdump -n 2 -e '/2 "%u"' /dev/urandom )
|
|
echo "$(( $RANDOM_NUMBER % 60 )) 10,16 * * 1-5 root test -x /root/sauvegarde_mysql.sh && /root/sauvegarde_mysql.sh --delete-olds" >>/etc/crontab
|
|
git commit -a -m "cron: sauvegarde MySQL"
|
|
;;
|
|
|
|
'postgresql')
|
|
echo "INFO: aptitude install php5-pgsql postgresql-9.1 ..."
|
|
aptitude install php5-pgsql postgresql-9.1
|
|
git add .
|
|
git commit -a -m "Install PostgreSQL"
|
|
# mise en place de la sauvegarde
|
|
RANDOM_NUMBER=$( hexdump -n 2 -e '/2 "%u"' /dev/urandom )
|
|
echo "$(( $RANDOM_NUMBER % 60 )) 11,17 * * 1-5 root test -x /root/sauvegarde_postgresql.sh && /root/sauvegarde_postgresql.sh --delete-olds" >>/etc/crontab
|
|
git commit -a -m "cron: sauvegarde PostgreSQL"
|
|
;;
|
|
esac
|
|
|
|
if [ ! -z "$INSTALL_PHPBDDADMIN" ]; then
|
|
case "$TYPE_BDD" in
|
|
'mysql')
|
|
echo "mdp root MySQL à copier : $MYSQL_ADMIN_PASSWORD"
|
|
echo "appuyer sur entree pour continuer"
|
|
read TOTO
|
|
aptitude install phpmyadmin
|
|
git add .
|
|
git commit -a -m "Install phpmyadmin"
|
|
|
|
patch_phpmyadmin_access_from_lan | git apply -
|
|
a2ensite phpmyadmin
|
|
git add .
|
|
git commit -a -m "PHPMyAdmin: vhost dédié"
|
|
;;
|
|
|
|
'postgresql')
|
|
aptitude install phppgadmin
|
|
git add .
|
|
git commit -a -m "Install phppgadmin"
|
|
|
|
# TODO vhost
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
echo "INFO: a2enmod rewrite..."
|
|
a2enmod rewrite
|
|
git add .
|
|
git commit -a -m "apache: activation mod-rewrite"
|
|
|
|
# création vhost site Drupal
|
|
if [ ! -f "/etc/apache2/sites-available/$NOM_SITE" ]; then
|
|
echo "INFO: Création du vhost..."
|
|
cat <<EOF >/etc/apache2/sites-available/$NOM_SITE
|
|
<VirtualHost *:80>
|
|
ServerAdmin webmaster@localhost
|
|
|
|
ServerName $NOM_SITE
|
|
#ServerAlias www
|
|
#ServerAlias www.*
|
|
|
|
DocumentRoot /var/www/$NOM_SITE
|
|
<Directory />
|
|
Options None
|
|
AllowOverride None
|
|
deny from all
|
|
</Directory>
|
|
<Directory /var/www/$NOM_SITE/>
|
|
Options FollowSymLinks
|
|
AllowOverride All
|
|
Order allow,deny
|
|
allow from all
|
|
</Directory>
|
|
|
|
ErrorLog \${APACHE_LOG_DIR}/${NOM_SITE}_error.log
|
|
|
|
# Possible values include: debug, info, notice, warn, error, crit,
|
|
# alert, emerg.
|
|
LogLevel warn
|
|
|
|
CustomLog \${APACHE_LOG_DIR}/${NOM_SITE}_access.log combined
|
|
</VirtualHost>
|
|
EOF
|
|
mkdir /var/www/$NOM_SITE
|
|
a2ensite $NOM_SITE
|
|
apache2ctl configtest
|
|
git add .
|
|
git commit -a -m "apache: création vhost $NOM_SITE"
|
|
else
|
|
echo "WARNING: pas de création du vhost." >&2
|
|
fi
|
|
|
|
if ! id "$NOM_UTILISATEUR" >/dev/null 2>&1; then
|
|
echo "INFO: Création compte utilisateur..."
|
|
adduser "$NOM_UTILISATEUR"
|
|
usermod -a -G www-data "$NOM_UTILISATEUR"
|
|
git commit -a -m "création utilisateur dédié pour déploiement/maintenance"
|
|
else
|
|
echo "WARNING: pas de création de l'utilisateur." >&2
|
|
fi
|
|
|
|
if [ -d "/home/$NOM_UTILISATEUR" ] && [ -d "/var/www/$NOM_SITE" ] && id "$NOM_UTILISATEUR" >/dev/null 2>&1; then
|
|
echo "INFO: on en profite pour mettre les droits spéciaux :)"
|
|
chown $NOM_UTILISATEUR:www-data /var/www "/var/www/$NOM_SITE"
|
|
chmod 2750 /var/www "/var/www/$NOM_SITE"
|
|
echo "INFO: création lien symbolique..."
|
|
su -c "ln -s '/var/www/$NOM_SITE' '/home/$NOM_UTILISATEUR/www-$NOM_SITE'" "$NOM_UTILISATEUR"
|
|
else
|
|
echo "WARNING: pas de création du lien symbolique dans le home de l'utilisateur." >&2
|
|
fi
|
|
|
|
# Installation de Drush pour l'utilisateur dédié, si demandée
|
|
if [ -n "$INSTALL_DRUSH" ]; then
|
|
aptitude install php-pear
|
|
git add .
|
|
git commit -a -m "Installation de php-pear (pour Drush)"
|
|
su -c 'pear config-create $HOME $HOME/.pearrc' "$NOM_UTILISATEUR"
|
|
su -c "pear channel-discover pear.drush.org" "$NOM_UTILISATEUR"
|
|
su -c "pear install drush/drush" "$NOM_UTILISATEUR"
|
|
printf '\n# modification PATH pour drush\nPATH=$PATH:$HOME/pear\n' >>"$( getent passwd $NOM_UTILISATEUR | cut -d : -f 6)/.bashrc"
|
|
fi
|
|
|
|
# Pré-remplissage du fichier /etc/exports
|
|
cat <<EOF
|
|
Ligne à ajouter à /etc/exports :
|
|
/var/www/$NOM_SITE/sites/default/files 172.20.0.0/255.255.0.0(rw,insecure,all_squash,anonuid=33,anongid=33)
|
|
EOF
|
|
|
|
cat <<EOF
|
|
Ne pas oublier :
|
|
- le serveur de mail,
|
|
- la taille des uploads (post_max_size & upload_max_filesize)
|
|
EOF
|
|
|
|
echo "The end."
|