Apache服务器的源码包安装及配置

来源:互联网 发布:站长工具js格式化 编辑:程序博客网 时间:2024/06/16 04:58

Apache服务器

一、     安装

1.      lamp源码包的安装

 

2.      rpm包的安装

httpd

mysql

mysql-server

php

php-devel

php-mysql

二、     配置

1.      相关文件

Apache的配置文件

      源码包的安装

           /usr/local/apache2/etc/httpd.conf

           /usr/local/apche2/etc/extra/*.conf

      rpm包的安装

           /etc/local/conf/httpd.conf

默认网页的保存位置

      源码包

           /usr/local/apache2/htdocs/

      rpm包安装

           /var/www/html/

日志保存位置

      源码包

           /usr/local/apache2/logs/

      rpm包

           /var/log/httpd/

2.      配置说明

注意:Apache的配置文件严格区分大小写

1)针对主机环境的基本配置

ServerRoot        apache主目录

      Listen              监听端口

      LoadModule           加载的相关模块

     

      User

      Group              用户和组

      ServerAdmin          管理员邮箱

      ServerName           服务器名(没有域名解析时,使用临时解析。不开启)

      ErrorLog "logs/error_log    错误日志

      CustomLog "logs/access_log"common       正确访问日志

      DirectoryIndex index.html index.php          默认网页文件名,优先级顺序

      Include etc/extra/httpd-vhosts.conf          子配置文件中内容也会加载生效

      2)主页目录及权限

      DocumentRoot"/usr/local/apache2//htdocs"           主页目录

 

      <Directory"/usr/local/apache2//htdocs">

           #Directory关键字定义目录权限

           OptionsIndexes FollowSymLinks

           #options

                       None:没有任何额外权限

                       All:所有权限

                       Indexes:   浏览权限(当此目录下没有默认网页文件时,显示目录内容)

                       FollowSymLinks:准许软连接到其他目录

           AllowOverrideNone           #定义是否允许目录下.htaccess文件中的权限生效

                      None:.htaccess中权限不生效

                       All:文件中所有权限都生效

                      AuthConfig:文件中,只有网页认证的权限生效。

           Requireall granted 访问控制列表

#定义此目录的允许访问权限

例1:  仅允许IP为192.168.1.1的主机访问

<RequireAll>

     Require all  granted

     Require ip 192.168.1.1

</RequireAll>

例子2.仅允许192.168.0.0/24网络的主机访问

<RequireAll> 

     Require all  granted 

     Require ip 192.168.1.0/24

</RequireAll> 

例子3.禁止192.168.1.2的主机访问,其他的都允许访问,

<RequireAll>

     Require all  granted

     Require not ip 192.168.1.2

</RequireAll>

例子4.允许所有访问,

Require all granted                     #可以不写在<RequireAll>。。。</RequireAll>中

例子5.拒绝所有访问,

Require all denied                 #可以不写在<RequireAll>。。。</RequireAll>中

      3)目录别名

      子配置文件名   /etc/extra/httpd-autoindex.conf

Alias /icons/"/usr/local/apache2//icons/"

   apache以为在这里      实际目录位置

      定义别名 /icons/----

      访问地址: http://192.168.1.253/icons/

<Directory"/usr/local/apache2//icons">

   Options Indexes MultiViews                MultiViews多编码支持

   AllowOverride None

   Require all granted

</Directory>

      4)用户认证

      限制特定目录,只有指定用户可以访问

a)    建立需要保护的目录

①在/usr/local/apache2/htdocs/11下建立目录,然后保护

②使用别名,在系统位置建立目录,然后保护

mkdir  -p /share/soft

b)    修改配置文件,允许权限文件生效

vi  /usr/local/apache2/etc/httpd.conf

Alias /soft/"/share/soft/"

 

<Directory"/share/soft">

    Options Indexes

    AllowOverride All            #开启权限认证文件.htaccess

    Require all granted

</Directory>

 

        重启apache

c)    在指定目录建立权限文件

cd  /share/soft

vi  .htaccess            #不区分大小写

AuthName "50docs"

            #提示信息

AuthType basic

            #加密类型

AuthUserFile/share/apache.passwd

            #密码文件,文件名自定义。

require valid-user

            #允许密码文件中所有用户访问

d)    建立密码文件,加入允许访问的用户。用户和系统用户无关

/usr/local/apache2/bin/htpasswd  -c /share/apache.passwd  test1

            -c  建立密码文件,只有添加第一个用户时,才能-c

        /usr/local/apache2/bin/htpasswd -m  /share/apache.passwd  test2

            -m  再添加更多用户时,用-m表示修改

      5)虚拟主机

      分类

           基于IP的虚拟主机:   一台服务器,多个IP,搭建多个网站

           基于端口的主机:        一台服务器,一个IP,搭建多个网站,每个网站用不同端口访问。

           基于名字的虚拟主机:  一台服务器,一个IP,每个网站使用不同的域名访问。

      步骤:

      ①  解析试验域名

           www.sina.com

           www.sohu.com

      hosts文件位置:

      C:\WINDOWS\system32\drivers\etc\hosts        windows

/etc/hosts                                      Linux

      ②  规划网站主目录

           /share/sina--------------www.sina.com

            /share/sohu ------------ www.sohu.com

      ③ 修改配置文件

           vi  /usr/local/apache2/etc/httpd.conf

                 Includeetc//extra/httpd-vhosts.conf

                      #打开虚拟主机配置文件

           vi/usr/local/apache2/etc/extra/httpd-vhosts.conf

<Directory "/usr/local/apache2/htdocs/sina">

    Options Indexes

    AllowOverrideNone

Require all granted

</Directory>

 

<Directory"/usr/local/apache2/htdocs/sohu">

    Options Indexes

    AllowOverrideNone

    Require allgranted

</Directory>

 

<VirtualHost 192.168.150.253>

    #注意,只能写ip

    ServerAdminwebmaster@sina.com

          #管理员邮箱

    DocumentRoot"/usr/local/apache2/htdocs/sina"

          #网站主目录

    ServerNamewww.sina.com

          #完整域名

    ErrorLog"logs/sina-error_log"

          #错误日志

    CustomLog"logs/sina-access_log" common

          #访问日志

</VirtualHost>

 

<VirtualHost 192.168.150.253>

    ServerAdminwebmaster@sohu.com

    DocumentRoot"/usr/local/apache2/htdocs/sohu"

    ServerNamewww.sohu.com

    ErrorLog"logs/sohu.com-error_log"

    CustomLog"logs/sohu.com-access_log" common

</VirtualHost>

 

      6)rewrite  重写功能

      在URL中输入一个地址,会自动跳转为另一个

      ①  域名跳转    www.sina.com-------->  www.sohu.com

           开启虚拟机,并正常访问

      [root@localhost ~]# vi/usr/local/apache2/etc/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

#打开重写模块,记得重启apache

 

      修改配置文件,使sina目录的。htaccess文件生效

      [root@localhost etc]# viextra/httpd-vhosts.conf

 

<Directory"/usr/local/apache2/htdocs/sina">

    Options IndexesFollowSymLinks

    AllowOverride All

Require all granted

</Directory>

 

vi  /usr/local/apache2/htdocs/sina/.htaccess

 

RewriteEngine on

  #开启rewrite功能

RewriteCond %{HTTP_HOST} www.sina.com

  把以www.sina.com     开头的内容赋值给HTTP_HOST变量

RewriteRule  .*   http://www.sohu.com

  .*  输入任何地址都跳转到http://www.sohu.com

      ②  静态网页向动态网页跳转

      修改配置文件

      <Directory"/usr/local/apache2/htdocs/sohu">

    Options Indexes FollowSymLinks

    AllowOverride All

    Require all granted

</Directory>

 

 

vi /usr/local/apache2/htdocs/sohu/.htaccess

 

RewriteEngine on

 

RewriteRule index(\d+).html index.php?id=$1

      #   输入index(数值).html跳转到index.php文件同时把数值当成变量传入index.php

 

      7)常用的子配置文件

      httpd-autoindex.conf          Apache系统别名

      httpd-default.conf              线程控制

httpd-info.conf              状态统计网页

httpd-languages.conf           语言编码

httpd-manual.conf               Apache帮助文档

httpd-mpm.conf            最大连接数

  MaxRequestWorkers           250(默认worker MPM模块生效)

httpd-multilang-errordoc.conf    错误页面

httpd-ssl.conf                      SSL套接字访问

httpd-userdir.conf               用户主目录配置

httpd-vhost.conf            虚拟主机

 

 

 

 

 

 

0 0
原创粉丝点击