LAMP架构应用实战—Apache服务基于端口虚拟主机配置

时间:2022-07-25
本文章向大家介绍LAMP架构应用实战—Apache服务基于端口虚拟主机配置,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

LAMP架构应用实战—Apache服务

基于端口虚拟主机配置

前面介绍了基于域名、IP的虚拟主机配置,实际生产环境中使用最多的还是基于域名的虚拟主机,今天介绍的基于端口的虚拟主机也不常用,但用的最多的场景就是:公司内网(如网站后台页面、其它发布类的页面)

基于端口的虚拟配置非常简单

默认情况http默认监听的是80端口,所以配置基于端口的虚拟主机,就是增加相应的监听端口

一:配置之前备份配置文件

[root@Centos extra]# cp httpd-vhosts.conf httpd-vhosts.conf.$(date +%F)
[root@Centos extra]# ls 
httpd-autoindex.conf  httpd-info.conf       httpd-mpm.conf      httpd-userdir.conf httpd-dav.conf          httpd-languages.conf    httpd-multilang-errordoc.conf  

httpd-vhosts.conf httpd-vhosts.conf.2016-09-09

httpd-default.conf httpd-manual.conf httpd-ssl.conf proxy-html.conf

二:配置站点目录(方便测试不同端口)

[root@Centos extra]# mkdir -p /data/www/blog/
[root@Centos extra]# echo "welcome to the server of blogs">>/data/www/blog/index.html
[root@Centos extra]# cat /data/www/blog/index.html
welcome to the server of blogs

三:配置虚拟主机配置文件

1、配置前还需要到主配置文件中增加目录控制权限

[root@Centos extra]# vi ../httpd.conf
<Directory "/data/www/bbs">
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

增加如下配置

<Directory "/data/www/blog">
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

实际生产环境中还是使用规范路径比较好,也可以将配置修改成如下

[root@Centos extra]# vi ../httpd.conf
<Directory "/data/www">
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

对上一级目录统一授权

2、主配置文件增加监听端口

[root@Centos extra]# vi ../httpd.conf    
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
............................其中一些配置部分省略
#
#Listen 12.34.56.78:80
Listen 80
Listen 8888
Listen 9999

3、配置虚拟主机配置文件

[root@Centos extra]# vi httpd-vhosts.conf
# Virtual Hosts
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost 192.168.1.20:8888>
    ServerAdmin admini@abc.com
    DocumentRoot "/data/www/bbs"
    ServerName 192.168.1.20
    ServerAlias abc.com
    ErrorLog "logs/bbs-error_log"
    CustomLog "logs/bbs-access_log" common
</VirtualHost>
<VirtualHost 192.168.1.2:9999>
    ServerAdmin admini@abc.com
    DocumentRoot "/data/www/blog"
    ServerName 192.168.1.2
    ServerAlias abc.com
    ErrorLog "logs/bbs-error_log"
    CustomLog "logs/bbs-access_log" common
</VirtualHost>

4、检查配置、重启服务

[root@Centos extra]# ../../bin/apachectl -t
Syntax OK
[root@Centos extra]# ../../bin/apachectl graceful
[root@Centos extra]# ps -ef |grep http  
root 23901019:42 ?  00:00:00 /application/apache2.4.23/bin/httpd -k graceful
daemon     2725   2390  0 20:33 ?        00:00:00 /application/apache2.4.23/bin/httpd -k graceful
daemon     2726   2390  0 20:33 ?        00:00:00 /application/apache2.4.23/bin/httpd -k graceful
daemon     2727   2390  0 20:33 ?        00:00:00 /application/apache2.4.23/bin/httpd -k graceful
root       2835   1934  0 20:39 pts/1    00:00:00 grep http
[root@Centos extra]# netstat -lnt |grep 8888
tcp        0      0 :::8888                     :::*                        LISTEN      
[root@Centos extra]# netstat -lnt |grep 9999
tcp        0      0 :::9999                     :::*                        LISTEN  

四:测试配置

[root@Centos extra]# cat /data/www/bbs/index.html 
welcome to bbs.abc.com
192.168.1.20:80 this server is the server of bbs stie
[root@Centos extra]# cat /data/www/blog/index.html 
welcome to the server of blogs

本地浏览器测试

经过测试,访问正常,表明配置正确

五:主机别名的应用

修改下刚刚的虚拟主机配置

#port bash ip
<VirtualHost 192.168.1.20:8888>
    ServerAdmin admini@abc.com
    DocumentRoot "/data/www/bbs"
    ServerName 192.168.1.20
    ServerAlias abc.com
    ErrorLog "logs/bbs-error_log"
    CustomLog "logs/bbs-access_log" common
</VirtualHost>
#port bash name
<VirtualHost *:9999>
    ServerAdmin admini@abc.com
    DocumentRoot "/data/www/blog"
    ServerName blog.abc.com
    ServerAlias blog1.com
    ErrorLog "logs/bbs-error_log"
    CustomLog "logs/bbs-access_log" common
[root@Centos extra]# ../../bin/apachectl -t
Syntax OK
[root@Centos extra]# ../../bin/apachectl graceful

表明别名配置也是正确的