马哥教育网络班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基于密钥的认证

    生成密钥对儿: [root@Ams ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa):  Enter passphrase (empty for no passph…

    Linux干货 2016-08-02
  • 磁盘配额的操作步骤

    磁盘配额 操作步骤: 1、创建一个10G的分区 /dev/sdd1并将其格式化,挂载 2、如果是新硬盘便不存在同步问题。旧硬盘得使用命令partx  -a /dev/sdd1 3、临时创建挂载文件夹 /mnt/home    4、将/home/*  mv  到 /mnt/home 中,再将/dev/…

    Linux干货 2016-09-01
  • 很认真的第十三周作业哦

    1、建立samba共享,共享目录为/data,要求:(描述完整的过程) mkdir /data #建立共享目录 1)共享名为shared,工作组为magedu vim /etc/samba/smb.conf 修改 workgroup = MYGROUP 为 workgroup = magedu 修改 passdb backend = tdbsam 为 pas…

    Linux干货 2017-03-27
  • 学习笔记–权限管理

    文件系统的权限管理 1、Linux系统下每个目录或文件的权限对象主要分为:属主(u)、属组(g)、其他用户(o),针对每类访问对象都定义了三种权限:读(r)、写(w)、执行(x),这三种权限针对目录和文件的作用各不相同,以下将做详细说明: 对于文件来说: 读(r)权限:可以使用文本查看类工具读取文件内容 写(w)权限:可以修改文件内容,但不能删除文件本身 执…

    Linux干货 2016-08-05
  • 第二周作业

    一、文件管理类命令     1、cp:复制文件        单文件复制cp source dest,如果dest存在且dest不是目录,则覆盖目标文件。如果dest不存在,则自行创建dest文件;如果source是目录,则cp命令不会执行。如果source是链接文件,则复制的是链接文件指向的文件内…

    Linux干货 2016-12-11
  • 互联网安全之iptables/netfilter入门到进阶

    随着互联网技术的方兴未艾,各种网络应用层出不穷,网络攻击、黑客入侵也成了网民畅游互联网的心头大患,互联网安全也愈加受到了人们的重视。网络防火墙,作为一种简单高效的互联网防御手段,逐渐成为了网民畅游网络世界的保护伞。下面笔者介绍下Linux系统的守卫者——iptables/netfilter。  一 兄弟齐心,其利断金  iptables/netfilter就…

    Linux干货 2017-05-06

评论列表(1条)

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

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