技术分享 网络技术

尝试Apache2通过不同端口开多个网站🤔

最近有需求在主机的两个端口分别开两个网站,解析两个域名。但是看了网上许多资料,都没有发现一个明确且方便的方法。经过摸索后终于成功,理论上只要你端口够用,你想开几个网页都成。不废话下面直接上教程!!!

首先,准备你的网页根目录文件夹:

/var/www/site1/
/var/www/site2/
……

然后,准备好你的端口,这里我用的是8080和8081

接着,打开并编辑 /etc/apache2/ports.conf

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80
Listen 8080
Listen 8081

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>
                    

Listen 80一行不需要可以删除。

然后,打开并编辑此文件: /etc/apache2/sites-enabled/000-default.conf

加入两个网站的配置,如下:

<VirtualHost *:8080>
	# 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 Domain1.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/site1
	
	<Directory "/var/www/site1">
	AllowOverride ALL
	</Directory>

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

<VirtualHost *:8081>
	# 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 Domain2.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/site2
	<Directory "/var/www/site2">
	AllowOverride ALL
	</Directory>
	# 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>

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

需要注意的是, <VirtualHost *:80> 及其内容是默认自带的,如果删除了 ports.conf 中默认自带的 Listen 80,这里也应该删除。

此时,使用命令重启apache2的服务:

sudo service apache2 restart

现在输入 ip地址:8080 和 ip地址:8081,就可以访问两个根目录下的网页了。

至于配置文件中ServerName的值,我还没进行域名解析,测试后更新。关于这一点参考https://cloud.tencent.com/developer/article/1544296

另外,这篇博客https://blog.csdn.net/jochen_M/article/details/84073588 提供的方法是先从sites-available文件夹中创建配置文件,再从sites-enabled目录下建立符号链接,这好像是正规做法。还不懂为啥,如果有懂的还烦请赐教!!

5.27 2023备注,通过域名访问失败。


Reference

https://cloud.tencent.com/developer/article/1544296

https://blog.csdn.net/jochen_M/article/details/84073588

Leave a Reply

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