apache配置虚拟站点

- 1 min


客户的服务器重装了系统,由原来的nginx改成了apache,需要在apache下配置多个虚拟站点,通过不同的端口号访问项目。

首先,找到apache为虚拟站点创建的默认配置文件

    /usr/local/apache/conf/extra/httd-vhosts.conf

    NameVirtualHost *:80
    <VirtualHost *:80>
        #ServerAdmin webmaster@example.com
        #php_admin_value open_basedir "/home/wwwroot/default:/tmp/:/var/tmp/:/proc/"
        DocumentRoot "/home/wwwroot/default"
        ServerName _
        #ErrorLog "/home/wwwlogs/IP-error_log"
        #CustomLog "/home/wwwlogs/IP-access_log" combined
        <Directory "/home/wwwroot/default">
            SetOutputFilter DEFLATE
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            DirectoryIndex index.html index.php
        </Directory>
    </VirtualHost>
    Include /usr/local/apache/conf/vhost/*.conf

可以看到,一个项目的配置首先是指定访问端口virtualhost,默认是80端口指定项目路径DocumentRoot,如果需要用域名访问则指定项目的域名,以及包含在目录内的为该目录内设置权限的配置项,直接将这个文件复制作为虚拟站点的配置参考,

    cp /usr/local/apache/conf/extra/httd-vhosts.conf /usr/local/apache/conf/vhost/example.com.conf

将example.com.conf改成如下:

    <VirtualHost *:8080>
        #ServerAdmin webmaster@example.com
        #php_admin_value open_basedir "/home/wwwroot/default:/tmp/:/var/tmp/:/proc/"
        DocumentRoot /home/wwwroot/default/example.com/web/
        ServerName example.com
        DirectoryIndex index.php
        #ErrorLog /home/wwwlogs/example.com_log
        #CustomLog /home/wwwlogs/example.com_log combined
        <Directory "/home/wwwroot/default/example.com/web/">
            Order deny,allow
            Allow from all
        </Directory>    
    </VirtualHost>

注意,这里指定了该项目的访问端口为8080,需要在主配置文件里监听该端口

    vi /usr/local/apache/conf/httpd.conf

    找到Listen 80,"/Listen 80"回车,在其下方添加一行,Listen 8080

然后保存后记得重启apache:service httpd restart

rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora list2