最後更新: 2015-09-22
目錄
mod_macro
mod_macro 令 apache 的 configuration file 可以用 macros
# Debian Install
apt-get install libapache2-mod-macro
# Enable
a2enmod macro
## Define a VHost Macro for repetitive configurations
/etc/httpd/conf.d/macro.conf
# macro_name var1 var2 <Macro VHOST $host $port $dir> <VirtualHost *:$port> ServerName $host DocumentRoot /home/vhosts/$dir/public_html ErrorLog /home/vhosts/$dir/logs/error.log CustomLog /home/vhosts/$dir/logs/access.log combined <Directory /home/vhosts/$dir/public_html> # do something here... </Directory> IndexOptions FancyIndexing </VirtualHost> </Macro>
## Use of VHost with different arguments.
/etc/httpd/conf.d/vhosts.conf
# Example: # mkdir -p /home/vhosts/$mydir/{public_html,logs} # touch /home/vhosts/$mydir/logs/{error.log,access.log} Use VHOST test.datahunter.org 80 test
Remark
# used so that later macros using the same variable names don't result in conflicting definitions
UndefMacro VHOST
* Parameters prefixed with either $ or % are not escaped. Parameters prefixes with @ are escaped in quotes.
* If you want to use a value within another string
<Macro DocRoot ${docroot}> DocumentRoot "/var/www/${docroot}/htdocs" </Macro>
macro version
不同 version 的 apache 是要用安裝不同 version 的 macro 的 !!
- Apache >= 2.4.5 (available since 2.4.5)
- Apache 2.2, use 1.1.11
人手安裝 macro
Download:
wget http://people.apache.org/~fabien/mod_macro/mod_macro-1.1.11.tar.gz
安裝 apxs
yum install httpd-devel
yum groupinstall "Development tools"
tar -xzf mod_macro-1.1.11.tar.gz
apxs -cia mod_macro-1.1.11/mod_macro.c
..................... Libraries have been installed in: /usr/lib/httpd/modules ..................... chmod 755 /usr/lib/httpd/modules/mod_macro.so [activating module `macro' in /etc/httpd/conf/httpd.conf]
service httpd reload
mod_define
* doesn't provide a general macro mechanism
Install:
https://code.google.com/p/mod-define/
svn checkout http://mod-define.googlecode.com/svn/trunk/ mod-define-read-only
Example:
Define static_path = /var/www/html/static
You can latter use this variable like:
${static_path} OR $static_path
ServerRoot = ${static_path}
Remark
在 apache2.4 已內建支援 "Define"
Usage
Define rootDir /var/www ${rootDir}
My Setting
# File: /etc/apache2/conf-enabled/macro.conf
#### vhost <Macro vhost $domain> <VirtualHost *:80> Servername $domain ServerAdmin tech@xxxxxx DocumentRoot /home/vhosts/$domain/public_html ErrorLog /home/vhosts/$domain/logs/error.log CustomLog /home/vhosts/$domain/logs/access.log combined <Directory /home/vhosts/$domain/public_html> Options -Indexes +FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> </Macro> #### redirect2ssl <Macro redirect2ssl $domain> <VirtualHost *:80> ServerName $domain ServerAdmin tech@xxxxxx ErrorLog /dev/null CustomLog /dev/null combined Redirect / https://$domain/ </VirtualHost> </Macro>
# File: /etc/apache2/apache2.conf
....... IncludeOptional vhosts.conf
# File: /etc/apache2/vhosts.conf
# rm /etc/apache2/sites-enabled/000-default.conf # mkdir -p /home/vhosts/IP/public_html # mkdir -p /home/vhosts/IP/logs # echo '<html><h1>Server is working</h1></html>' >> /home/vhosts/IP/public_html/index.htm Use vhost IP Use redirect2ssl domain.name