apache2 单IP多端口创建虚拟站点
1. 转到配制目录虚拟站点配制目录
cd /etc/apache2/
ubuntu@VM-0-4-ubuntu:/etc/apache2$ ls
apache2.conf conf-enabled magic mods-enabled sites-available
conf-available envvars mods-available ports.conf sites-enabled
2. 配置要用的端口
sudo vim 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 4096 #新增
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
添加 Listen + port 就行
3. 拷贝默认站点 000-default.conf 文件 ,并修改为你的配置
cd /etc/apache2/sites-available/
# 拷贝文件
sudo cp 000-default.conf 你的配置文件名.conf
# 编辑文件
sudo vim 你的配置文件名.conf
把80端口和网站路径改了就行
<VirtualHost *:4096>
# 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/html2
# 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>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
4. sites-enabled 中建立软链接
sudo ln -s /etc/apache2/sites-available/你的配置文件名.conf /etc/apache2/sites-enabled/你的配置文件名.conf
不加-s是建立硬链接;
软链接只会在选定的位置上生成一个文件的镜像,不会占用磁盘空间,类似与windows的快捷方式。
硬连接会在选定的位置上生成一个和源文件大小相同的文件,无论是软链接还是硬链接,文件都保持同步变化。
5. 重启apache
sudo systemctl restart apache2
到这里一般就可以了
如果出现403等情况,可能是文件没有访问权限,修改文件权限为可读写
ln: failed to create symbolic link ‘xxxxxxxxxxx’: File exists
链接的文件已存在,查看下链接时路径文件是否正确