How to enable all site confs with a2ensite (while passing over 000-default.conf && default-ssl.conf)?

I use Ubuntu server 16.04.2 with Apache2, on which I store my personal websites.

To enable all my conf files under /etc/apache2/sites-available (besides 000-default.conf and ssl-default.conf), I went to /var/www/html and executed a2ensite.

I was then told:

Your choices are: 
000-default default-ssl domain.tld1 domain.tld2 domain.tld3 domain.tld4 
Which site(s) do you want to enable (wildcards ok)?

I then did Ctrl+C aiming to execute something else.

I am looking for a way to automatically enable all site confs, without noting a specific one --- I just want to run a command that will enable all site confs that I myself added.


Might aswell just use find on your config directory.

find /etc/apache2/sites-available/ -type f -and -not -name "*default*" -exec a2ensite {} \;

This finds all your configuration files that are not having "default" in their name and activates them.


You need to navigate to /etc/apache2/sites-available and then run the command:

sudo a2ensite *

It will enable all sites in the directory. (the files should be somthing like xxx.conf)

And then reload apache using sudo service apache2 reload.

So your command sequence should be like so:

cd /etc/apache2/sites-available
sudo a2ensite *
sudo service apache2 reload

No matter what our current location is, the command sudo a2ensite "*.conf" will attempt to enable all configuration files placed in /etc/apache2/sites-available/.

Next we can use a2dissite 000-default.conf default-ssl.conf to disable the default Apache's configurations and then we can use systemctl restart apache2.service to restart it.

We can run this all like a single command:

sudo bash -c "a2ensite '*.conf' && a2dissite 000-default.conf default-ssl.conf && systemctl restart apache2.service"

Something more, we can create a custom command via a function in bash:

function a2ensites {
        sudo bash -c "a2ensite '*.conf' && a2dissite 000-default default-ssl && systemctl restart apache2.service"
}
export -f a2ensites

Now we have the command a2ensites, designed to do this job. To make this command permanent, we must place the above lines into the bottom of ~/.bashrc file and then source it. Next simple script will accomplish this task:

 printf "\nfunction a2ensites { \n\tsudo bash -c \"a2ensite '*.conf' && a2dissite 000-default.conf default-ssl.conf && systemctl restart apache2.service\" \n}\nexport -f a2ensites\n" | tee -a $HOME/.bashrc; source $HOME/.bashrc