dd

Ubuntu环境下apache多端口配置虚拟主机的方法

jerry ubuntu 2015年09月18日 收藏

linux环境apache多端口配置虚拟主机的方法

Linux(Ubuntu)
默认情况下,linux上apache使用的
默认文档目录是:/var/www
默认端口是:80

如果想发布自己的一个系统资源目录,可以使用下面的方法,执行如下命令:

(1)添加监听端口

#cd /etc/apache2
#vim ports.conf


文件添加:

NameVirtualHost *:8000
Listen 8000


(2)配置虚拟目录

#cd /etc/apache2/sites-available
#cp default default-me
#vim default-me


文件内容如下:

<VirtualHost *:8000>
    ServerAdmin webmaster@localhost
    DocumentRoot /wwwroot
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /wwwroot/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    ErrorLog /var/log/apache2/error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined
</VirtualHost>


粗体部分是关键点。

(3)发布站点

# ln -s /etc/apache2/sites-available/default-me /etc/apache2/sites-enabled/001-default


(4)重启服务

#/etc/init.d/apache2 restart


(5)测试
http://localhost:8000/
如果能够正常访问就说明配置正确了。


Ubuntu系统配置Apache2虚拟目录和端口

1.
在 sites-available/ 目录下创建新的文件填写此虚拟主机的配置指令
2.
使用 a2ensite 命令启用虚拟主机; 使用 a2dissite 命令停用虚拟主机
例如要创建一个基于端口的虚拟主机,要执行如下的步骤:
S1. 修改 /etc/apache2/ports.conf 文件添加一个新的监听端口

/etc/apache2/ports.conf
Listen 80
Listen 8080


S2. 在 sites-available/ 目录下创建新的文件 192.168.0.222-8080,添加如下的配置

/etc/apache2/sites-available/192.168.0.222:8080
<VirtualHost 192.168.0.222:8080>
ServerAdmin webmaster@localhost
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
DocumentRoot /home/www/192.168.0.222-8080
<Directory /home/www/192.168.0.222-8080/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
</VirtualHost>


S3. 创建 /home/www 目录和 index 文件

sudo mkdir -p /home/www/192.168.0.222-8080
sudo touch /home/www/192.168.0.222-8080/index.php

S4. 使用 a2ensite 命令启用虚拟主机

sudo a2ensite 192.168.0.222-8080

S5. 重新启动 Apache

sudo /etc/init.d/apache2 restart

以下是一个实例
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
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz
#如果开多个虚拟主机,所有的NameVirtualHost,都应该设置为以下一样,或不写
#不然会错 如 *:3008 has no VirtualHosts
NameVirtualHost *:80
#每增加一个端口,就增加一条 Listen 端口
Listen 3008
Listen 8080
<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>
<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

在sites-available增加一个文件,访问时输入 http://IP:8080/labelhttp/,这样,非网页文件便可下载
内容如下(内容仿default)
只改动了三个地方

 Alias /labelhttp /home/ftp/users/labelhttp
 DocumentRoot /home/ftp/users/labelhttp
 <Directory /home/ftp/users/labelhttp/>

详细其他内容如下

 Alias /labelhttp /home/ftp/users/labelhttp
        ServerAdmin webmaster@localhost
        DocumentRoot /home/ftp/users/labelhttp
        <Directory />
Options FollowSymLinks
AllowOverride None
        </Directory>
        <Directory /home/ftp/users/labelhttp/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
        </Directory>
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>



dd