Apache

来源:互联网 发布:路由器mac地质作用 编辑:程序博客网 时间:2024/06/06 10:03

安装Apache

yum install httpd

systemctl start httpd
systemctl stop firewalld


1.修改默认发布文件

vim /etc/httpd/conf/httpd.conf


164 DirectoryIndex westos.html

##默认发布文件


vim westos.html

##创建发布文件


测试:


2.修改默认发布目录vim /etc/httpd/conf/httpd.conf

##只允许172.25.254.250访问admin

    ##当selinux是disabled状态


vim /westos/www/test/westos.html

##创建发布文件


vim /etc/httpd/conf/httpd.conf


120 DocumentRoot "westos/www/test"
<Directory "/westos/www/test">
Require all granted
</Directory>


systemctl restart httpd


测试:


    ##当selinux是enforcing状态


vim /etc/httpd/conf/httpd.conf


systemctl restart httpd

    然后修改安全上下文:

ls -Z

##查看安全上下文


semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’

##修改安全上下问

restorecon -RvvF /westos

##重新加载安全上下文


测试:



3.Apache的访问控制

##设定IP的访问

vim /etc/httpd/conf/httpd.conf

##允许所有人访问admin除了172.25.254.60


##添加默认发布文件


##创建发布文件


测试:172.25.254.60


其他用户


vim /etc/httpd/conf/httpd.conf

##只允许172.25.254.250访问admin


测设:



##设定用户的访问

htpasswd -cm /etc/httpd/accessuser admin 

##创建访问的用户认证文件 -c create -m 指定名称


htpasswd -m /etc/httpd/accessuser tom 

##添加tom用户认证


vim /etc/httpd/conf/httpd.conf
<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访问,二写一
</Director>


测试:




4.Apache的语言支持

html语言

##默认支持


php语言

yum install php


vim /var/www/html/index.php

##创建php发布文件


编辑内容php语言


systemctl restart httpd

测试:



cgi语言

mkdir /var/www/html/cgi
vim /var/www/html/cgi/index.cgi


##编辑cgi语言,显示date


chmod +x index.cgi

##给予发布文件执行权限


vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/cgi">##目录
Options +ExecCGI##运行cgi
AddHandler cgi-script .cgi##添加cgi脚本
</Directory>


测试:



5.Apache的虚拟主机

(1)定义

以让我们的一台apache服务器在被访问不同域名的时候显示不同主页

(2)建立测试页

cd /var/www/
mkdir virtual/news.westos.com -p
mkdir virtual/money.westos.com -p
mkdir virtual/news.westos.com/html -p
mkdir virtual/money.westos.com/html -p
echo "money.westos.com's page" > virtual/money.westos.com/html/index.html
echo "news.westos.com's page" > virtual/news.westos.com/html/index.html


(3)配置

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>


<Virtualhost *:80>
ServerName "money.westos.com"
DocumentRoot "/var/www/virtual/money.westos.com/html"
CustomLog "logs/news.log" combined
</Virtualhost>
<Directory "/var/www/virtual/money.westos.com/html"> ##默认发布目录的访问权限
Require all granted
</Directory>


(4)测试

在浏览器所在主机中

vim /etc/hosts


172.25.254.100 www.westos.com news.westos.com money.westos.com


测试1:


测试2:


测试3:




6.HTTPS

(1)https定义

Hyper text transfer protocol over Secure socker layer

通过ssl加密

(2)配置

yum install mod_ssl -y


yum install crypto-utils -y


genkey www.westos.com

##证书,密钥位置

##密钥字节大小

##wait

##生成密码(敲键盘或移动鼠标生成)

##选择不向CA发送请求

##不加密密钥

##修改证书

/etc/pki/tls/private/www.westos.com.key
/etc/pki/tls/certs/www.westos.com.crt
vim /etc/httpd/conf.d/login.conf


<Virtualhost *:443>
ServerName "login.westos.com"
DocumentRoot "/var/www/virtual/login.westos.com/html"
CustomLog "logs/login.log" combined
SSLEngine on ##开启https功能
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##证书
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##密钥
</Virtualhost>

<Directory "/var/www/virtual/login.westos.com/html">
Require all granted
</Directory>


<Virtualhost *:80> ##网页重写实现自动访问https
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>



##^g(/.*)$ 客户主机在地址栏中写入的素有字符不包括换行符
##https:// 定向成为的访问协议
##%{HTTP_HOST} 客户请求主机
##$1 $1的值就表示##^(/.*)$
##[redirect=301] 临时重定向 302永久重定向

mkdir /var/www/virtual/login.westos.com/html -p

vim /var/www/virtual/login.westos.com/html/index.html


systemctl restart httpd


测试:

在客户主机中添加解析


172.25.254.127 login.westos.com


访问http://login.westos.com会自动跳转带

https://login.westos.com实现网页数据加密传输

##选择I Understand the Risks


##Get Certificate---->

##---->Confirm Security Exception


##自动跳转


##证书信息