Ubuntu环境下配置Apache2,配置多个站点

最初在配置Apache2的时候发现网上的教程大多是配置httpd.conf的,然而我发现Ubuntu下并没有httpd.conf,有的是apache2.conf及多个文件夹。原来Apache2的Ubuntu发行版并没有httpd.conf,而是通过apache2.conf,及conf-available等多个文件夹里的文件来配置的。
Apache2每个虚拟主机的配置文件都放在sites-available下,当在sites-enabled下建立一个指向某个虚拟主机配置文件的链接时,就启用了该虚拟主机。
一、在/etc/apache2/sites-available中创建一新的文件,命名为site1.conf

二、配置site1.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 youdomain.com
	
<virtualhost *:80="">
	# 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 blog.lastation.me

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/yourdictionary

	# 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
</virtualhost>

三、建立链接文件

sudo ln -s /etc/apache2/sites-available/site1.conf  /etc/apache2/sites-enabled/site1.conf

四、重启apache2服务器

service apache2 restart

现在服务器就可以配置多个站点了。但是Apache的发行版本众多,并不是每一版的配置文件的位置都是这个,若是未能在该路径找到配置文件,则需要根据所用的版本来搜索配置文件路径。

Leave a Reply

Your email address will not be published. Required fields are marked *