Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a new vhost in Apache (httpd only). Tested on Manjaro. More information (in French): https://www.armandphilippot.com/automatiser-virtual-host-apache-manjaro/
#! /bin/bash
# newvhost.sh
# Script to create a new vhost in Apache.
# Originally written by Armand Philippot <contact@armandphilippot.com>.
echo -e "Ce script permet de créer de manière automatisée un nouveau vhost dans Apache.\n"
echo -e "Le script doit être exécuté avec les droits d'administrateur (sudo).\n"
echo -e "Il est préférable d'utiliser ce script uniquement pour un environnement de développement local.\n"
# We are testing whether Apache is installed. If it is not, we stop the script.
if command -v httpd &>/dev/null; then
echo -e "Apache est bien installé, poursuivons."
else
echo -e "Apache n'est pas installé.\n"
exit 2
fi
if command -v mkcert &>/dev/null; then
echo -e "mkcert est bien installé, poursuivons.\n"
else
echo -e "mkcert n'est pas installé. Il est nécessaire pour générer l'accès au site en https. Veuillez l'installer puis réessayer.\n"
exit -2
fi
# We ask for the username to grant the correct permissions to the created folders.
read -p "Saisir votre nom d'utilisateur : " _username
read -p 'Saisir le chemin du dossier de travail : ' _basedir
if [ -d "$_basedir" ]; then
echo -e "Le dossier existe, poursuivons.\n"
[[ "${_basedir}" != */ ]] && _basedir="${_basedir}/"
else
until [ -d "$_basedir" ]; do
echo -e "Le dossier n'existe pas. Veuillez saisir un chemin valide.\n"
read -p 'Saisir le chemin du dossier de travail : ' _basedir
done
fi
# We ask for the name to use to create the virtual host (preferably with a TLD).
read -p 'Saisir le nom du vhost à créer : ' _vhost
# We create the configuration file for the virtual host then we create the working folders. We grand correct permissions and we create a symlink in Apache folder.
if command -v httpd &>/dev/null; then
if [ -d "/etc/httpd/conf/vhosts/$_vhost" ]; then
echo -e "Le vhost existe déjà.\n"
exit 2
else
cat <<-EOF >/etc/httpd/conf/vhosts/${_vhost}
<VirtualHost *:80>
ServerAdmin webmaster@${_vhost}
DocumentRoot "/srv/http/${_vhost}/htdocs/"
ServerName www.${_vhost}
ServerAlias *.${_vhost}
ErrorLog "/srv/http/${_vhost}/logs/error.log"
CustomLog "/srv/http/${_vhost}/logs/access.log" combined
<Directory /srv/http/${_vhost}/htdocs/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@${_vhost}
DocumentRoot "/srv/http/${_vhost}/htdocs/"
ServerName www.${_vhost}:443
ServerAlias *.${_vhost}:443
SSLEngine on
SSLCertificateFile "/home/${_username}/.local-certificates/${_vhost}.pem"
SSLCertificateKeyFile "/home/${_username}/.local-certificates/${_vhost}-key.pem"
ErrorLog "/srv/http/${_vhost}/logs/error.log"
CustomLog "/srv/http/${_vhost}/logs/access.log" combined
<Directory /srv/http/${_vhost}/htdocs/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
mkdir -p "${_basedir}/${_vhost}/htdocs"
mkdir -p "${_basedir}/${_vhost}/logs"
chown -R ${_username}:${_username} ${_basedir}/${_vhost}
chmod -R 755 ${_basedir}/${_vhost}
ln -s ${_basedir}${_vhost}/ /srv/http/
echo "Include conf/vhosts/${_vhost}" >>/etc/httpd/conf/httpd.conf
mkdir -p "/home/${_username}/.local-certificates"
mkcert -cert-file /home/${_username}/.local-certificates/${_vhost}.pem -key-file /home/${_username}/.local-certificates/${_vhost}-key.pem ${_vhost} *.${_vhost}
fi
fi
# We add a line in the hosts file.
echo "127.0.0.1 www.${_vhost}" >>/etc/hosts
echo -e "Le vhost et les répertoires correspondants ont été créés.\n"
# We display the folders so that the user can verify the creation of the virtual host.
if command -v httpd &>/dev/null; then
ls /srv/http/
fi
echo -e "\nVous pouvez y accéder en saisissant ${_vhost} ou www.${_vhost} dans votre navigateur.\n"
# If Apache is running, we restart it.
systemctl is-active --quiet httpd && echo -e "Nous redémarrons Apache.\n" && systemctl restart httpd
echo -e "Fin du script.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.