如何使用a2ensite启用所有站点confs(同时通过000-default.conf && default-ssl.conf)?
我在Apache2上使用Ubuntu服务器16.04.2,我存储了我的个人网站。
要启用/etc/apache2/sites-available
下的所有conf文件(除了000-default.conf
和ssl-default.conf
),我去了/var/www/html
并执行了a2ensite
。
然后我被告知:
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)?
然后我按Ctrl + C执行其他操作。
我正在寻找一种方法来自动启用所有站点confs,而不需要注明一个特定的 – 我只想运行一个命令,将启用我自己添加的所有站点confs。
也可以在配置目录中使用find。
find /etc/apache2/sites-available/ -type f -and -not -name "*default*" -exec a2ensite {} \;
这会找到名称中没有“默认”的所有配置文件并激活它们。
您需要导航到/etc/apache2/sites-available
,然后运行命令:
sudo a2ensite *
它将启用目录中的所有站点。 (文件应该像xxx.conf一样)
然后使用sudo service apache2 reload
加载apache。
所以你的命令序列应该是这样的:
cd /etc/apache2/sites-available sudo a2ensite * sudo service apache2 reload
复制粘贴文件名apache_enable.sh
的以下行
cd /etc/apache2/sites-available/ a2ensite * service apache2 reload
并将read + execute命令提供给apache_enable.sh
并使用root用户执行脚本
无论我们当前的位置是什么,命令sudo a2ensite "*.conf"
将尝试启用放在/etc/apache2/sites-available/
所有配置文件。
接下来我们可以使用a2dissite 000-default.conf default-ssl.conf
来禁用默认的Apache配置,然后我们可以使用systemctl restart apache2.service
来重启它。
我们可以像一个命令一样运行它:
sudo bash -c "a2ensite '*.conf' && a2dissite 000-default.conf default-ssl.conf && systemctl restart apache2.service"
更多,我们可以通过bash中的函数创建自定义命令:
function a2ensites { sudo bash -c "a2ensite '*.conf' && a2dissite 000-default default-ssl && systemctl restart apache2.service" } export -f a2ensites
现在我们有命令a2ensite s
,旨在完成这项工作。 要使此命令永久化,我们必须将上面的行放在~/.bashrc
文件的底部,然后将其source
。 下一个简单的脚本将完成此任务:
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
执行a2ensite /var/www/html/*/
将使Apache启用与站点目录具有相同名称的每个站点conf。
作为副作用,它还将尝试匹配与站点配置文件不匹配的站点目录,如以下输出所预测的那样,但我认为可以安全地忽略它:
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!
启用conf文件后,您可能需要重启Apache:
systemctl restart apache2.service
将默认配置移动到另一个文件夹,然后*应该按照您的需要为您工作。
转到“sites-available”文件夹,因为它只适用于此文件夹。
cd / etc / apache2 / sites-available /
sudo a2ensite *