17

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.

Zanna
  • 72,312

6 Answers6

26

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.

muru
  • 207,228
Ziazis
  • 2,184
14

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
1

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
pa4080
  • 30,621
0

Executing a2ensite /var/www/html/*/ will make Apache enable each site conf that has the same name, as the site dir.

As a side effect, it will also try to match site dirs that aren't matching for site conf files, as predicted from the following output, but I think this can be ignored safely:

ERROR: Site /var/www/html/domain.tld1 does not exist!
ERROR: Site /var/www/html/domain.tld2 does not exist!
ERROR: Site /var/www/html/nice_directory does not exist!
ERROR: Site /var/www/html/nice_file does not exist!

You will likely need to restart Apache after enabling conf files:

systemctl restart apache2.service
0

copy paste the following lines in file name apache_enable.sh

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

and give read+execute command to apache_enable.sh and execute the script using root user

0

Move the default configs to another folder and then the * should work for you as you want it to.

Go to the folder "sites-available" since it only works in this folder.

cd /etc/apache2/sites-available/

sudo a2ensite *

tested

Carobell
  • 155