這裏說的是Apache基金會下的httpd web服務器組件,官網地址:http://httpd.apache.org/
【1】安裝httpd
① 檢測是否安裝
Centos6下通常默認安裝,Centos7未安裝。
[root@janus www]# rpm -qa|grep httpd
httpd-tools-2.2.15-53.el6.centos.x86_64
httpd-2.2.15-53.el6.centos.x86_64
② yum命令安裝
如果沒有安裝,則可以使用yum命令直接安裝:
yum install httpd -y
當然也可以自行下載二進制包手動編譯安裝。
③ 操作命令
service httpd status #查看服務狀態
service httpd start #啓動服務
service httpd stop #停止服務
service httpd restart #重啓服務
systemctl enable httpd #Centos7下設置服務開機啓動
另外,httpd默認監聽80端口,所以需要保證80端口沒有被佔用,且被防火牆放開。或者,換個監聽端口也可以。關於防火牆的操作可以參考博文CentOS7下操作iptables防火牆和firewalld防火牆 。
④ 關閉selinux
如果不關閉selinux,就要做針對性配置,否則會出現諸如403 Forbidden,或者you have no privileges…之類錯誤提示。
參考博文:Centos7下查看並關閉selinux
⑤ 瀏覽器訪問默認頁面
【2】創建用戶家目錄站點
Centos6下直接修改httpd.conf:
[root@janus www]# vim /etc/httpd/conf/httpd.conf
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
#UserDir disabled #這裏註釋掉
#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
#
UserDir public_html #這裏放開註釋
</IfModule>
設置每個用戶 web 站點目錄的訪問權限:
<Directory "/home/*/public_html">
Options Indexes FollowSymLinks
AllowOverride None
Allow from all
</Directory>
創建用戶並設置密碼:
useradd jane
passwd jane
在用戶家目錄創建文件夾public_html
並添加index.html即可訪問!
su jane
mkdir public_html
cat > ./public_html/index.html
this is jane virtual host!
需要注意的是,若是提示404或者403,不妨查看下httpd的錯誤日誌根據錯誤進行解決。如果遇到權限問題,可以使用如下命令添加執行權限:
chmod -R a+x /home/*
Centos7下主要步驟一致,區別是你用yum安裝的httpd默認的httpd.conf中沒有<IfModule mod_userdir.c>
,但是有配置文件userdir.conf,在這裏面修改即可!
瀏覽器訪問實例如下:
【3】配置基於同一IP不同端口的站點目錄
需求:訪問不同端口,展示不同站點目錄的文件。
① 創建兩個目錄並分別在目錄下創建文件index.html
mkdir /var/www/vhost-ip8000
mkdir /var/www/vhost-ip8888
將編輯好的index.html放到對應目錄下。
② 修改httpd.conf
追加配置如下:
Listen 8000
Listen 8888
<VirtualHost 192.168.18.130:8000>
DocumentRoot /var/www/vhost-ip8000
</VirtualHost>
<VirtualHost 192.168.18.130:8888>
DocumentRoot /var/www/vhost-ip8888
</VirtualHost>
重啓服務,即可訪問測試!
【4】配置基於不同IP地址的虛擬主機
① 在/var/www下分別創建vhost-ip30 、vhost-ip40目錄
② 分別在兩個目錄中創建對應的index.html
[root@janus www]# cat > vhost-ip30/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>virtual host 192.168.18.30</p>
</body>
</html>
[root@janus www]# cat > vhost-ip40/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>virtual host 192.168.18.40</p>
</body>
</html>
^C
[root@janus www]#
③ 設置網卡的別名,併爲其分別設置ip地址
[root@janus www]# ifconfig eth0:0 192.168.18.30 netmask 255.255.255.0 up
[root@janus www]# ifconfig eth0:1 192.168.18.40 netmask 255.255.255.0 up
[root@janus www]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:F7:4F:28
inet addr:192.168.18.130 Bcast:192.168.18.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef7:4f28/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2780 errors:0 dropped:0 overruns:0 frame:0
TX packets:777 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:221194 (216.0 KiB) TX bytes:111819 (109.1 KiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:F7:4F:28
inet addr:192.168.18.30 Bcast:192.168.18.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:F7:4F:28
inet addr:192.168.18.40 Bcast:192.168.18.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
lo Link encap:Local Loopback
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:226 errors:0 dropped:0 overruns:0 frame:0
TX packets:226 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:11340 (11.0 KiB) TX bytes:11340 (11.0 KiB)
需要注意的是,這種方法設置別名,重啓即會實效,有兩種方案可以解決:
- 1.將增加ip別名的命令填寫到/etc/rc.local文件中
- 2.手動編寫ip別名的網卡配置文件
所有網卡的配置文件都保存在/etc/sysconfig/network-scripts目錄中,一個網卡對應一個配置文件,文件ifcfg-eth0就是本機網卡的配置文件,我們可以通過仿照這個文件的格式來編寫其他ip別名的配置文件。
清除ip別名:
ifconfg eth0:0 down
ifconfg eth0:1 down
④ 編輯/etc/httpd/conf/httpd.conf
配置文件
追加配置如下:
<VirtualHost 192.168.18.30>
DocumentRoot /var/www/vhost-ip30
</VirtualHost>
<VirtualHost 192.168.18.40>
DocumentRoot /var/www/vhost-ip40
</VirtualHost>
重啓httpd服務,使用不同ip地址訪問!