8. apache服务实验笔记

时间:2023-03-08 18:13:39

Apache服务器

一 简介

1 www:world  wide  web 万维网

http 协议: 超文本传输协议

HTML语言: 超文本标识语言

2 URL:统一资源定位 协议+域名:端口+网页文件名

http://www.sina.com.cn:80/11/index.html

3 搭建www的服务器的方法

windows   IIS+asp+SQLserver

Internet  Information  server

Linux apache+mysql+php

二 安装

1、lamp源码安装

生产环境    安全  稳定   错误提示关闭    源码包,技术要求高,复杂

实验环境(复制的生产环境 错误提示开启)

开发环境 源码包 二进制包

2、rpm包安装     yum安装

httpd

mysql

mysql-server

php

php-devel

php-mysql

三 相关文件

apache配置文件

源码包安装:/usr/lcoal/apache2/etc/httpd.conf

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

rpm包安装:/etc/httpd/conf/httpd.conf

默认网页保存位置:

网站根目录:/usr/local/apache2/htdocs/

rpm包安装:/var/www/html/

日志保存位置

网站日志:/usr/local/apache2/logs/

rpm包: /var/log/httpd/

rpm包默认使用日志处理程序   /var下都会轮替   源码包才需要设置

日志处理:

#tail -f  access_log   动态查看文件

1日志切割  apache自带日志里面自带日志切割

2日志轮替  linux自带日志管理logrotate.conf加入

#vim /etc/logrotate.conf

/usr/local/apache2/logs/access_log{

daily

rotate  30

}

#logrotate -f /etc/logrotate.conf 手动运行
#cd /usr/local/apache2/logs/
#ls

所有日志都要进行日志轮替

四 配置文件

vi /root/.bashrc

alias sta=’/usr/local/apache2/bin/apachectl start’

alias sto=’/usr/local/apache2/bin/apachectl stop’

source /root/.bashrc

#sto
#sta

注意: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关键字定义目录权限

Options Indexes FollowSymLinks

#options

None:没有任何额外权限

All: 所有权限

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

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

AllowOverride None

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

None:.htaccess中权限不生效

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

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

Require all granted 访问控制列表   403错误   404错误

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

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

Require all  denied

Require ip 192.168.1.1

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

Require  all  denied

Require ip 192.168.1.0/24

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

<RequireAll>

Require all  granted

Require not ip 192.168.1.2

</RequireAll>

例子4.允许所有访问,

Require all  granted

例子5.拒绝所有访问,

Require all  denied

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

AllowOverride None

Require all granted

</Directory>

修改子配置文件
#vim /usr/local/apache2/extra/httpd-autoindex.conf
29 Alias /www/ "/usr/local/apache2/www/"
30 <Directory "/usr/local/apache2/www/">
31 Options Indexes
32 AllowOverride None
33 Require all granted
34 </Directory>

3)建立/usr/local/apache2/www目录
#mkdir /usr/local/apache2/www

4)重启服务 测试
#sto
#sta

测试 192.168.172.251/www/

4 用户认证

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

1) 建立需要保护的目录

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

mkdir  -p  /share/soft

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

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

453 Include etc//extra/httpd-autoindex.conf

#vim /usr/local/apache2/etc/extra/httpd-autoindex.conf
37 Alias /soft/ "/share/soft/"
38 <Directory "/share/soft/">
39 Options Indexes
40 AllowOverride All
41 Require all granted
42 </Directory>

Alias /soft/ "/share/soft/"

<Directory "/share/soft">

Options Indexes

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

Require all granted

</Directory>

重启apache

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

建立/share/soft/.htaccess权限文件
#vim /share/soft/.htaccess
1 AuthName "172PHP"
2 AuthType basic
3 AuthUserFile /share/apache.passwd
4 require valid-user

cd  /share/soft

vi  .htaccess #不区分大小写

AuthName "50 docs"

#提示信息

AuthType basic

#加密类型

AuthUserFile /share/apache.passwd

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

require valid-user

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

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

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

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

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

-m  再添加更多用户时

重启服务 测试
#sto
#sta

测试  192.168.172.251/soft/

5 虚拟主机

1)分类

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

基于端口的虚拟主机: 一台服务器,一个ip,搭建多个网站,每个网络使用不同端口访问

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

2)步骤:

① 解析试验域名

192.168.172.251 www.sina.com
192.168.172.251 www.sohu.com

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

/etc/hosts   Linux

② 规划网站主目录

#mkdir /share/sina/
#mkdir /share/sohu/
#vim /share/sina/index.html
#vim /share/sohu/index.html

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

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

③  修改配置文件

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

465 Include etc//extra/httpd-vhosts.conf

#打开虚拟主机配置文件

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

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

Options Indexes

AllowOverride None

Require all granted

</Directory>

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

Options Indexes

AllowOverride None

Require all granted

</Directory>

23 <Directory "/share/sina">
24 Options Indexes
25 AllowOverride None
26 Require all granted
27 </Directory>
28
29 <Directory "/share/sohu">
30 Options Indexes
31 AllowOverride None
32 Require all granted
33 </Directory>

<VirtualHost 192.168.150.253>

#注意,只能写ip

ServerAdmin webmaster@sina.com

#管理员邮箱

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

#网站主目录

ServerName www.sina.com

#完整域名

ErrorLog "logs/sina-error_log"

#错误日志

CustomLog "logs/sina-access_log" common

#访问日志

</VirtualHost>

<VirtualHost 192.168.150.253>

ServerAdmin webmaster@sohu.com

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

ServerName www.sohu.com

ErrorLog "logs/sohu.com-error_log"

CustomLog "logs/sohu.com-access_log" common

</VirtualHost>

35 <VirtualHost 192.168.172.251>
36 ServerAdmin webmaster@sina.com
37 DocumentRoot "/share/sina/"
38 ServerName www.sina.com
39 ErrorLog "logs/sina-error_log"
40 CustomLog "logs/sina-access_log" common
41 </VirtualHost>
42
43 <VirtualHost 192.168.172.251>
44 ServerAdmin webmaster@sohu.com
45 DocumentRoot "/share/sohu/"
46 ServerName www.sohu.com
47 ErrorLog "logs/sohu-error_log"
48 CustomLog "logs/sohu-access_log" common
49 </VirtualHost>

重启服务 测试
#sto
#sta

测试 www.sina.com www.sohu.com

6 rewrite 重写功能   URL

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

1)域名跳转 www.sina.com  ------>  www.sohu.com

开启虚拟主机,并正常访问

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

147LoadModule rewrite_module modules/mod_rewrite.so

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

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

# vi extra/httpd-vhosts.conf

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

Options Indexes FollowSymLinks

AllowOverride All

Require all granted

</Directory>

#vim /usr/local/apache2/etc/extra/httpd-vhosts.conf
23 <Directory "/share/sina">
24 Options Indexes FollowSymLinks
25 AllowOverride All
26 Require all granted
27 </Directory>

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

建立/share/sina/.htaccess
#vim /share/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

1 RewriteEngine on
2 RewriteCond %{HTTP_HOST} www.sina.com
3 RewriteRule .* http://www.sohu.com

重启服务 测试
#sto
#sta

测试   www.sina.com  -> www.sohu.com

2)网页文件跳转

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

RewriteEngine on

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

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

#vim /share/sina/.htaccess
1 RewriteEngine on
2 RewriteRule index(\d+).html index.php?id=$1

#cd /share/sina
#vim index.php
<?php
echo "yemian rewrite";
?>

测试 www.sina.com/index5.html

7 常用子配置文件

httpd-autoindex.conf apache系统别名

httpd-default.conf 线程控制 *

httpd-info.conf    状态统计网页

httpd-languages.conf 语言编码 *

httpd-manual.conf apache帮助文档

httpd-mpm.conf     最大连接数 *

httpd-multilang-errordoc.conf 错页面 *

httpd-ssl.conf    ssl安全套接字访问

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

httpd-vhosts.conf 虚拟主机