怎樣用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.