需求描述:
在本机上之前已经部署了一个项目,可以通过localhost/pro1访问,现在要新开发一个项目,如何通过localhost/pro2访问?
本机环境:
ubuntu1604
apache2: 在/etc/apache2/目录下,通过sudo apt-get apache2自动安装。
若在安装好后启动apache2时提示各种directory未配置时,可以先source /etc/apache2/envvars,让apache2的配置文件生效。
解决方法
首先需要添加新端口。在site-available文件夹下复制000-default.conf文件,并重命名,可随意,假设为8081.conf。打开文件进行修改,下面是我修改好后的内容。
其中8081是我设置的新端口,可根据自己需求进行修改;
ServerName与ServerAlias也需要根据需求修改,ServerName即在浏览器端访问的路径,ServerAlias即路径别名,
在此处我省略掉了端口号,并将项目名称添加进来,这样有多个项目时,即可通过项目名称来加以区分,而不用考虑其端口区别,对外界访问更友好;
Directory节点是项目所在路径,要保证目录下存在index.html文件,这样在访问ServerAlias或ServerName时会自动加载改页面;
其他内容不变。
<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 127.0.0.1:8081
ServerAlias 127.0.0.1/two
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /home/mamq/work/two/>
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>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
输入以下命令使端口生效:sudo a2ensite 8081.conf
然后,修改/etc/apache2/apache2.conf文件,添加如下内容:
ServerName 127.0.0.1:80
Alias /VES "/home/mamq/work/one"
<Directory "/home/mamq/work/one">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
Alias /ARCTIC "/home/mamq/work/two"
<Directory "/home/mamq/work/two">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
重启apache2. service apache2 restart
通过以上设置,在浏览器端输入localhost/one与localhost/two即可分别访问两个项目,两个项目分别对应80和8081端口。若需要在局域网内其他机器访问,需要在防火墙中开启端口。
在centos服务器上apache是以httpd的形式存在,要达到上述目的要稍微复杂点,方法略有不同,等之后有时间再更新上来。
以上,欢迎交流。