(一)请描述一次完整的http请求处理过程
- http请求图解
(二)httpd所支持的处理模型有哪些,他们的分别使用于哪些环境。
- 所谓httpd支持的处理模型及httpd的mpm机制
- perfork:多进程模型,每个进程响应一个请求
- 一个主进程:负责生成子进程及回收子进程,负责创建套接字,负责接收请求,并将其派发给某子进程进行处理
- n个子进程:每个子进程处理一个请求
- 工作模型:会预先生成5个空闲进程,随时等待用于响应用户请求;在其配置文件中可以指定负责最大空闲子进程和最小空闲子进程;
- 适用场景:它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求,适合于并发量适中而又追求稳定的用户使用。
- worker:多进程多线程模型,每线程处理一个用户请求
- 一个主进程:负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理
- 多个子进程:每个子进程负责生成多个线程
- 每个线程:负责响应用户请求
- 并发响应数量:m*n,m:子进程数量,n:每个子进程所能创建的最大线程数量
- 适用场景:占据更少的内存,高并发下表现更优秀。
- event:事件驱动模型,多进程模型,每个进程响应多个请求
- 一个主进程 :负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理
- 子进程:基于事件驱动机制直接响应多个请求
- 适用场景:event模型是三种模型中效率最高的一种,可以突破10K的限制(即并发数1W),对海量的系统特别适用。
- 简要图解
- perfork:多进程模型,每个进程响应一个请求
(三)源码编译安装LAMP环境(基于wordpress程序),并写出详细的安装、配置、测试过程。
- LAMP部署前,主机规划
- 启用两台CentOS 6主机
- IP:192.168.10.217,安装httpd-2.4,php-5.4
- IP:192.168.10.218,安装MariaDB-5.5
- 启用两台CentOS 6主机
- LAMP程序的安装顺序
- 先安装:在 IP:192.168.10.218的主机上安装MariaDB-5.5
- 再安装:在IP:192.168.10.217的主机上安装httpd-2.4
- 最后安装:在IP:192.168.10.217的主机上安装php-5.4
- 安装MariaDB-5.5
- 在 IP:192.168.10.218的主机上的/root目录下下载 mariadb-5.5.54-linux-x86_64.tar源码包
- 准备数据目录
mkdir -pv /mydata/data
chown -R mysql.mysql /mydata/data
(这是为了mysql能够向目录里面写数据)
- 安装配置mariadb
groupadd mysql
useradd -g mysql mysql
tar xf mariadb-5.5.54-linux-x86_64.tar -C /usr/local
cd /usr/local
ln -sv mariaDB-version mysql
cd /usr/local/mysql
chown -R root:mysql ./*
(防止mysql进程被劫持,具有目录的属主权限)cp support-files/my-large.cnf /etc/mysql/my.cnf
(mysql进程读取配置文件的顺序/etc/my.cnf –> /etc/mysql/my.cnf –>–default-extra-file=/PATH/TO/CONF_FILE –> ~/.my.cnf)vi /etc/mysql/my.cnf
添加:
datadir = /mydata/data
innodb_file_per_table = ON
skip_name_resolve = ON
scripts/mysql_install_db --user=mysql --datadir=/mydata/data
这个地方好像不能使用复制,必须手打cp support-files/mysql.server /etc/init.d/mysqld
chkconfig –add mysqld
service mysqld start
- 将mysql的库文件共享链接出来
vi /etc/ld.so.conf.d/mysql.conf
- 添加:
/usr/local/msyql/lib
- 重读配置文件:
ldconfig
- 检查mysql的库文件是否可以读取:
ldconfig -p | grep mysql
- 将mysql的客户端程序的路径添加到环境变量中
vi /etc/profile.d/mysql.sh
- 添加
export PATH=/usr/local/mysql/bin:$PATH
- 让内核重新读取配置文件:. /etc/profile.d/mysql.sh
- 使用mysql的安全安装工具对mysql进行安全加固
mysql_secure_installation
- 登入mysql , 授权远程登入的root用户
grant all on *.* to root@'192.168.%.%' identified by 'yhy3426356';
flush privileges;
- 安装httpd-2.4
- 由于在CentOS 6上安装httpd-2.4,需要编译安装,依赖apr-1.4+ , 依赖apr-util-1.4+,那么如果需要在CentOS 6上安装httpd需要编译安装,并且安装Development tools、Server Platform Development包组和pcre-devel包
- 下载apr-1.5.2.tar和apr-util-1.5.4.tar在当前目录下 , 解压 ,下载httpd-2.4到当前目录下,解压
- 1:编译安装apr-1.4+
./configure --prefix=/usr/local/apr/
make && make install
- 2:编译安装apr-util-1.4+
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
- 3:编译安装httpd-2.4
./configure --prefix=/usr/local/apache-2.4 --sysconfdir=/etc/httpd-2.4 --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=prefor
make && make install
- 4:自带的Apache服务控制脚本:apachectl脚本在/usr/local/httpd-2.4/bin/目录下,可以将这个目录添加到环境变量中,编辑
vi /etc/profile.d/httpd.sh
, 添加export PATH=/usr/local/apache-2.4/bin/:$PATH
,那么启动的时候,就可以直接使用apachectl start
- 5:另外,一般来说需要将头文件和库文件输出
ln -sv /usr/local/apache-2.4/include /usr/include/httpd
- 6:编写服务脚本实现service httpd start 启动httpd
- 可以拷贝一个启动脚本,修改中对应的变量
cp /etc/init.d/httpd httpd2.4
vi httpd-2.4
#注释
# if [ -f /etc/sysconfig/httpd ]; then
# . /etc/sysconfig/httpd
# fi
# 修改为编译安装的apachectl路径
apachectl=/usr/local/apache-2.4/bin/apachectl
# 修改为编译安装的httpd路径
httpd=${HTTPD-/usr/local/apache-2.4/bin/httpd}
prog=httpd
# 修改为编译安装的pidfile路径
pidfile=${PIDFILE-/usr/local/apache-2.4/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd-2.4}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
虽然可以这样修改,但是服务控制脚本一定要会编写,属于shell编程部分chkconfig --add /etc/rc.d/init.d/httpd-2.4
service httpd-2.4 start
- 添加apache 用户和用户组,并且修改主配置文件指定user为apache,group为apache
- CentOS 7 编译安装php5.4安装 (编译为httpd的模块)
- 下载php-5.4.26.tar ,解压
yum install libxml2-devel libmcrypt-devel bzip2-devel curl-devel -y
- 如果MariaDB和PHP安装不在一台主机上使用:
./configure --prefix=/usr/local/php5.4 --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache-2.4/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
- 拷贝php的主配置文件:
cp php.ini-production /etc/php.ini
- 编辑httpd的主配置文件,注释中心主机,启用虚拟机,在主配置文件中添加:
include /etc/httpd-2.4/extra/httpd-vhosts.conf
include /etc/httpd-2.4/extra/httpd-php.conf
- 编辑/etc/httpd-2.4/extra/httpd-vhosts.conf文件
- 修改:
<VirtualHost *:80>
ServerAdmin 307443272@qq.com
DocumentRoot "/apps/vhosts/b.net"
ServerName www.b.net
ErrorLog "logs/b.net_error_log"
CustomLog "logs/b.net_access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 307443272@qq.com
DocumentRoot "/apps/vhosts/c.org"
ServerName www.c.org
ErrorLog "logs/c.org_error_log"
CustomLog "logs/c.org_access_log" common
</VirtualHost>
- 修改:
- 编辑/etc/httpd-2.4/extra/httpd-php.conf
- 添加:
DirectoryIndex index.php
AddType application/x-httpd-php .php
- 添加:
- 首先在/apps/vhosts/b.net中测试下php和mysql
- vi index.php
<?php
$conn=mysql_connect('192.168.10.218','root','yhy3426356');
if ($conn)
echo "ok";
else
echo "failured";
phpinfo();
?>- 显示结果为ok!!!和PHP的编译参数
- vi index.php
- 下载WordPress源码,将wordpress里面的所有的文件解压至/apps/vhosts/b.net中
- 并且在数据库中创建Wordpress的数据库,和授权一个用户给WordPress
(四)建立httpd服务器(基于编译的方式进行),要求:
提供两个基于名称的虚拟主机:
(a)www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;
(b)www2.stuX.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;
(c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;
(d)通过www1.stuX.com/server-status输出httpd工作状态相关信息,且只允许提供帐号密码才能访问(status:status)。
- 思路:提过一台CentOS 7主机,yum 安装 httpd,在主配置文件中注释中心主机,在虚拟主机中启用两个server name 。www1.stuX.com虚拟机中,错误日志设置:ErrorLog “/var/log/httpd/www1.err”, 访问日志设置:CustomLog “/var/log/httpd/www1.access” combined”。在 www1.stuX.com虚拟机中,错误日志设置:/var/log/httpd/www2.err,访问日志设置:/var/log/httpd/www2.access。并且在每一个DocumentRoot中写入index.html,内容为其对应的主机名
试验过程如下:
- (1)在主配置文件中注销中心主机
- (2)编辑子配置文件中的虚拟主机文件
vi /etc/httpd/conf.d/vhost.conf
- 添加内容如下:
<VirtualHost *:80>
ServerName www1.stuX.com
DocumentRoot "/web/vhosts/www1"
ErrorLog "/var/log/httpd/www1.err"
CustomLog "/var/log/httpd/www1.access" combined
<Directory "/web/vhosts/www1">
Options none
AllowOverride none
Require all granted
</Directory>
<Location /server-status>
SetHandler server-status
AuthType Basic
AuthName "please enter your username and password!"
AuthUserFile "/etc/httpd/conf/.htpasswd"
Require valid-user
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName www2.stuX.com
DocumentRoot "/web/vhosts/www2"
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/www2.access" combined
<Directory "/web/vhosts/www2">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
- (3)生成虚拟用户文件
htpasswd -m -c /etc/httpd/conf/.htpasswd tom
(4)测试结果
![Alt text](./屏幕快照 2017-02-13 上午12.30.23.png)
![Alt text](./屏幕快照 2017-02-13 上午12.38.25.png)
- ![Alt text](./屏幕快照 2017-02-13 上午12.49.49.png)
- ![Alt text](./屏幕快照 2017-02-13 上午12.50.03.png)
(五)为第4题中的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;
(1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(MageEdu);
(2)设置部门为Ops,主机名为www2.stuX.com,邮件为admin@stuX.com;
- (1)准备CA主机,负责签证(IP:192.168.10.219)
(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)
openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655
mkdir -pv /etc/pki/CA/{certs,crl,newcerts}
touch /etc/pki/CA/{serial,index.txt}
echo 01 > /etc/pki/CA/serial
- (2)在httpd服务器上(IP:192.168.10.216),申请签证请求
mkdir /etc/httpd/ssl
cd /etc/httpd/ssl
(umask 077;openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)
openssl req -new -key /etc/httpd/ssl/httpd.key -out httpd.csr -days 365
scp httpd.csr root@192.168.10.219:/root/
- (3)CA主机上给httpd服务器签证
openssl ca -in /root/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
scp /etc/pki/CA/certs/httpd.crt root@192.168.10.216:/etc/httpd/ssl/
- (4)在httpd服务端安装mod_ssl模块,并设置https虚拟主机
yum -y install mod_ssl
- 编辑配置文件
/etc/httpd/conf.d/ssl.conf
- 修改:
DocumentRoot "/web/vhosts/www2"
ServerName www2.stuX.com
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
<Directory "/web/vhosts/www2">
Options none
AllowOverride none
Require all granted
</Directory>
- (5)检验结果
- ![Alt text](./屏幕快照 2017-02-13 上午1.43.41.png)
(六)在LAMP架构中,请分别以php编译成httpd模块形式和php以fpm工作为独立守护进程的方式来支持httpd,列出详细的过程。
- (1)
php作为httpd的模块编译安装
- 当用户的请求到达的时候,到达web服务器,静态的内容都会在本地装载,都由httpd通过系统调用进行装载,如果装载的是静态内容,直接返回,如果是动态内容,交给httpd的php模块进行处理,将处理的结果进行返回给客户端,如果动态页面需要请求数据,那么php代码会通过网络套接字连接到MariaDB数据库。这种模式可以使用两天服务器,httpd+php 一台,MariaDB一台
- (2)
php作为php-fpm编译安装,作为一项单独的服务
- 当用户的请求到达的时候,到达web服务器,静态的内容都会在本地装载,都由httpd通过系统调用进行装载,如果装载的是静态内容,直接返回。如果是动态内容,将整个请求的URL通过fcgi模块反向代理至php-fpm服务,php-fpm进程会通过URL加载本地的动态资源路径,如果动态页面需要请求数据,那么php代码会通过网络套接字连接到MariaDB数据库。可以将三个服务分别拆分至不同的主机上