ubuntu上apache是通过apt-get install apache2 命令安装
apache配置文件是/etc/apache2/apache2.conf,apache启动的时候会自动读取此文件的配置信息。而其他的一些配置文件,如 httpd.conf等,则是通过Include指令包含进来。在apache2.conf中可以找到这些Include行:
# Include module configuration: Include /etc/apache2/mods-enabled/*.load Include /etc/apache2/mods-enabled/*.conf # Include all the user configurations: Include /etc/apache2/httpd.conf # Include ports listing Include /etc/apache2/ports.conf …… # Include generic snippets of statements Include /etc/apache2/conf.d/ # Include the virtual host configurations: Include /etc/apache2/sites-enabled/
实例:
1. 打开目录 /etc/apache2/sites-available/, 发现 default 和 default-ssl 两个文件, 其中 default 是 http 虚拟主机服务的配置文件, default-ssl 是配置 https 服务使用的. 可以复制一份 default 文件. 并修改配置文件名, 文件名必须与域名一致 (如: localword.com)
2. 打开新建的配置文件, 修改 DocumentRoot, ServerName 和对应的配置目录. 例子如下:
# # DocumentRoot 是网站文件存放的根目录 # ServerName 是网站域名, 需要跟 DNS 指向的域名一致 # <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/localhost/word ServerName localword.com ErrorLog ${APACHE_LOG_DIR}/localword.com-error.log CustomLog ${APACHE_LOG_DIR}/localword.com-access.log combined </VirtualHost>
sudo a2ensite localword.com
sudo a2dissite localword.com
5. 重启 Apache 服务, 激活虚拟主机
sudo /etc/init.d/apache2 restart