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

httpd学习前知识必备:

I/O类型

  • 同步和异步 synchronous asynchronous 

关注的是消息通知机制,如何通知调用者,站在被调用者的角度

同步:调用发出后不会立即返回,一旦返回即是最终结果

异步:调用发出后会立即返回消息,但不是最终结果,被调用者通过状态,通知机制,或回调函数处理结果

  • 阻塞和非阻塞 block nonblock

关注的是调用者等待调用结果时的状态,站在调用者角度

阻塞: 调用结果返回之前,调用者会被挂起,调用者只有在得到返回结果后才能继续

非阻塞: 调用结果返回之前,调用者不会被挂起,调用不会阻塞调用者,调用者不断的检查调用结果

I/O模型

  • 阻塞式blocking

磁盘数据到内核内存blocking;数据从内核内存到进程内存blocking,进程不能响应其他请求(进程被挂起)

  • 非阻塞nonblocking

磁盘数据到内核内存nonblocking;数据从内核内存到进程内存blocking,忙等待状态,进程不能响应其他请求

  • IO复用IO multiplexing

假如进程需要网络IO,磁盘IO ,进程阻塞到内核的select,epoll等函数上,没有被挂起,因此进程也可也处理网络IO或其他IO

  • 事件驱动signal driven IO

磁盘数据到内核内存nonblocking;但是有通知机制,数据从内核内存到进程内存blocking,进程可以响应其他请求

水平触发(多次通知),边缘触发(只通知一次,当进程没有收到时,之后进程可以调用相应的函数回调得到通知结果)

  • asynchronous IO AIO

进程进行IO调用,内核有通知机制,之后内核负责磁盘数据到内核内存,数据从内核内存到进程内存,进程能响应其他请求

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

  • 建立或处理连接:接收请求或拒绝请求

  • 接收请求:

  • 处理请求:对请求报文进行解析,并获取请求的资源及请求方法等相关信息

  • 访问资源:获取请求报文中请求的资源

  • 构建响应报文:

  • 发送响应报文:

  • 记录日志

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

  • prefork 多进程模型,主进程,生成多个子进程,每个子进程响应一个请求,当没有用户请求时,也

预留几个子进程等待用户请求

  • work  多线程模型,主进程,生成多个子进程,每个子进程负责产生多个线程,每个线程处理一个请求

  • event 事件驱动模型,主进程,生成多个子进程,每个子进程直接响应用户多个请求

3、源码编译安装LAMP环境(基于wordpress程序),并写出详细的安装、配置、测试过程。

  • apache配置

[root@centos apr-1.5.0]# ./configure --prefix=/usr/local/apr
[root@centos apr-util-1.5.2]# ./configure --prefix=/usr/local/apr_utils --with-apr=/usr/local/apr/
[root@centos httpd-2.4.9]# yum install *pcre* -y
[root@centos httpd-2.4.9]#  ./configure --prefix=/usr/local/apache 
--sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite
 --enable-defalte --with-zlib --with-pcre --with-apr=/usr/local/apr 
 --with-apr-util=/usr/local/apr_utils --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
修改httpd的主配置文件,添加ServerName fqdn 或 ip  PidFile "/var/run/httpd.pid" ,
其中网站根目录为 DocumentRoot "/usr/local/apache/htdocs",默认启用了中心主机
[root@centos ~]# cat /usr/local/apache/htdocs/index.html 
<html><body><h1>It works!</h1></body></html>
[root@centos ~]# curl 192.168.40.128
<html><body><h1>It works!</h1></body></html>
  • Mysql安装和配置

[root@centos local]# groupadd -r mysql
[root@centos local]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
[root@centos ~]# ln -sv  /usr/local/mysql-5.6.13 /usr/local/mysql
[root@centos mysql]# yum install cmake
[root@centos mysql]# cmake .
[root@centos mysql]# make && make install
[root@centos scripts]# chmod +x mysql_install_db.sh 
[root@centos scripts]# ./mysql_install_db.sh --user=mysql --basedir=/usr/local/mysql 
--datadir=/mydata
[root@centos support-files]# vim /etc/my.cnf 
[root@centos support-files]# service mysql start
Starting MySQL.                                            [  OK  ]
[root@centos support-files]# cat /etc/my.cnf 
basedir =/usr/local/mysql
datadir =/mydata
port =3306
# server_id = .....
# socket = .....
--------------------
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on wordpress.* to root@127.0.0.1 identified by "centos6";
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 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 --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 test
# make intall
# cp php.ini-production /etc/php.ini
# vim /etc/httpd/httpd.conf
   AddType application/x-httpd-php  .php
   AddType application/x-httpd-php-source  .phps
   DirectoryIndex  index.php  index.html
[root@centos ~]# tar zxf wordpress-4.5.3-zh_CN.tar.gz /test/
  • 测试

[root@centos test]# mv wordpress /usr/local/apache/htdocs/

之后在浏览器中输入http://web/wordpress 按照导向安装完成即可,重点编辑wp-config.php文件来定义wordpress使用的数据库,登录用户名密码等信息

QQ截图20160817102744.jpg

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);

禁用中心主机
[root@centos ~]# vim /etc/httpd24/httpd.conf
#DocumentRoot "/usr/local/apache/htdocs"
Include /etc/httpd24/extra/httpd-vhosts.conf
配置虚拟主机
[root@centos ~]# vim /etc/httpd24/extra/httpd-vhosts.conf 
<VirtualHost 192.168.40.128:80>
    DocumentRoot "/web/vhost/www1/"
    ServerName www1.test.com
    ErrorLog "/var/log/httpd/www1.err"
    CustomLog "/var/log/httpd/www1.access" common
<Directory "/web/vhost/www1">
 AllowOverride None
    Options None
    Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.40.128:80>
    DocumentRoot "/web/vhost/www2/"
    ServerName www2.stuX.com
    ErrorLog "/var/log/httpd/www2.err"
    CustomLog "/var/log/httpd/www2.access" common
<Directory "/web/vhost/www2/">
 AllowOverride None
    Options None
    Require all granted
</Directory>
</VirtualHost>
[root@centos ~]# vim /etc/hosts
127.0.0.1 www1.stuX.com www2.stuX.com
[root@centos ~]# mkdir -p /web/vhost/{www1,www2} && echo "www1.site" > /web/vhost/www1/index.html && echo "www2.site" 
> /web/vhost/www2/index.html
[root@centos ~]# /usr/local/apache/bin/apachectl restart
[root@centos ~]# curl www1.stuX.com
www1.site
[root@centos ~]# curl www2.stuX.com
www2.site
更改第二个虚拟主机为下面所示
<VirtualHost 192.168.40.128:80>
    DocumentRoot "/web/vhost/www2/"
    ServerName www2.stuX.com
    ErrorLog "/var/log/httpd/www2.err"
    CustomLog "/var/log/httpd/www2.access" common
<Directory "/web/vhost/www2/">
 AllowOverride None
    Options None
    Require all granted
</Directory>
<Location /server-status>
AuthType Basic
AuthName "Only for Admin"
AuthUserFile "/usr/local/apache/.htpasswd"
Require valid-user
SetHandler server-status
 AllowOverride None
    Options None   
</Location>
</VirtualHost>
[root@centos bin]# ./htpasswd -m -c /usr/local/apache/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admin
[root@centos bin]# curl www2.stuX.com/server-status
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

5、为第4题中的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;

   (1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(MageEdu);

   (2)设置部门为Ops,主机名为www2.stuX.com,邮件为admin@stuX.com;

[root@centos CA]# touch index.txt serial
[root@centos CA]# echo 01 > serial 
[root@centos CA]# (umask 077;openssl genrsa -out ./private/cakey.pem 1024)
Generating RSA private key, 1024 bit long modulus
.......................................................++++++
.......++++++
e is 65537 (0x10001)
[root@centos CA]# openssl req -new  -x509 -key ./private/cakey.pem -out ./cacert.pem
[root@centos CA]# cd /usr/local/apache/
[root@centos apache]# mkdir ssl
[root@centos apache]# (umask 077;openssl genrsa -out ./ssl/http.key 1024)
[root@centos CA]# cd /usr/local/apache/
[root@centos apache]# mkdir ssl
[root@centos apache]# (umask 077;openssl genrsa -out ./ssl/http.key 1024)
Generating RSA private key, 1024 bit long modulus
..................................++++++
.......................................++++++
e is 65537 (0x10001)
[root@centos apache]# openssl req -new -key ./ssl/http.key -out ./ssl/http.crst -days 3600
[root@centos apache]# openssl ca -in ./ssl/http.crst -out ./ssl/http.crt -days 3600
[root@centos ~]# cd /usr/local/apache/ssl/
[root@centos ssl]# ls
http.crst  http.crt  http.key
之后修改/etc/httpd24/httpd.conf
LoadModule ssl_module modules/mod_ssl.so
Include /etc/httpd24/extra/httpd-ssl.conf
然后编辑/etc/httpd24/extra/httpd-ssl.conf如下
<VirtualHost _default_:443>
    DocumentRoot "/web/vhost/www2/"
    ServerName www2.stuX.com
    ErrorLog "/var/log/httpd/www2.err"
    CustomLog "/var/log/httpd/www2.access" common
<Directory "/web/vhost/www2/">
 AllowOverride None
    Options None
    Require all granted
</Directory>
SSLCertificateFile "/usr/local/apache/ssl/http.crt"
SSLCertificateKeyFile "/usr/local/apache/ssl/http.key"
</VirtualHost>

测试

QQ截图20160817160801.jpg

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

  • 把php编译成httpd以fpm工作为独立守护进程的方式来支持httpd,需要在上例编译http的基础上修改和配置php编译时要加上–enable-fpm选项

[root@centos ~]# cd /usr/local/php-5.6.24/sapi/fpm/
[root@centos fpm]# ls
config.m4       init.d.php-fpm.in  php-fpm.8        php-fpm.service     tests
CREDITS         LICENSE            php-fpm.8.in     php-fpm.service.in
fpm             Makefile.frag      php-fpm.conf     status.html
init.d.php-fpm  php-fpm            php-fpm.conf.in  status.html.in
[root@centos fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@centos php-5.6.24]# chkconfig --add php-fpm
[root@centos php-5.6.24]# chkconfig php-fpm on
[root@centos fpm]# cd /usr/local/php/etc/
[root@centos etc]# ls
pear.conf  php-fpm.conf.default
[root@centos etc]# mv php-fpm.conf.default php-fpm.conf
[root@centos etc]# service php-fpm start
Starting php-fpm  done
[root@centos etc]# ss -antl    
LISTEN      0      128               127.0.0.1:9000                     *:*     
[root@centos htdocs]# cp test.php /web/vhost/www2/
#LoadModule php5_module        modules/libphp5.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
在上面的例子的www2.stuX.虚拟主机站点添加下面两行
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/vhost/www2/$1
  • 测试

QQ截图20160818125532.jpg

  • 总结 关于http,php,mysql编译选项,由于篇幅有限请多–help了解

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

(0)
SnooSnoo
上一篇 2016-08-15
下一篇 2016-08-15

相关推荐

  • 学习宣言

    别人笑我太疯癫,我笑他人看不穿。不见五陵豪杰墓,无花无酒锄作田。

    Linux干货 2016-10-24
  • N21沉舟第六周作业

    N21沉舟第六周作业 1、  复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; cp /etc/rc.d/rc.sysinit /tmp vim /tmp/rc.sysinit %s/^[[:space:]]\+/#&…

    Linux干货 2016-08-08
  • 26期全程班-第二周博客作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     cp  复制命         ex:cp -a /etc/initab /var/log/message /tmp/   …

    Linux干货 2017-01-10
  • M20 – 1- 第三周博客:Linux中的权限以及特殊权限

    上一篇讲到了用户与用户组,相信大家都明白了Linux中用户与用户组的含义和用处,那接下来讲的就是Linux中设置用户与用户组有使用权限。 Linux用户权限 何为权限,权限(privilege)是指某个特定的用户具有特定的系统资源使用权力,而权限又有哪几种呢。 Linux用户中主要针对三类着三种权限:读、写、执行,而Linux中存在着目录与文件,而目录的权限…

    Linux干货 2016-08-08
  • shell脚本3——循环语句

    流程控制:      顺序执行      选择执行      循环执行 循环执行:      将某代码段重复运行多次      重复运行多少次:  &…

    Linux干货 2016-08-18
  • 马哥教育21期网络班—第11周课程+练习—-成长进行时–不退步–上

    1、详细描述一次加密通讯的过程,结合图示最佳。 对称加密: 加密和解密使用同一个密钥; 缺点:如何通信方多的话,需要保存多组密钥 公钥加密:密钥是成对儿出现 公钥:公开给所有人;pubkey 私钥:自己留存,必须保证其私密性;secret key 特点:用公钥加密的数据,只能使用与之配对儿的私钥解密;反之亦然; 数字签名:主要在于让接收方确认发送方…

    Linux干货 2016-09-19

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-22 15:06

    写的很好,排版还可以在漂亮一点,加油