Apache – Httpd 实践 一 安装与虚拟主机配置

Apache – Httpd 理论:http://www.01wneo.xin/?p=895

Apache 安装

[root@1-6 ~]# yum -y install httpd
[root@1-6 ~]# service httpd restart # selinux 需要关闭,防火墙需要关闭 启动 apache 浏览器访问 主机ip地址
Redirecting to /bin/systemctl restart httpd.service

[root@1-6 ~]# rpm -qc httpd # 查看 apache 的配置文件
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd
[root@1-6 ~]# cd /etc/httpd/conf/
[root@1-6 conf]# ll
总用量 28
-rw-r--r-- 1 root root 11753 10月 20 00:44 httpd.conf
-rw-r--r-- 1 root root 13077 10月 20 04:39 magic
[root@1-6 conf]# vim httpd.conf # apache 主配置文件

Apache 虚拟主机

基于端口的虚拟主机 centos6 最多可以配置 65535 个

[root@1-101 ~]# cd /var/www/html/
[root@1-101 html]# mkdir test1
[root@1-101 html]# ls
test1
[root@1-101 html]# cd test1/
[root@1-101 test1]# pwd
/var/www/html/test1
[root@1-101 httpd]# echo www.test1.com >/var/www/html/test1/index.html
[root@1-101 httpd]# cat /var/www/html/test1/index.html 
www.test1.com
[root@1-101 ~]# vim /etc/httpd/conf/httpd.conf
...
Listen 80
Listen 81
...
<VirtualHost *:81>
 ServerAdmin 1548274293@qq.com
 DocumentRoot /var/www/html/test1
 ServerName www.test1.com
 ErrorLog logs/test1-error_log
 CustomLog logs/test1-access_log common
</VirtualHost>
...
[root@1-101 conf]# service httpd restart
[root@1-101 conf]# cd /var/log/httpd/
[root@1-101 httpd]# ls
access_log error_log test1-access_log test1-error_log

# 配置本地 host

基于ip的虚拟主机 centos6 关机添加网卡

# 开启虚拟机,配置第二个ip地址

# 配置虚拟 ip (子ip) 这个方法不用关机
# [root@1-101 network-scripts]# cp ifcfg-eth2 ifcfg-eth2:1
# [root@1-101 network-scripts]# vim ifcfg-eth2:1 
# DEVICE=eth2:1
# TYPE=Ethernet
# ONBOOT=yes
# BOOTPROTO=yes
# IPADDR=192.168.1.103
# [root@1-101 network-scripts]# service network restart
[root@1-101 ~]# cd /etc/sysconfig/network-scripts/
[root@1-101 network-scripts]# service network restart
[root@1-101 network-scripts]# ifconfig | grep eth*
eth2      Link encap:Ethernet  HWaddr 00:50:56:26:99:51  
          inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fe26:9951/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:412 errors:0 dropped:0 overruns:0 frame:0
          TX packets:214 errors:0 dropped:0 overruns:0 carrier:0
eth3      Link encap:Ethernet  HWaddr 00:0C:29:C7:67:B6  
          inet6 addr: fe80::20c:29ff:fec7:67b6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:193 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
[root@1-101 network-scripts]# ll ifcfg-eth*
-rw-r--r-- 1 root root 179 3月   5 19:07 ifcfg-eth2
[root@1-101 network-scripts]# cp ifcfg-eth2 ifcfg-eth3
DEVICE=eth3
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=yes
IPADDR=192.168.1.102
[root@1-101 network-scripts]# service network restart
[root@1-101 network-scripts]# vim /etc/httpd/conf/httpd.conf
...
<VirtualHost 192.168.1.102:80>
 ServerAdmin 1548274293@qq.com
 DocumentRoot /var/www/html/test1
 ServerName www.test1.com
</VirtualHost>
...
[root@1-101 network-scripts]# vim /var/www/html/test1/index.html 
www.test1.com
[root@1-101 network-scripts]# vim /var/www/html/index.html 
web_server
[root@1-101 network-scripts]# service httpd restart
[root@1-101 network-scripts]# iptables -F

# 通过浏览器访问

基于ip的虚拟主机 centos6

[root@1-101 ~]# vim /etc/hosts
# 127.0.0.1             localhost.localdomain localhost
127.0.0.1       1-101
::1             localhost6.localdomain6 localhost6
192.168.1.101 www.test1.com
192.168.1.101 www.test2.com
192.168.1.101 www.test3.com
192.168.1.101 www.test4.com
[root@1-101 ~]# vim /etc/httpd/conf/httpd.conf
...
NameVirtualHost *:80 # 将该行的注释去掉
#
# NOTE: NameVirtualHost cannot be used without a port specifier 
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
...
<VirtualHost *:80>
    ServerAdmin 1548274293@qq.com
    DocumentRoot /var/www/html/test1
    ServerName www.test1.com
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin 1548274293@qq.com
    DocumentRoot /var/www/html/test2
    ServerName www.test2.com
</VirtualHost>
[root@1-101 ~]# service httpd restart
[root@1-101 ~]# curl www.test1.com
www.test1.com
[root@1-101 ~]# curl www.test2.com
www.test2.com

–访问规则限制-基于ip的虚拟主机 centos6

[root@1-101 ~]# vim /etc/httpd/conf/httpd.conf
...

<VirtualHost *:80>
    ServerAdmin 1548274293@qq.com
    DocumentRoot /var/www/html/test1
    ServerName www.test1.com
    <Directory "/var/www/html/test1">
        Order allow,deny
        Deny from 192.168.1.3
        Allow from 192.168.1.4
    </Directory>
</VirtualHost>
...
[root@1-101 ~]# service httpd restart
[root@1-4 ~]# curl 192.168.1.101
www.test1.com
[root@1-3 ~]# curl 192.168.1.101
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
....

# centos 7 附上配置 /etc/httpd/conf/httpd.conf 并添加发布目录权限 g+x

Listen 8080
Listen 81
Listen 82

<VirtualHost *:81>
ServerAdmin root@localhost
DocumentRoot /home/wl
ServerName www.zowneo.com
ErrorLog logs/81-error-log
CustomLog logs/81-access-log common
</VirtualHost>
<Directory "/home/wl">
   Options Indexes FollowSymLinks
   AllowOverride None
   Require all granted
</Directory>

<VirtualHost *:82>
ServerAdmin root@localhost
DocumentRoot /home/gyf
ServerName www.zowneo.com
ErrorLog logs/82-error-log
CustomLog logs/82-access-log common
</VirtualHost>
<Directory "/home/gyf">
  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

 

发表评论

zh_CNChinese
zh_CNChinese