马哥教育网络班21期+第12周课程练习 ​

1、请描述一次完整的http请求处理过程;

  1)客户端和服务器端建立连接。服务器接收或者拒绝请求。
  2)服务器端接收客户端请求。接收来自于网络的请求报文中对某资源的一次请求。对请求的处理响应,可分为单进程(启动一个进程处理请求,一次只处理一个)和多进程(并行启动多个进程,每个进程处理一个请求)。
  3)服务器端处理客户端请求。对请求报文进行解析,并获取请求的资源和请求方法等相关信息。
  4)服务器访问资源。web服务器负责向请求者提供对方请求的静态资源,或动态运行后生成的资源。
  5)服务器构建响应报文。
  6)服务器发送响应报文
  7)服务器端记录日志

2、httpd所支持的处理模型有哪些,他们的分别使用于哪些环境。

prefork:多进程模型,每个进程响应一个请求。
一个主进程,生成n个子进程,每个子进程处理一个用户进程。没有用户请求时,也会预先生成多个空闲进程,随时等待请求到达。进程最大不会超过1024个。
使用于对系统要求稳定请求不是很多的环境。
worker:一个主进程,生成多个子进程,每个子进程生成多个线程,每个线程响应一个请求。会预生成n个空闲线程。
event:事件驱动模型,每个线程响应n个请求。一个主进程,生成m个子进程,每个进程处理n个请求。

3、源码编译安装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
编译安装httpd-2.4.9
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/mysq/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data
添加 innodb_file_per_table = on   skip_name_resolve = on

(2) 为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
1、添加如下二行
   AddType application/x-httpd-php  .php
   AddType application/x-httpd-php-source  .phps

 2、定位至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
新建数据库wordpress
create database wordpress;
修改root用户的密码
进入mysql数据库,use mysql;
修改root密码,update user set password=PASSWORD('magedu') where User='root';
重启msqld服务
在浏览器中输入http://192.168.194.129/wordpress
根据提示填写数据库名称,用户名,密码。完成后根据提示创建wp-config.php文件。设置admin的密码等。完成WordPress的安装。

4、建立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

<VirtualHost *:80>
DocumentRoot "/web/vhosts/www1"
ServerName www1.stuX.com
ErrorLog "/var/log/httpd/www1.err"
CustomLog "/var/log/httpd/www1.access" common
<Location /server-status>
    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
</Location>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/web/vhosts/www2"
ServerName www2.stuX.com
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/www2.access" common
</VirtualHost>

5、为第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,并把证书发还回来

6、在LAMP架构中,请分别以php编译成httpd模块形式和php以fpm工作为独立守护进程的方式来支持httpd,列出详细的过程。

题3LAMP架构中,php是以模块形式编译的。
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
(3) 安装并初始化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

(4) 为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
 1、添加如下二行
   AddType application/x-httpd-php  .php
   AddType application/x-httpd-php-source  .phps

 2、定位至DirectoryIndex index.html 
   修改为:
DirectoryIndex  index.php  index.html
3、添加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();
?>

原创文章,作者:hhaa0352,如若转载,请注明出处:http://www.178linux.com/50052

(0)
上一篇 2016-10-09 09:02
下一篇 2016-10-09 09:05

相关推荐

  • LVM

       本节主要为raid和LVM相关 一、作业 1、创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项 2、写一个脚本,完成如下功能: (1) 列出当前系统识别到的所有磁盘设备 (2) 如磁盘数量为1,则显示其空间使用信息 …

    Linux干货 2016-08-30
  • 马哥教育网络班21期+第6周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@localhost ~]#cp /etc/rc.d/rc.sysinit /tmp/ [root@localhost&nbs…

    Linux干货 2016-07-29
  • 马哥教育网络班22期+第3周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# w | tail -n+3 | cut -d" " -f1 | sort |&…

    Linux干货 2016-08-29
  • 用shell脚本打等腰三角新

       *   ***  ***** *******   预览图形 发现图形由三个变量组成 行数n 每行的空格数a 和每行的符号数b 关系是    a=n-当前行数    b=2xn-1   并且空格都在符号前 根据关系编写脚本 #!/bin/bash read…

    Linux干货 2016-08-22
  • 重定向与管道

         本次内容    1.三种I/O设备    2.把I/O从定向入文件    3.命令tr    4.使用管道链接命令    5.tee   我们都知道程序是由:指令+数据    &n…

    2017-07-23
  • 计划任务

    作业管理: Linux的作业控制(job )     前台作业:通过终端启动,且启动后一直占据终端;     后台作业:可通过终端启动,但启动后即转入后台运行(释放终端) 如何让作业运行于后台?     (1) 运行中的…

    Linux干货 2016-09-08

评论列表(1条)

  • 马哥教育
    马哥教育 2016-11-02 14:21

    博客写得非常的好,32个赞,给出了详细操作步骤,加油!