怎样用apache2设置多个网站? | How to set up multiple websites using apache2?

怎样用apache2设置多个网站

添加网站内容

Ubuntu里apache2的网站内容在

/var/www/html/

我们可以把网站内容(php, html files)放在上面路径的子路径中, 比如

/var/www/html/site1
/var/www/html/site2

设置Apache2

本文以Ubuntu为例子. 我们需要设置的文件在

/etc/apache2

首先要在sites-available里添加新的网站, 比如叫site1

cd /etc/apache2/sites-available
vim site1.conf

添加文件内容如下

<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 www.example.com

	ServerAdmin webmaster@localhost
	ServerName site1.com
	ServerAlias www.site1.com
	DocumentRoot /var/www/html/site1

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

然后添加第二个网站, 比如叫site2

cd /etc/apache2/sites-available
vim site2.conf

给site2添加内容

<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 www.example.com

	ServerAdmin webmaster@localhost
	ServerName site2.com
	ServerAlias www.site2.com
	DocumentRoot /var/www/html/site2

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

最后需要给以上两个site的conf file创建symbolic link, 放在sites-enabled里

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

可能需要重启apache2

sudo service apache2 restart

更新域名指向

以Namecheap为例子, 到所选域名, 比如site1.com里, 点击Manage, 然后在Advanced DNS里, 如下设置

Namecheap Advanced DNS setup

给site2也做类似的配置, 因为都在同一台服务器上, host ip是一样的, 把site1.com换成site2.com就可以

最后在浏览器打开site1.com和site2.com看看是否正常工作.

Leave a Comment

Your email address will not be published.