赞
踩
Apache(或httpd)服务,是Internet上使用最多的Web服务器技术之一,通俗来讲就是一个用于搭建网站的服务。
有两个版本:
1、配置文件的位置
配置文件 存放位置
服务目录 /etc/httpd
主配置文件 /etc/httpd/conf/httpd.conf
虚拟主机的配置文件目录
配置文件 存放位置
服务目录 /etc/httpd
主配置文件 /etc/httpd/conf/httpd.conf
虚拟主机的配置文件目录 /etc/httpd/conf.d
基于用户的配置文件 /etc/httpd/conf.d/userdir.conf
日志文件目录 /etc/httpd/logs
默认的网站数据目录 /var/www/html
2、主配置文件的重要参数
主配置文件:/etc/httpd/conf/httpd.conf
参数 作用 参数 作用
serverRoot 服务目录 Servername 网站服务器的域名
Listen 监听的IP地址端口号 DocumentRoot 默认网站数据目录
User 运行服务的用户 Directory 文件目录权限
Group 运行服务的用户组 DirectoryIndex 默认的索引页面
Serveradmin 管理员邮箱 ErrorLog 错误日志文件
基本环境:主机名、网卡网络、yum源
[root@localhost ~]# hostnamectl set-hostname Ayaka
[root@localhost ~]# bash
(1)虚拟机NAT网段配置为192.168.123.0网段(可随意)、网卡适配器选择仅主机模式
(2)配置网卡:
需要修改的参数:
BOOTPROTO=static
IPADDR=192.168.123.101
NETMASK=255.255.255.0
(3)重新启动网络服务
[root@ayaka ~]# systemctl restart network
1、搭建简单的httpd服务
1.1、安装Apache服务
[root@ayaka ~]# yum install -y http
1.2、关闭防火墙
[root@ayaka ~]# systemctl stop firewalld
1.3启动Apache服务
[root@ayaka ~]# systemctl restart httpd
访问Apche网站
[root@ayaka ~]# curl 192.168.123.101
·首先确定已经安装了httpd服务、
[root@localhost ~]# useradd ayaka
[root@localhost ~]# mkdir /home/ayaka/public_html
[root@localhost ~]# cd /home/ayaka/public_html/
[root@localhost ~]# echo “welcome to ayaka’s website” >>index.html
[root@localhost ~]# chmod -R 705 /home/ayaka
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf
修改第17行和24行
UserDir enable 改为开启,表示开启个人用户主页功能
UserDir public_html 去注释,表示网站数据在用户家目录中的名称
[root@localhost public_html]# systemctl stop firewalld
[root@localhost public_html]# setenforce 0
[root@localhost public_html]# systemctl restart httpd
没有图形化:Curl httpd://192.168.123.101/~ayaka/
图形化:firefox //192.168.123.101/~ayaka/
或在主机浏览器搜索192.168.123.101/~ayaka/
以“www.toto.com”为域名来创建一个虚拟网站
1.网站数据存放在/www/toto/下
2.网站主页内容为:“welcome to toto’s website”
3.网站对所有客户端开放
#老样子 首先还是确认安装了httpd服务
[root@localhost public_html]# rpm -q httpd
httpd-2.4.6-95.el7.centos.x86_64
[root@localhost public_html]# mkdir /www/toto -p
[root@localhost toto]# chmod o+x /www
[root@localhost toto]# chmod o+x index.html
[root@localhost toto]# cd /etc/httpd/conf.d
[root@localhost conf.d]# vim toto.conf
<Virtualhost 192.168.123.101>
ServerName www.toto.com //定义域名
DocumentRoot /www/toto //网站主页文件的目录
<Directory /www/toto>
require all granted //所有客户端都可以访问
</Directory>
</Virtualhost>
~
[root@localhost conf.d]# vim /etc/hosts
第三行添加 192.168.123.101 www.toto.com
[root@localhost conf.d]# firewall-cmd --reload
[root@localhost conf.d]# firewall-cmd --permanent --add-service=http
[root@localhost conf.d]# systemctl restart httpd
访问:
无图形化界面
Curl www.toto.com
又图形画界面
Firefox www.toto.com
配置两个新的访问端口,分别为8088和8089
1.网站域名为 www.toto.com
2.网页数据分别存在/www/8088和/www/8089下面
3.每个端口主页内容分别为:“this is new port (8088或8089)for www.toto.com”
配置:
#首确安服
[root@localhost conf.d]# mkdir /www/8088 -p
[root@localhost conf.d]# mkdir /www/8089 -p
[root@localhost conf.d]# cd /www/8088
[root@localhost 8088]# echo "this is a new port 8088 for www.toto.com" >>index.html
[root@localhost 8088]# cd /www/8089
[root@localhost 8089]# echo "this is a new port 8089 for www.toto.com" >>index.html
[root@localhost 8089]# chmod o+x /www
[root@localhost 8089]# chmod o+x /www/8088/index.html
[root@localhost 8089]# chmod o+x /www/8089/index.html
[root@localhost conf.d]# vim 8088.conf
<Directory /www/8088/>
Require all granted
</Directory>
<VirtualHost 192.168.123.101:8088>
DocumentRoot /www/8088
Servername www.toto.com
</VirtualHost>
<VirtualHost 192.168.123.101:8089>
DocumentRoot /www/8089
ServerName www.toto.com
</VirtualHost>
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
42 Listen 80
43 Listen 8088
44 Listen 8089
[root@localhost conf.d]# firewall-cmd --add-port=8088/tcp
success
[root@localhost conf.d]# firewall-cmd --add-port=8089/tcp
success
[root@localhost conf.d]# firewall-cmd --reload
success
systemctl restart httpd
搭建网站并完成认证
以www.basic.com为域名创建一个虚拟网站
1.网页数据放在/www/basic 主页为basic.html
2.网页主内容为 “Hello world”
3.创建用户webuser1,webuser2 密码为1,实现网站的认证访问,只有这两个用户才能访问
[root@localhost ~]# mkdir /www/basic -p
[root@localhost ~]# cd /www/basic/
[root@localhost toto]# echo “Hello world” >>index.html
[root@localhost toto]# chmod +x /www
[root@localhost toto]# chmod +x /www/basic/index.html
需要修改的参数
119 DocumentRoot "/www"
124 <Directory "/www">
131 <Directory "/www">
在服务目录的最后添加认证信息
355 <VirtualHost 192.168.123.101:80>
356 ServerName www.basic.com
357 DocumentRoot /www/basic
358 <Directory /www/basic>
359 AuthType basic
360 Authname passwd
361 AuthUserfile /etc/httpd/webpasswd
362 require user webuser1
363 </Directory>
364
365 </VirtualHost
[root@www ~]# htpasswd -c /etc/httpd/webpasswd webuser1 //创建认证用户webuser1
New password: //输入密码
Re-type new password: //输入密码
Adding password for user webuser1
[root@www ~]# htpasswd /etc/httpd/webpasswd webuser2
New password: //输入密码
Re-type new password: //输入密码
Adding password for user webuser2
[root@www ~]# systemctl stop firewalld
[root@www ~]# setenforce 0
[root@www ~]# systemctl restart httpd
字符界面
[root@www ~]# curl www.basic.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
图形化访问
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。