确保yum仓库正常
安装httpd服务
yum install httpd
输入Y继续安装依赖包
本人使用虚拟机环境的话需要先安装完httpd后创建快照!!!!方便还原状态!
2:启动httpd服务,默认未启动
systemctl start httpd //启动httpd服务,启动未回应及开启成功,如有错误则会报错
查看httpd是否启动命令:systemctl status httpd
显示active(running)即为开启
3:将httpd服务加入开机启动项,默认没有加入开机启动项
[root@localhost yum.repos.d]# systemctl enable httpd //设置自启动
[root@localhost yum.repos.d]# systemctl is-enabled httpd //查看自启动是否打开
4:打开火狐浏览器,输入127.0.0.1,即可查看是否安装成功服务
尝试做一个最最最简单的网页
1:在/var/www/html下创建一个index.html
[root@localhost ~]# cd /var/www/html //进入网页文件存放目录
[root@localhost html]# touch index.html //创建 index.html文件
[root@localhost html]# vim index.html //编辑 index.html文件
键盘按O编辑
输入文字或者其它代码
按下键盘左上角esc
再同时按下shift+;键
输入wq回车保存
打开自带火狐浏览器
输入127.0.0.1即可看到刚才保存得index.html文件内容
但是服务器提供apache服务是给用户访问的,服务器自己能访问自己只能算成功了一部分,现在尝试使用宿主机(物理机)的浏览器访问虚拟机提供的apache服务。
物理机ping虚拟机可以ping通
发现物理机输入虚拟机IP打不开网页则需要将防火墙关闭
一条命令干进去
systemctl stop firewalld //关闭防火墙命令
systemctl status firewalld //查看防火墙状态命令
如下显示 inactive(dead)即为关闭
再次 物理机输入虚拟机ip即可访问网页地址
接下来基于不同IP的虚拟主机
改为手动,添加三个IP及子网掩码等,网关可以不用,然后点击右下角的应用
将这里关闭再次打开就生效成功
命令行输入ip address,即可查看ip
在命令行粘贴一下代码生成文件
mkdir /var/www/html/131 //创建131目录
mkdir /var/www/html/132 //创建132目录
mkdir /var/www/html/133 //创建132目录
echo "192.168.152.131" > /var/www/html/131/index.html //创建index.html文件
echo "192.168.152.132" > /var/www/html/132/index.html //创建index.html文件
echo "192.168.152.133" > /var/www/html/133/index.html //创建index.html文件
'
vim /etc/httpd/conf/httpd.conf,编辑httpd.conf文件
按下键盘左上角的esc
同时按下shift+;键
输入 set nu,可以显示行号
复制下方内容至113行开始,上下左右键更改ip及目录等信息,分别代表三个网站,ip记得改正确!!!
# 131
<virtualHost 192.168.152.131>
DocumentRoot /var/www/html/131
<Directory /var/www/html/131>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
# 132
<virtualHost 192.168.152.132>
DocumentRoot /var/www/html/132
<Directory /var/www/html/132>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
# 133
<virtualHost 192.168.152.133>
DocumentRoot /var/www/html/133
<Directory /var/www/html/133>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
按下键盘的esc,同时按下shift+;输入wq 回车保存
重启httpd
systemctl restart httpd //重启httpd服务
systemctl stop firewalld //关闭防火墙,已经关闭请忽略
结果图
端口访问测试
分别创建1234,4567文件夹及这两个文件夹下的index文件
然后在index.html文件中写出这是哪个端口的文件
mkdir /var/www/html/1234
mkdir /var/www/html/4567
echo "port:1234" > /var/www/html/1234/index.html
echo "port:5678" > /var/www/html/5678/index.html
再次编辑 vim /etc/httpd/conf/httpd.conf
再次按下esc,输入set nu回车可显示行数
上下左右按键移动至42行处按英文字母O,输入一下内容,实现监听这两个端口
listen 1234
listen 5678
继续下滑至115行处按o换行右击粘贴下方代码,并更改目录及ip端口等信息
<virtualHost 192.168.152.131:1234>
DocumentRoot /var/www/html/1234
<Directory /var/www/html/1234>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<virtualHost 192.168.152.131:5678>
DocumentRoot /var/www/html/5678
<Directory /var/www/html/5678>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
操作完成后按esc
同时按下shirt+;键
输入wq保存
重启httpd
systemctl restart httpd
发现报错了!!!!!systemctl status httpd.serivce以及journalctl -xe的报错
Permission Denied \1234\Failed \SELinux可以看到这个selinux机制拒绝了此次重启,导致失败。
SELinux服务有三种配置模式,具体如下。
Ø enforcing:强制启用安全策略模式,将拦截服务的不合法请求。
Ø permissive:遇到服务越权访问时,只发出警告而不强制拦截。
Ø disabled:对于越权的行为不警告也不拦截
临时允许使用命令:setenforce 0 解决此问题
再次输入:systemctl restart httpd 可以正常重启httpd
长期则需要进入/etc/selinux/conf,将SELINUX=ENFORCING 修改成disabled(这里就不设置了)
输入getenforce ,当看到permissive则表示成功
虚拟机内使用端口访问网站