zoukankan      html  css  js  c++  java
  • redhat6下的网络服务的详解、安装及配置

    1.web服务 Apache

    a.简介

    HTTP是一个属于应用层的面向对象的协议,由于其简洁,快速的方式 适用于分布式超媒体信息系统 

    HTTP协议的特点:

    是一个标准的客户端服务器模型 简单快速 灵活 无连接 无状态

    b.安装

      一、安装编译环境

    在安装apache之前,我们需要安装编译apache时所需要的相关软件包,如下:
    yum -y install gcc gcc++ zlib zlib-devel

    二、卸载原有Apache

    在源码安装apache之前,我们要先卸载系统中,已经通过rpm包安装的apache。如下:
    yum remove httpd* -y

    三、进入http://httpd.apache.org中下载httpd*.tar.gz软件包

    四、安装apache

    1. 解压httpd-2.4.12.tar.gz
    [root@主人 ~]# tar xf httpd-2.4.6.tar.gz

    2.在编译安装apache前,我们需要先安装它的依赖包
    apr-1.5.2.tar.gz
    apr-iconv-1.2.1.tar.gz
    apr-util-1.5.4.tar.gz
    2015 pcre-8.36.tar.gz

    3. 安装这些依赖包
    要求将httpd安装到/usr/local/httpd目录下
    (1)安装apr
    # ./configure --prefix=/usr/local/apr --enable-profile --enable-threads
    # make && make install

    (2)安装apr-iconv
    # ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr/
    # make && make install

    (3)安装apr-util
    # ./configure --prefix=/usr/local/apr-util --with-iconv=/usr/local/apr-iconv --with-apr=/usr/local/apr
    # make && make install

    (4)安装pcre
    # ./configure --prefix=/usr/local/pcre --enable-utf8 --enable-jit
    # make && make install

    4. 安装httpd
    # ./configure --prefix=/usr/local/httpd --enable-cache --enable-cache-disk --enable-cache-socache --enable-so --enable-echo --enable-data --enable-include --enable-sed --enable-proxy --enable-proxy-connect --enable-proxy-http --enable-proxy-fcgi --enable-proxy-scgi --enable-proxy-ajp --enable-proxy-balancer --enable-session --enable-session-cookie --enable-ssl --enable-info --enable-rewrite --enable-deflate --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre --with-mpm=prefork

    注:如果需要重新编译源码,需要先用 make clean 清除之前编译产生的数据

    [root@主人 httpd-2.4.6]# ./configure --help |more #查看安装apache的可选功能
    --enable-cache-disk:磁盘缓存,可以对所有内容进行缓存
    --cache-socache:有关模块的缓存
    --enable-so:动态编译模块
    --enable-echo:回显信息
    --enable-include:在apache配置文件中可以通过include加载其他的子文件
    --enable-proxy:使用代理服务
    --enable-rewrite:支持地址重写

    --with-mpm=prefork:指定apache以哪种方式工作,这里指定以prefork(进程)方式工作,适用于静态网页,另一种是以worker(线程)方式工作,适用于动态网页


    修改apache的连接数,编译后就不能在修改,除非重新编译
    1) 以prefork方式工作:
    [root@主人 ~]# vim /root/httpd-2.4.6/server/mpm/prefork/prefork.c
    #define DEFAULT_SERVER_LIMIT 256
    #define MAX_SERVER_LIMIT 200000

    2) 以worker方式工作
    [root@主人 ~]# vim /root/httpd-2.4.6/server/mpm/worker/worker.c
    #define DEFAULT_SERVER_LIMIT 16
    #define MAX_SERVER_LIMIT 20000

    3) 为了apache的安全性,可以通过修改版本信息来提高一定的安全性
    [root@主人 ~]# vim httpd-2.4.6/include/ap_release.h
    file://C:UsersADMINI~1AppDataLocalTempct_tmp/1.png


    /etc/ld.so.conf:存放相关的库文件的路径

    5. 安装完apache后,我们有关的apache配置信息都放在/usr/local/httpd/下
    [root@主人 httpd]# ls
    bin cgi-bin error icons logs manual
    build conf htdocs include man modules

    其中
    bin:主要存放程序命令目录
    logs:error目录下面放置了两个日志文件:错误日志、访问日志
    conf:放置httpd的配置文件
    htdocs:存放站点目录
    modules:放置apache服务的相关模块

    6. 设置后就可以用service httpd *启动httpd服务
    (1) 将apache的启动脚本复制到/etc/init.d这个目录下
    [root@主人 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd

    (2) 将httpd交由chkconfig进行管理
    [root@主人 ~]# vim /etc/init.d/httpd
    添加这一行
    #chkconfig: - 85 15
    [root@主人 ~]# chkconfig --add httpd

    (3) 检查启动脚本是否有语法问题
    [root@主人 bin]# ./apachectl -t #检查语法
    httpd: Syntax error on line 128 of /usr/local/httpd/conf/httpd.conf: Cannot load modules/mod_session_cookie.so into server: /usr/local/httpd/modules/mod_session_cookie.so: undefined symbol: ap_hook_session_save

    7. 查看apache的配置文件(/usr/local/httpd/conf)
    [root@主人 conf]# ls
    extra httpd.conf magic mime.types original
    [root@主人 conf]# ls extra/ #该目录下面放置额外的一些配置文件,这样做是为了防止主配置文件过大
    httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
    httpd-dav.conf httpd-manual.conf httpd-userdir.conf
    httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
    httpd-info.conf httpd-multilang-errordoc.conf proxy-html.conf

    apache主配置目录/usr/local/httpd/conf/httpd.conf

    <Directory "/usr/local/httpd/htdocs">
    Options Indexes FollowSymLinks #FollowSymLinks: 在该目录下允许文件系统使用符号连接。 Indexes: 当用户访问该目录时,如果用户找不到DirectoryIndex指定的主页文件(例如index.html),则返回该目录下的文件列表给用户。
    AllowOverride None #允许存在于.htaccess文件中的指令类型(.htaccess文件名是可以改变的,其文件名由AccessFileName指令决定): 当AllowOverride被设置为None时。不搜索该目录下的.htaccess文件(可以减小服务器开销)
    Require all granted
    </Directory>


    <Files ".ht*">
    Require all denied #.ht*不允许客户端查询
    </Files>

    ErrorLog "logs/error_log" #指定错误日志的存放路径
    LogLevel 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"" 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" common #选用上面common格式的日志

    ScriptAlias /cgi-bin/ "/usr/local/httpd/cgi-bin/" #定义别名

    #以那种方式去解压.Z、.gz、.tgz文件
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    服务器响应错误:显示错误的html文件以/usr/local/httpd/htdocs/为根,在该目录下
    #ErrorDocument 500 "The server made a boo boo."
    #ErrorDocument 404 /missing.html
    #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
    #ErrorDocument 402 http://www.example.com/subscription_info.html

    8. 启动httpd服务,出现一下错误的解决办法

    (1) 如果发现启动httpd服务,名面上并没有报什么错,但在用lsof -i:80 查看,却发现该服务并没有启动,则我们可以通过查看/usr/local/httpd/logs/error_log内容,根据报错来解决问题

    (2) 启动时发生下面的错误,我们看报错信息,发现是因为httpd.conf文件的128行那里不能加载mod_session_cookie.so模块
    解决办法:我们将httpd.conf文件中mod_session_cookie.so模块前的注释去掉就解决问题
    [root@主人 extra]# service httpd start #
    httpd: Syntax error on line 128 of /usr/local/httpd/conf/httpd.conf: Cannot load modules/mod_session_cookie.so into server: /usr/local/httpd/modules/mod_session_cookie.so: undefined symbol: ap_hook_session_save

    (3) 出现下面的错误是在httpd.conf文件中没有修改ServerName的域名
    [root@主人 conf]# service httpd start #启动报错
    AH00557: httpd: apr_sockaddr_info_get() failed for xe4xb8xbbxe4xbaxba
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

    9. 测试:在浏览器输入本机IP看看能不能出现htdocs目录下的默认html文件

    c.配置文件(*)

     配置基本的web站点
    (1)设置web站点的主目录为/sxjy/web
    DocumentRoot "/sxjy/web"

    (2)设置web站点的主页文件名为index.html,index.htm,index.php三个
    DocumentRoot index.html index.htm index.php

    (3)设置服务器使用(/usr/local/httpd/conf/extra/httpd-mpm.conf )prefork模式工作时启动10个空闲进程,最小保持10个空闲进程,最大空闲进程数为50个,并发最大用户为1000
    <IfModule mpm_prefork_module>
    StartServers 10
    MinSpareServers 10
    MaxSpareServers 50
    ServerLimit 1000
    MaxRequestWorkers 1000
    MaxConnectionsPerChild 0
    </IfModule>


    (4)(/usr/local/httpd/conf/extra/httpd-info.conf)启用服务器信息和状态查看页面
    <Location /server-status>
    SetHandler server-status
    Require host .sxjy.com
    Require ip 172.16.0.0/16
    </Location>

    (5)网络链接超时时间为10秒
    TimeOut 10

    (6)保活连接超时时间为3秒
    KeepAlive On
    KeepAliveTimeout 3

    (7)设置错误日志的级别为info
    LogLevel info

    (8)设置访问日志记录客户端地址、请求协议、浏览器类型和版本、请求方法、状态、请求时间、响应请求的子进程ID、{referer}等信息
    LogFormat "%t %h %H %P %m %l %u %U "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
    CustomLog "logs/access_log" combined

    2、apache的Alias,ScriptAlias,options FollowSymLinks,options Indexs配置
    作用:访问不在web目录下的文件和列出目录下的内容
    Alias /meizu "/usr/local/httpd/meizu"
    ScriptAlias /cgi-bin/ "/usr/local/httpd/cgi-bin/"
    <Directory "/usr/local/httpd/meizu">
    Require all granted
    Require ip 172.16.0.0/16
    </Directory>

    <Directory />
    options Indexes FollowSymLinks
    </Directory>

    3、apache的段配置
    <Directory "/web">*</Directory> \设置指定目录的访问规则
    <Files ".ht*">*</Files> \设置特定文件的访问规则
    <Location /server-status>*</Location> \设置URL包含特殊信息的访问规则
    <IfModule dir_module>*</IfModule> \判定模块是否加载,如果加载则使用指定的配置项

    4、访问控制、认证和授权
    访问控制
    Require all granted \允许所有的访问
    Require all denied \拒绝所有的访问

    例:(1)对于/meizu目录下的内容,除了172.16.0.7外所有人都可以访问
    apache 2.4版
    <Directory "/meizu">
    <RequireAll>
    Require all granted
    Require not ip 172.16.0.7
    </RequireAll>
    </Directory>

    apache 2.2版
    <Directory "/meizu">
    Order deny,allow
    Deny from 172.16.0.7
    Allow from all
    </Directory>

    (2)对于/meizu目录下的内容,除了172.16.0.254外所有人都不可以访问
    <Directory "/meizu">
    Require all denied
    Require ip 172.16.0.254
    </Directory>

    认证和授权
    例:(1)对于/meizu目录下的内容,只允许admin用户访问
    <Directory "/usr/local/httpd/meizu">
    AuthType Basic
    AuthName "Restricted Files"
    AuthBasicProvider file
    AuthUserFile "/usr/local/httpd/passwd/password"
    Require valid-user
    </Directory>

    # ./htpasswd -c /usr/local/httpd/passwd/password admin \创建用户认证的密码文件,第一次使用加-c选项

    练习:(1)在web网站的根目录下创建一个myweb目录,将web根目录外的一个文件通过符号链接到myweb目录下并能够访问
    # mkdir /usr/local/httpd/myweb
    # ln -s /usr/local/httpd/myweb/test.html /usr/local/httpd/htdocs/test.html
    # vim httpd.conf
    <Directory />
    Options FollowSymLinks
    </Directory>

    (2)当myweb目录下不存在index.html主页文件时,不允许列出目录下的内容
    <Directory /myweb>
    Options -Indexs
    </Directory>

    (3)在web服务器根目录下的test.html文件,要求此文件只能被一个特定的ip地址访问,地址自己定义
    <Location "/test.html">
    Require all denied
    Require ip 172.16.0.250
    </Location>

    (4)在web根目录之外创建一个sxjy目录并随便放入几个html文件,当客户端访问的URL中包含/sxjy地址时则访问实际的sxjy目录下的内容(sxjy目录在web站点根目录之外)
    Alias /sxjy/ /usr/local/httpd/sxjy/
    <Directory /usr/local/httpd/sxjy>
    Require all granted
    </Directory>

    (5)对于sxjy目录的内容,只允许经过认证的user01,user02两个用户访问
    <Directory /sxjy>
    AuthType Basic
    AuthName "Restricted Files"
    AuthBasicProvider file
    AuthUserFile "/usr/local/httpd/passwd/password"
    Require user user01 user02
    </Directory>

    # htpasswd -c /usr/local/httpd/passwd/password user01
    # htpasswd /usr/local/httpd/passwd/password user02
    # vim httpd.conf
    LoadModule authn_file_module modules/mod_authn_file.so
    LoadModule authz_host_module modules/mod_authz_host.so
    LoadModule authz_user_module modules/mod_authz_user.so
    LoadModule authz_groupfile_module modules/mod_authz_groupfile.so

    假设web的根目录为:/usr/local/httpd/htdocs

    常用认证命令
    AuthType Basic
    AuthName "Restricted Files"
    AuthBasicProvider file
    AuthUserFile "/usr/local/httpd/passwd/password"

    Require user user01 user02
    Require Valid-User

    AuthGroupFile "/usr/local/httpd/passwd/groups"
    Require group sxjy

    satisfy all | any \访问控制、认证和授权的满足条件控制

  • 相关阅读:
    yii2.0安装redis
    composer
    Windows下安装redis
    Windows下启动redis闪退
    svn的使用及安装
    mysql主从
    linux下远程链接mysql报错1045
    git命令行克隆报错fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
    git克隆报错128
    yii phpexcel <转>
  • 原文地址:https://www.cnblogs.com/gzcheng/p/5813655.html
Copyright © 2011-2022 走看看