马哥教育网络班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

相关推荐

  • keepalived高可用实例

    1、keepalived单实例 1)实验环境为VMware虚拟机,两台centos7.2,主机1IP地址为10.1.252.55,主机2IP地址为10.1.252.34,虚拟路由IP为10.1.252.246, 2)确保iptables和selinux不影响实验 (1)确保两台主机的时间同步 [root@localhost ~]# vim…

    Linux干货 2016-11-01
  • Nginx 编译安装

    简介 Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配…

    Linux干货 2016-12-01
  • 软件管理

    ldd   /path/to/binary_file       查看二进制所依赖的库文件 ldconfig  -p  查看已缓存的库文件名和文件路径 配置文件 :/etc/ld.so.conf,   /etc/ld.so.conf.d/*.conf 缓存文件:/etc/ld.so.cac…

    Linux干货 2017-06-11
  • tab键 history 命令的相关巧用

    Tab键的妙用       1.command没有完全输入时: 点击tab,如果能唯一匹配到命令,则直接补全,否则需要点击两下tab来显示能够匹配到的命令。                2./2tab : 显…

    2017-02-18
  • Nginx 常见配置解析

    结构上: 核心模块:HTTP模块、EVENT模块、MAIL模块。 基础模块:HTTP access模块、HTTP FastCGI模块、HTTP Proxy模块、HTTP Rewrite模块。 第三方模块:HTTP Upstream Request Hash模块。 功能上: Handlers…

    Linux干货 2016-12-05
  • 查找find及压缩与解压

    find  根据时间戳:以“天”为单位;              -atime [+|-]#,                #: [#,#+1)   2 那就是大于等于二天小于…

    2017-08-12

评论列表(1条)

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

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