1、请描述一次完整的http请求处理过程;
(1)建立和处理连接:接收请求或者拒绝请求;
(2)接收请求:接收来自于网络上的主机请求报文中对某特定的资源的一次请求的过程;
(3)处理请求:对请求报文进行解析,获取客户端请求的资源及请求方法等相关信息
(4)访问资源:获取请求报文中请求的资源
(5)构建响应报文;
(6)发送响应报文;
(7)记录日志;
2、httpd所支持的处理模型有哪些,他们的分别使用于哪些环境。
prefork:多进程模型,每个进程响应一个请求;
一个主进程:负责生成子进程及回收子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理;
n个子进程:每个子进程处理一个请求;
工作模型:会预先生成几个空闲进程,随时等待用于响应用户请求;最大空闲和最小空闲;
适用场景:它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求,适合于并发量适中而又追求稳定的用户使用。
worker:多进程多线程模型,每线程处理一个用户请求;
一个主进程:负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理;
多个子进程:每个子进程负责生成多个线程;
每个线程:负责响应用户请求;
并发响应数量:m*n
m:子进程数量
n:每个子进程所能创建的最大线程数量;
适用场景:占据更少的内存,高并发下表现更优秀。
event:事件驱动模型,多进程模型,每个进程响应多个请求;
一个主进程 :负责生成子进程;负责创建套接字;负责接收请求,并将其派发给某子进程进行处理;
子进程:基于事件驱动机制直接响应多个请求;
适用场景:event模型是三种模型中效率最高的一种,可以突破10K的限制(即并发数1W),对海量的系统特别适用。
3、源码编译安装LAMP环境(基于wordpress程序),并写出详细的安装、配置、测试过程。
1.httpd源码编译
(1) apr-1.4+ # ./configure --prefix=/usr/local/apr # make && make install (2) apr-util-1.4+ # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr # make && make install (3) httpd-2.4 # ./configure --prefix=/usr/local/apache24 --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork # make && make install (4)pcre-devel # yum install pcre-devel -y (5) openssl # yum install openssl-devel -y (6)导出二进制程序目录至PATH环境变量中 vim /etc/profile.d/httpd.sh export PATH=/usr/local/apache24/bin:$PATH (7)导出httpd头文件 #ln -s /usr/local/apache24/include/ /usr/include/httpd/ (8)添加chkconfig启动服务 [root@www apache24]# cd /etc/rc.d/init.d/ [root@www init.d]# cp httpd httpd24 [root@www init.d]# vim httpd24 #if [ -f /etc/sysconfig/httpd ]; then //将此行注释掉 . /etc/sysconfig/httpd #fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules may not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/local/apache24/bin/apachectl //修改路径 httpd=${HTTPD-/usr/local/apache24/bin/httpd} //修改路径 prog=httpd pidfile=${PIDFILE-/usr/local/apache24/logs/httpd.pid} //修改路径 lockfile=${LOCKFILE-/var/lock/subsys/httpd24} //修改路径 RETVAL=0 STOP_TIMEOUT=${STOP_TIMEOUT-10} [root@www init.d]# chkconfig --add httpd24 [root@www init.d]# chkconfig --list httpd24 httpd24 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@www init.d]# service httpd24 start Starting httpd: [ OK ] [root@www init.d]# service httpd24 status httpd (pid 37687) is running... [root@www init.d]# ss -ntl |grep 80 LISTEN 0 128 :::80 :::*
2.mysql二进制安装
1.创建mysql用户与组 [root@www mysql]# id mysql uid=27(mysql) gid=27(mysql) groups=27(mysql) 2.创建Mysql目录并修改属主与属组 [root@localhost ~]# mkdir -pv /mydata/data/ [root@localhost mysql]# chown mysql:mysql /mydata/data/ 3.解压mysql安装包到/usr/local [root@www mnt]# tar -zxf mysql-5.5.54-linux2.6-x86_64.tar.gz -C /usr/local/ 4.创建软连接 [root@www local]# ln -sv mysql-5.5.54-linux2.6-x86_64 mysql 5.修改/usr/local/mysql/目录下所有文件和子目录的属主,属组: [root@localhost mysql]# chown -R root:mysql ./* 6.创建/etc/mysql/目录,并复制 support-files/my-large.cnf到/etc/mysql/目录下,更名为my.cnf [root@localhost mysql]# mkdir /etc/mysql [root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf 7.编辑/etc/mysql/my.cnf文件: [root@localhost mysql]# vim /etc/mysql/my.cnf (在[mysqld]段下面加入以下三行) datadir = /mydata/data skip_name_resolve = ON innodb_file_per_table = ON 8.初始化mysql数据库: [root@localhost mysql]# scripts/mysql_install_db –user=mysql –datadir=/mydata/data 9.创建mysql服务启动脚本: [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql]# chkconfig –add mysqld 10.把/etc/my.cnf文件移到/tmp目录下: [root@localhost mysql]# mv /etc/my.cnf /tmp/ 11.启动mysql服务: [root@localhost mysql]# service mysqld start 12.修改PATH变量: [root@localhost mysql]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH 13.重载配置文件: [root@localhost mysql]# . /etc/profile.d/mysql.sh 14.添加/usr/local/mysql/lib/目录下的库文件到系统搜索路径下: [root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib 15.重新加载模块,并查看 [root@localhost mysql]# ldconfig [root@www local]# ldconfig -p |grep mysql libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient_r.so.16 libmysqlclient.so.18 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.18 libmysqlclient.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.16 libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so
3.php源码编译
1.安装依赖包 [root@www php-5.4.26]# yum install libxml2-devel libmcrypt-devel 2.检查 [root@www 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-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 3.编译并安装 [root@www php-5.4.26]# make -j 4 && make install 4.复制配置文件至/etc/php.ini [root@www php-5.4.26]# cp php.ini-production /etc/php.ini 5.修改/etc/httpd.conf [root@localhost httpd24]# vim httpd.conf 加入下面两行: DirectoryIndex index.php index.html AddType application/x-httpd-php .php 6.新建一个php页面测试 [root@localhost httpd24]vim /usr/local/apache24/htdocs/index.php <?php phpinfo() ?> 7.测试成功的话,再进行测试与mysql连接 [root@localhost httpd24]vim /usr/local/apache24/htdocs/index.php <?php $conn = mysql_connect('127.0.0.1','root',''); if ($conn) echo "ok"; else echo "failure"; ?>
4.wordpress源码编译
1.切换到/usr/local/apache24/htdocs/目录下: [root@localhost ~]# cd /usr/local/apache24/htdocs/ 2.解压缩: [root@localhost htdocs]# tar -xf wordpress-4.5.3-zh_CN.tar.gz 3.切换到wordpress目录下: [root@localhost htdocs]# cd wordpress/ 复制一份wordpress配置文件: 4.[root@localhost wordpress]# cp wp-config-sample.php wp-config.php 登陆mariadb数据数据库: 5.[root@localhost wordpress]# mysql 授权用户: ariaDB [(none)]> grant all on wpdb.* to wpuser@'%' identified by '123456'; 创建数据库: MariaDB [(none)]> create database wpdb; 6.编辑wo-config.php文件; [root@localhost wordpress]# vim wp-config.php /** WordPress数据库的名称 */define('DB_NAME', 'wpdb'); /(修改为wpdb数据库) /** MySQL数据库用户名 */ define('DB_USER', 'wpuser'); (修改为wpuser用户) /** MySQL数据库密码 */define('DB_PASSWORD', '123456'); /(修改密码为123456) /** MySQL主机 */define('DB_HOST', '192.168.180.130'); /(修改192.168.180.130这台主机可以访问mysql) 至此所有配置完成。 7.可通过网页http://192.168.180.130/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);
1.创建目录与日志文件 [root@www ~]# mkdir /web/vhosts/www{1,2} -p [root@www ~]# touch /var/log/httpd/www{1,2}.{err,access} [root@www ~]# vim /web/vhosts/www1/index.html <p> www1 </p> [root@www ~]# vim /web/vhosts/www2/index.html <p> www2 </p>
2.编辑httpd主配置文件/etc/httpd/conf/httpd.conf
NameVirtualHost 192.168.2.164:80 <VirtualHost 192.168.2.164:80> DocumentRoot /web/vhosts/www1 ServerName www.a.com ErrorLog "/var/log/httpd/www1.err" CustomLog "/var/log/httpd/www1.access" combind <Location /server-status> SetHandler server-status Authtype Basic Authname "status area" AuthUserFiLE "/etc/httpd/users/.htpasswd" Require valid-user </Location> </VirtualHost> <VirtualHost 192.168.2.164:80> DocumentRoot /web/vhosts/www2 ServerName www.b.com ErrorLog "/var/log/httpd/www2.err" CustomLog "/var/log/httpd/www2.access" combind </VirtualHost>
3.创建用户认证文件
[root@www ~]# mkdir /etc/httpd/users [root@www ~]# htpasswd -c -m /etc/httpd/users/.htpasswd tom New password: Re-type newpassword: Adding passwordfor user status [root@www ~]## service httpd restart
5、为第4题中的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;
(1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(MageEdu);
(2)设置部门为Ops,主机名为www2.stuX.com,邮件为admin@stuX.com;
1.创建私有CA
(1)生成私钥 [root@localhost ~]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096) Generating RSA private key, 4096 bit long modulus .......................++ .........................................................................................++ e is 65537 (0x10001) (2)生成自签名证书 [root@localhost ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655 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 (3)为CA提供所需要的文件和目录 [root@localhost ~]# mkdir -pv /etc/pki/CA/{certs,crl,newcerts} [root@localhost ~]# touch /etc/pki/CA/{serial,index.txt} [root@localhost ~]# echo 01 > /etc/pki/CA/serial
2.http服务器向CA申请证书
(1) 用到证书的主机生成私钥; [root@localhost ~]# mkdir /etc/httpd/ssl [root@localhost ~]# cd /etc/httpd/ssl [root@localhost ssl]# (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048) (2) 生成证书签署请求 ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365 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 Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:Ops (3) 将请求通过可靠方式发送给CA主机; (4) 在CA主机上签署证书; ~]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365 Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details: Serial Number: 1 (0x1) Validity Not Before: Dec 11 16:31:18 2016 GMT Not After : Dec 11 16:31:18 2017 GMT Subject: countryName = CN stateOrProvinceName = HA organizationName = MageEdu organizationalUnitName = Ops commonName = www2.stuX.com emailAddress = admin@stuX.com X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 42:64:3B:CE:2B:0D:D8:0A:33:00:61:89:F4:78:49:8E:17:18:95:FC X509v3 Authority Key Identifier: keyid:E4:B9:8C:C4:EB:B8:72:7F:6A:87:37:0F:A6:54:04:4A:CE:B2:90:8E Certificate is to be certified until Dec 11 16:31:18 2017 GMT (365 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated
3.配置http服务器加密传输
1.安装ssl模块 [root@localhost certs]# yum install mod_ssl -y [root@localhost certs]# rpm -q mod_ssl mod_ssl-2.2.15-29.el6.centos.x86_64 2.修改/etc/httpd/conf.d/ssl.conf Listen 443 <VirtualHost 192.168.180.130:443> DocumentRoot /web/vhosts/www2 ServerName www2.stuX.com:443 SSLEngine on SSLCertificateFile /etc/pki/CA/certs/httpd.crt SSLCertificateKeyFile /etc/httpd/ssl/httpd.key </VirtualHost> 3.重启httpd服务 [root@localhost ssl]# service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName [ OK ]
6、在LAMP架构中,请分别以php编译成httpd模块形式和php以fpm工作为独立守护进程的方式来支持httpd,列出详细的过程
原创文章,作者:a295053193,如若转载,请注明出处:http://www.178linux.com/62841
评论列表(1条)
写的很好,按照你的步骤来,完全可以搭建出来想要的东西,排版也很棒,继续保持