linux学习 Apache

Apache的基本安装

<一>Apache简介

一、Apache的主要优点:

  支持HTTP/1.1协议:实现了HTTP/1.1与HTTP/1.0兼容;

  支持CGI协议:使用mod_cgi模块来支持CGI功能。

  支持HTTP认证:

  Apache支持虚拟主机:即在一台服务器上使用不同的主机名来提供多个HTTP服务等等

二、Apache的软件安装及基本配置文件

(1)rpm  -qa |  grep  httpd

  yum install  httpd  -y  ##Apache被重新命名为HTTP

(2)安装成功后生成的主要文件及目录有:

1.apache的默认发布目录及文件:

  /var/www/html    ##默认目录

  /var/www/html/index.html   ##默认发布文件

2.apache的配置文件:

  /etc/httpd/conf/httpd.conf   ##Apache的主配置文件

  /etc/httpd/conf.d/*.conf     ##apache的子配置文件

(3)apache的默认端口:80端口

可以使用命令来查看Apache是否已经监听80端口

netstat  -an  |grep  :80

为保证客户端能够访问Apache服务,可以在防火墙中开放对80端口的限制,命令如下:

iptables  -I INPUT  -p  tcp --dport  80  -j ACCEPT

linux学习 Apache

<二>Apache的基本配置

一、修改默认发布文件

vim  /etc/httpd/conf/httpd.conf

164行原内容为:DirectoryIndex  index.html  ##默认发布文件为/var/www/html/index.html

修改为:DirectoryIndex westos.html  ##默认发布文件为westos.html

:wq

linux学习 Apache

二、修改默认发布目录

1.先建立目录

mkdir  /westos/www/test  -p   ##第归建立目录/westos/www/test

vim  /westos/www/test/westos.html  ##编辑默认发布文件

linux学习 Apache

2.修改默认发布目录

vim  /etc/httpd/conf/httpd.conf

编辑:

120DocumentRoot "/westos/www/test"  ##默认发布目录为/westos/www/test

<Directory  "/westos/www/test">

       Require all  granted   ##授权

</Directory>

原内容如下:

linux学习 Apache

修改后内容如下:

linux学习 Apache

3.systemctl  restart httpd   ##一定要重启服务

三、.测试:在客户端访问http://172.25.254.109

linux学习 Apache

注意:当访问时出现下列现象,需检查selinux是否是enforcing状态。若是enforcing状态,则需要更改为disabled状态或者查看并更改默认发布文件的安全上下文。

 linux学习 Apache

查看文件安全上下文:

cd  /westos/www/test

ls -Z

安全上下文修改:semanage  fcontext  -a -t  http_sys_content_t  '/westos(/.*)?'

restorecon  -RvvF /westos    ##刷新

或者:查看selinux状态,编辑配置文件使selinux为disabled状态

vim  /etc/selinux/config

SELINUX=disabled

:wq

reboot

<三>设定IP访问

一、设定IP访问权限vim  /etc/httpd/conf/httpd.conf

1.去掉119行注释,使默认发布目录/var/www/html生效

  164行改回:DirectoryIndex  index.html

linux学习 Apache

2.cd  /var/www/html

  mkdir admin   ##新建目录

  cd admin

  vim index.html   ##编辑发布文件

linux学习 Apache

3.vim  /etc/httpd/conf/httpd.conf

 编辑:

(1)  ##允许所有人访问admin目录但是拒绝IP为172.25.254.90的机主

124  <Directory"/var/www/html/admin">

125              order Allow,Deny

126         Allow from All

127         Deny from 172.25.254.90

128</Directory>

:wq

linux学习 Apache

重启HTTP服务

在172.25.254.90主机测试结果如下:

linux学习 Apache

在其他主机测试结果如下:

linux学习 Apache

(2)#只允许172.25.254.90的机主访问

124<Directory "/var/www/html/admin">

125         order  Deny,Allow  

126         Allow from 172.25.254.90

127         Deny from All

128</Directory>

:wq

linux学习 Apache

重启服务

在172.25.254.109主机测试如下:

linux学习 Apache

在其他主机测试如下:

linux学习 Apache

<三>设定用户的访问

1.对发布目录加密

[[email protected]]# htpasswd -cm /etc/httpd/accessuser admin   ##对该目录加密

Newpassword:   ##不显行输入密码

Re-typenew password:   ##不显行确认密码

Addingpassword for user admin   ##设置成功

可以查看密码:cat /etc/httpd/accessuser

linux学习 Apache

2.编辑配置文件

vim  /etc/httpd/conf/httpd.conf

124<Directory "/var/www/html/admin">

125         AllowOverride  All  ##允许使用目录中.htaccess文件

126         AuthUserFile  /etc/httpd/accessuser  ##设置文件密码的路径

127         AuthName  "Please input your name andpassword!"  ##在浏览器访问时显示的内容

128         AuthType  basic ##用户认证的类型是由mod_auth提供的basic

129         Require  valid-user ##定义允许访问的用户

##Require user  admin   也可以用此命令来定义可以访问的用户

130</Directory>

:wq

linux学习 Apache

3.systemctl  restart httpd

4.测试:

若输入正确的用户和密码即可访问成功:

linux学习 Apache

linux学习 Apache

若输入密码或用户错误会显示“Authorization Required”的错误信息

linux学习 Apache

<四>Apache语言支持

1.php语言

vim /var/www/html/index.php

<?php

       phpinfo();

?>

systemctl restart  httpd

2.cgi语言

(1)mkdir /var/www/html/cgi

  cd /var/www/html/cgi

(2)vim index.cgi

    #!/usr/bin/perl

    print "Content-type: text/html\n\n";

    print `date` ;         ##执行该命令

   :wq

(3)执行脚本

    perl  index.cgi      ##执行脚本

linux学习 Apache

   chmod  +x  index.cgi      ##添加执行权限

  则可以执行该命令来执行脚本:  ./index.cgi

linux学习 Apache

3.客户端访问设置

(1)http服务端

vim /etc/httpd/conf/httpd.conf

编辑内容如下:

   <Directory"/var/www/html/cgi">

       Options+ExecCGI

       AddHandler  cgi-script.cgi

   </Directory>

  :wq

linux学习 Apache

(2)systemctl  restart  httpd      ##重启服务

(3)在客户端访问:172.25.254.109/cgi/index.cgi,即在客户端执行该文件内容

linux学习 Apache

<五>Apache的虚拟主机配置

一、定义:可以让一台Apache服务器在被访问不同域名时,显示不同的主页

二、配置过程

(1)先建立不同域名的默认发布目录及默认发布文件

mkdir /var/www/virtual  -p  

mkdir /var/www/virtual/news.westos.com -p   

mkdir /var/www/virtual/news.westos.com/html -p    ##建立news.westos.com网页的默认发布目录

cd /var/www/

echo "news.westos.com'page" >virtul/news.westos.com/html/index.html  

##建立news.westos.com的默认发布文件

linux学习 Apache

(2)配置子配置文件:指定域名的默认发布目录及默认发布文件

cd /etc/httpd/conf.d                     ##访问其他域名时默认执行该文件内容

vim default.conf    

<Virtualhost _default_:80>                ##访问默认80端口时看以下文件内容

       DocumentRoot  "/var/www/html"      ##默认发布目录为/var/www/html

       CustomLog  "logs/default.log"combined    ##将Apache的四种日志文件均记录

</Virtualhost>

注:Apache的有四种日志类型:被访问时的日志、拒绝访问的日志、警告日志、Apache出现报错的日志

linux学习 Apache

(3)systemctl  restart httpd    ##重启服务

(4)测试端

1.修改本机解析

vim /etc/hosts

IP news.westos.com  www.westos.com

linux学习 Apache

2.浏览器端访问:news.westos.com

linux学习 Apache

 

(5)vim news.westos.conf                    ##子配置文件

<Virtualhost *:80>                       

       Servername "news.westos.com"      ##访问域名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>

linux学习 Apache

(3)systemctl  restart httpd    ##重启服务

(3)测试端

1.修改本机解析

vim /etc/hosts

IP news.westos.com  www.westos.com

linux学习 Apache

3.浏览器端访问:news.westos.com

linux学习 Apache

(5)vim  money.westos.conf

<Virtualhost *:80>                       

       Servername "money.westos.com"      ##访问域名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>

:wq

linux学习 Apache

linux学习 Apache

(7)重启服务

(8)测试端访问money.westos.com

linux学习 Apache

<六>网页访问https

一、下载相关软件

1.yum install  mod_ssl  -y   ##下载加密认证模块

2.yum install crypto-utils ##下载证书

3.执行命令:genkey  www.westos.com     ##生成该网页的证书和**

linux学习 Apache

如下:选择密码长度

linux学习 Apache

 linux学习 Apache

 linux学习 Apache

 

如下生成密码过程中,需要敲键盘或者移动鼠标

linux学习 Apache

如下:选择将不给CSR发送认证申请

linux学习 Apache

linux学习 Apache

linux学习 Apache

linux学习 Apache

二、建立域名默认目录及文件

1.cd /var/www/virtual

 mkdir login.westos.com/html -p

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

 <h1>login.westos.com</h1>

linux学习 Apache

linux学习 Apache

2.vim /etc/httpd/conf.d/login.conf    ##写子配置文件

<Virtualhost *:443>                     ##设置以下内容访问的端口是443,即https访问

       Servername "login.westos.com"  ##域名login.westos.com

       DocumentRoot "/var/www/virtual/login.westos.com/html" 

       CustomLog "logs/login.log" combined    ##保存日志

       SSLEngine on                    ##开启网络认证

       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>

linux学习 Apache

3.systemctl  restart httpd

4.测试端:

vim /etc/hosts

Ip login.westos.com   ##解析

再访问:https:login.westos.com

linux学习 Apache

会出现以下提示信息,选择risk

linux学习 Apache

再选择获得证书或认证

linux学习 Apache

查看证书信息

linux学习 Apache

(4)网页重写

vim /etc/httd/cond.g/login.conf

添加内容如下

<Virtualhost *:80>                  ##当域名访问端口80时,即http访问,执行以下内容

       Servername login.westos.com 

       RewriteEngine on             ##域名重写功能开启

       RewriteRule ^ (/.*)$ https://%{HTTP_HOST}$1 [redirect=301]   ##将所有字符临时转换为https格式的字符

</Virtualhost>

###[redirect=301]:表示临时转换

###[redirect=302]:表示永久转换

:wq

linux学习 Apache

(5)systemctl  restart httpd

(6)测试

在证书所在端进行访问先作地址解析

vim /etc/login.westos.com

172.25.254.109  login.westos.com

:wq

再访问:login.westos.com

linux学习 Apache

linux学习 Apache