环境准备
编译安装httpd参考
虚拟主机
虚拟主机有三类:
-
相同IP不同端口
-
不同IP相同端口
-
相同IP相同端口不同域名
相同IP不同端口
yum安装文件位置:vim /etc/httpd/conf/httpd.conf 源码安装文件位置:vim /usr/local/apache/conf/httpd.conf
设置主机名
[root@node2 ~]# vim /usr/local/apache/conf/httpd.conf
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80 //取消此行前面的#号
......
在配置文件的最后加上如下内容
[root@node2 ~]# cd /usr/local/apache/conf/
[root@node2 conf]# vim httpd.conf
[root@node2 conf]# tail -25 httpd.conf
#virtual host 1 # 虚拟主机1的配置
<VirtualHost 192.168.59.129:80>
ServerName www.mashuangle.com
DocumentRoot "/var/www/html/www"
ErrorLog "/var/log/httpd/www/error_log"
CustomLog "/var/log/httpd/www/access_log" combined
<Directory /var/www/html/www>
<RequireAll>
Require all granted
Require not ip 192.168.1
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2 # 虚拟主机2的配置
<VirtualHost 192.168.59.129:8080>
ServerName blog.mashuangle.com
DocumentRoot "/var/www/html/blog"
ErrorLog "/var/log/httpd/blog/error_log"
CustomLog "/var/log/httpd/blog/access_log" combined
<Directory /var/www/html/blog>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
[root@node2 conf]#
创建网页目录并修改属主属组
[root@node2 ~]# mkdir -p /var/www/html/{www,blog}
[root@node2 ~]# cd /var/www/html/
[root@node2 html]# ls
blog www
[root@node2 html]# chown -R apache.apache blog
[root@node2 html]# chown -R apache.apache www
[root@node2 html]# ll
total 0
drwxr-xr-x 2 apache apache 6 Dec 26 19:35 blog
drwxr-xr-x 2 apache apache 6 Dec 26 19:35 www
[root@node2 html]#
创建网页
[root@node2 ~]# echo 'www test' > /var/www/html/www/index.html
[root@node2 ~]# echo 'blog test' > /var/www/html/blog/index.html
创建相应网页的日志目录
[root@node2 ~]# mkdir -p /var/log/httpd/{blog,www}
[root@node2 ~]# ls //var/log/httpd
blog www
[root@node2 ~]#
设置监听8080端口
[root@node2 conf]# vim httpd.conf
#Listen 12.34.56.78:80
Listen 80
Listen 8080 #//在此添加8080端口号
启动服务并查看是否有80端口
[root@node2 ~]# systemctl start httpd
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:8080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@node2 ~]#
curl访问网站
[root@node2 ~]# curl http://192.168.59.129:80
www test
[root@node2 ~]# curl http://192.168.59.129:8080
blog test
[root@node2 ~]#
不同IP相同端口
其他步骤同上已省略,只修改部分内容
修改配置文件内容如下
[root@node2 conf]# vim httpd.conf
[root@node2 conf]# tail -25 httpd.conf
#virtual host 1 # 虚拟主机1的配置
<VirtualHost 192.168.59.129:80>
ServerName www.mashuangle.com
DocumentRoot "/var/www/html/www"
ErrorLog "/var/log/httpd/www/error_log"
CustomLog "/var/log/httpd/www/access_log" combined
<Directory /var/www/html/www>
<RequireAll>
Require all granted
Require not ip 192.168.1
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2 # 虚拟主机2的配置
<VirtualHost 192.168.59.131:80>
ServerName blog.mashuangle.com
DocumentRoot "/var/www/html/blog"
ErrorLog "/var/log/httpd/blog/error_log"
CustomLog "/var/log/httpd/blog/access_log" combined
<Directory /var/www/html/blog>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
[root@node2 conf]#
添加临时IP
[root@node2 conf]# ip addr add 192.168.59.131/24 dev ens160
[root@node2 conf]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:87:47:3b brd ff:ff:ff:ff:ff:ff
altname enp3s0
inet 192.168.59.129/24 brd 192.168.59.255 scope global dynamic noprefixroute ens160
valid_lft 1445sec preferred_lft 1445sec
inet 192.168.59.131/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe87:473b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@node2 conf]#
重启服务并用使用curl命令访问网站
[root@node2 ~]# systemctl restart httpd
[root@node2 ~]# curl http://192.168.59.129
www test
[root@node2 ~]# curl http://192.168.59.131
blog test
[root@node2 ~]#
相同IP相同端口不同域名
其他步骤同上已省略,只修改部分内容 修改配置文件内容如下
[root@node2 conf]# vim httpd.conf
[root@node2 conf]# tail -25 httpd.conf
#virtual host 1 # 虚拟主机1的配置
<VirtualHost 192.168.59.129:80>
ServerName www.mashuangle.com
DocumentRoot "/var/www/html/www"
ErrorLog "/var/log/httpd/www/error_log"
CustomLog "/var/log/httpd/www/access_log" combined
<Directory /var/www/html/www>
<RequireAll>
Require all granted
Require not ip 192.168.1
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2 # 虚拟主机2的配置
<VirtualHost 192.168.59.129:80>
ServerName blog.mashuangle.com
DocumentRoot "/var/www/html/blog"
ErrorLog "/var/log/httpd/blog/error_log"
CustomLog "/var/log/httpd/blog/access_log" combined
<Directory /var/www/html/blog>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
[root@node2 conf]#
修改hosts文件
windows的host文件位置:c:\windows\system32\drivers\hosts 将文件拉出用写字板打开添加ip和域名再放回去
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
192.168.59.129 www.mashuangle.com blog.mashuangle.com
重启服务并用使用curl命令用域名访问网站
[root@node2 ~]# systemctl restart httpd
[root@node2 ~]# curl www.mashuangle.com
www test
[root@node2 ~]# curl blog.mashuangle.com
www test
[root@node2 ~]#
标签:httpd,www,log,虚拟主机,配置,conf,node2,root From: https://www.cnblogs.com/msl1105/p/17006893.html