分类目录归档:Linux-Unix

apache2虚拟端口配置,在一台机器配置多个站点

环境:Ubuntu16.4  Apache/2.4.18

在server端,一个站点需要有一个独立的根目录,例如默认的站点就以/var/www/html/作为站点的根目录。浏览器给到一个http请求的时候,服务器就会在这个目录下面寻找需要返回的文件或信息。

浏览器请求server的时候,会包含一些域名信息,这样服务器就可以建立多个目录。当浏览器请求通过aaa.com或bbb.com发出http请求的时候,server就可以到对应的目录下面返回需要的信息。

在本环境下,进行多站点配置的目录主要是/etc/apache2/sites-available 和  /etc/apache2/sites-enabled。
前者是可用的站点配置,后者是启用的站点配置,而后者目录里面基本上是通过符号链接,链接到前者目录下面的内容的。

root@iZuf6ajm9b61u7hmifsrjaZ:/etc/apache2/sites-enabled# ls -l total 0 lrwxrwxrwx 1 root root 35 Aug 4 2019 000-default.conf -> ../sites-available/000-default.conf

默认的配置如上面,是一个符号链接链接到/sites-available下的文件,抽象的来说可以认为这两个目录下是同一个文件。

我们看下文件里面的内容:

root@iZuf6ajm9b61u7hmifsrjaZ:/etc/apache2/sites-available# cat 000-default.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.inncc.ink

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

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

很清楚,里面描述了在80端口的一个站点,站点的目录是/var/www/html  ServerName www.inncc.ink对应域名。

所以我们直接把这个默认的文件cp两份,分别用作site1.com site2.com  修改ServerName 和DocumentRoot 即可。
修改完成后,在sites-enabled下面建立两个链接到这俩文件的符号链接即可。
文件名没有特殊的要求。

最后重启apache2 :service apache2 restart即可