J’ai écris un script il y a quelques temps, et j’ai donc décidé de le partager à la communauté. Ce script a pour but de créer des vhost de manière “automatique” grâce à un simple code BASH. Pour un admin, c’est du temps précieux de gagné et des erreurs évitées. Le script crée un répertoire de travail, crée un virtual host et vérifie son intégrité, assigne les bons droits et crée une nouvelle ligne dans notre fichier /etc/hosts. Ce script a été créé sur CentOs. Il suffit de faire quelques changements niveau répertoire pour l’adapter à une Debian !
Le code :
#!/bin/bash # This script is used for create virtual hosts on CentOs. # Created by alexnogard from http://alexnogard.com # Feel free to modify it # PARAMETERS # # $usr - User # $dir - directory of web files # $servn - webserver address without www. # $cname - cname of webserver # EXAMPLE # Web directory = /var/www/ # ServerName = domain.com # cname = devel # # # Check if you execute the script as root user # # This will check if directory already exist then create it with path : /directory/you/choose/domain.com # Set the ownership, permissions and create a test index.php file # Create a vhost file domain in your /etc/httpd/conf.d/ directory. # And add the new vhost to the hosts. # # if [ "$(whoami)" != 'root' ]; then echo "You have to execute this script as root user" exit 1; fi read -p "Enter the server name your want (without www) :" servn read -p "Enter a CNAME (e.g. :www or dev for dev.website.com) :" cname read -p "Enter the path of directory you wanna use (e.g. : /var/www/, dont forget the /):" dir read -p "Enter the user you wanna use (e.g. : apache) :" usr read -p "Enter the listened IP for the server (e.g. : *):" listen if ! mkdir -p $dir$cname.$servn; then echo "Web directory already Exist !" else echo "Web directory created with success !" fi echo "<?php echo '<h1>$2</h1>'; ?>" > $dir$cname.$servn/index.php chown -R $usr:$usr $dir$cname.$servn/ chmod -R '755' $dir$cname.$servn mkdir /var/log/$cname.$servn/ echo "#### $cname.$servn <VirtualHost $listen:80> ServerName $servn ServerAlias $cname.$servn DocumentRoot $dir$cname.$servn <Directory $homedir$cname.$servn> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost>" > /etc/httpd/conf.d/$cname.$servn.conf if ! echo -e /etc/httpd/conf.d/$cname.$servn.conf; then echo "Virtual host wasn't created !" else echo "Virtual host created !" fi echo "127.0.0.1 $servn" >> /etc/hosts echo "127.0.0.1 $cname.$servn" >> /etc/hosts echo "Testing configuration" service httpd configtest echo "Would you like me to restart the server [y/n]?" read q if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then service httpd restart fi
Explications :
Lignes 26 à 29 : nous regardons si l’utilisateur est bien en root, sinon, on quitte le script.
Lignes 31 à 35 : On demande à l’utilisateur les paramètres qu’il souhaite utiliser.
Lignes 37 à 41 : On test avant si le nom de répertoire est disponible.
Lignes 42 à 45 : On crée un fichier index.php dans notre nouveau répertoire puis on y assigne les bons droits.
Lignes 47 à 60 : Nous créons notre vhost.
Lignes 62 à 66 : On check si notre fichier Vhost a bien été créé.
Lignes 68 à 69 : On ajoute nos lignes dans le fichier Hosts.
Lignes 71 à 77 : On test notre configuration puis on choisit de redémarrer ou non notre process httpd.
Démonstration :
Si vous avez aimé cet article, n’hésitez pas à partager avec les boutons ci-dessous. Vous pouvez également partagez vos commentaires / remarques ;).
Salut,
Pourquoi ne pas simplement utiliser les VirtualHost dynamiques ?
Pour le site “la-maman-de.marcel.com” on créé un répertoire “la-maman-de.marcel.com” dans “/var/www/”.
Puis dans la configuration de ton VirtualHost tu ajoutes :
VirtualDocumentRoot /var/www/%0
En plus avec cette méthode tu n’as pas besoin de redémarrer ton serveur Apache…
Nico