linux学习-apache的安装和配置1

apache是最为常见和流行的web服务器,研究和使用很有必要。

一、安装和配置apache


安装apache

 yum install httpd httpd-tools

apche的相关配置文件

image.png


管理和控制apache

systemctl {start|stop|resart|reload} httpd
systemctl {enable|diable} httpd

apachectl控制apache

apachctl {start|stop|gracful|status} #启动、停止、重启、状态
apchectl fullstatus #显示mod_status模块输出
apachectl -V 或 httpd -V  #显示apache的编译参数
apachectl -l 或 httpd -l  #查看编译模块
apachectl -M 或 httpd -M    #列出所有模块
apachectl -t 或 httpd -t    #检查配置文件的正确性
apachectl -S 或 httpd -S    #主机虚拟机主机配置的正确性

apache配置文件

#/etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd"                 #根目录设置
Listen 80                        #监听本机所有80端口
Include conf.modules.d/*.conf              #包含动态模块加载配置文件
User apache                        #以apache用户执行服务进程/线程
Group apache                       #以apche组执行服务进程/线程 
ServerAdmin root@localhost                #Apache用户E-mail为rootlocalhost
<Directory />                      #设置对ServerRoot目录的访问控制
    AllowOverride none                #禁止使用基于目录的配置文件
    Require all denied               #拒绝一切客户访问ServerRoot目录的访问控制
</Directory>
DocumentRoot "/var/www/html"              #主服务器的文档根目录为/var/www/html
<Directory "/var/www">                 #设置对/var/www目录的访问控制
    AllowOverride None               #禁止使用基于目录的配置文件
    Require all granted                            #允许一切客户访问/var/www目录
</Directory>
<Directory "/var/www/html">                             #设置对/var/www/html目录的访问控制
    Options Indexes FollowSymLinks                 #允许为此目录生成文件列表,语序符号链接跟随
    AllowOverride None                              #禁止使用基于目录的配置文件
    Require all granted                            #允许一切客户访问/var/www/html目录
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html                       #指定目录的主页文件为index.html
</IfModule>
<Files ".ht*">
    Require all denied                             #拒绝一切客户访问“.ht*”文件
</Files>
ErrorLog "logs/error_log"                               #指定错误文件日志的位置
LogLevel warn                                           #指定记录高于warn级别的错误日志
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
          LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined           #指定访问日志文件的位置和格式
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8                                #指定默认字符集
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on                                       #启用Sendfile机制以提高apche性能
IncludeOptional conf.d/*.conf                           #包含conf.d/*.conf目录下的配置文件


二、apache的基本配置

访问控制

    apache可以根据访问者的IP地址或域名来决定是否提供资源。apache2.4版本中,访问控制功能由mod_authz_core和mode_authz_host模块,使用Require指令实现访问控制

Require all granted            #表示允许所有主机访问
Require all denied             #表示拒绝所有主机访问
Require lcoal                  #表示仅允许本地主机访问
Reuire [not} host              #表示允许或禁止指定主机或域访问
Require {not] ip               #表示允许或禁止指定ip地址访问

别名

    使用别名(ALIAS)机制可以将文档根目录(/var/www/html)以外的内容加入站点。配置别名可以使用Alias指令,使用Alias指令可以映射URL到文件系统。

格式1:Alias/URL-path/"/path/to/other/directory/"
格式2:Alias/URL-path-filename "/path/to/other/directory/filename"

例1:配置文件/etc/httpd/conf.d/autoindex.conf中定义Alias

grep -w^Alias/etc/httpd/conf.d/autoindex.conf
Alias /icons/ "/usr/share/httpd/icons/"
====================================
将http://localhost/icons/的访问映射到文件系统的/usr/share/httpd/icons/的目

例2:配置文件/etc/httpd/conf,d/welcome.conf中定义Alias

grep -w ^Alias/etc/httpd/conf.d/welcome.conf|tail -1
Alias /images/powerrdby.png /usr/share/httpd/noindex/images/powererdby.png
====================================
将http://localhost/images/powerdby.png的访问映射到文件/ usr/share/httpd/noindex/images/powerdby.png


猜你喜欢

转载自blog.51cto.com/11555417/2154150