APACHE架构WEB服务器

这是我的总结,然后简单的排版了一下,希望大家用心观看。

课程目标:

  • 了解常见的WEB服务器

  • 熟悉apache服务的基本配置

  • 掌握apache服务的虚拟主机配置

  • 掌握apache的访问控制

  • 掌握MySQL的安装

一、WEB服务概述

web服务是我们日常使用最多的服务,所有的网站都是以web服务的形式为我们呈现

1. WEB服务的特点

  • web服务分为客户端和服务端

  • web客户端通常是我们所使用的浏览器(IE/Chrome/Firefox/Safari或者命令行浏览器等)

  • web服务端就是我们所访问的网站提供的web服务器

    • 常见的web服务端程序有:

      Apache/Nginx/Tengine/Lighttpd/IIS等,不同的web服务针对不同的系统平台各自有优缺点

2. HTTP协议

  • web服务端与客户端是通过HTTP协议(HyperText Transfer Protocol)超文本传输协议进行交互

  • Apache或Nginx都只支持静态页面的解析

3. 静态页面和动态页面

  • HTML语言

    • HTML(HyperText Markup Language)超文本标记语言,是绝大多数网页使用的语言,几乎所有的网页都是以HTML作为源代码,通过浏览器解释后展现出来

    • HTML有固定语法,用来存储网页数据,定义不同元素,如文字、标题、图片等,现在的网页都用CSS语言来存储网页的表现形式

    • 现代主流的网页设计架构: ​ 内容存储:HTML ​ 网页架构:div ​ 网页样式:css

    • HTML形式的页面我们称之为静态页面,包含一些图片、文本、表格等

  • 动态语言

    • 网站使用特定的语言编写的一些程序,在用户访问的时候基于不同的条件生成不同的HTML代码返回给用户浏览器,这样就实现网页的动态化

    • 常见的动态语言: .php .jsp .cgi .asp、python等

4. WEB服务的中间件

  • php: PHP-FPM、HHVM (HipHop Virtual Machine)

  • jsp: Tomcat、JBOSS、Resin、IBM WebSphere

5.常见的WEB组合方式

  • LAMP (Linux + Apache + MySQL + PHP) //php作为Apache的模块

  • LNMP (Linux + Nginx + MySQL + PHP) //php-fpm进程(服务)

  • Nginx /Apache+ Tomcat

二、Apache的介绍

Apache是著名的web服务器软件,开源,由apche软件基金会负责管理开发。

  • Apache服务特点:

  1. 开放源码

  2. 跨平台

  3. 支持多种编程语言

  4. 采用模块化设计

  5. 安全稳定

三、开胃菜-搭建简易WEB服务

1. 关闭防火墙和selinux
2. 配置yum源
3. 软件三步曲(查看安装|确认成功安装|查看软件的文件列表)
4. 了解配置文件   man 5 xxx.conf |程序的官方手册(apache、mysql、oracle)
5. 根据需求通过修改配置文件来完成服务搭建
6. 启动服务,开机自启动
7. 测试验证
需求1:访问一个静态页面,内容:hello world 五一快乐!
环境:
web-server:10.1.1.1
步骤:
1.略
2.略
3.软件三步曲
​
/etc/httpd
/etc/httpd/conf             //主配置文件的主目录
/etc/httpd/conf.d           //子配置文件的目录
/etc/httpd/conf.d/README
/etc/httpd/conf.d/welcome.conf  //欢迎页
/etc/httpd/conf/httpd.conf  //主配置文件
/etc/httpd/conf/magic
/etc/httpd/logs     /var/log/httpd      //互为硬连接
/etc/httpd/modules
/etc/httpd/run      /var/run/httpd      //互为硬连接     
/etc/logrotate.d/httpd              //日志轮转
/etc/rc.d/init.d/htcacheclean
/etc/rc.d/init.d/httpd              //服务启动脚本
/etc/sysconfig/htcacheclean
​
/usr/sbin/apachectl
/usr/sbin/htcacheclean
/usr/sbin/httpd             //二进制命令
​
/var/log/httpd
/var/run/httpd
/var/www                    //apache服务的数据目录
/var/www/html               //apache服务的静态页面存放路径
​
//以下两个目录互为硬连接,互为硬连接的文件是同一个文件,一个改变另一个也改变
[root@web-server ~]# ll -id /var/log/httpd/
798080 drwx------. 2 root root 4096 Apr 25 10:17 /var/log/httpd/
[root@web-server ~]# ll -id /etc/httpd/logs/
798080 drwx------. 2 root root 4096 Apr 25 10:17 /etc/httpd/logs/
​
4. 了解配置文件
5. 根据需求通过修改配置文件完成服务搭建
1)创建一个首页文件
echo "hello world 五一快乐" > /var/www/html/index.html
​
2)启动服务
​
6. 测试验证
IE:http://10.1.1.1

四、Apache服务的基本配置

RHEL6/CentOS6:2.2版本

RHEL7/CentOS7:2.4版本

1. Apache基础

服务端口:
80/tcp(http)
配置文件:
/etc/httpd/conf/httpd.conf      //主配置文件
/etc/httpd/conf.d/*.conf        //子配置文件
/etc/httpd/conf.d/welcome.conf  //默认测试页面
​
/etc/httpd/conf             配置文件目录  
/etc/httpd/conf.d           子配置文件目录
/etc/httpd/conf.d/README     说明书
/etc/httpd/conf.d/welcome.conf   当没有首页index.html 显示红帽欢迎页面
/etc/httpd/conf/httpd.conf      主配置文件
/etc/httpd/logs                 日志目录 /var/log/httpd/硬链接
/etc/httpd/modules              库文件 /usr/lib64/httpd/modules硬链接
/etc/httpd/run                  pid信息
/etc/logrotate.d/httpd          日志轮循
/etc/rc.d/init.d/httpd          启动脚本
/etc/sysconfig/httpd            额外配置文件
/usr/lib64/httpd               
/usr/lib64/httpd/modules        库文件
/usr/lib64/httpd/modules/mod_actions.so
/usr/sbin/apachectl             apache官方启动脚本
/usr/sbin/httpd
/var/www                        apache数据目录
/var/www/cgi-bin                存放apache的cgi脚本数据目录
/var/www/html                   存放apache的html数据目录
/var/www/error                 
/var/www/error/HTTP_NOT_FOUND.html.var    404
/var/www/error/HTTP_FORBIDDEN.html.var    403

2. Apache基本配置选项

ServerRoot "/etc/httpd"                       //服务主目录
Listen 80                                   //监听端口
IncludeOptional conf.d/*.conf                //包含conf.d下的*.conf文件
User apache                                //运行Apache的用户
Group apache                               //运行Apache的用户组
DirectoryIndex index.html index.php      //设置默认主页
DocumentRoot               /var/www/html/               //站点默认数据主目录
​
<Directory />       //系统的根目录授权
    Options FollowSymLinks      //支持软链接
    AllowOverride None         //不支持.htaccess 访问列表 .htaccess文件提供了针对每个目录改变配置的方法
</Directory>
​
RHEL6:
<Directory "/var/www/html">         //授权
    Options Indexes FollowSymLinks      //支持索引 支持软链接
    AllowOverride None          //不支持 .htaccess 访问列表
    Order allow,deny            //排序 先允许再拒绝
    Allow from all                 //允许all
</Directory>
​
REHL7:
<Directory "/var/www/html">
     AllowOverride None
     Require all granted        //允许所有人访问
</Directory>
扩展:配置进程和线程 针对apache2.2 针对面试
# prefork MPM                                   //进程模式
<IfModule prefork.c>
StartServers            10                      //初始建立的进程数
MinSpareServers     10                          //最小空闲的进程数
MaxSpareServers    15                           //最大空闲的进程数
ServerLimit          2000                       //最大启动的进程数 默认256
MaxClients           2000                       //最大并发连接数  默认256    
MaxRequestsPerChild  4000            //每个子进程在其生命周期内允许响应的最大请求数,0不限制
</IfModule>
​
# worker MPM                                   //线程模式
<IfModule worker.c>
StartServers            2                          //初始建立的进程数
ThreadsPerChild     50                         //每个进程建立的线程数
MinSpareThreads   100                      //最小空闲的线程数
MaxSpareThreads  200                       //最大空间的线程数
MaxClients           2000                      //最大的并发访问量(线程)
MaxRequestsPerChild 0                //每个子进程在其生命周期内允许响应的最大请求数,0不限制
</IfModule>

3.Apache更改默认数据根目录

DocumentRoot "/webserver"
<Directory "/webserver/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
​
[root@web-server conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: Syntax error on line 293 of /etc/httpd/conf/httpd.conf:
DocumentRoot must be a directory
                                                           [FAILED]
[root@web-server conf]# mkdir /webserver
​
[root@web-server conf]# service httpd restart
Stopping httpd:                                            [FAILED]
Starting httpd: httpd: apr_sockaddr_info_get() failed for web-server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
原因:主机名和IP地址解析问题                                                           [  OK  ]
​
[root@web-server conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 10.1.1.1 for ServerName
                                                           [  OK  ]
vim /etc/httpd/conf/httpd.conf
...
ServerName  10.1.1.1:80
​
[root@web-server conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
​
​
root@web-server conf]# cp /var/www/html/index.html /webserver/
​
测试验证

4.Apache共享文件

Apache如何共享文件呢?

  • 软连接方式

需求1:共享web-server服务端的/data目录里的所有文件
1. apache服务默认情况下到哪个目录里找相应的文件
2. 将本地的/data目录做一个快捷方式(软连接)到指定的目录里
​
步骤:
1. 创建/data/共享目录
[root@web-server ~]# touch /data/file{1..5}
[root@web-server ~]# mkdir /data/kangjian
[root@web-server ~]# ll /data/
total 4
-rw-r--r-- 1 root root    0 Apr 25 11:21 file1
-rw-r--r-- 1 root root    0 Apr 25 11:21 file2
-rw-r--r-- 1 root root    0 Apr 25 11:21 file3
-rw-r--r-- 1 root root    0 Apr 25 11:21 file4
-rw-r--r-- 1 root root    0 Apr 25 11:21 file5
drwxr-xr-x 2 root root 4096 Apr 25 11:22 kangjian
[root@web-server ~]# ll /webserver/
total 4
-rw-r--r-- 1 root root 25 Apr 25 11:16 index.html
2. 创建快捷方式(软连接)
[root@web-server ~]# ln -s /data/ /webserver/share
[root@web-server ~]# ll /webserver/share/
total 4
-rw-r--r-- 1 root root    0 Apr 25 11:21 file1
-rw-r--r-- 1 root root    0 Apr 25 11:21 file2
-rw-r--r-- 1 root root    0 Apr 25 11:21 file3
-rw-r--r-- 1 root root    0 Apr 25 11:21 file4
-rw-r--r-- 1 root root    0 Apr 25 11:21 file5
drwxr-xr-x 2 root root 4096 Apr 25 11:22 kangjian
[root@web-server ~]# ll /webserver/
total 4
-rw-r--r-- 1 root root 25 Apr 25 11:16 index.html
lrwxrwxrwx 1 root root  6 Apr 25 11:23 share -> /data/
​
3. 测试验证
结论:
1. 当默认数据目录里有index.html的首页文件时,优先找首页文件
2. 当默认数据目录里没有index.html的首页文件时,会显示测试页
3. 不管有没有首页文件都不会直接找数据文件
解决:
1. 加入路径
http://10.1.1.1/share
2. 将测试页欢迎页删除或者重命名即可
[root@web-server conf.d]# mv welcome.conf welcome.conf.bak
[root@web-server conf.d]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
​

  • 别名方式

​
Alias /test/ "/data/"
​
<Directory "/data">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
​
注意:
别名后面的路径"/""会影响访问
​
IE:http://10.1.1.1/test     ——>不能访问
IE:http://10.1.1.1/test/    可以访问

课堂练习:

通过2种方式共享你的/home目录和/tmp目录里的所有文件

三、Apache服务的访问控制

1. 开启基本认证(用户密码)

AuthType Basic
AuthName "Restricted Files"
#(下面这一行是可选的)
AuthBasicProvider file
AuthUserFile /usr/local/apache2/passwd/passwords
Require user rbowen
​
​

需求:/webserver——>index.html

步骤:
1. 创建密码文件
[root@web-server conf]# htpasswd -cm /etc/httpd/conf/.passfile harry
New password: 
Re-type new password: 
Adding password for user harry
[root@web-server conf]# cat .passfile 
harry:$apr1$eMymxFmU$A1/l6yw29iVRdwE968TBT0
[root@web-server conf]# htpasswd -mb /etc/httpd/conf/.passfile jack 123
Adding password for user jack
[root@web-server conf]# cat .passfile 
harry:$apr1$eMymxFmU$A1/l6yw29iVRdwE968TBT0
jack:$apr1$C3TPLnU8$s2F2Dv8Aol2LjbSqqizDI/
​
htpasswd:
-c:创建新的密码文件
-m:密码使用md5加密
-b:非交互式
​
2. 修改配置文件开启基本认证
vim /etc/httpd/conf/httpd.conf
。。。
<Directory "/webserver/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthType Basic          //开启基本认证
    AuthName "Input your name and password:"    //认证信息
    AuthBasicProvider file      //文件
    AuthUserFile  /etc/httpd/conf/.passfile     //刚刚创建的密码文件
    Require user  harry         //可用用户
</Directory>
​
3. 重启服务
service httpd restart
​
​
总结:
1. 开启用户名密码认证有几点注意
1) 需要知道网站的数据根目录  /webserver
2) 将认证信息加入到 <Directory "/webserver"> .....</Directory>
3) 创建一个密码文件来保存用户名和密码
​
​
需求2:允许多个人来访问如何实现
1. 创建一个文件保存所有的用户(将很多用户加入到一个组里)
vim /etc/httpd/conf/groups
admin:user01 user02 user03
​
2. 将组成员加入到密码文件中
htpasswd -mb /etc/httpd/conf/.passfile user01 123
htpasswd -mb /etc/httpd/conf/.passfile user02 123
htpasswd -mb /etc/httpd/conf/.passfile user03 123
​
3. 修改主配置文件
<Directory "/webserver/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthType Basic
    AuthName "Input your name and password:"
   AuthBasicProvider file
   AuthUserFile /etc/httpd/conf/.passfile
   AuthGroupFile /etc/httpd/conf/groups
   Require group admin
</Directory>
​
4. 重启服务
service httpd restart
​
5. 测试验证
​
总结:
1. 将很多人加入到组里(创建一个文件建立组和用户的关系)   /etc/httpd/conf/groups
2. 在配置文件中指定允许属于组成员来访问       Require group admin
结论:只允许admin组成员访问,那么密码文件中的其他用户不可以访问
思考:想允许其他用户和admin组成员都可以访问,咋办?
​
   Require group admin
   Require valid-user harry
​
Require valid-user:指定多个用户访问
Require user:指定某个人访问

2. 网络访问控制

RHEL6:http 2.2
Order allow,deny  如果allow和deny冲突,deny为准
Order deny,allow  如果allow和deny冲突,allow为准
1、禁止部分ip不能访问网站
Order allow,deny
Allow from  all
Deny from  192.168.0.254 172.16.2.10
​
2、针对某个网段
Order allow,deny
Allow from  all
Deny from  192.168.0.0/255.255.255.0
​
3、针对域名
Order allow,deny
Allow from  all
Deny from  node1.itcast.cc *.itcast.cc
​
4、拒绝大部分,只允许某个ip
Order deny,allow
Deny from  all
Allow from 192.168.0.254
​
需求3:只拒绝10.1.1.2主机访问我的网站
​
Order allow,deny
Allow from  all
Deny from  10.1.1.2
​
引申扩展:
RHEL7:http 2.4+
案例1:允许所有主机访问
<Directory "/var/www/html">
     AllowOverride None
     Require all granted
</Directory>
​
AllowOverride All        允许子目中的 .htaccess 中的设置覆盖当前设置
AllowOverride None       不允许子目中的 .htaccess 中的设置覆盖当前设置
​
案例2:只允许网段192.168.0.0/24和192.168.10.254/24访问
<Directory "/var/www/html">
     AllowOverride None
     Require ip 192.168.0.0/24
     Require ip 192.168.10.254
</Directory>
​
案例3:只拒绝某些主机访问
<Directory "/var/www/html">
        AllowOverride None
        <RequireAll>
                Require not ip 10.1.1.254
                Require all granted
        </RequireAll>
</Directory>
  • HTTP通过状态码来标记返回信息,以下为常见的状态码:

常用状态码分类:
200:成功,请求的所有的数据通过响应报文的entity-body部分发送;ok
301:请求的URL指向的资源已经被删除,但是在响应报文中通过首部Location指明了资源现在所处的新位置;Moved Permanently
302:与301相似,但是在响应报文中通过Location指明资源现在所处的临时的新位置;Found
304:客户端发出了条件式请求,但是服务器上的资源未发送改变,通过响应此响应状态码通知客户端,Not Modified
401:需要客户端输入账号和密码才能访问资源;Unauthorized
403:请求被禁止;Forbidden
404:服务器无法找到客户端请求的资源;Not Found
500:服务器内部错误;Internal Server Error
502:代理服务器从后端服务器收到了一条伪响应;Bad Gateway

课堂练习1:

  1. 根据如下需求搭建web服务

  2. 更改默认数据根目录/webserver

  3. 只允许sysadmin组里的user01成员去访问你的默认数据根目录,密码是123;同时拒绝192.168.0.0网段的人去访问,除了192.168.0.1

默认数据目录  /webserver      index.html
1. 创建一个密码文件
htpasswd -cmb /etc/httpd/conf/.passfile user01 123
2. 创建一个文件来保存用户和组的信息
echo "sysadmin:user01 user02" > /etc/httpd/conf/groups
​
3. 修改配置文件开启基本认证
vim /etc/httpd/conf/httpd.conf
。。。
<Directory "/webserver/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from 192.168.0.0/255.255.255.0
    Allow from 192.168.0.1
    AuthType Basic
    AuthName "Input your name and password:"
   AuthBasicProvider file
   AuthUserFile /etc/httpd/conf/.passfile
   AuthGroupFile /etc/httpd/conf/groups
   Require group sysadmin
   Require user user01
</Directory>
​

四、Apache服务的虚拟主机

1. 基于IP的虚拟主机

环境准备:
方法1:增加一个虚拟网卡,配置IP地址 (两个网卡是同一种网络模式)
eth0:10.1.1.1
eth1:192.168.0.1
方法2:通过增加子接口的方式
ifconfig eth0:1 192.168.0.1/24      //临时增加子接口(重启失效)
​
注意:不管什么方式只需要当前主机有2个不同网段的IP即可
​
需求:
IE:http://10.1.1.1  ———> this is 10.1.1.1 test page
    http://192.168.0.1 ——> this is 192.168.0.1 test page
    
    
步骤:
1. 创建相应的数据目录及首页文件
mkdir /web{1,2}/data -p
echo "this is 10.1.1.1 test page" > /web1/data/index.html
echo "this is 192.168.0.1 test page" > /web2/data/index.html
​
2. 发布网站
vim /etc/httpd/conf/httpd.conf
最后面增加以下内容:
<VirtualHost 10.1.1.1:80>
    ServerAdmin webmaster@dummy-host.example.com    //管理员邮箱(可改可不改)
    DocumentRoot /web1/data/                    //定义网站文件的数据目录
    #ServerName dummy-host.example.com
    ErrorLog logs/10.1.1.1-error_log            //错误日志文件名(/etc/httpd/logs/下)
    CustomLog logs/10.1.1.1-access_log common   //正常的访问日志文件
</VirtualHost>
​
<VirtualHost 192.168.0.1:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /web2/data/
    #ServerName dummy-host.example.com
    ErrorLog logs/192.168.0.1-error_log
    CustomLog logs/192.168.0.1-access_log common
</VirtualHost>
​
3. 重启服务
​
4. 测试验证
client:10.1.1.2
[root@client Desktop]# route add -net 192.168.0.0/24 dev eth0   //临时添加一条道192.168.0.0网络的路由
[root@client Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.1.1.0        0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         10.1.1.254      0.0.0.0         UG    0      0        0 eth0
[root@client Desktop]# ping 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=1.85 ms
​
[root@client Desktop]# yum -y install elinks
[root@client Desktop]# elinks http://10.1.1.1
[root@client Desktop]# elinks http://192.168.0.1
​
​
​
​
​
​

2. 基于端口的虚拟主机

需求2:
IE: 
http://10.1.1.1:80          this is 80 test page
http://10.1.1.1:10086       this is 10086 test page
​
1. 创建相应的数据目录及首页文件
mkdir /data/{80,10086} -p
echo "this is 80 test page" > /data/80/index.html
echo "this is 10086 test page" > /data/10086/index.html
​
2. 发布虚拟主机
Listen 80
Listen 10086
​
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /data/80
    #ServerName dummy-host.example.com
    ErrorLog logs/80-error_log
    CustomLog logs/80-access_log common
</VirtualHost>
​
<VirtualHost *:10086>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /data/10086
    #ServerName dummy-host.example.com
    ErrorLog logs/10086-error_log
    CustomLog logs/10086-access_log common
</VirtualHost>
​
[root@web-server conf]# netstat -nltp|grep 10086
tcp        0      0 :::10086                    :::*                        LISTEN      7570/httpd          
[root@web-server conf]# netstat -nltp|grep 80
tcp        0      0 :::80                       :::*                        LISTEN      7570/httpd       

3.基于域名的虚拟主机

需求3:
http://www.myweb.cc     ——> this is myweb.cc
http://ftp.test.net     ——> this is myftp
​
思路:
1. 搭建DNS服务器
2. 搭建web服务器
3. 客户端指定DNS服务器测试验证
​
环境:
WEB-server:10.1.1.1
DNS-server:10.1.1.3
client:10.1.1.2
​
步骤:
1. 搭建DNS服务器(在10.1.1.3上完成)
[root@DNS-server ~]# vim /etc/named.conf 
options {
    listen-on port 53 { 127.0.0.1;any; };
    listen-on-v6 port 53 { ::1; };
    directory   "/var/named";
    dump-file   "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
    allow-query     { localhost; any;};
    recursion yes;
。。。。
​
​
​
[root@DNS-server ~]# vim /etc/named.rfc1912.zones 
...
增加以下内容:
zone "myweb.cc" IN {
    type master;
    file "myweb.cc.zone";
    allow-update { none; };
};
​
zone "test.net" IN {
    type master;
    file "test.net.zone";
    allow-update { none; };
};
​
[root@DNS-server ~]# cd /var/named/
[root@DNS-server named]# ll
total 28
drwxrwx--- 2 named named 4096 Aug 27  2013 data
drwxrwx--- 2 named named 4096 Aug 27  2013 dynamic
-rw-r----- 1 root  named 1892 Feb 18  2008 named.ca
-rw-r----- 1 root  named  152 Dec 15  2009 named.empty
-rw-r----- 1 root  named  152 Jun 21  2007 named.localhost
-rw-r----- 1 root  named  168 Dec 15  2009 named.loopback
drwxrwx--- 2 named named 4096 Aug 27  2013 slaves
[root@DNS-server named]# cp -p named.localhost myweb.cc.zone
[root@DNS-server named]# cp -p named.localhost test.net.zone
[root@DNS-server named]# vim myweb.cc.zone 
[root@DNS-server named]# vim test.net.zone 
[root@DNS-server named]# service named start
Generating /etc/rndc.key:                                  [  OK  ]
Starting named:                                            [  OK  ]
[root@DNS-server named]# 
​
​
相关文件内容:
[root@DNS-server named]# cat myweb.cc.zone test.net.zone 
$TTL 1D
@   IN SOA  myweb.cc. rname.invalid. (
                    0   ; serial
                    1D  ; refresh
                    1H  ; retry
                    1W  ; expire
                    3H )    ; minimum
@   NS  a.myweb.cc.
a   A   10.1.1.3
www A   10.1.1.1
$TTL 1D
@   IN SOA  test.net. rname.invalid. (
                    0   ; serial
                    1D  ; refresh
                    1H  ; retry
                    1W  ; expire
                    3H )    ; minimum
    NS  b.test.net.
b   A   10.1.1.3
ftp A   10.1.1.1
​
​
​
2. 搭建web服务器,发布2个虚拟主机(网站)  [在10.1.1.1上完成]
1) 创建相应的数据目录和首页文件
[root@web-server ~]# mkdir /data/{myweb,myftp}
[root@web-server ~]# echo "this is myweb.cc" > /data/myweb/index.html
[root@web-server ~]# echo "this is myftp" > /data/myftp/index.html
​
2)发布网站
[root@web-server ~]# vim /etc/httpd/conf/httpd.conf
...
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /data/myweb
    ServerName www.myweb.cc
    ErrorLog logs/myweb-error_log
    CustomLog logs/myweb-access_log common
</VirtualHost>
​
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /data/myftp
    ServerName ftp.test.net
    ErrorLog logs/ftp-error_log
    CustomLog logs/ftp-access_log common
</VirtualHost>
​
3) 重启服务
​
​
3. 客户端测试验证 [10.1.1.2]
​
[root@client Desktop]# echo nameserver 10.1.1.3 > /etc/resolv.conf 
[root@client Desktop]# elinks http://www.myweb.cc
[root@client Desktop]# elinks http://ftp.test.net
​
​
​
​

课堂练习2:

搭建web服务器,要求如下:

  1. 当访问www.zhangsan.net时看到“welcome to myweb!!!”

  2. 当访问www.test.com时看到“this is test page!”

  3. 拒绝192.168.0.0/24网段和example.com这个域所管理的主机访问www.zhangsan.net网站,但是允许harry用户访问

  4. www.test.com网站的数据根目录为/web/www

环境:
web-server:10.1.1.1
client:10.1.1.2
​
1. 搭建基于域名的虚拟主机(10.1.1.1)
1) 创建相应的数据目录和网页文件
[root@web-server ~]# rpm -q httpd
httpd-2.2.15-29.el6.centos.x86_64
[root@web-server ~]# mkdir /web/{zhangsan,test} -p
[root@web-server ~]# echo "this is zhangsan.com test page " > /web/zhangsan/index.html
[root@web-server ~]# echo "this is test.com test page " > /web/test/index.html
​
2) 发布虚拟主机
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /web/zhangsan
    ServerName www.zhangsan.net
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /web/test
    ServerName www.test.com
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
​
3. 拒绝192.168.0.0/24网段和example.com这个域所管理的主机访问www.zhangsan.net网站,但是允许harry用户访问
​
vim /etc/httpd/conf/httpd.conf
...
<Directory "/web/zhangsan/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from 192.168.0.0/24 .example.com
    AuthType Basic
    AuthName "Input your name and password:"
    AuthBasicProvider file
    AuthUserFile  /etc/httpd/conf/.passfile
    Require user  harry
</Directory>

五、MySQL数据库的安装

1. 通用的二进制包安装

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

2.源码包安装

根据需求进行配置,参考官方文档

​
默认情况下载当前目录下直接可以配置
shell> cmake . -LAH  查看所有支持的配置选项
shell> cmake .
//配置选项
CMAKE_INSTALL_PREFIX  /usr/local/mysql      安装路径
DEFAULT_CHARSET  latin1      拉丁  默认字符集
DEFAULT_COLLATION    latin1_swedish_ci     指定服务器默认的校对规则(排序规则)
ENABLED_LOCAL_INFILE    OFF     是否开启内部加载外部文件功能 默认off 1代表开启0代表关闭
MYSQL_DATADIR       数据文件目录
SYSCONFDIR         初始化参数文件目录(主配置文件路径(默认优先去/etc目录去找))
MYSQL_TCP_PORT                    服务端口号,默认3306
MYSQL_UNIX_ADDR                   socket文件路径,默认/tmp/mysql.sock 
WITHOUT_xxx_STORAGE_ENGINE        指定不编译的存储引擎
WITH_xxx_STORAGE_ENGINE       指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认已被编译至服务器,不需要特别指定。
WITH_EXTRA_CHARSETS    扩展字符集

  • 根据需求进行配置

编写cmake脚本:
vim cmake.sh
#!/bin/bash
cmake . \
-DCMAKE_INSTALL_PREFIX=/mysql25 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/mysql25/data \
-DSYSCONFDIR=/mysql25 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/mysql25/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DMYSQL_TCP_PORT=3309 \
-DMYSQL_USER=mysql
说明:如果第一次配置出错,再次配置前删掉上一次的缓存文件
rm -f  CMakeCache.txt
  • 编译

make

  • 安装

make install

引申拓展:

  • 字符集与字符编码

字符是各种文字和符号的总称,包括各个国家文字、标点符号、图形符号、数字等。字符集是多个字符的集合,字符集种类较多,每个字符集包含的字符个数不同,计算机要准确的处理各种字符集文字,需要进行字符编码,以便计算机能够识别和存储各种文字。也就是说字符编码是字符集的实现方式。 注意:有的字符编码和字符集的名称是一致的。

  • 常见的字符集

    • Unicode:也叫统一字符集,它包含了几乎世界上所有的已经发现且需要使用的字符(如中文、日文、英文、德文等)

    • ASCII:早期的计算机系统只能处理英文,所以ASCII也就成为了计算机的缺省字符集,包含了英文所需要的所有字符

    • GB2312:中文字符集,包含ASCII字符集。ASCII部分用单字节表示,剩余部分用双字节表示

    • GBK:GB2312的扩展,但完整包含了GB2312的所有内容

    • GB18030:GBK字符集的超集,常叫大汉字字符集,也叫CJK(Chinese,Japanese,Korea)字符集,包含了中、日、韩三国语言中的所有字符

  • 常见的字符编码

UTF-8 UTF-16 UCS-2 UCS-4 
GBK/GB2312
GB18030
  • 生产环境字符集建议

    • 非常肯定终端用户只有中文时,可选择gbk / gb2312

    • 为了方便数据迁移、以及多种终端展示,最好是utf8

    • 字符无需区分大小写时,采用默认的xx_ci校验集可以,否则选择xx_bin校验集(生产环境中,尽量不要修改校验集)

    • 默认字符集是latin1,该字符集存放汉字是分开存放,以至于检索结果时不够精确,好处就是节省空间,不推荐使用

字符编码每个字符字节数
ASCII1
UCS-2(Unicode)2
UCS-4(Unicode)4
UTF-8(Unicode)1-6
UTF-16(Unicode)2-4
GBK/GB2312(中文)1-2
GB18030(CJK)1-4

3.常见启动报错

初始化数据库(安装默认的库和表):
​
cd /mysql25
# scripts/mysql_install_db --user=mysql
错误:
2017-07-16 10:55:20 22725 [ERROR] InnoDB: Unable to lock ./ibdata1, error: 11
2017-07-16 10:55:20 22725 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
​
原因:
默认情况下会到/usr/local/mysql里去找文件;通用的二进制包的安装路径正好在该目录写,并且是启动的
​
# ./mysql_install_db --help
--no-defaults:不要从任何的配置文件中读取相应的参数,忽略掉mysql安装过程中的默认配置,如创建默认用户并设置默认密码等
解决:
# ./mysql_install_db --basedir=/mysql25 --datadir=/mysql25/data --user=mysql 
​
启动数据库:
# ./mysqld_safe --user=mysql 
170716 11:05:51 mysqld_safe Logging to '/var/lib/mysql/node1.uplook.com.err'.
170716 11:05:51 mysqld_safe A mysqld process already exists
​
原因:是因为/var/lib/mysql启动的是另外一个数据库库,所以进程冲突
解决:如下
# ./mysqld_safe --basedir=/mysql25 --datadir=/mysql25/data --user=mysql
170716 11:09:13 mysqld_safe Logging to '/mysql25/data/node1.uplook.com.err'.
170716 11:09:13 mysqld_safe Starting mysqld daemon with databases from /mysql25/data
170716 11:09:14 mysqld_safe mysqld from pid file /mysql25/data/node1.uplook.com.pid ended
原因:
mysql用户无法在安装目录里创建pid文件和sock文件
解决:
chown -R mysql. /mysql25
再次启动,依然无法启动:
# ./mysqld_safe --basedir=/mysql25 --datadir=/mysql25/data --user=mysql
170716 11:14:42 mysqld_safe Logging to '/mysql25/data/node1.uplook.com.err'.
170716 11:14:42 mysqld_safe Starting mysqld daemon with databases from /mysql25/data
170716 11:14:44 mysqld_safe mysqld from pid file /mysql25/data/node1.uplook.com.pid ended
​
查看错误日志:
2017-07-16 11:14:43 23957 [Note] Server socket created on IP: '::'.
2017-07-16 11:14:43 23957 [ERROR] Can't start server: Bind on TCP/IP port: Address already in use
2017-07-16 11:14:43 23957 [ERROR] Do you already have another mysqld server running on port: 3308 ?
2017-07-16 11:14:43 23957 [ERROR] Aborting
​
分析:
mysql 5.6.19 ——>rpm——>/etc/mysql/my.cnf
# cat /etc/mysql/my.cnf 
[mysqld]
basedir=/usr
datadir=/var/lib/mysql
port=3308
socket=/var/lib/mysql/mysql.sock
​
mysql 5.6.25 配置文件/mysql25/my.cnf
该文件没有做任何配置,所以默认去到/etc/my.cnf——>/etc/mysql/my.cnf....
​
最终如何解决:
vim /mysql25/my.cnf
[mysqld]
 basedir = /mysql25
 datadir = /mysql25/data
 port = 3309
 socket = /mysql25/mysql.sock
​
​
​
报错:
# service mysql start
Starting MySQL.The server quit without updating PID file (/mysql/data/vm1.uplook.com.pid).[FAILED]
解决:
chown mysql.mysql /mysql -R
​
环境变量:
export PATH=$PATH:/usr/local/mysql/bin
​
# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@vm1 data]# mysql --socket=/data/mysql.sock 
​
​
# ln -s /data/mysql.sock /var/lib/mysql/mysql.sock
​
# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.25 Source distribution
​
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
​
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
​
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
​
mysql> exit
Bye

  • 17
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值