Bonjour à tous, Il y a quelques temps, j’avais posté un script bash permettant de créer des vhosts automatiquement avec Apache. Ce script a été repris et amélioré par Mattmezza, puisqu’il y a intégré la création d’un certificat auto-signé, et le vhost qui va avec. Je vais à mon tour poster une nouvelle version, qui intègre cette fois ci la génération d’un certificat lets encrypt pour une déploiement automatisé.
Pré-requis
- Installation des packages necessaires au fonctionnement de lets encrypt
# Install Epel Repository yum install epel-release # Install IUS Repository rpm -ivh https://rhel6.iuscommunity.org/ius-release.rpm # Install Python 2.7 and Git yum --enablerepo=ius install git python27 python27-devel python27-pip python27-setuptools python27-virtualenv -y # Install git & others package to avoid futurs errors yum install git mod-ssl gcc libffi-devel python-devel openssl-devel # Install EPEL repo if not present yum install epel-release # Now let us clone the github repository of Let's encrypt cd /root/ git clone https://github.com/letsencrypt/letsencrypt
Maintenant que c’est fait, concentrons nous sur le script :
Le Script
#!/bin/bash # This script is used for create virtual hosts on CentOs. # Created by alexnogard from http://alexnogard.com # Improved by mattmezza from http://you.canmakethat.com # Improved by alexnogard from https://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>$cname.$servn</h1>'; ?>" > $dir$cname.$servn/index.php chown -R $usr:$usr $dir$cname.$servn chmod -R '755' $dir$cname.$servn mkdir /var/log/$cname.$servn alias=$cname.$servn if [[ "${cname}" == "" ]]; then alias=$servn fi echo "#### $cname $servn <VirtualHost $listen:80> ServerName $servn ServerAlias $alias DocumentRoot $dir$cname.$servn RewriteEngine On RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent] # Logging Options for $cname.$servn ErrorLog logs/$cname.$servn CustomLog logs/$cname.$servn-access_log common <Directory $dir$cname.$servn> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all Require all granted </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 "Would you like me to create ssl virtual host [y/n]? " read q if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then /root/letsencrypt/letsencrypt-auto --apache -d $cname.$servn if ! echo -e /etc/letsencrypt/live/$cname.$servn/cert.pem; then echo "Certificate key wasn't created !" else echo "Certificate key created !" fi if ! echo -e /etc/letsencrypt/live/$cname.$servn/cert.pem; then echo "Certificate wasn't created !" else echo "Certificate created !" fi if ! echo -e /etc/httpd/conf.d/$cname.$servn-le-ssl.conf; then echo "SSL Virtual host wasn't created !" else echo "SSL Virtual host created !" fi fi echo "127.0.0.1 $servn" >> /etc/hosts if [ "$alias" != "$servn" ]; then echo "127.0.0.1 $alias" >> /etc/hosts fi 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 echo "======================================" echo "All works done! You should be able to see your website at http://$cname.$servn" echo "" echo "" echo "Share the love! <3" echo "======================================" echo "" echo "wanna contribute to improve this script? Found a bug? https://gist.github.com/mattmezza/2e326ba2f1352a4b42b8" echo "Comments on https://alexnogard.com"
Explications
- Lignes 26 à 29 : nous regardons si l’utilisateur est bien en root, sinon, on quitte le script.
- Lignes 30 à 34 : On demande à l’utilisateur les paramètres qu’il souhaite utiliser.
- Lignes 35 à 48 : On test avant si le nom de répertoire est disponible, puis on crée un fichier index.php avec les bons droits
- Lignes 50 à 66 : Nous créons notre vhost.
- Lignes 67 à 71 : On check si notre fichier Vhost a bien été créé.
- Lignes 68 à 69 : On ajoute nos lignes dans le fichier Hosts.
- Lignes 72 à 91 : On demande si on veut mettre en place le SSL, et déployons un certificat ainsi que le vhost
- Lignes 92 à 95 : On ajoute notre host dans le fichiers hots
- Lignes 96 à 99 : Puis on choisit de redémarrer ou non notre process httpd.
Thats all !!
Ultra simple et peut vous faire gagner un temps fou ;)
Si vous avez des commentaires, n’hésitez pas, c’est en dessous que ça se passe