1, tells httpd works in a manner rpm package installed LAMP, configure name-based virtual machine host and deploy PhpMyAdmin, WordPress (provided https), discuz
http protocol Introduction
HTTP / 0.9 : 1991 , a prototype version, features simple, only one command GET. GET / index.html, the server can only respond HTML format string, you can not respond to other formats http / 1.0 : 1996, supports cache, MIME, method each TCP connection only send a request to send data is completed, the connection is closed, even if the request for additional resources, it is necessary to re-create a new connection introduction of command POST and HEAD command header information is ASCII code, after the data may be in any format. The client tells the server to respond, what format the data is that the Content role -Type field. These data types are collectively referred to as MIME Multipurpose Internet Mail Extensions
exhibition, including a value for each type and secondary type, predefined types, can also customize the type of common values Content-Type: text / xml image / jpeg audio / mp3 HTTP / 1.1 : January 1997 introduced persistent connections (persistent connection), i.e. does not close the TCP connection by default, multiple requests can be multiplexed without declaration Connection: Keep - Alive. For the same domain name, most browsers allow
simultaneous connection to establish lasting 6 introduction pipeline mechanism (PIPELINING), i.e. in the same TCP connection, the client may send multiple requests at the same time, to further improve the efficiency of the HTTP protocol New method: PUT, PATCH, OPTIONS, DELETE same TCP connection, all of the data communication is carried out in sequence. Order processing server can only respond, in front of the slow response, there will be many requests are queued, resulting in " team head clogging " (Head-of- Line blocking) To avoid the above problems, two methods: one is to reduce the number of requests, while the second is more open persistent connection. Page optimization techniques, such as mergers scripts and stylesheets, the picture is embedded CSS code, domain fragment (domain sharding), etc. HTTP agreement without a state, each request must be accompanied by all the information. Many of the fields requests are repetitive, wasted bandwidth and affect the speed
1 , connection is established: accept or reject the connection request 2 , receives the request: a process request packet of a resource request from a client Web access response model (Web the I / O) Single process the I / O model: start a process to handle user requests, and process only one request is a plurality of serially response Multi-process the I / O model: Start a plurality of parallel processes, each process in response to a connection request Multiplexing the I / O configuration: starting a process and respond to the connection request N Method: multithreading and event-driven model Multi-threading model: a process generating N threads, each thread in response to a connection request Event-driven: a process to handle requests N 复用的多进程I/O模型:启动M个进程,每个进程响应N个连接请求,同时接收M*N个请求 3、处理请求:服务器对请求报文进行解析,并获取请求的资源及请求方法等相关信息,根据方法,资源,首部和可选的主体部分对请求进行处理 元数据:请求报文首部 <method> <URL> <VERSION> HEADERS 格式 name:value <request body> 示例: Host: www.magedu.com 请求的主机名称 Server: Apache/2.4.7 HTTP常用请求方式,Method:标明客户端希望服务器对资源执行的动作
GET:从服务器获取一个资源
HEAD:只从服务器获取文档的响应首部
POST:向服务器输入数据,通常会再由网关程序继续处理,常见为提交表单
PUT:将请求的主体部分存储在服务器中,如上传文件
DELETE:请求删除服务器上指定的文档
TRACE:追踪请求到达服务器中间经过的代理服务器
OPTIONS:请求服务器返回对指定资源支持使用的请求方法
status:状态码
1xx:100-101 信息提示
2xx:200-206 成功
3xx:300-305 重定向
4xx:400-415 错误类信息,客户端错误
5xx:500-505 错误类信息,服务器端错误
200:成功,请求数据通过响应报文的entity-body部分发送;OK
301:请求的URL执行的资源已经被删除;但在响应报文中通过首部LOCATION指明了资源现在所处的新位置;Moved Permanently
302:响应报文Location指明资源临时新位置;Moved Temporarily
304:客户端发出了条件式请求,但服务器上的资源未曾发生改变,则通过响应此响应状态码通知客户端;Not Modified
401:需要输入账号和密码认证方能访问资源:Unauthorized
403:请求被禁止;Forbidden
404:服务器无法找到客户端请求的资源;Not Found
500:服务器内部错误;Internal Server Error
502:代理服务器从后端服务器收到了一条伪响应,如无法连接到网关;Bad Gateway
503:服务不可用,临时服务器维护或过载,服务器无法处理请求
504:网关超时
4、访问资源: 服务器获取请求报文中请求的资源web服务器,即存放了web资源的服务器,负责向请求者提供对方请求的静态资源,或动态运行后生成的资源 资源放置于本地文件系统特定的路径:DocRoot DocRoot /var/www/html /var/www/html/images/logo.jpg http://www.magedu.com/images/logo.jpg web服务器资源路径映射方式: (a) docroot (b) alias (c) 虚拟主机docroot (d) 用户家目录docroot 5、构建响应报文: 一旦Web服务器识别除了资源,就执行请求方法中描述的动作,并返回响应报文。响应报文中 包含有响应状态码、响应首部,如果生成了响应主体的话,还包括响应主体 1)响应实体:如果事务处理产生了响应主体,就将内容放在响应报文中回送过去。响应报文中通常包括: 描述了响应主体MIME类型的Content-Type首部 描述了响应主体长度的Content-Length 实际报文的主体内容 2)URL重定向:web服务构建的响应并非客户端请求的资源,而是资源另外一个访问路径 永久重定向301:http://www.360buy.com 临时重定向302:http://www.taobao.com 6、发送响应报文 Web服务器通过连接发送数据时也会面临与接收数据一样的问题。服务器可能有很多条到各个客户端的连接,有些是空闲的,有些在向服务器发送数据,还有一些在向客户端回送响
应数据。服务器要记录连接的状态,还要特别注意对持久连接的处理。对非持久连接而言,服务器应该在发送了整条报文之后,关闭自己这一端的连接。对持久连接来说,连接可能
仍保持打开状态,在这种情况下,服务器要正确地计算Content-Length首部,不然客户端就无法知道响应什么时候结束了 7、记录日志 最后,当事务结束时,Web服务器会在日志文件中添加一个条目,来描述已执行的事务
httpd mpm三种工作模型
prefork:多进程I/O模型,每个进程响应一个请求,默认模型 一个主进程:生成和回收n个子进程,创建套接字,不响应请求 多个子进程:工作work进程,每个子进程处理一个
请求;系统初始时,预先生成多个空闲进程,等待请求,最大不超过1024个
worker: 复用的多进程I/O模型,多进程多线程,IIS使用此模型 一个主进程:生成m个子进程,每个子进程负责生个n个线程,每个线程响应一个请求,并发响应请求:m*n
event: 事件驱动模型(worker模型的变种) 一个主进程:生成m个子进程,每个进程直接响应n个请求,并发响应请求:m*n,有专门的线程来管理这些keep-alive类型的线
程,当有真实请求时,将请求传递给服务线程,执行完毕后,又允许释放。这样增强了高并发场景下的请求处理能力
httpd-2.2:event 测试版,centos6默认 httpd-2.4:event 稳定版,centos7默认
以rpm包的方式安装LAMP,配置基于域名的虚拟机主机并部署PhpMyAdmin、WordPress(提供https)、discuz
快速部署LAMP CentOS 7: Modules:httpd, php, php-mysql, mariadb-server FastCGI:httpd, php-fpm, php-mysql, mariadb-server CentOS 6: Modules:httpd, php, php-mysql, mysql-server FastCGI:默认不支持 安装LAMP CentOS 6: yum install httpd, php, mysql-server, php-mysql service httpd start service mysqld start CentOS 7: yum install httpd, php, php-mysql, mariadb-server systemctl start httpd.service systemctl start mariadb.service 注意:要使用prefork模型
部署phpMyadmin yum -y install httpd mariadb-server php php-mysql systemctl start httpd systemctl start mariadb mysql_secure_installation 下载:https://www.phpmyadmin.net/downloads/ tar xvf phpMyAdmin-4.0.10.20-all-languages.tar.xz -C /var/www/html
mv phpMyAdmin-4.0.10.20-source pma cd pma/ cp config.sample.inc.php config.inc.php vim config.ini.php 修改默认密码 $cfg['blowfish_secret'] = 'a8b7c6d'; $cfg['Servers'][$i]['host'] = '192.168.242.129'; # 地址 $cfg['Servers'][$i]['connect_type'] = 'tcp'; # 类型 $cfg['Servers'][$i]['compress'] = false; # 压缩 yum -y install php-mbstring systemctl reload httpd
创建wordpress数据库给wordpress使用,并创建用户
修改wpuser权限,可以管理wordpress库
部署WordPress 下载wordpress https://cn.wordpress.org/download/ 注意。使用rpm方式实现lamp架构因为httpd php等版本更新较慢。下载wordpress时需要注意版本兼容问题。建议rpm安装时下载旧版wordpress unzip wordpress-4.9.4-zh_CN.zip -C /var/www/html cd wordpress/ cp wp-config-sample.php wp-config.php 修改配置文件,添加数据库账号 vim wp-config.php
部署discuz 论坛 可在论坛中下载http://www.discuz.net/forum.php unzip Discuz_X3.3_SC_UTF8.zip -C /var/www/html解压包 解压完成进入upload目录中可发现直接又install的目录。 http://192.168.64.136/upload/install/ 在游览器中直接输入开始安装
有些目录需要设置写权限。需要配置权限
setfacl -R -m u:apache:rw ./upload/
安装完成
搭建wordpress 写入/etc/hosts 文件。加入域名
vim /etc/httpd/conf.d/test.conf 增加配置文件。 <virtualhost *:80> DocumentRoot "/var/www/html/wordpress" servername "www.a.com" <directory /var/www/html/wordpress> Require all granted </directory> </virtualhost> 添加以上字段。如果实现discurz论坛则需要修改discuz的目录 将documentroot 指向discuz的目录 curl www.a.com | less 测试连接成功
实现https 访问
利用mod_ssl实现加密功能
yum install mod_ssl
安装完成之后会自动生成公钥和私钥对,就自动完成了https加密的过程
cd /etc/pki/CA目录 (umask 066;openssl genrsa -out private/cakey.pem 2048)生成私钥文件 生成自签名证书并签名 openssl req -new -x509 -key private/cakey.pem -out cacert.pem
创建ca的证书编号文件
touch index.txt
echo 01 > serial
客户机生成私钥,及证书申请文件,(我这里是在本机操作)
(umask 077;openssl genrsa -out httpd.key 1024)
openssl req -new -key httpd.key -out httpd.csr
给生成的证书申请签名
openssl ca -in /etc/httpd/conf.d/ssl/httpd.csr -out certs/httpd.crt -days 365
将生成证书和ca公钥颁发,(这里是拷贝,因为一台机器。)
cp certs/httpd.crt /etc/httpd/conf.d/ssl
cp /etc/pki/CA/cacert.pem /etc/httpd/conf.d/ssl/
修改mod_ssl模块的配置文件
vim /etc/httpd/conf.d/ssl.conf
修改证书文件路径。和key路径
和ca的公钥路径
到此就可以利用https访问网站了。
2、编译安装LAMP且PHP以FASTCGI的方式工作并使用XCache为php加速
建议安装顺序httpd->mysql->php 下载httpd,php,mariadb,源码包,因为新版httpd包不支持老版本的apr包所以还需要下载apr 和aprutil包 解压缩 [root@centos7 ~]# tar xf apr-1.6.5.tar.bz2 [root@centos7 ~]# tar xf apr-util-1.6.1.tar.bz2 [root@centos7 ~]# tar xf httpd-2.4.37.tar.bz2 将新版apr复制到httpd目录并且更名 [root@centos7 ~]# cp -r apr-1.6.5 httpd-2.4.37/srclib/apr [root@centos7 ~]# cp -r apr-util-1.6.1 httpd-2.4.37/srclib/apr-util 下载开发包组用于编译安装 yum groupinstall "Development Tools" -y 安装依赖包 yum install pcre-devel openssl-devel expat-devel 进入httpd目录开始编译安装cd httpd-2.4.37/ ./configure --prefix=/app/httpd24 \ --enable-so \ --enable-ssl \ --enable-cgi \ --enable-rewrite \ --with-zlib \ --with-pcre \ --enable-modules=most \ --enable-mpms-shared=all \ --with-mpm=prefork \ --with-included-apr make && make install
解压数据库到/usr/local目录中,创建软连接,创建账号及数据库目录 tar -xvf mariadb-10.2.15-linux-x86_64.tar.gz -C /usr/local/ ln -s mariadb-10.2.15-linux-x86_64/ mysql useradd -s /sbin/nologin -r mysql -d /data/mysql chown -R root.root mariadb-10.2.15-linux-x86_64/ mkdir /data/mysql chown mysql.mysql /data/mysql 进入/usr/local/mysql/目录执行初始化安装脚本。生成数据库文件 scripts/mysql_install_db --user=mysql --datadir=/data/mysql
自定义一个数据库配置文件,并且修改数据库路径
mkdir /etc/mysql/
cp support-files/my-huge.cnf /etc/mysql/my.cnf
mysqld下加入一行datadir=/data/mysql
将启动脚本设置为开机自启动
加入环境变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
创建数据库并授权wordpress用户,如果连接拒绝,也可以使用root用连接。
编译安装php 需要安装依赖包 yum install libxml2-devel bzip2-devel libmcrypt-devel (epel) 解压缩php。进入php目录 tar xf php-7.1.18.tar.bz2 ./configure --prefix=/app/php \ --enable-mysqlnd \ --with-mysqli=mysqlnd \ --with-openssl \ --with-pdo-mysql=mysqlnd \ --enable-mbstring \ --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir=/usr \ --enable-xml \ --enable-sockets \ --enable-fpm \ --with-config-file-path=/etc \ --with-config-file-scan-dir=/etc/php.d \ --enable-maintainer-zts \ --disable-fileinfo make && make sintall 生成配置文件和启动脚本 cp php.ini-production /etc/php.ini cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cd /app/php/etc #进入php编译完成后的目录
cp php-fpm.conf.default php-fpm.conf #将范例文件名修改成配置文件
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
更改apache配置文件,使其能够支持php
vim /app/httpd24/conf/httpd.conf
取消下面两行的注释
LoadModule proxy_module modules/mod_proxy.so #开启代理功能模块
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
在末尾加入
ProxyRequests Off #//是否开启正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1 ##//开启FCGI反向代理,//前面的/相对于后面的/var/www/html而言,后面的$1是指前面的/(.*\.php)
wordpress安装 unzip wordpress-4.9.4-zh_CN.zip 将wordpress目录中的文件移动至httpd的首页目录/app/httpd24/htdocs/
生成配置文件并修改配置文件的数据库内容
mv wp-config-sample.php wp-config.php
运行mysql_secure_installation安全脚本生成密码
写入wordpress配置文件中。库名和账户密码。及主机
搭建成功