马哥教育网络班22期+第十二周课程练习

1、请描述一次完整的http请求处理过程。
    (1) 客户端与服务端通过TCP三次握手建立或处理连接:接收请求或拒绝请求
    (2) 接收请求:接收来自于网络上的主机请求报文中对某特定资源的一次请求的过程
    (3) 处理请求:对请求报文进行解析,获取客户端请求的资源及请求方法等相关信息
    (4) 访问资源:获取请求报文中请求的资源
    (5) 构建响应报文
    (6) 发送响应报文
    (7) 记录日志
2、httpd所支持的处理模型有哪些,他们的分别使用于哪些环境。
prefork:多进程模型,每个进程响应一个请求
    一个主进程:负责生成子进程及回收子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理
    n个子进程:每个子进程处理一个请求
    工作模型:会预先生成几个空闲进程,随时等待用于响应用户请求;最大空闲和最小空闲
worker:多进程多线程模型,每线程处理一个用户请求
    一个主进程:负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理
    多个子进程:每个子进程负责生成多个线程
    每个线程:负责响应用户请求
    并发响应数量:m*n
        m:子进程数量
        n:每个子进程所能创建的最大线程数量
event:事件驱动模型,多进程模型,每个进程响应多个请求
    一个主进程:负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理
    子进程:基于事件驱动机制直接响应多个请求
3、源码编译安装LAMP环境(基于wordpress程序),并写出详细的安装、配置、测试过程。

源码编译安装LAMP环境准备:
httpd-2.4:prefork模型
mairadb-5.5:通用二进制格式(php5需要依赖于mariadb,所以得先装mariadb)
php-5.4:编译为httpd的modules

注意:首先配置好EPEL源
安装编译环境:
# yum -y groupinstall "Development Tools" "Server Platform Development"

(1)安装httpd
# yum -y install pcre-devel apr-devel apr-util-devel openssl-devel
# tar xf httpd-2.4.23.tar.bz2
# cd httpd-2.4.23
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --enable-so --enable-ssl --enable-rewrite --with-zlib 
--with-pcre --with-apr=/usr --with-apr-util=/usr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
# make && make install
# vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache24/bin:$PATH
# . /etc/profile.d/httpd.sh
# ln -sv /usr/local/apache24/include /usr/include/httpd
# apachectl start

(2)安装mairadb通用二进制格式
# useradd -r mysql
# mkdir -pv /mydata/data
# chown -R mysql:mysql /mydata/data
# tar xf mariadb-5.5.54-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-5.5.54-linux-x86_64 mysql
# cd /usr/local/mysql
# chown -R root:mysql ./*
# cp support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# mkdir /var/log/mariadb
# chown mysql:mysql /var/log/mariadb
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf,在[mysqld]段添加如下三个选项:
    datadir=/mydata/data
    innodb_file_per_table = ON
    skip_name_resolve = ON
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
测试配置文件语法:
# service mysqld configtest
# service mysqld start
# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
# ldconfig
# ln -sv /usr/local/mysql/include /usr/include/mysql

(3)安装php
# yum -y install libxml2-devel libmcrypt-devel bzip2-devel
# tar xf php-5.4.45.tar.gz
# cd php-5.4.45
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-mbstring --with-png-dir 
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-apxs2=/usr/local/apache/bin/apxs
# make && make install

# cp php-5.4.45/php.ini-production /etc/php.ini
# cp /etc/apache/httpd.conf{,.backup}
# vim /etc/apache/httpd.conf,在相应位置添加如下两个选项:
    AddType application/x-httpd-php .php
    DirectoryIndex index.php index.html
测试httpd配置文件语法:
# apachectl -t
# apachectl restart

(4)部署wordpress
# tar xf wordpress-4.7-zh_CN.tar.gz -C /usr/local/apache/htdocs
# chmod -R 777 /usr/local/apache/htdocs/wordpress
创建wordpress数据库、用户名及密码
# mysql
mysql> CREATE DATABASE wordpress;
mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY 'wordpress';
mysql> FLUSH PRIVILEGES;

清除防火墙规则:
# iptables -F
在浏览器进行访问及部署wordpress:
http://192.168.0.200/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)。

# mkdir -pv /web/vhosts/{www1,www2}
# mkdir /var/log/httpd
# echo "<h1>www1.stuX.com</h1>" > /web/vhosts/www1/index.html
# echo "<h1>www2.stuX.com</h1>" > /web/vhosts/www2/index.html
# vim /etc/apache/httpd.conf
注释中心主机 #DocumentRoot "/usr/local/apache/htdocs"
引用虚拟主机配置文件:Include /etc/apache/extra/httpd-vhosts.conf
# vim /etc/apache/extra/httpd-vhosts.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 "server-status"
        AuthUserFile "/etc/apache/.status_pwd"
        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>
# htpasswd -c /etc/apache/.status_pwd status
# apachectl -t
# apachectl restart

测试主机:
# vim /etc/hosts
192.168.20.200 www1.stuX.com www2.stuX.com
在浏览器进行访问测试:
http://www1.stuX.com
http://www2.stuX.com

5、为第4题中的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点:
   (1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(MageEdu);
   (2)设置部门为Ops,主机名为www2.stuX.com,邮件为admin@stuX.com。

构建私有CA颁发SSL证书
# (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 3650
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]:MageEdu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:www2.stuX.com
Email Address []:admin@stuX.com
# mkdir -pv /etc/pki/CA/{certs,crl,newcerts}
# touch /etc/pki/CA/{serial,index.txt}
# echo 01 > /etc/pki/CA/serial

在请求主机生成私钥,并向CA申请签署证书
# (umask 077; openssl genrsa -out /etc/apache/ssl/httpd.key 2048)
# openssl req -new -key /etc/apache/ssl/httpd.key -out /etc/apache/ssl/httpd.csr -days 365
CA签署证书
# openssl ca -in /etc/apache/ssl/httpd.csr -out /etc/pki/CA/certs/httpd.crt
# cp /etc/pki/CA/certs/httpd.crt /etc/apache/ssl/

# vim /etc/apache/httpd.conf
引用SSL配置文件:Include /etc/apache/extra/httpd-ssl.conf
加载如下模块:
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
# vim /etc/apache/extra/httpd-ssl.conf
<VirtualHost _default_:443>
DocumentRoot "/web/vhosts/www2"
ServerName www2.stuX.com
ErrorLog "/var/log/httpd/www2.ssl.err"
TransferLog "/var/log/httpd/www2.ssl.access"
<Directory "/web/vhosts/www2">
    Options None
    AllowOverride None
    Require all granted
</Directory>
SSLEngine on
SSLCertificateFile "/etc/apache/ssl/httpd.crt"
SSLCertificateKeyFile "/etc/apache/ssl/httpd.key"
</VirtualHost>
# apachectl -t
# apachectl restart

在浏览器进行访问测试:
https://www2.stuX.com

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

(1)php编译成httpd模块的形式在第3题中已实现。

(2)php编译成以fpm形式工作为独立守护进程的详细步骤如下:
注意:首先配置好EPEL源
安装编译环境:
# yum -y groupinstall "Development Tools" "Server Platform Development"

编译安装php-fpm:
# yum -y install libxml2-devel libmcrypt-devel bzip2-devel
# tar xf php-5.4.45.tar.gz
# cd php-5.4.45
# ./configure --prefix=/usr/local/phpfpm --with-mysql=/usr/local/mysql --with-openssl --enable-mbstring --with-png-dir 
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm
# make && make install

# cp php-5.4.45/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# chkconfig --add php-fpm
# cp /usr/local/phpfpm/etc/php-fpm.conf.default /usr/local/phpfpm/etc/php-fpm.conf
# vim /usr/local/phpfpm/etc/php-fpm.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
# service php-fpm start
# ss -tnl
若tcp的9000端口处于监听状态,表明php-fpm启动成功。

# vim /etc/apache/httpd.conf
    DirectoryIndex index.php index.html
加载如下模块:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# vim /etc/apache/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ServerName 192.168.0.200
    DocumentRoot "/usr/local/apache/htdocs"
    ErrorLog "/usr/local/apache/logs/httpd_fpm.error"
    CustomLog "/usr/local/apache/logs/httpd_fpm.access" combined
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1
    <Directory "/usr/local/apache/htdocs">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
# apachectl -t
# apachectl restart

在浏览器进行访问测试:
http://192.168.0.200/phpinfo.php
若Server API为FPM/FastCGI,表明php-fpm安装成功。

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

(0)
萝卜萝卜
上一篇 2016-12-29
下一篇 2016-12-30

相关推荐

  • Python修饰器的函数式编程

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西。虽然好像,他们要干的事都很相似——都是想要对一个已有的模块做一些“修饰工作”,所谓修饰工作就是想给现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小…

    Linux干货 2016-08-15
  • 8月2日作业

            1、在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@localhost test…

    Linux干货 2016-08-05
  • 2018近期it运维大会合集,这五场值得关注!

    17年,我们不仅看到Google、Facebook、Amazon、LinkedIn、Netflix、Airbnb等互联网巨头在与DevOps亲密接触,传统软件公司如Adobe、IBM、Microsoft、SAP等,亦或是网络业务非核心的苹果、沃尔玛、索尼影视娱乐、星巴克等都在采用DevOps,更看到了国内一大片企业开始钟情于此。

    2018-03-01
  • MooseFS性能图表[原创]

    对MooseFS有了一定的了解,现在可以压压它的性能了,使用的是iozone IO测试工具。测试命令为:./iozone -a -n 512m -g 4g -i 0 -i 1 -f /mnt/mfs/logs/test.tar.gz -Rb ./iozone.xls -C测试说明1、/mnt/mfs/logs/test.tar.gz大小为9.2G,大于服务器…

    Linux干货 2015-03-27
  • 第六周博客作业

                   1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; %s@^[[:space:]]\+[^[:…

    Linux干货 2016-12-05
  • shell-语句总结

    shell脚本语法总结 过程式编程语言: 顺序执行 选择执行 循环执行 shell默认是顺序执行,如果有判断或循环语句则执行判断或循环。 条件判断 if     单分支         if 判断条件:then  &nb…

    Linux干货 2016-08-21

评论列表(1条)

  • 马哥教育
    马哥教育 2017-01-04 16:13

    写的很棒,可以把tcp的四次断开也写上,如果能画个图说明一下是最好的