一、请描述一次完整的http请求处理过程
(1).建立或处理连接:接收请求或拒绝请求;
(2).接收请求:接收来自于网络上的主机请求报文中对某特定资源的一次请求的过程;
接收请求的模型:
并发访问响应模型:
单进程I/O模型:启动一个进程处理用户请求;这意味着,一次只能处理一个请求,多个请求被串行响应;
多进程I/O结构:并行启动多个进程,每个进程响应一个请求;
复用的I/O结构:一个进程响应n个请求;
多线程模式:一个进程生成n个线程,一个线程处理一个请求;
事件驱动(event-driven):一个进程直接n个请求;
复用的多进程I/O结构:启动多个(m)个进程,每个进程生成(n)个线程;
响应的请求的数量:m*n
(3).处理请求:对请求报文进行解析,获取客户端请求的资源及请求方法等相关信息;
处理请求:分析请求报文的http请求报文首部http协议:http请求报文首部 http响应报文首部请求报文首部的格式。
<method><URL><VERSION>
HEADERS:(name:value)
<request body>
(4).访问资源:获取请求报文中请求的资源;
访问资源:获取请求报文中请求的资源。web服务器,即存放了web资源的主机,负责向请求者提供对方请求的静态资源,或动态资源运行生成的结果;这些资源通常应该放置于本地文件系统某路径下;此路径称为DocRoot
(5).构建响应报文:
(6).发送响应报文:
(7).记录日志:
二、httpd所支持的处理模型有哪些,他们的分别使用于哪些环境
MPM:Multipath processing Modules (多路处理模块)
(1).prefork:多进程模型,每个进程响应一个请求;
一个主进程:负责生成子进程及回收子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理;
n个子进程:每个子进程处理一个请求;
工作模型:会预先生成几个空闲进程,随时等待用于响应用户请求;最大空闲和最小空闲;
(2).worker:多进程多线程模型,每线程处理一个用户请求;
一个主进程:负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理;
多个子进程:每个子进程负责生成多个线程;
每个线程:负责响应用户请求;
并发响应数量:m*n
m:子进程数量
n:每个子进程所能创建的最大线程数量;
(3).event:事件驱动模型,多进程模型,每个进程响应多个请求;
一个主进程 :负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理;
子进程:基于事件驱动机制直接响应多个请求;
httpd-2.2: 仍为测试使用模型;
httpd-2.4:event可生产环境中使用;
三、源码编译安装LAMP环境(基于wordpress程序),并写出详细的安装、配置、测试过程。
首先安装开发环境:
yum groupinstall “Development Tools” “Server Platform Development” -y
1.编译安装Apache
解决依赖关系
1).编译安装apr
tar xf apr-1.5.0.tar.bz2
cd apr-1.5.0
./configure –prefix=/usr/local/apr
make && make install
2).编译安装apr-util
tar xf apr-util-1.5.3.tar.bz2
cd apr-util-1.5.3
./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr
make && make install
3).httpd-2.4.9编译过程也要依赖于pcre-devel软件包,需要事先安装。
yum install -y pcre-devel
tar xf httpd-2.4.9.tar.bz2
cd httpd-2.4.9
./configure –prefix=/usr/local/apache –sysconfdir=/etc/httpd24 –enable-so –enable-ssl –enable-cgi –enable-rewrite –with-zlib –with-pcre –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –enable-modules=most –enable-mpms-shared=all –with-mpm=prefork
make && make install
提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:
#!/bin/bash
#
# httpdStartup script for the Apache HTTP Server
#
# chkconfig: – 85 15
# description: Apache is a World Wide Web server. It is used to serve
#HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-“C”}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=””
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based “worker” MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $”Starting $prog: ”
LANG=$HTTPD_LANG daemon –pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $”Stopping $prog: ”
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $”Reloading $prog: ”
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $”not reloading due to configuration syntax error”
failure $”not reloading $httpd due to configuration syntax error”
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $”Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}”
exit 1
esac
exit $RETVAL
由于脚本中涉及到pid的设置,在/etc/httpd24/httpd.conf主配置文件中添加
PidFile “/var/run/httpd.pid”
给脚本执行权限
chmod +x /etc/rc.d/init.d/httpd
添加服务到开机启动里
chkconfig –add httpd
chkconfig httpd on
完成操作后就可以使用service httpd start/stop 等命令来起停服务。
2.编译安装MySQL
添加mysql用户
groupadd –r mysql
useradd –g mysql –r –s /sbin/nologin –M –d /mydata/data mysql
创建数据存放目录 /mydata/data
mkdir –pv /mydata/data
修改目录所属主及所属组
chown –R mysql:mysql /mydata/data
1). 安装并初始化mysql-5.5.33
tar xf mysql-5.5.33-linux2.6-x86_64-C /usr/local
cd /usr/local/
ln -sv mysql-5.5.33-linux2.6-x86_64 mysql
cd mysql
chown -R root.mysql .
2).执行mysql初始化:
/usr/local/mysql/scripts/mysql_install_db –datadir=/mydata/data –user=mysql
为mysql提供主配置文件
mkdir /etc/mysql
cd /usr/local/mysql
cp support-files/my-large.cnf /etc/mysql/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data
添加
innodb_file_per_table = on skip_name_resolve = on
3). 为mysql提供sysv服务脚本
cd /usr/local/mysql
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
chkconfig –add mysqld
chkconfig mysqld on
3. 编译安装php-5.4.26
解决依赖关系
yum -y install bzip2-devel libmcrypt-devel libxml2-devel
1). 编译安装php-5.4.26
tar xf php-5.4.26.tar.bz2
cd php-5.4.26
./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-openssl –with-mysqli=/usr/local/mysql/bin/mysql_config –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –with-apxs2=/usr/local/apache/bin/apxs –with-mcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2 –enable-maintainer-zts
make && make install
为php提供配置文件:
cp php.ini-production /etc/php.ini
2). 编辑apache配置文件/etc/httpd24/httpd.conf,以使apache支持php
a、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
b、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
测试页面:在/usr/local/apache/htdoc/下添加index.php页面
<?php
phpinfo();
?>
4.wordpress程序的安装
解压WordPress到/usr/local/apache/htdoc/wordpress
unzip wordpress-3.3.1-zh_CN.zip –d /usr/local/apache/htdoc/wordpress
启动msyql服务,进入mysql
systemctl start httpd.service
cd /usr/local/mysql/bin
./mysql -uroot -p
新建数据库wordpress
create database wordpress;
修改root用户的密码
进入mysql数据库,use mysql;
修改root密码,update user set password=PASSWORD(‘magedu’) where User=’root’;
重启msqld服务
在浏览器中输入http://192.168.4.240/wordpress
根据提示填写数据库名称,用户名,密码。完成后根据提示创建wp-config.php文件。设置admin的密码等。完成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);
配置文件位置
首先要在主配置文件中找到Include /etc/httpd24/extra/httpd-vhosts.conf,去掉其前面的注释,或者复制一行,使得配置文件能读取到vhosts.conf文件。
访问控制命令:
htpasswd –c –m /etc/httpd24/extra/.htpasswd status
/etc/httpd24/extra/httpd-vhosts.conf
DocumentRoot “/web/vhosts/www1”
ServerName www1.stuX.com
ErrorLog “/var/log/httpd/www1.err”
CustomLog “/var/log/httpd/www1.access” common
SetHandler server-status
Order deny,allow
Deny from all
Allow from 192.168.194
Options None
AllowOverride None
AuthType Basic
AuthName “Warring~~~~~~~~~~~”
AuthUserFile “/etc/httpd24/extra/.htpasswd”
Require user status
DocumentRoot “/web/vhosts/www2”
ServerName www2.stuX.com
ErrorLog “/var/log/httpd/www2.err”
CustomLog “/var/log/httpd/www2.access” common
五、为第4题中的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;
(1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(MageEdu);
(2)设置部门为Ops,主机名为www2.stuX.com,邮件为admin@stuX.com;
在主配置文件/etc/httpd24/httpd.conf中找到Include /etc/httpd24/extra/httpd-ssl.conf,取消注释或者复制一行。
相关的模块LoadModule ssl_module modules/mod_ssl.so取消注释
配置文件/etc/httpd24/extra/httpd-ssl.conf
修改配置文件
DocumentRoot “/web/vhosts/www2”
ServerName www2.stuX.com:443
SSLCertificateKeyFile “/etc/httpd24/ssl/httpd.key”
SSLCertificateFile “/etc/httpd24/ssl/www2.httpd.crt”
在/etc/httpd24/ 创建目录ssl
mkdir /etc/httpd24/ssl
生成证书请求
umask 077; openssl genrsa -out /etc/httpd24/ssl/httpd.key 2048
openssl req -new -key /etc/httpd24/ssl/httpd.key -days 365 -out /etc/httpd24/ssl/httpd.csr
openssl req -new -key /etc/httpd24/ssl/httpd.key -days 365 -out /etc/httpd24/ssl/httpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:MagEdu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server’s hostname) []:www2.stuX.com
Email Address []:admin@stuX.com
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
将请求文件发送给CA,在CA服务器上生成www2.httpd.crt,并把证书发还回来
六、在LAMP架构中,请分别以php编译成httpd模块形式和php以fpm工作为独立守护进程的方式来支持httpd,列出详细的过程。
php以fpm方式工作的LAMP架构,编译过程如下:
首先安装开发环境:
yum groupinstall “Development Tools” “Server Platform Development” -y
1. 编译安装Apache
1). 编译安装apr,apr-util
tar xf apr-1.5.0.tar.bz2
cd apr-1.5.0
./configure –prefix=/usr/local/apr
make && make install
tar xf apr-util-1.5.3.tar.bz2
cd apr-util-1.5.3
./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr
make && make install
2).编译安装Apache
tar xf httpd-2.4.9.tar.bz2
cd httpd-2.4.9
./configure –prefix=/usr/local/apache –sysconfdir=/etc/httpd24 –enable-so –enable-ssl –enable-cgi –enable-rewrite –with-zlib –with-pcre –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –enable-modules=most –enable-mpms-shared=all –with-mpm=prefork
# make && make install
3).提供SysV服务脚本/etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpdStartup script for the Apache HTTP Server
#
# chkconfig: – 85 15
# description: Apache is a World Wide Web server. It is used to serve \
#HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-“C”}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=””
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based “worker” MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $”Starting $prog: ”
LANG=$HTTPD_LANG daemon –pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $”Stopping $prog: ”
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $”Reloading $prog: ”
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $”not reloading due to configuration syntax error”
failure $”not reloading $httpd due to configuration syntax error”
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $”Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}”
exit 1
esac
exit $RETVAL
给脚本执行权限
chmod +x /etc/rc.d/init.d/httpd
将服务加入运行级别
chkconfig –add httpd
修改httpd的主配置文件,设置其Pid文件的路径
编辑/etc/httpd24/httpd.conf,添加如下行即可:
PidFile “/var/run/httpd.pid”
2. 编译安装mysql
添加mysql用户
groupadd –r mysql
useradd –g mysql –r –s /sbin/nologin –M –d /mydata/data mysql
创建数据存放目录 /mydata/data
mkdir –pv /mydata/data
修改目录所属主及所属组
chown –R mysql:mysql /mydata/data
1). 安装并初始化mysql-5.5.33
tar xf mysql-5.5.33-linux2.6-x86_64-C /usr/local
cd /usr/local/
ln -sv mysql-5.5.33-linux2.6-x86_64 mysql
cd mysql
chown -R root.mysql .
2).执行mysql初始化:
/usr/local/mysql/scripts/mysql_install_db –datadir=/mydata/data –user=mysql
为mysql提供主配置文件
mkdir /etc/mysql
cd /usr/local/mysql
cp support-files/my-large.cnf /etc/mysq/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data
添加 innodb_file_per_table = on skip_name_resolve = on
3). 为mysql提供sysv服务脚本
cd /usr/local/mysql
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
chkconfig –add mysqld
chkconfig mysqld on
3. 编译安装fpm方式工作的php
tar xf php-5.4.26.tar.bz2
cd php-5.4.26
./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-openssl –with-mysqli=/usr/local/mysql/bin/mysql_config –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –enable-fpm –with-mcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2
其中—enable-fpm 选项指定php为使用fpm工作方式
make
make intall
1).配置php-fpm
为php-fpm提供SysV init脚本,并将其添加至服务列表:
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
给脚本执行权限
chmod +x /etc/rc.d/init.d/php-fpm
将脚本加入运行级别并设置为开机启动
chkconfig –add php-fpm
chkconfig php-fpm on
为php-fpm提供配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php/var/run/php-fpm.pid
接下来就可以启动php-fpm了:
service php-fpm start
默认情况下,fpm监听在127.0.0.1的9000端口
netstat -nltp | grep php-fpm
tcp0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 10720/php-fpm
2).配置httpd-2.4.9
启用httpd的相关模块
在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持php格式的主页
# vim /etc/httpd/httpd.conf
a、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
b、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
c、添加php页面重定向,使得php页面的请求都转到php-fpm服务
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1
在/usr/local/apache/htdocs/index.php里添加测试代码,测试php
<?php
phpinfo();
?>
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/88356
评论列表(1条)
写的不错,搭建过程很清晰,mysql连接那一步应该写上。以后注意。