第三本书 apache
1.apache的安装
yum install httpd –y
systemctl start httpd
systemctl stop firewalld
systemctl enable httpd
systemctl disable firewalld
setenforce 0
2.apache信息
(1)apache的默认发布文件
index.html
(2)apache的配置文件
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
(3)apache的默认发布目录
/var/www/html
(4)apache的默认端口
80
3.apache的基本配置
(1)修改默认发布文件
(2)修改默认发布目录
##当selinux处于disable状态
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot “/westos/www/test” ##设置权限
<Directory “/westos/www/test”>
Require all granted
##当selinux是enforcing状态
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot “/westos/www/test”
<Directory “/westos/www/test”>
Require all granted
</Directory>
systemctl restart httpd
semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’ #设置安全上下文
##设定ip的访问
mkdir /var/www/html/admin
vim /var/www/html/westos.html
vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/admin”> ##允许所有人访问admin目录但拒绝73
Order Allow,Deny
Allow from All
Deny from 172.25.254.73
</Directory>


<Directory “/var/www/html/admin”> ##只允许130访问admin目录
Order Deny,Allow
Deny from All
Allow from 172.25.254.130
</Directory>
##设定用户的访问
<Directory “/var/www/html/admin”>
AuthUserFile /etc/httpd/accessuser ##用户认证文件
Authname “Please input your name and password” ##用户认证提示
AuthType basic ##认证类型
Require valid-user ##认证文件中所有用户都可以通过
[Require user admin] ##只允许文件中的admin通过(二选一)
php html cgi
html语言默认支持
php语言
yum install php –y
systemctl restart httpd
cgi语言
mkdir /var/www/html/cgi
vim index.cgi
如图
chmod +x index.cgi
在主配置文件的默认发布文件中加入index.cgi
(1)定义
可以让一台服务器被访问不同的域名时显示不同的主页
(2)建立测试页
cd /var/www
mkdir /virtual/money.westos.com/html
mkdir /virtual/news.westos.com/html
echo “money” > /virtual/money.westos.com/html/index.html
vim /etc/httpd/conf.d/default.conf ##未指定域名的访问都访问default
<Virtualhost default_:80> ##虚拟主机开启的端口
DocumentRoot “/var/www/ html”##默认发布目录
CustomLog “logs/default.log” combined ##虚拟主机日志
</Virtualhost>

vim /etc/httpd/conf.d/news.conf ##指定news.westos.com的访问到默认发布目录
<Virtualhost *:80>#虚拟主机开启的端口
Servername “news.westos.com”
DocumentRoot “/var/www/virtual/news.westos.com/html”
CustomLog “logs/news.log” combined
</Virtualhost>
<Directory “/var/www/virtual/news.westos.com/html”> ##默认发布目录访问授权
Require all granted
在浏览器所在主机中
vim /etc/hosts
yum install mod_ssl crypto-utils -y
genkey www.westos.com
vim /etc/httpd/conf.d/login.conf
如图

#^(/.*)$ 客户主机在地址栏中写入的所有字符
#http:// 定向成为的访问协议
#%{HTTP_HOST} 客户请求主机
#$! $1的值就表示^(/.*)$的值
#【redirect=301】 临时重定向 302永久重定向
mkdir /var/www/virtual/login.westos.com/html -p
测试:
在客户主机 /etc/hosts中添加
172.25.254.130 login.westos.com
yum install httpd –y
systemctl start httpd
systemctl stop firewalld
systemctl enable httpd
systemctl disable firewalld
setenforce 0
2.apache信息
(1)apache的默认发布文件
index.html
(2)apache的配置文件
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
(3)apache的默认发布目录
/var/www/html
(4)apache的默认端口
80
3.apache的基本配置
(1)修改默认发布文件
164 DirectoryIndex westos.html
(2)修改默认发布目录
##当selinux处于disable状态
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot “/westos/www/test” ##设置权限
<Directory “/westos/www/test”>
Require all granted
</Directory>
##当selinux是enforcing状态
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot “/westos/www/test”
<Directory “/westos/www/test”>
Require all granted
</Directory>
systemctl restart httpd
semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’ #设置安全上下文
restorecon -Rvvf /westos ##重启
##设定ip的访问
mkdir /var/www/html/admin
vim /var/www/html/westos.html
vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/admin”> ##允许所有人访问admin目录但拒绝73
Order Allow,Deny
Allow from All
Deny from 172.25.254.73
</Directory>
<Directory “/var/www/html/admin”> ##只允许130访问admin目录
Order Deny,Allow
Deny from All
Allow from 172.25.254.130
</Directory>
##设定用户的访问
htpasswd -m /etc/httpd/accessuser admin
<Directory “/var/www/html/admin”>
AuthUserFile /etc/httpd/accessuser ##用户认证文件
Authname “Please input your name and password” ##用户认证提示
AuthType basic ##认证类型
Require valid-user ##认证文件中所有用户都可以通过
[Require user admin] ##只允许文件中的admin通过(二选一)
</Directory>
php html cgi
html语言默认支持
php语言
yum install php –y
systemctl restart httpd
cgi语言
mkdir /var/www/html/cgi
vim index.cgi
如图
chmod +x index.cgi
在主配置文件的默认发布文件中加入index.cgi
vim /etc/httpd/conf/httpd.conf
如图
测试
(1)定义
可以让一台服务器被访问不同的域名时显示不同的主页
(2)建立测试页
cd /var/www
mkdir /virtual/money.westos.com/html
mkdir /virtual/news.westos.com/html
echo “money” > /virtual/money.westos.com/html/index.html
echo “news” > /virtual/news.westos.com/html/index.htm
l
vim /etc/httpd/conf.d/default.conf ##未指定域名的访问都访问default
<Virtualhost default_:80> ##虚拟主机开启的端口
DocumentRoot “/var/www/ html”##默认发布目录
CustomLog “logs/default.log” combined ##虚拟主机日志
</Virtualhost>
vim /etc/httpd/conf.d/news.conf ##指定news.westos.com的访问到默认发布目录
<Virtualhost *:80>#虚拟主机开启的端口
Servername “news.westos.com”
DocumentRoot “/var/www/virtual/news.westos.com/html”
CustomLog “logs/news.log” combined
</Virtualhost>
<Directory “/var/www/virtual/news.westos.com/html”> ##默认发布目录访问授权
Require all granted
<Directory>
在浏览器所在主机中
vim /etc/hosts
172.25.254.130 www.westos.com money.westos.com news.westos.com
yum install mod_ssl crypto-utils -y
genkey www.westos.com
vim /etc/httpd/conf.d/login.conf
如图
#^(/.*)$ 客户主机在地址栏中写入的所有字符
#http:// 定向成为的访问协议
#%{HTTP_HOST} 客户请求主机
#$! $1的值就表示^(/.*)$的值
#【redirect=301】 临时重定向 302永久重定向
mkdir /var/www/virtual/login.westos.com/html -p
vim /var/www/virtual/login.westos.com/html/index.html
测试:
在客户主机 /etc/hosts中添加
172.25.254.130 login.westos.com
访问http://login.westos.com 会自动跳转到https://login.westos.com实现网页加密