1:安裝 yum install httpdhtml
2:systemctl start httpdlinux
systemctl enable httpdweb
3:更改網站數據目錄vim
[root@localhost ~]# mkdir /home/wwwroot網絡
[root@localhost ~]# echo "this is /home/wwwroot/index.html" > /home/wwwroot/index.html
[root@localhost ~]# vim /etc/httpd/conf/httpd.conftcp
#網站數據保存目錄
DocumentRoot "/home/wwwroot"
#網站數據目錄權限
<Directory "/home/wwwroot">
AllowOverride None
Require all granted
</Directory>ide
[root@localhost ~]# systemctl restart httpd.service網站
[root@localhost ~]# ls -Zd /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@localhost ~]# ls -Zd /home/wwwroot
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwrootui
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*this
[root@localhost ~]# restorecon -Rv /home/wwwroot/
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
成功!
4:配置我的用戶主頁
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf
修改<IfModule mod_userdir.c>模塊內容
在 UserDir disabled 行前面加#
去掉 UserDir public_html 行前面的#號
[languang@localhost ~]$ mkdir public_html
[languang@localhost ~]$ echo "this is languang's website" > public_html/index.html
[languang@localhost ~]$ chmod -Rf 755 /home/languang/
[root@localhost ~]# getsebool -a | grep httpd
查看selinux管理域內關於httpd的全部項
[root@localhost ~]# setsebool -P httpd_enable_homedirs=on
[root@localhost ~]#systemctl restart httpd.service
完成!
添加訪問我的用戶主頁驗證
[root@localhost ~]# htpasswd -c /etc/httpd/passwd zhangsan
New password:
Re-type new password:
Adding password for user zhangsan
[root@localhost ~]# htpasswd /etc/httpd/passwd lisi
New password:
Re-type new password:
Adding password for user lisi
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf
<Directory "/home/*/public_html">
AllowOverride all
authuserfile "/etc/httpd/passwd"
authname "this is alex'website,please enter username and password"
authtype basic
Require user zhangsan lisi
</Directory>
[root@localhost ~]# systemctl restart httpd
完成!
5:配置基於IP地址的虛擬主機功能(使用虛擬機)
配置網絡IP地址(192.168.0.104,192.168.0.105,192.168.0.106)
[root@localhost ~]# nmtui
[root@localhost ~]# systemctl restart network
[root@localhost ~]# mkdir -p /home/wwwroot/104
[root@localhost ~]# mkdir -p /home/wwwroot/105
[root@localhost ~]# mkdir -p /home/wwwroot/106
[root@localhost ~]# echo "192.168.0.104" > /home/wwwroot/104/index.html
[root@localhost ~]# echo "192.168.0.105" > /home/wwwroot/105/index.html
[root@localhost ~]# echo "192.168.0.106" > /home/wwwroot/106/index.html
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf (大約在113行處添加)
<VirtualHost 192.168.0.104>
DocumentRoot "/home/wwwroot/104"
ServerName www.linuxprobe.com
<Directory "/home/wwwroot/104">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.0.106>
DocumentRoot "/home/wwwroot/106"
ServerName tech.linuxprobe.com
<Directory "/home/wwwroot/106">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.0.105>
DocumentRoot "/home/wwwroot/105"
ServerName bbs.linuxprobe.com
<Directory "/home/wwwroot/105">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/104
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/104/*
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/105
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/105/*
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/106
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/106/*
[root@localhost ~]# restorecon -Rv /home/wwwroot/
[root@localhost ~]# firewall-config (配置防火牆容許http服務)
[root@localhost ~]# systemctl restart httpd
完成!
6:配置基於域名的虛擬主機功能 (在虛擬機上配置)
[root@localhost ~]# vim /etc/hosts (添加)
192.168.0.188 www.languang.com bbs.languang.com tech.languang.com
[root@localhost ~]# ping tech.languang.com
PING www.languang.com (192.168.0.188) 56(84) bytes of data.
64 bytes from www.languang.com (192.168.0.188): icmp_seq=1 ttl=64 time=0.200 ms
[root@localhost ~]# mkdir -p /home/wwwroot/www
[root@localhost ~]# mkdir -p /home/wwwroot/bbs
[root@localhost ~]# mkdir -p /home/wwwroot/tech
[root@localhost ~]# echo "www.languang.com" > /home/wwwroot/www/index.html
[root@localhost ~]# echo "bbs.languang.com" > /home/wwwroot/bbs/index.html
[root@localhost ~]# echo "tech.languang.com" > /home/wwwroot/tech/index.html
[root@localhost ~]# ls -Zd /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/*
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/*
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/*
[root@localhost ~]# restorecon -Rv /home/wwwroot/
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/www context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/www/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/bbs context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/bbs/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/tech context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/tech/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.0.188>
DocumentRoot "/home/wwwroot/www"
ServerName "www.languang.com"
<Directory "/home/wwwroot/www">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.0.188>
DocumentRoot "/home/wwwroot/bbs"
ServerName "bbs.languang.com"
<Directory "/home/wwwroot/bbs">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.0.188>
DocumentRoot "/home/wwwroot/tech"
ServerName "tech.languang.com"
<Directory "/home/wwwroot/tech">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# systemctl restart httpd
完成!
7:配置基於端口號的虛擬主機功能 (在虛擬機上配置)
[root@localhost ~]# mkdir -p /home/virtualhost/6111
[root@localhost ~]# echo "this is 6111" > /home/virtualhost/6111/index.html
[root@localhost ~]# mkdir -p /home/virtualhost/6222
[root@localhost ~]# echo "this is 6222" > /home/virtualhost/6222/index.html
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6111
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6111/*
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6222
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6222/*
[root@localhost ~]# restorecon -Rv /home/virtualhost/
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf(在大概113行處添加)
<VirtualHost 192.168.0.188:6111>
DocumentRoot "/home/virtualhost/6111"
ServerName "www.languang.com"
<Directory /home/virtualhost/6111>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.0.188:6222>
DocumentRoot "/home/virtualhost/6222"
ServerName "www.languang.com"
<Directory /home/virtualhost/6222>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
(在Listen 80 處添加)
Listen 6111
Listen 6222
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 6111
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 6222
[root@localhost ~]# firewall-config(添加開放6111 6222 tcp端口,並放開http協議)
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
完成!