sudo apt-get install apache2
安装完成后,所有配置文件都在/etc/apache2/目录下。
apache2在启动的时候自动读取/etc/apache2/apache2.conf文件的配置信息,不同的配置项按功能分布在不同的文件中,然后被Include包含到apache2.conf这个主配置文件中,方便管理。就是说事实上apache2主配置文件只有一个,即apache2.conf,其他的都是被include进来的。可以把所有的配置都放在apache2.conf或者任何一个配置文件中,但是划分到不同文件会让我们管理起来方便很多。
.
├── apache2.conf 全局配置
├── conf-available 可用的配置文件
├── conf-enabled 已启用的配置文件
├── envvars 环境变量
├── magic
├── mods-available 已安装的模块
├── mods-enabled 已启用的模块
├── ports.conf http服务端口信息
├── sites-available 可用站点信息
├── sites-enabled 已经启用的站点信息,当中的文件是到/etc/apache2/sites-available/ 文件的软连接。
其中,apache2.conf是apache2的主配置文件,包括三个级别的配置。
初始情况下,Apache2主配置文件/etc/apache2/apache2.conf。其最后两行为:
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
显然/etc/apache2/sites-enabled下存放着有关虚拟站点(VirtualHost)的配置。经查看,初始情况下,该目录下包含一个符号链接:
000-default.conf -> ../sites-available/000-default.conf
这里又引出另外一个配置目录:/etcc/apache2/sites-available。这个目录下放置了所有可用站点的真正配置文件,对于Enabled的站点,Apache2在sites-enabled目录建立一个到sites-available目录下文件的符号链接。
/etc/apache2/sites-available下有两个文件:000-default.conf和default-ssl.conf。000-default链接的文件为000-default.conf。
以000-default.conf为例,看看一个VirtualHost的配置是啥样的:
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
AllowOverride All
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
DocumentRoot是这个站点的根目录,这样Apache2启动时会扫描/etc/apache2/sites-enabled中可用的站点配置并加载。当用户访问localhost:80时,Apache2就将default站点根目录/var/www/html下的index.html作为请求的响应返回给浏览器。
Apache2的默认站点我们不要去动它,我们新增站点配置来满足我们的要求。我们可能有两类需求:
第一种需求讲的是我要在一个Apache2服务器上配置两个站点:site1.com和site2.com。
我们可以按照下面步骤来做:
在sites-available中建立两个站点的配置文件site1_com和site2_com:
sudo cp 000-default.conf site1_com.conf
sudo cp 000-default.conf site2_com.conf
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName site1.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/site1_com
AllowOverride All
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
注意上面配置中:ServerName、DocumentRoot是我们重点关注的配置点。site1的ServerName为site1.com,根目录为/var/www/site1_com。(site2_com.conf也做同样的改动)
sudo ln -s /etc/apache2/sites-available/site1_com.conf /etc/apache2/sites-enabled/site1_com.conf
sudo ln -s /etc/apache2/sites-available/site2_com.conf /etc/apache2/sites-enabled/site2_com.conf
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
sudo a2enmod rewrite
sudo service apache2 restart
或
sudo /etc/init.d/apache2 restart
添加如下两行:
127.0.0.1 site1.com
127.0.0.1 site2.com
打开浏览器,输入http://site1.com,即可看到我们的网站。
第二类需求是希望通过端口号来区分虚拟站点。
修改/etc/apache2/ports.conf,增加两行:
Listen 8081
Listen 8082
以site2_com为例,将监听的端口改为8082,ServerName改为site.com。(site1_com同理。)
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName site.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/site2_com
AllowOverride All
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
添加一行:
127.0.0.1 site.com
sudo service apache2 restart
或
sudo /etc/init.d/apache2 restart
访问 http://site.com:8082 即可看到我们的网站。