马哥教育网络班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 14:31
下一篇 2016-08-15 16:39

相关推荐

  • 107-tomcat

    一.编程语言基础  1.1 编程语言: 硬件级:微码编程,汇编语言 系统级:C,C++,…

    2016-12-02
  • 通过堡垒机代理SSH运行Ansible(译)

    有一种常见的网络安全模式是阻止私有网络外部对应用服务器的所有连接(指除了业务数据外其它的连接,如后台管理系统和内部业务系统。译者注),然后使用 DMZ 区域中的 堡垒机 来选择性的将到服务器的流量加入白名单。 我们有这样的一个服务器池,只允许来自特定 IP 地址的 SSH 流量。这些服务器还由 Ansible…

    Linux干货 2015-02-14
  • date用法心得

    Linux时间格式极大程度的遵循了人类习惯,以下为部分常见时间。
    date命令本身提供了日期的加减运算,shell编程时灵活运用

    2017-11-09
  • 文本过滤、文本查找工具应用示例

    文本过滤、文本查找工具应用示例 1.显示当前系统上root、fedora或user1用户的默认shell [root@localhost ~]# cat /etc/passwd|grep “^root\>\|^fedora\>\|^user1\>”|cut -d: -f7 /bin/bash /bin/bash /bin/bash 2.找出…

    Linux干货 2017-07-30
  • 磁盘管理与文件系统

      关于磁盘与文件系统大致思路:要想对磁盘进行充分利用,必须要对磁盘进行分区,第二步就是要对分区进行高级格式化,也就是在分区上创建文件系统,在此过程中可以对磁盘的各种属性进行自定义。打个比方来说,创建磁盘分区好比刚买来一个毛坯房,还未装修,还不适宜人居住,那么创建文件系统就好比在光秃秃的毛坯房上进行了装修,这样才更适于人居住使用。 要想搞懂磁盘,首…

    Linux干货 2016-08-30
  • 2016/10/26作业:用户和组的相关配置文件

    linux系统是通过文件来保存配置的,其中关于用户和组的配置文件包括以下几个: /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/login.defs /etc/default/useradd /etc/passwd 存放用户信息的配置文件,其基本格式如下 root:x:0:0:root:/root:/…

    Linux干货 2016-10-26

评论列表(1条)

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

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