apache简述

Ngnix是我们在搭建网站过程中一个很重要的元素。
提到Ngnix我们就不得不说说Apache,他和Ngnix有什么区别呢?

Ngnix第一次接触是在搭建一个项目框架的时候。
而Apache第一次接触是在维护一个项目的时候,这个项目是没有用到Ngnix的。

因为这些主要的是配置方法,对于底层实现现阶段不需要了解得那么深刻,所以,先对其配置文件作出分析,等以后有机会的话分析更深层次的地方。

1.Apache

1.Apache目录结构(针对于Linux)

服务器通常是linux系统的,所以,这个配置文件也主要是针对于Linux
主要目录和文件(通常而言,即通过rpm安装的)
– 服务目录:/etc/httpd/
– 主配置文件:/etc/httpd/conf/httpd.conf
– 网页目录:/var/www/html/
– 服务脚本:/etc/init.d/httpd
– 执行程序:/usr/sbin/httpd
– 访问日志:/var/log/httpd/access_log
– 错误日志:/var/log/httpd/error_log

Apache服务目录结构说明
根目录

|– bin 程序命令目录
|– build
|– cgi-bin
|– conf 配置文件目录
|– error
|– htdocs 编译安装时站点目录
|– icons
|– include
|– lib
|– logs 默认日志文件存放包括错误日志(error_log)和访问日志(access_log) tail -f access_log可以随时看访问记录, 里面httpd.pid还会记录主进程号
|– man
|– manual
|– modules 模块目录 例如PHP MEMCACHE编译后的模块在这里面

[root@htdocs]# ls ../
bin启动文件目录说明

|– bin # apache服务的命令目录,类似的jmeter loadrunner webbench
| |– ab # http服务性能测试工具
| |– apachectl # apache启动命令
| |– apr-1-config
| |– apu-1-config
| |– apxs # http服务编译和安装扩展模块使用工具,在DSO方式模块编译时用到
| |– checkgid
| |– dbmmanage
| |– envvars
| |– envvars-std
| |– htcacheclean # 清理磁盘缓冲区的控制命令
| |– htdbm
| |– htdigest
| |– htpasswd # 建立和更新基本认证文件
| |– httpd # httpd为apache的控制命令程序
| |– httxt2dbm
| |– logresolve
| – rotatelogs # apache自带的日子轮询命令(cronolog生产环境使用比较多)。

conf配置文件目录
[root@htdocs]# ls ../conf/
extra  httpd.conf  magic  mime.types  original
/application/apache/conf/         # apache配置所有的配置文件目录

|– extra # 额外的apache配置文件目录 如:httpd-vhost.conf
| |– httpd-autoindex.conf
| |– httpd-dav.conf dav支持配置
| |– httpd-default.conf apache相关服务配置 如超时时间 保持连接时间
| |– httpd-info.conf
| |– httpd-languages.conf 语言支持
| |– httpd-manual.conf
| |– httpd-mpm.conf 服务器池管理,如apache模式配置 连接等
| |– httpd-multilang-errordoc.conf
| |– httpd-ssl.conf 支持SSL加密配置
| |– httpd-userdir.conf
| |– httpd-vhosts.conf 虚拟机配置文件
|– httpd.conf # apache主配置文件
|– magic
|– mime.types
|– original
| |– extra
| | |– httpd-autoindex.conf
| | |– httpd-dav.conf # dav支持配置
| | |– httpd-default.conf # 这个文件里配置的是apache的相关服务参数:超时时间、保持链接
| | |– httpd-info.conf
| | |– httpd-languages.conf # 语言支持
| | |– httpd-manual.conf
| | |– httpd-mpm.conf # 服务器池管理,也是优化apache的一个配置文件(apache的模式以及配置链接数,常用模式为worker模式和profork模式,默认porfork)
| | |– httpd-multilang-errordoc.conf
| | |– httpd-ssl.conf # 提供apache ssl支持配置文件
| | |– httpd-userdir.conf
| | |– httpd-vhosts.conf # 虚拟机的配置文件
| |– httpd.conf

站点存放目录
[root@apache]# ls -l htdocs/    # 默认的首页文件都放在此目录当中。

total 4
-rw-r–r– 1 root root 44 Nov 21 2004 index.html

日志存放目录
[root@es apache]# tree logs

logs # apache服务默认的日志路径,包括错误日志和访问日志
|– access_log # apache服务访问日志 tail -f命令可以查看网页用户访问的信息
|– cgisock.16406
|– error_log # apache服务的错误日志。如:apache服务启动故障
|– httpd.pid # httpd的pid文件,http进程启动后,会把所有进程的id号写到此文件

模块目录
[root@apache]# ls -l manual/     # apache的模块目录 如php mencache
total 2984

-rw-r–r– 1 root root 543 May 6 2008 bind.html
-rw-r–r– 1 root root 11422 Jan 3 2012 bind.html.de

2.httpd.conf主要参数

ServerTokens OS:
//在出现错误页的时候是否显示服务器操作系统的名称,设置Prod为不显示
ServerRoot "/etc/httpd":
//用于指定Apache的运行目录。服务器启动之后自动将目录改变为当前目录,在后面使用的所有相对路径都是相对于这个目录下

User daemon                          
//# apache的用户,默认为daemon
Group daemon                         
//# apache的用户,默认为daemon


PidFile run/httpd.pid
//记录httpd守护进程的pid号码,这是系统识别一个进程的方法,系统中httpd进程可以有多个,但这个PID对应的进程是其他的父进程

Timeout 60
//服务器与客户端断开的时间

KeepAlive Off
//是否持续连接(因为每次连接都得三次握手,如果是访问量不大,建议打开此项,如果网站访问量比较大关闭此项比较好),修改为:KeepAlive On 表示允许程序性联机

MaxKeepAliveRequests 100
//表示一个连接的最大请求数

KeepAliveTimeout 15
//断开连接前的时间

<IfModule prefork.c> 
StartServers       8 
MinSpareServers    5 
MaxSpareServers   20 
ServerLimit      256 
MaxClients       256 
MaxRequestsPerChild  4000 
</IfModule>
//系统默认的模块,表示为每个访问启动一个进程(即当有多个连接公用一个进程的时候,在同一时刻只能有一个获得服务)。
    //StartServer开始服务时启动8个进程,最小空闲5个进程,最多空闲20个进程。
    //MaxClient限制同一时刻客户端的最大连接请求数量超过的要进入等候队列。
    //MaxRequestsPerChild每个进程生存期内允许服务的最大请求数量,0表示永不结束

<IfModule worker.c> 
StartServers         4 
MaxClients         300 
MinSpareThreads     25 
MaxSpareThreads     75 
ThreadsPerChild     25 
MaxRequestsPerChild  0 
</IfModule>
//为Apache配置线程访问,即每对WEB服务访问启动一个线程,这样对内存占用率比较小。
    //ServerLimit服务器允许配置进程数的上限。
    //ThreadLimit每个子进程可能配置的线程上限
    //StartServers启动两个httpd进程,
    //MaxClients同时最多能发起250个访问,超过的要进入队列等待,其大小有ServerLimit和ThreadsPerChild的乘积决定
    //ThreadsPerChild每个子进程生存期间常驻执行线程数,子线程建立之后将不再增加
    //MaxRequestsPerChild每个进程启动的最大线程数,如达到限制数时进程将结束,如置为0则子线程永不结束

Listen 80
//监听的端口,如有多块网卡,默认监听所有网卡

LoadModule auth_basic_module modules/mod_auth_basic.so 
...... 
LoadModule version_module modules/mod_version.so
//启动时加载的模块

Include conf文件路径
//conf.d/*.conf
//加载的配置文件
//启动服务后转换的身份,在启动服务时通常以root身份,然后转换身份,这样增加系统安全

ServerAdmin root@localhost    
//#管理员的邮箱,如果出现问题,会在首页显示

#ServerName www.example.com:80
//默认是不需要指定的,服务器通过名字解析过程来获得自己的名字,但如果解析有问题(如反向解析不正确),或者没有DNS名字,也可以在这里指定IP地址,当这项不正确的时候服务器不能正常启动。前面启动Apache时候提示正在启动  httpd:httpd: apr_sockaddr_info_get() failed forjustin httpd: Could not  reliably determine the server's fully qualified domain name, using  127.0.0.1forServerName,解决方法就是启动该项把www.example.com:80修改为自己的域名或者直接修改为localhost

UseCanonicalName Off
//如果客户端提供了主机名和端口,Apache将会使用客户端提供的这些信息来构建自引用URL。这些值与用于实现基于域名的虚拟主机的值相同,并且对于同样的客户端可用。CGI变量SERVER_NAME和SERVER_PORT也会由客户端提供的值来构建

DocumentRoot "/var/www/html"    
//# apache的默认web站点目录路径,结尾不要添加斜线

<Directory />   //  #行为对根目录的限制
    Options FollowSymLinks  
         // # followsymlinks表示允许使用符号链接,默认为禁用
    AllowOverride None     
         //# 表示禁止用户对目录配置文件(.htaccess进行修改)重载,普通站点不建议开启
</Directory>

<Directory "/var/www/html"> 
    Options Indexes FollowSymLinks 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
</Directory>
     //对/var/www/html目录的一个权限的设置,options中Indexes表示当网页不存在的时候允许索引显示目录中的文件,FollowSymLinks是否允许访问符号链接文件。
      // /有的选项有ExecCGI表是否使用CGI,如Options  Includes ExecCGI  FollowSymLinks表示允许服务器执行CGI及SSI,禁止列出目录。SymLinksOwnerMatch表示当符号链接的文件和目标文件为同一用户拥有时才允许访问。
       //AllowOverrideNone表示不允许这个目录下的访问控制文件来改变这里的配置,这也意味着不用查看这个目录下的访问控制文件,修改为:AllowOverride  All  表示允许.htaccess。
        //Order对页面的访问控制顺序后面的一项是默认选项,如allow,deny则默认是deny,Allowfromall表示允许所有的用户,通过和上一项结合可以控制对网站的访问控制

<IfModule mod_userdir.c> 
    UserDir disabled 
</IfModule>
     //是否允许用户访问其家目录,默认是不允许

#<Directory /home/*/public_html> 
#    AllowOverride FileInfo AuthConfig Limit 
#    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec 
#    <Limit GET POST OPTIONS> 
#        Order allow,deny 
#        Allow from all 
#    </Limit> 
#    <LimitExcept GET POST OPTIONS> 
#        Order deny,allow 
#        Deny from all 
#    </LimitExcept> 
#</Directory>
     //如果允许访问用户的家目录中的网页文件,则取消以上注释,并对其中进行修改

DirectoryIndex index.html index.html.var   
     // #指定所要访问的主页的默认主页名字,默认首页文件名为index.html

AccessFileName .htaccess    
      //#定义每个目录下的访问控制文件名,缺省为.htaccess

<Files ~ "^\.ht"> 
    Order allow,deny 
    Deny from all 
    Satisfy All 
</Files>
     //控制不让web上的用户来查看.htpasswd和.htaccess这两个文件

TypesConfig /etc/mime.types     
     //#用于设置保存有不同MIME类型数据的文件名

DefaultType text/plain  
     // #默认的网页的类型

<IfModule mod_mime_magic.c> 
#   MIMEMagicFile /usr/share/magic.mime 
     MIMEMagicFile conf/magic
</IfModule>
     //指定判断文件真实MIME类型功能的模块

HostnameLookups Off   
     //#当打开此项功能时,在记录日志的时候同时记录主机名,这需要服务器来反向解析域名,增加了服务器的负载,通常不建议开启

#EnableMMAP off
     //是否允许内存映射:如果httpd在传送过程中需要读取一个文件的内容,它是否可以使用内存映射。
      //如果为on表示如果操作系统支持的话,将使用内存映射。在一些多核处理器的系统上,这可能会降低性能,如果在挂载了NFS的DocumentRoot上如果开启此项功能,可能造成因为分段而造成httpd崩溃

#EnableSendfile off
     //这个指令控制httpd是否可以使用操作系统内核的sendfile支持来将文件发送到客户端。
      //默认情况下,当处理一个请求并不需要访问文件内部的数据时(比如发送一个静态的文件内容),如果操作系统支持,Apache将使用sendfile将文件内容直接发送到客户端而并不读取文件

ErrorLog logs/error_log   // #错误日志存放的位置
LogLevel warn    //#Apache日志的级别

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 
LogFormat "%h %l %u %t \"%r\" %>s %b" common 
LogFormat "%{Referer}i -> %U" referer 
LogFormat "%{User-agent}i" agent
 //定义了日志的格式,并用不同的代号表示

#CustomLog logs/access_log common 
CustomLog logs/access_log combined
 //说明日志记录的位置,这里面使用了相对路径,所以ServerRoot需要指出,日志位置就存放在/etc/httpd/logs

ServerSignature On     //#定义当客户请求的网页不存在,或者错误的时候是否提示apache的版本的一些信息

Alias /icons/ "/var/www/icons/"   // #定义一些不在DocumentRoot下的文件,而可以将其映射到网页根目录中,这也是访问其他目录的一种方法,但在声明的时候切记目录后面加”/”

<Directory "/var/www/icons"> 
    Options Indexes MultiViews FollowSymLinks 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
</Directory>
 //定义对/var/www/icons/的权限,修改为 Options MultiViews FollowSymLinks表示不在浏览器上显示树状目录结构

<IfModule mod_dav_fs.c> 
     # Location of the WebDAV lock database. 
     DAVLockDB /var/lib/dav/lockdb
</IfModule>
 //对mod_dav_fs.c模块儿的管理

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"   // #对CGI模块儿的的别名,与Alias相似

<Directory "/var/www/cgi-bin"> 
    AllowOverride None 
    Options None 
    Order allow,deny 
    Allow from all 
</Directory>
 //对/var/www/cgi-bin文件夹的管理,方法同上

# Redirect old-URI new-URL
 //Redirect参数是用来重写URL的,当浏览器访问服务器上的一个已经不存在的资源的时候,服务器返回给浏览器新的URL,告诉浏览器从该URL中获取资源。
  //这主要用于原来存在于服务器上的文档改变位置之后,又需要能够使用老URL能访问到原网页

IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable Charset=UTF-8 
AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
... 
IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
 //当一个HTTP请求的URL为一个目录的时候,服务器返回这个目录中的索引文件,如果目录中不存在索引文件,并且服务器有许可显示目录文件列表的时候,就会显示这个目录中的文件列表,为了使得这个文件列表能具有可理解性,而不仅仅是一个简单的列表,就需要前这些参数。
  //如果使用了IndexOptionsFancyIndexing选项,可以让服务器针对不同的文件引用不同的图标。如果没有就使用DefaultIcon定义缺省图标。
   //同样,使用AddDescription可以为不同类型的文档介入描述

AddLanguage ca .ca 
...... 
AddLanguage zh-TW .zh-tw
 //添加语言

LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
 //Apache支持的语言

AddDefaultCharset UTF-8  // #默认支持的语言

#AddType application/x-tar .tgz   // #支持的应用如果想支持对php的解析添加这样一行

#AddEncoding x-compress .Z 
#AddEncoding x-gzip .gz .tgz
 //支持对以.Z和.gz.tgz结尾的文件

AddType application/x-compress .Z 
AddType application/x-gzip .gz .tgz
 //添加对上述两种文件的应用

#AddHandler cgi-script .cgi  //  #修改为:AddHandler cgi-script .cgi .pl 表示允许扩展名为.pl的CGI脚本运行
AddType text/html .shtml 
AddOutputFilter INCLUDES .shtml
 //添加动态处理类型为server-parsed由服务器预先分析网页内的标记,将标记改为正确的HTML标识

#ErrorDocument 404 /missing.html    //#当服务器出现404错误的时候,返回missing.html页面
Alias /error/ "/var/www/error/"   // #赋值别名

<IfModule mod_negotiation.c> 
<IfModule mod_include.c> 
    <Directory "/var/www/error"> 
        AllowOverride None 
        Options IncludesNoExec 
        AddOutputFilter Includes html 
        AddHandler type-map var 
        Order allow,deny 
        Allow from all 
        LanguagePriority en es de fr 
        ForceLanguagePriority Prefer Fallback 
    </Directory>
 //对/var/www/error网页的权限及操作

BrowserMatch "Mozilla/2" nokeepalive 
BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0 
BrowserMatch "RealPlayer 4\.0" force-response-1.0 
BrowserMatch "Java/1\.0" force-response-1.0 
BrowserMatch "JDK/1\.0" force-response-1.0 
.....
 //设置特殊的参数,以保证对老版本浏览器的兼容,并支持新浏览器的特性

#NameVirtualHost *:80  //  #如果启用虚拟主机的话,必须将前面的注释去掉,而且,第二部分的内容都可以出现在每个虚拟主机部分。
# VirtualHost example: 
#<VirtualHost *:80> 
#    ServerAdmin webmaster@www.linuxidc.com 
#    DocumentRoot /www/docs/www.linuxidc.com 
#    ServerName www.linuxidc.com 
#    ErrorLog logs/www.linuxidc.com-error_log 
#    CustomLog logs/www.linuxidc.com-access_log common 
#</VirtualHost>
//www.linuxidc.com替换为你的网址

3.Apache的安装

1.使用二进制安装
在定制程度和性能上要低于编译的版本,但是是最快速的安装方法

所有标准的Apache模块在二进制包中都有包含,因为他们是作为DSO模块进行编译的,用户只需要选择自己想要的模块,或者减少模块减少消耗

2.使用rpm或者deb格式安装

使用最广泛的两种包管理系统
(1)以Redhat为代表的rpm格式
(2)以Debian为代表的deb格式
(3)yum安装

(1)以Redhat为代表的rpm格式

rpm全称Red Hat Package Manager

安装步骤

//1.查询rpm包的信息
# rpm -qip httpd-2.2.3.rpm
//2.安装rpm包,使用ivh参数,i表示安装,v表示显示更多信息,h表示安装进程
#rpm -ivh http-2.3.rpm
//3.删除rpm包,e参数表示删除
#rpm -e http-2.3.i386
(2)以Debian为代表的deb格式

#dpkg -i安装
#dpkg -i apache2.3.deb
apt 或者yum程序安装,无须自己解决软件包的依赖问题

#apt -get install apache2.2.deb
#apt -get remove --purge apache2.2……deb
(3)yum安装
[1] 安装 httpd.
[root@linuxprobe ~]# yum -y install httpd
# 删除默认欢迎页面
[root@linuxprobe ~]# rm -f /etc/httpd/conf.d/welcome.conf
[2] 配置httpd,将服务器名称替换为您自己的环境
[root@linuxprobe ~]# vi /etc/httpd/conf/httpd.conf
# line 86: 改变管理员的邮箱地址
ServerAdmin root@linuxprobe.org
# line 95: 改变域名信息
ServerName www.linuxprobe.org:80
# line 151: none变成All
AllowOverride All
# line 164: 添加只能使用目录名称访问的文件名
DirectoryIndex index.html index.cgi index.php
# add follows to the end
# server's response header(安全性)
ServerTokens Prod
# keepalive is ON
KeepAlive On
[root@linuxprobe ~]# systemctl start httpd
[root@linuxprobe ~]# systemctl enable httpd
[3] 如果Firewalld正在运行,请允许HTTP服务。,HTTP使用80 / TCP
[root@linuxprobe ~]# firewall-cmd --add-service=http --permanent
success
[root@linuxprobe ~]# firewall-cmd --reload
success
[4] 创建一个HTML测试页,并使用Web浏览器从客户端PC访问它。如果显示以下页面,是正确的
[root@linuxprobe ~]# vi /var/www/html/index.html

4.Apache多站点

配置多站点方式

方式一 拷贝配置文件,安装新服务

1、将httpd.conf复制一个新的文件httpdNew.conf
2、将httpdNew.conf中的侦听端口改成未使用的自定义的端口。 Listen:8111
3、将httpdNew.conf中的网站目录 DocumentRoot “原绝对路径” 改成 DocumentRoot “新绝对路径”
4、将httpdNew.conf中的权限设置

方式二 添加新端口,新虚拟目录的方式

1、httpd.conf在之前侦听端口的基础上,加上新侦听端口 Listen 8333.这样Apache有多个侦听端口
2、httpd.conf中加入虚拟主机支持。加入指令:NameVirtualHost *
3、找到 definition注释所在位置。加入指令:

<VirtualHost localhost:8333>
ServerName localhost
DocumentRoot "虚拟站点新绝对路径"
</VirtualHost>

方式三 建立虚拟主机方式

1、修改虚拟主机配置文件 X:\Apache安装目录根目录\conf\extra\httpd-vhosts.conf文件.加入配置:

<VirtualHost 127.0.1.1:8222>
DocumentRoot "虚拟站点新绝对路径"
ServerName url.com
ErrorLog "虚拟站点新绝对路径/error.log"
</VirtualHost>

2、修改主配置文件X:\Apache安装目录根目录\conf\httpd.conf。去掉#Include conf/extra/httpd-vhosts.conf前面的#号
3、修改主配置文件X:\Apache安装目录根目录\conf\httpd.conf。加目录权限:

<Directory "虚拟站点新绝对路径">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>

4、重启对应的Apache服务

4.apache对其他的支持配置

4.1 配置Apache支持Url重写

1,.加载重写模块。httpd.conf中找到#LoadModule rewrite_module modules/mod_rewrite.so,去掉#号
2、httpd.conf中找到AllowOverride None,改成 AllowOverride All (All表示整台服务器上都支持URL规则重写)
3、httpd.conf加入

<Directory "站点绝对路径">
Options FollowSymLinks
AllowOverride All
</Directory>

4、重启Apache服务器并启动服务
5、在URL重写的网站目录下放一个.htaccess文件,文件名是.htaccess。如无此文件或此文件中没定义任何关于URL重写的规则,则重写不会生效。
6、在.htaccess中通过正则表达式映射需要伪静态的页面。URL正则表达式如下:

RewriteEngine on
RewriteRule index.html index.php
RewriteRule (\d+).html$ info\.php\?infoid=$1
效果为:www.abc.cn/info/info.php?infoid=100 映射为 www.abc.cn/info/100.html

可用于以下用途:
1、http错误的自定义页。格式 ErrorDocument 错误代码 /目录名/名.扩展名
ErrorDocument 404 /errors/notfound.html
ErrorDocument 401 “你权限访问该页面!”
2、拒绝某IP的访问
order allow,deny
deny from 210.10.56.32
deny from 219.5.45.
deny from www.baidu.com
allow from all
3、保护.htaccess文档
4、Url转向(文档迁移,目录更改,显示友好易记的url)。格式:
Redirect /旧目录/旧文档名 新文档名
Redirect 旧目录 新目录
5、更改默认主页
DirectoryIndex 新的缺省名
DirectoryIndex filename.html index.cgi index.pl default.htm
6、反盗链

RewriteEngine on 
RewriteCond %{ HTTP_REFERER } !^$ 
RewriteCond %{ HTTP_REFERER } !^http://(www.)?domain.com/.*$ [NC] 
RewriteRule .(gif&line;jpg)$ - [F] 
RewriteRule .(gif&line;jpg)$ http://www.mydomain.com/替代名 [R,L]

4.2支持Perl

启用CGI执行并使用Perl脚本

  • [1] 安装Perl.
[root@linuxprobe ~]# yum -y install perl perl-CGI
  • [2] 默认情况下,在“/var/www/cgi-bin”目录下允许CGI。

可以使用Perl Scripts放在目录下。然而,它下面的所有文件都被处理为CGI。

# 下面的设置是CGI的设置
[root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
  • [3] 如果你想允许在其他目录中的CGI,配置如下。

例如,在“/var/www/html/cgi-enabled”中允许。

[root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
# create new
# processes .cgi and .pl as CGI scripts
<Directory "/var/www/html/cgi-enabled">
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
</Directory>
[root@linuxprobe ~]# systemctl restart httpd
  • [4] 如果SELinux被启用,并且允许CGI在不是像上面[3]的默认目录下,更改规则如下。
[root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/linuxprobe/html/cgi-enabled
[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
  • [5] 创建一个CGI测试页面,并使用Web浏览器从客户端PC访问它。如果显示以下页面,说明配置正确。
[root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n";
print "CGI Test Page";
print "\n</div>\n";
print "</body>\n</html>\n";
[root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.cgi 

这里写图片描述

4.3、支持PHP

配置httpd以使用PHP脚本

  • [1] 安装PHP.
[root@linuxprobe ~]# yum -y install php php-mbstring php-pear
[root@linuxprobe ~]# vi /etc/php.ini
# line 878: 取消注释,设置时区
date.timezone = "Asia/Shanghai"
[root@linuxprobe ~]# systemctl restart httpd
  • [2] 创建一个PHP测试页面,并使用Web浏览器从客户端PC访问它。如果显示以下页面,它是确定。
[root@linuxprobe ~]# vi /var/www/html/index.php
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
<?php
   print Date("Y/m/d");
?>
</div>
</body>
</html>

这里写图片描述

  • [3] 创建phpinfo测试页,确认是都开启php支持
[root@linuxprobe ~]# echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

这里写图片描述

4.4、支持Ruby

配置httpd以将Ruby脚本用作CGI

  • [1] 安装Ruby.
[root@linuxprobe ~]# yum -y install ruby
  • [2] 默认情况下,在“/var/www/cgi-bin”目录下允许CGI。

可以使用Perl Scripts放在目录下。然而,它下面的所有文件都被处理为CGI。

# 下面的设置是CGI的设置
[root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
  • [3] 如果你想允许在其他目录中的CGI,配置如下。

例如,在“/var/www/html/cgi-enabled”中允许。

[root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
# create new
# processes .rb as CGI scripts
<Directory "/var/www/html/cgi-enabled">
    Options +ExecCGI
    AddHandler cgi-script .rb
</Directory>
[root@linuxprobe ~]# systemctl restart httpd
  • [4] 如果SELinux被启用,并且允许CGI在不是像上面[3]的默认目录下,更改规则如下。
[root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/www/html/cgi-enabled

[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
  • [5] Create a CGI test page and access to it from client PC with
    web browser. It’s OK if following page is shown.
[root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.rb
#!/usr/bin/ruby
print "Content-type: text/html\n\n"
print "<html>\n<body>\n"
print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n"
print "Ruby Script Test Page"
print "\n</div>\n"
print "</body>\n</html>\n" 
[root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.rb 

这里写图片描述

4.5、支持Python

启用CGI执行并使用Python脚本

  • [1] 安装python.
[root@linuxprobe ~]# yum -y install python
  • [2] 默认情况下,在“/var/www/cgi-bin”目录下允许CGI。

可以使用Perl Scripts放在目录下。然而,它下面的所有文件都被处理为CGI。

# 下面的设置是CGI的设置
[root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
  • [3] 如果你想允许在其他目录中的CGI,配置如下。

例如,在“/var/www/html/cgi-enabled”中允许。

[root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
# create new
# processes .py as CGI scripts
<Directory "/var/www/html/cgi-enabled">
    Options +ExecCGI
    AddHandler cgi-script .py
</Directory>
[root@linuxprobe ~]# systemctl restart httpd
  • [4] 如果SELinux被启用,并且允许CGI在不是像上面[3]的默认目录下,更改规则如下。
[root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
  • [5] Create a CGI test page and access to it from client PC with
    web browser. It’s OK if following page is shown.
[root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.py

#!/usr/bin/env python

print "Content-type: text/html\n\n"
print "<html>\n<body>\n"
print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n"
print "Python Script Test Page"
print "\n</div>\n"
print "</body>\n</html>\n" 

[root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.py

这里写图片描述

4.6、支持Userdir ————- 启用userdir,用户可以使用此设置创建网站 - [1] 配置 httpd.

[root@linuxprobe ~]# vi /etc/httpd/conf.d/userdir.conf
# line 17: comment out
#UserDir disabled
# line 24: uncomment
UserDir public_html
# line 31 - 35

<Directory "/home/*/public_html">
    AllowOverride All
# change

    Options None
# change

    Require method GET POST OPTIONS
</Directory>
[root@linuxprobe ~]# systemctl restart httpd
  • [2] 创建一个测试页,使用普通用户通过客户端PC与Web浏览器和访问它,如果显示以下页面,就是正确的

[cent@linuxprobe ~]$ mkdir public_html

[cent@linuxprobe ~]$ chmod 711 /home/cent

[cent@linuxprobe ~]$ chmod 755 /home/cent/public_html

[cent@linuxprobe ~]$ vi ./public_html/index.html

<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
UserDir Test Page
</div>
</body>
</html>

浏览器访问:http://linuxprobe.org/~wang/,出现如下界面
这里写图片描述

4.7、设置虚拟主机

配置虚拟主机以使用多个域名。
以下示例在域名为[linuxprobe.org],虚拟域名为[virtual.host(根目录[/home/wang/public_html]]的环境中设置。
必须为此示例设置Userdir的设置

  • [1] 配置虚拟主机
[root@linuxprobe ~]# vi /etc/httpd/conf.d/vhost.conf
# for original domain

<VirtualHost *:80>
   DocumentRoot /var/www/html
   ServerName www.linuxprobe.org
</VirtualHost>
# for virtual domain

<VirtualHost *:80>
   DocumentRoot /home/cent/public_html
   ServerName www.virtual.host
   ServerAdmin webmaster@virtual.host
   ErrorLog logs/virtual.host-error_log
   CustomLog logs/virtual.host-access_log combined
</VirtualHost>
[root@linuxprobe ~]# systemctl restart httpd
  • [2]创建测试页并使用Web浏览器从客户端计算机访问它。如果显示以下页面,则是正确的:
[cent@linuxprobe ~]$ vi ~/public_html/virtual.php
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Virtual Host Test Page
</div>
</body>
</html>
  • [3]如果访问测试时看不到相应页面,可通过下面命令进行测试:

[root@linuxprobe ~]# yum -y install elinks^C
[root@linuxprobe ~]# elinks http://www.virtual.host/virtual.php

4.8、创建SSL证书

创建自己的SSL证书。但是,如果您使用您的服务器作为业务,最好购买和使用来自Verisigh的正式证书等。

[root@linuxprobe ~]# cd /etc/pki/tls/cert
cert.pem  certs/    
[root@linuxprobe ~]# cd /etc/pki/tls/certs/
[root@linuxprobe certs]# make server.key
umask 77 ; \
/usr/bin/openssl genrsa -aes128 2048 > server.key
Generating RSA private key, 2048 bit long modulus
...............................................................+++
....................................................................................................+++
e is 65537 (0x10001)
Enter pass phrase:
Verifying - Enter pass phrase:
[root@linuxprobe certs]# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:
writing RSA key
[root@linuxprobe certs]# make server.csr
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN    #国家后缀
State or Province Name (full name) []:Shanghai  #省
Locality Name (eg, city) [Default City]:Shanghai  #市
Organization Name (eg, company) [Default Company Ltd]:LinuxProbe  #公司
Organizational Unit Name (eg, section) []:DevOps  #部门
Common Name (eg, your name or your server's hostname) []:linuxprobe.org  #主机名
Email Address []:root@linuxprobe.org  #邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:    #默认
An optional company name []:    #默认
#
[root@linuxprobe certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
Signature ok
subject=/C=CN/ST=Shanghai/L=Shanghai/O=LinuxProbe/OU=DevOps/CN=linuxprobe.org/emailAddress=root@linuxprobe.org
Getting Private key

4.9、配置SSL

[1] 配置SSL.

[root@linuxprobe ~]# yum -y install mod_ssl
[root@linuxprobe ~]# vi /etc/httpd/conf.d/ssl.conf
# line 59: 取消注释
DocumentRoot "/var/www/html"
# line 60: 取消注释,定义域名
ServerName linuxprobe.org:443
# line 75: 改变SSLProtocol
SSLProtocol -All +TLSv1 +TLSv1.1 +TLSv1.2

# line 100: 改成刚刚创建的server.crt
SSLCertificateFile /etc/pki/tls/certs/server.crt
# line 107: 改成刚刚创建的server.key
SSLCertificateKeyFile /etc/pki/tls/certs/server.key
[root@www ~]# systemctl restart httpd

[2] 如果Firewalld正在运行,请允许HTTPS服务。 HTTPS使用443 / TCP

[root@www ~]# firewall-cmd --add-service=https --permanent
success
[root@www ~]# firewall-cmd --reload
success

[3] 使用Web浏览器通过HTTPS从客户端计算机访问测试页。下面的示例是Fiorefix。显示以下屏幕,因为证书是自己创建的,但它没有ploblem,继续下一步。
这里写图片描述

4.10、启用基本身份验证

启用基本身份验证以限制特定网页的访问
[1]例如,在目录[/var/www/html/auth-basic]下设置基本身份验证设置。

 [root@linuxprobe ~]# vi /etc/httpd/conf.d/auth_basic.conf
# 创建新配置文件
<Directory /var/www/html/auth-basic>
    AuthType Basic
    AuthName "Basic Authentication"
    AuthUserFile /etc/httpd/conf/.htpasswd
    require valid-user
</Directory>
# 添加用户:使用“-c”创建新文件(仅为初始注册添加“-c”选项)
[root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wang

New password: # set password

Re-type new password: # confirm

Adding password for user wang
[root@linuxprobe ~]# systemctl restart httpd
[root@linuxprobe ~]# mkdir /var/www/html/auth-basic

[root@linuxprobe ~]# vi /var/www/html/auth-basic/index.html
# create a test page

<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: wanger;">
Test Page for Basic Auth
</div>
</body>
</html>

[2] 使用Web浏览器从客户端计算机访问测试页。然后需要认证,如下所示作为设置,用在[1]中添加的用户回答
这里写图片描述
[3] 访问成功
这里写图片描述

4.11、基本Auth + PAM

限制特定网页上的访问,并使用OS用户通过SSL连接进行身份验证
[1] 创建证书,请参照上文所述。
[2] 例如,在[/var/www/html/auth-pam]目录下设置Basic Auth。

# install from EPEL
[root@linuxprobe ~]# yum --enablerepo=epel -y install mod_authnz_external pwauth
[root@linuxprobe ~]# vi /etc/httpd/conf.d/authnz_external.conf
# add to the end

<Directory /var/www/html/auth-pam>
    SSLRequireSSL
    AuthType Basic
    AuthName "PAM Authentication"
    AuthBasicProvider external
    AuthExternal pwauth
    require valid-user
</Directory>

[root@linuxprobe ~]# mkdir /var/www/html/auth-pam

[root@linuxprobe ~]# vi /var/www/html/auth-pam/index.html
# create a test page

<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Test Page for PAM Auth
</div>
</body>
</html>

[root@linuxprobe ~]# systemctl restart httpd

[3] 在客户端上使用Web浏览器访问测试页面https://linuxprobe.org/auth-pam/,并与操作系统上的用户进行身份验证。
这里写图片描述
这里写图片描述

4.12、使用WebDAV

下面是使用SSL连接配置WebDAV设置的示例
[1] 创建证书,请参照上文所述
[2] 例如,创建一个目录[webdav],它使得可以仅通过SSL连接到WebDAV目录。

[root@linuxprobe ~]# mkdir /home/webdav
[root@linuxprobe ~]# chown apache. /home/webdav
[root@linuxprobe ~]# chmod 770 /home/webdav
[root@linuxprobe ~]# vi /etc/httpd/conf.d/webdav.conf
# create new
DavLockDB "/tmp/DavLock"
Alias /webdav /home/webdav
<Location /webdav>
    DAV On
    SSLRequireSSL
    Options None
    AuthType Basic
    AuthName WebDAV
    AuthUserFile /etc/httpd/conf/.htpasswd
    <RequireAny>
        Require method GET POST OPTIONS
        Require valid-user
    </RequireAny>
</Location>

#  添加用户:使用“-c”创建新文件(仅为初始注册添加“-c”选项)
[root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wang
New password:     # set password
Re-type new password:
Adding password for user wang
# **注意:用户wang的htpasswd已经创建过,不需要重复创建**
[root@linuxprobe ~]# systemctl restart httpd

[3] 如果启用了SELinux,请更改以下规则。

[root@linuxprobe ~]# chcon -R -t httpd_sys_rw_content_t /home/webdav
[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_rw_content_t /home/webdav

[4] 这是PC上的WebDAV客户端的设置(Windows 10)。
下载“CarotDAV”,这是一个免费的WebDAV客户端,从以下网站⇒ http://www.rei.to/carotdav_en.html ,下载后,安装并启动CarotDAV,然后显示以下屏幕,单击“文件”按钮并选择“WebDAV”。

这里写图片描述

[5]在“设置名称”字段中输入任何名称,并在“URI”字段中输入[服务器名称/ webdav目录],并输入用户名和密码
这里写图片描述

[7]配置添加如下,点击它连接到服务器。
这里写图片描述

[8] waring显示如下,它的SSL证书没有安装在您的电脑上,它没有ploblem,点击“忽略”,然后去下一步。
这里写图片描述

[9] 到webdav目录下创建测试目录和文件

[root@linuxprobe tmp]# cd /home/webdav/
[root@linuxprobe webdav]# mkdir linuxprobe
[root@linuxprobe webdav]# mkdir linuxcool
[root@linuxprobe webdav]# touch vdevops.txt
[root@linuxprobe webdav]# touch linuxcool.txt

这里写图片描述

转载自:http://blog.csdn.net/wh211212/article/details/52982917

参考:

https://www.cnblogs.com/janas/archive/2012/08/27/2659230.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值