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
  • 70,465

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
  • 197,895
  • 55
  • 485
  • 740
Ziazis
  • 2,174
  • 2
    You should also search for files that have a *.conf extension. Or else a2ensite would complain that the site does not exist – Dan May 25 '17 at 09:47
  • @Dan not necessary since we are searching in the conf directory. – Ziazis May 25 '17 at 16:29
  • I guess this method is also good, by principle, when creating Nginx Sblocks, and not only for Apache Vhosts. –  May 28 '17 at 04:21
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
    This also enables the conf file default-ssl.conf... This is clear from the output Enabling site default-ssl. –  May 18 '17 at 11:23
  • You can use dpkg -S to see if the configuration file is from a package. – muru May 21 '17 at 12:53
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
  • 29,831
  • The first line is not true. The * wildcard is being expanded before it gets passed to the a2ensite command in my case. However, this works sudo a2ensite "*.conf". – Dan May 25 '17 at 10:01
  • Hi, @Dan, in my case (Ubuntu 16.04.2) it works properly without quotes. However I've updated the answer. Could you check if a2ensite '*.conf' works correct in your system? – pa4080 May 25 '17 at 11:14
  • 1
    Yep it works, I'm on Ubuntu 17.04 btw. – Dan May 25 '17 at 15:04
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
  • 1
    I'm not sure, but why are your site configuration in the webroot folder? Try using the /etc/apache2/sites-available folder for configurations for your sites. Once you enable them via a2ensite, they are automatically linked into /etc/apache2/sites-enabled. What you are doing right now is pretty weird and everyone that has access to your webserver can look into your configurations? – Ziazis May 18 '17 at 10:19
  • Hi @Ziazis . I really don't understand, why you even ask me "everyone that has access to your webserver can look into your configurations?" I am the onlyone who uses the server; If I wasn't, than usually other users wouldn't have access to document root. Anyway, can you please clarify why you ask that and what exactly you find wrong? –  May 18 '17 at 11:15
  • Well I'm not sure what you are using this webserver for. However if this is a live system with an access which is reachable by a third party you don't want your configuration files lying in /var/www/html since that is the DocumentRoot of the default.html-Site. So in your case you seem to be running the configurations in there... It's like eating with an open mouth, don't do that ;-). – Ziazis May 22 '17 at 08:20
  • I don't understand. You think I putted the conf files in document root? Because I didn't. You suggest me of putting them there? I would want to avoid that... I just want to enable all of them which are not 000-default and default-ssl. BTW, I use the webserver to store my personal websites. –  May 22 '17 at 09:51
  • Ah, I see - you named all your RootDirectories the same same as your config files. And here I thought you changed your apache2 config to have /var/www/html as your config folder. Nvm then. What you actually need is just a find ./ -type f -and -not -name "*default*" -exec a2ensite {} \; – Ziazis May 22 '17 at 10:02
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

Krishna Chalise
  • 170
  • 2
  • 4
  • 17
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
  • AFAIK, you could do so from anywhere in the system when you document root is /var/www/html? –  May 26 '17 at 20:06
  • The important part is the /etc/apache2_or_other/sites-available folder. The document root is then linked in that config. You could have your root in /usr/share/htlm and it would not change anything except if you did not write it in the conf file. I can be anywhere in my server, as long as the conf files are in the sites-available folder a2ensite will do its work. – Carobell May 29 '17 at 12:10
  • Edited my answer as the first option does not work tested – Carobell May 29 '17 at 12:19