马哥教育网络班21期+第12周课程练习
1、请描述一次完整的http请求处理过程;
1)建立或处理连接:客户端发送http请求报文,服务器端接收或拒绝请求; 2)接收请求:服务器端接收来自客户端对某些资源的请求; 3)处理请求:服务器端解析客户端请求报文,获取客户端请求的资源及请求方法等信息; 4)访问资源:服务器端获取客户端请求的资源; 5)构建响应报文; 6)发送响应报文; 7)日志记录;
2、httpd所支持的处理模型有哪些,他们的分别使用于哪些环境。
1)prefork: 多进程模型,每个进程响应一个请求; 一个主进程:负责生成及回收子进程、创建套接字、接收请求、派发请求给子进程; 多个子进程:负责处理来自主进程派发的客户端请求,每个子进程处理一个请求; 工作模式:服务器端会预先生成几个空闲进程,用于响应客户端请求; 可以在配置文件中设置最大及最小空闲子进程数目; 2)worker: 多进程多线程模型,每个线程响应一个请求; 一个主进程:负责生成及回收子进程、创建套接字、接收请求、派发请求给子进程; 多个子进程:每个子进程生成多个线程; n个线程:每个线程响应一个请求; 并发数量:子进程数目x每个子进程所能生成的最大线程数 3)event: 事件驱动模型,多进程模型,每个进程响应多个请求; 一个主进程:负责生成及回收子进程、创建套接字、接收请求、派发请求给子进程; 多个子进程:每个子进程基于事件驱动机制响应多个请求;
3、源码编译安装LAMP环境(基于wordpress程序),并写出详细的安装、配置、测试过程。
环境:linux 2.6.32-358.el6.x86_64 + httpd 2.4.9 + mysql-5.5.33 + php-5.4.26
安装编译环境:
[root@web ~]# yum groupinstall "Desktop Platform Development" "Development tools" -y
[root@web ~]# yum install mode_ssl openssl-devel pcre-devel -y
注:这个必须得安装,编译Apache的时候用
一、编译安装Apache
1.解决依赖关系
httpd-2.4.9需要较新版本的apr和apr-util,因此需要事先对其进行升级。
升级方式有两种,一种是通过源代码编译安装,
一种是直接升级rpm包。这里选择使用编译源代码的方式进行。
APR(Apache portable Run-time libraries,Apache可移植运行库)
的目的如其名称一样,主要为上层的应用程序提供
一个可以跨越多操作系统平台使用的底层支持接口库。在早期 的Apache版本中,
应用程序本身必须能够处理各种具体操作系统平台的细节,
并针对不同的平台调用不同的处理函数。
(1)编译安装apr
[root@web ~]# tar xf apr-1.5.0.tar.bz2
[root@web ~]# cd apr-1.5.0
[root@web apr-1.5.0]# ./configure --prefix=/usr/local/apr
[root@web apr-1.5.0]# make && make install
(2)编译安装apr-util
[root@web ~]# tar xf apr-util-1.5.3.tar.bz2
[root@web ~]# cd apr-util-1.5.3
[root@web apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@web apr-util-1.5.3]# make && make install
2.编译安装httpd
[root@web ~]# tar xf httpd-2.4.9.tar.bz2
[root@web ~]# cd httpd-2.4.9
[root@web httpd-2.4.9]# ./configure --prefix=/usr/local/apache --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=event
[root@web httpd-2.4.9]# make && make install
注:configure后跟的参数解释
--prefix=/usr/local/apache设定httpd的安装目录
--sysconfdir=/etc/httpd24 设定httpd的配置文件目录
--enable-so 支持共享模块,如果没有这个PHP模块无法加入
--enable--ssl 支持ssl
--enable-cgi 支持cgi
--enable-rewrite 支持url重写
--with-zlib 支持zlib压缩,传输层的压缩(不指定具体的路径,默认在系统中搜索)
--with-pcre 支持正则化(不指定具体的路径,默认在系统中搜索)
--with-apr=/usr/local/apr 指定apr的路径
--with-apr-util=/usr/local/apr-util 指定apr-util的路径
--enable-modules=most 指定要编译的模块(most/all)
--enable-mpms-shared=all 指定要编译mpm
--with-mpm=event 指定httpd默认的MPM的工作方式
3.修改主配置文件,添加PidFile路径
[root@web ~]# vim /etc/httpd24/httpd.conf
PidFile "/var/run/httpd24.pid"
4.提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:
[root@web ~]# vim /etc/init.d/httpd24
这里是复制的/etc/init.d/httpd的脚本做了修改,具体修改内容:
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd24
pidfile=${PIDFILE-/var/run/httpd24.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
[root@web ~]# chmod +x /etc/init.d/httpd24
5.将httpd24加入服务列表并启动httpd24服务
[root@web ~]# chkconfig --add httpd24
[root@ftp ~]# service httpd24 start
出现了报错:
AH00558: httpd: Could not reliably determine the server's fully qualified domain name,
using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
解决方法:
[root@web ~]# vim /etc/httpd24/httpd.conf
ServerName localhost:80
6.可以将新编译的httpd24加入环境变量
[root@web ~]# vim /etc/profile.d/httpd24.sh
export PATH=/usr/local/apache/bin:$PATH
[root@web ~]# source /etc/profile.d/httpd24.sh
二、编译安装mysql-5.5.33
1.安装编译环境和需要的包
[root@web ~]# yum install ncurses-devel.x86_64 cmake.x86_64 -y
2.创建mysql用户和对应的mysql数据目录
[root@web ~]# groupadd -r mysql
[root@web ~]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
[root@web ~]# chown -R mysql:mysql /mydata/data
3.编译安装mysql-5.5.33
[root@web ~]# tar xf /mysql-5.5.33.tar.gz -C /usr/local/
[root@web ~]# ln -sv /usr/local/mysql-5.5.33/ /usr/local/mysql
[root@web ~]# cd /usr/local/mysql
[root@web mysql]# cmake .
[root@web mysql]# make install
[root@web mysql]# chmod +x scripts/mysql_install_db
[root@web mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql
--user=mysql --datadir=/mydata/data
4.为mysql提供配置文件
[root@web mysql]# mv /etc/my.cnf{,.bak}
[root@web mysql]# cp support-files/my-large.cnf /etc/my.cnf
[root@web mysql]# vim /etc/my.cnf
thread_concurrency = 2 //这里为cpu个数乘以2
datadir = /mydata/data //这行自己添加,为数据文件目录
5.为mysql提供SysV服务脚本
[root@web mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@web mysql]# chmod +x /etc/init.d/mysqld
[root@web mysql]# chkconfig --add mysqld
[root@web mysql]# chkconfig mysqld on
[root@web mysql]# service mysqld start
6.配置环境变量,使mysql的命令可以直接使用
[root@web mysql]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@web mysql]# chmod +x /etc/profile.d/mysql.sh
[root@web mysql]# source /etc/profile.d/mysql.sh
7.配置mysql的man手册
[root@web mysql]# vim /etc/man.config
MANPATH /usr/local/mysql/man
8.配置mysql的头文件到系统头文件
[root@web mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
9.配置mysql的库文件到系统库查找路径
[root@web mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@web mysql]# ldconfig //重新载入系统库
三、编译安装php-5.4.26
1.安装编译php时需要的包
[root@web ~]# yum install bzip2-devel libmcrypt-devel libxml2-devel -y
2.编译安装php
[root@web ~]# tar xf php-5.4.26.tar.bz2
[root@web ~]# cd php-5.4.26/
[root@web 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
说明:
(1)这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
(2)如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL
或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库
绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
[root@web php-5.4.26]# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd
--with-mysqli=mysqlnd
[root@web php-5.4.26]# make
[root@web php-5.4.26]# make test
[root@web php-5.4.26]# make intall
3.为php提供配置文件:
[root@web php-5.4.26]# cp php.ini-production /etc/php.ini
4.编辑apache配置文件httpd.conf,以apache支持php
[root@web php-5.4.26]# vim /etc/httpd/httpd.conf
添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
5.重启httpd24服务
[root@web php-5.4.26]# service httpd24 restart
四、编译安装wordpress
1.解压wordpress包到/usr/local/appache/htdocs
[root@web ~]# tar -xf wordpress-4.7-zh_CN.tar.gz -C /usr/local/apache/htdocs
2.创建数据库并授权登录用户
[root@web ~]# mysql
mysql> create database wordpress;
mysql> grant all on wordpress.* to wpuser@'192.168.%.%' identified by 'password';
3.编辑wordpress的配置文件
[root@web ~]# cp /usr/local/apache/htdocs/wordpress/wp-config-sample.php
/usr/local/apache/htdocs/wordpress/wp-config.php
[root@web ~]# vim /usr/local/apache/htdocs/wordpress/wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'password');
/** MySQL主机 */
define('DB_HOST', '192.168.82.58');
4.在浏览器中访问并配置
http://192.168.82.58/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.编辑httpd.conf将DocumentRoot注释并开启vhosts
[root@web ~]# vim /etc/httpd24/httpd.conf
#DocumentRoot "/usr/local/apache/htdocs"
Include /etc/httpd24/extra/httpd-vhosts.conf
2.创建所需的目录并创建index.html
[root@web ~]# mkdir -p /web/vhosts/www{1,2}
[root@web ~]# mkdir /var/log/httpd/
[root@web ~]# echo www1.stuX.com > /web/vhosts/www1/index.html
[root@web ~]# echo www2.stuX.com > /web/vhosts/www2/index.html
3.编辑vhosts配置文件创建两个虚拟主机并设置server-status
[root@web ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost 192.168.82.58:80>
DocumentRoot "/web/vhosts/www1"
ServerName www1.stuX.com
ErrorLog "/var/log/httpd/www1.err"
CustomLog "/var/log/httpd/www1.access" common
<Directory "/web/vhosts/www1">
Options None
AllowOverride None
Require all granted
</Directory>
<Location /server-status>
SetHandler server-status
AuthType Basic
Order deny,allow
Deny from all
Allow from 192.168.82
AuthName "Web Auth Server-status"
AuthUserFile "/etc/httpd24/extra/.htpasswd"
Require valid-user
</location>
</VirtualHost>
<VirtualHost 192.168.82.58:80>
DocumentRoot "/web/vhosts/www2"
ServerName www2.stuX.com
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/www2.access" common
<Directory "/web/vhosts/www2">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
4.生成密码文件
[root@web ~]# htpasswd -c -m /etc/httpd24/extra/.htpasswd status
5.添加hosts文件记录
[root@web ~]# vim /etc/hosts
192.168.82.58 www1.stuX.com
192.168.82.58 www2.stuX.com
6.重启服务并测试
[root@web ~]# service httpd24 restart
[root@web ~]# curl www1.stuX.com
www1.stuX.com
[root@web ~]# curl www2.stuX.com
www2.stuX.com

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

