1、 請描述一次完整的http請求處理過程;
HTTP:超文本傳輸協議
完整的http請求過程
1)、建立TCP/IP連接:經過3次握手,建立連接或拒絕。
2)、瀏覽器向服務器發送HTTP請求。
3)、瀏覽器發送請求頭信息。
4)、服務器應答:服務器接受請求后,會回送應答。
5)、服務器發送應答頭信息。
6)、服務器向瀏覽器發送數據。
7)、服務器關閉TCP連接。
2、httpd所支持的處理模型有哪些,他們的分別使用于哪些環境。
1)prefork:多進程模型,每個進程響應一個請求;一個主進程負責生成n個子進程,子進程也成為工作進程,每個子進程處理一個用戶請求;即便沒有用戶請求,也會預先生成多個空閑進程,隨時等待請求到達;最大不會超過1024個;
優點:成熟穩定,兼容所有新老模塊。
缺點:一個進程相對占用更多的系統資源,消耗更多的內存。而且,它并不擅長處理高并發請求,理論上不會超過1024個,在大并發場景下,它會將請求放進隊列中,一直等到有可用進程,請求才會被處理。
2)worker:多線程模型,每個線程響應一個請求;一個主進程生成多個子進程,每個子進程負責生成多個線程,每個線程響應一個請求;如有m個進程,每個進程有n個線程,則可處理的請求個數為:m*n
優點:占據更少的內存,高并發下表現更優秀。
缺點:
(1)線程的管理要比進程復雜得多。線程之間很多資源是共享的,所以它沒有prefork模型那種一個進程服務一個服務請求那么安全穩定;
(2)worker是一個線程服務一個請求,在請求沒有完成之前,該線程是與它服務的請求綁定的。worker需要大量的創建進程生成線程,銷毀線程,殺死進程的過程;
(3)由于linux不是真線程的操作系統,所以worker在linux上的表現與prefork相比并沒有明顯優勢。
3)event:事件驅動模型,是基于信號驅動I/O 通知機制,每個線程響應n個請求;
優點:并發能力強,并且解決了worker模型下由于線程與請求綁定而導致的線程資源浪費的問題;
缺點:2.4之前的版本僅為測試用,只有2.4之后event才可在生產使用。
3、 源碼編譯安裝LAMP環境(基于wordpress程序),并寫出詳細的安裝、配置、測試過程。
系統環境:CentOS7
編譯安裝Maridb
1、解壓到/usr/local目錄下,并創建連接
[root@CentOS7 ~]# tar xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/
[root@CentOS7 ~]# cd /usr/local
[root@CentOS7 local]# ln -sv mariadb-5.5.46-linux-x86_64/ mysql
“mysql” -> “mariadb-5.5.46-linux-x86_64/”
2、創建用戶組mysql,修改目錄/usr/local/mysql權限
[root@CentOS7 local]# cd mysql
[root@CentOS7 mysql]# chown -R root.mysql ./*
3、創建數據目錄并修改權限
[root@CentOS7 /]# mkdir -p /mydata/data
[root@CentOS7 /]# chown -R mysql.mysql /mydata/data
4、創建配置文件
[root@CentOS7 mysql]# cp support-files/my-large.cnf /etc/my.cnf
[root@CentOS7 support-files]# vim /etc/my.cnf
[mysqld]
datadir = /mydata/data
innodb_file_per_table = ON
skip_name_resolve = ON
5、創建啟動腳本
[root@CentOS7 support-files]# cp mysql.server /etc/rc.d/init.d/mysqld
[root@CentOS7 support-files]# chmod +x /etc/rc.d/init.d/mysqld
6、初始化數據庫
[root@CentOS7 support-files]# cd /usr//local/mysql/
[root@CentOS7 mysql]# scripts/mysql_install_db –user=mysql –datadir=/mydata/data
7、啟動數據庫
[root@CentOS7 mysql]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!
編譯安裝Apache
依賴的開發環境:Development Tools 和 Server Platform Development
依賴的包:pcre-devel arp-devel apr-util-devel openssl-devel
注:
CentOS6還依賴:
Apr1.4+和apr-util-1.4+以上的版本
1、安裝所需包
[root@CentOS7 ~]# yum groupinstall “Development Tools” “Server Platform Development” -y
[root@CentOS7 ~]# yum install arp-devel apr-util-devel openssl-devel pcre-devel
2、編譯安裝Apache
[root@CentOS7 ~]# tar xf httpd-2.4.9.tar.bz2
[root@CentOS7 ~]# cd httpd-2.4.9/
[root@CentOS7 httpd-2.4.9]# ./configure –prefix=/usr/local/apache –sysconfdir=/etc/httpd –enable-so –enable-ssl –enable-cgi –enable-rewrite –enable-zlib –with-pcre –with-apr=/usr –with-apr-util=/usr –enable-modules=most –enable-mpms-shared=all –with-mpm=prefork
[root@CentOS7 httpd-2.4.9]# make -j 2 && make install
3、修改環境變量
[root@CentOS7 httpd-2.4.9]# vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[root@CentOS7 httpd-2.4.9]# . /etc/profile.d/httpd.sh
4、啟動httpd服務
注:出現以下錯誤。修改 /etc/httpd/ httpd.conf文件。
ServerName www.example.com:80 取消注釋,使其生效
[root@CentOS7 httpd-2.4.9]# apachectl start
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 180.168.41.175. Set the ‘ServerName’ directive globally to suppress this message
編譯安裝php
1、安裝依賴包
[root@CentOS7 ~]#yum install libxml2-devel libmcrypt-devel bzip2-devel
2、編譯安裝php
[root@CentOS7 ~]# tar xf php-5.4.26.tar.bz2
[root@CentOS7 ~]# cd php-5.4.26
[root@CentOS7 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/apache/bin/apxs –with-mcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2
[root@CentOS7 php-5.4.26]# make -j 4 && make install
3、修改httpd配置文件,添加支持php
[root@CentOS7 httpd]# vim httpd.conf
252 <IfModule dir_module>
253 DirectoryIndex index.php index.html
254 </IfModule>
382 AddType application/x-httpd-php .php
4、創建php配置文件
[root@CentOS7 php-5.4.26]# cp php.ini-production /etc/php.ini
5、建立php測試文檔,并重啟Apache服務
[root@CentOS7 php-5.4.26]# vim /usr/local/apache/htdocs/index.php
this is php test.
<?php
phpinfo();
?>
[root@CentOS7 php-5.4.26]# apachectl restart
6、測試php與mariadb的聯動
[root@CentOS7 httpd-2.4.9]# vim /usr/local/apache/htdocs/index.php
<?php
$conn = mysql_connect(‘127.0.0.1′,’root’,”);
if ($conn)
echo “OK”;
else
echo “Failure”;
?>
安裝wordpress
[root@CentOS7 wordpress]# cp wordpress-4.7.4-zh_CN.zip /usr/local/apache/htdocs/
1、數據庫中創建用戶
[root@CentOS7 wordpress]# mysql
MariaDB [(none)]> CREATE DATABASE wpdb;
MariaDB [(none)]> GRANT ALL ON wpdb.* TO wpuser@’172.16.%.%’ IDENTIFIED BY ‘wppass’;
MariaDB [(none)]> FLUSH PRIVILEGES;
2、將WordPress安裝解壓到主頁目錄下/usr/local/apache/htdocs/
root@CentOS7 wordpress]# cp wordpress-4.7.4-zh_CN.zip /usr/local/apache/htdocs/
[root@CentOS7 wordpress]# cd /usr/local/apache/htdocs/
[root@CentOS7 wordpress]# unzip wordpress-4.7.4-zh_CN.zip
3、修改WordPress配置文件
[root@CentOS7 wordpress]# cd wordpress/
[root@CentOS7 wordpress]# cp wp-config-sample.php wp-config.php
[root@CentOS7 wordpress]# vim wp-config.php
/** MySQL數據庫用戶名 */
define(‘DB_USER’, ‘wpuser’);
/** MySQL數據庫密碼 */
define(‘DB_PASSWORD’, ‘wppass’);
/** MySQL主機 */
define(‘DB_HOST’, ‘192.168.0.108’);
[root@CentOS7 wordpress]# apachectl restart
5、通過頁面進行安裝
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@CentOS7 ~]# mkdir -p /web/vhosts/www1
[root@CentOS7 ~]# mkdir -p /var/log/httpd
[root@CentOS7 ~]# mkdir -p /web/vhosts/www2
[root@CentOS7~]#echo”www1.stuX.com” >/web/vhosts/www1/index.html
[root@CentOS7~]#echo”www2.stuX.com” >/web/vhosts/www2/index.htm
2、在/etc/httpd/extra/目錄下創建配置文件
[root@CentOS7 extra]# vim /etc/httpd/extra/vhost.conf
<VirtualHost *:80>
ServerName www1.stuX.com
DocumentRoot “/web/vhosts/www1”
<Directory “/web/vhosts/www1”>
Options None
AllowOverride None
Require all granted
</Directory>
ErrorLog “/var/log/httpd/www1.err”
CustomLog “/var/log/httpd/www1.access” common
#啟動server-status頁面并限制用戶訪問
<Location /server-status>
SetHandler server-status
AuthType Basic
AuthName “Admin Realm,show something”
AuthUserFile “/etc/httpd/.htpasswd”
Require user tom
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName www2.stuX.com
DocumentRoot “/web/vhosts/www2”
<Directory “/web/vhosts/www2”>
Options None
AllowOverride None
Require all granted
</Directory>
ErrorLog “/var/log/httpd/www2.err”
CustomLog “/var/log/httpd/www2.access” common
</VirtualHost>
3、創建可查看狀態頁的用戶
[root@CentOS7 extra]# htpasswd -c -m /etc/httpd/.htpasswd tom
New password:
Re-type new password:
Adding password for user tom
4、修改httpd的主配置文件,導入創建的文件
[root@CentOS7 httpd]# vim /etc/httpd/httpd.conf
Include /etc/httpd/extra/vhost.conf
5、修改hosts文件,重啟Apache服務
[root@CentOS7 httpd]# vim /etc/hosts
192.168.0.109 www1.stuX.com www2.stuX.com
[root@CentOS7 httpd]# apachectl restart
6、測試
5、為第4題中的第2個虛擬主機提供https服務,使得用戶可以通過https安全的訪問此web站點;
(1)要求使用證書認證,證書中要求使用的國家(CN)、州(HA)、城市(ZZ)和組織(MageEdu);
(2)設置部門為Ops,主機名為www2.stuX.com,郵件為admin@stuX.com;
環境:
Web服務器:192.168.0.109 CentOS7
CA主機:192.168.0.110 CentOS7
在CA主機
1、CA主機創建自簽證書
[root@CentOS7 ~]# cd /etc/pki/CA
[root@CentOS7 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
………………………………………………………………………………..+++
……………………………………………………………………………………………………………………………………………………+++
e is 65537 (0x10001)
[root@CentOS7 CA]# openssl req -new -x509 -key private/cakeynew.pem -out cacert.pem -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
[root@CentOS7 CA]# touch serial index.txt
[root@CentOS7 CA]# echo 01 > serial
Web服務器上
2、服務器生成證書請求,并發給CA主機
[root@CentOS7 extra]# cd /etc/httpd/
[root@CentOS7 httpd]# mkdir ssl
[root@CentOS7 httpd]# cd ssl
[root@CentOS7 ssl]# (umask 077 ;openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
.++++++
………..++++++
e is 65537 (0x10001)
[root@CentOS7 ssl]# openssl req -new -key httpd.key -out 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.stu2.com
Email Address []:admin@stu2.com
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@CentOS7 ssl]# scp httpd.csr root@192.168.0.110:/tmp
The authenticity of host ‘192.168.0.110 (192.168.0.110)’ can’t be established.
ECDSA key fingerprint is b6:53:54:63:50:3c:99:f3:5c:f1:94:da:60:29:50:b9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.0.110’ (ECDSA) to the list of known hosts.
root@192.168.0.110’s password:
httpd.csr
注:scp出現報錯,使用ssh-keygen -R 主機IP
[root@CentOS7 ssl]# scp httpd.csr root@192.168.0.110:/tmp
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
58:f2:c5:cf:d2:90:7d:4f:a0:1f:a8:e0:ee:6d:d6:41.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending RSA key in /root/.ssh/known_hosts:1
RSA host key for 192.168.0.110 has changed and you have requested strict checking.
Host key verification failed.
lost connection
[root@CentOS7 ssl]# ssh-keygen -R 192.168.0.110
3、CA主機簽署,并發送
[root@CentOS7 CA]# openssl ca -in /tmp/httpd.csr -out certs/httpdnew.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: May 15 06:16:31 2017 GMT
Not After : May 15 06:16:31 2018 GMT
Subject:
countryName = CN
stateOrProvinceName = HA
organizationName = MageEdu
organizationalUnitName = Ops
commonName = www2.stu2.com
emailAddress = admin@stu2.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
DD:5E:C4:8E:48:6C:EF:14:A0:EF:1F:80:2A:AF:CC:00:97:C8:23:1E
X509v3 Authority Key Identifier:
keyid:18:C2:6D:B5:63:A4:9C:A0:33:08:79:D1:02:62:37:92:51:D3:EF:09
[root@CentOS7 CA]# scp certs/httpdnew.crt root@192.168.0.109:/etc/httpd/ssl/
[root@CentOS7 ssl]# ls
httpd.csr httpd.key httpdnew.crt
4、服務器安裝mod_ssl模塊
[root@CentOS7 ssl]# yum -y install mod_ssl
5、修改配置文件
[root@CentOS7 extra]# vim /etc/httpd/conf.d/httpd-ssl.conf
DocumentRoot “/web/vhosts/www2”
SSLCertificateFile “/etc/httpd/ssl/httpdnew.crt”
SSLCertificateKeyFile “/etc/httpd/ssl/httpd.key”
[root@CentOS7 httpd]# vim httpd.conf
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule ssl_module modules/mod_ssl.so
6、重啟服務
[root@CentOS7 httpd]# systemtl restart httpd.service
6、在LAMP架構中,請分別以php編譯成httpd模塊形式和php以fpm工作為獨立守護進程的方式來支持httpd,列出詳細的過程。
本例只有php-fpm的相關配置,其他見第三題。
1、安裝httpd 、mariadb.server 、php-fpm 、php-mysql
[root@CentOS7 ~]# yum install -y httpd mariadb.server php-fpm php-mysql
2、修改php-fpm的配置文件
[root@CentOS7 ~]# vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000
3、創建fcgi的配置文件
[root@CentOS7 ~]# vim /etc/httpd/conf.d/fcgi.conf
DirectoryIndex index.php 設置主頁
ProxyRequests off 關閉正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
.php后 綴的URL請求轉發給php-fpm
4、開啟php-fpm與httpd服務
[root@CentOS7 ~]# systemctl start httpd.service
[root@CentOS7 ~]# systemctl start php-fpm.service
5 、測試
原創文章,作者:ning407631632,如若轉載,請注明出處:http://www.www58058.com/76066
實驗過程清晰,步驟詳細,另外如果能有對應的壓測性能報告會更好,尤其是perfork和worker原理理解和壓測驗證方面的知識。