1、請描述一次完整的http請求處理過程;
1)建立或處理連接:接收請求或拒絕請求;
2)接收請求:接收來自于網絡的請求報文中對某資源的一次請求的過程;接收請求的方式通常是并發訪問響應模型;
3)處理請求:對請求報文進行解析,并獲取請求的資源及請求方法等相關信息,解析后的報文信息首部稱為元數據;
4)構建響應報文:根據用戶請求的資源MIME類型以及URL重定向進行報文響應;
5)發送響應報文;
6)記錄日志信息。
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程序),并寫出詳細的安裝、配置、測試過程。
二進制方式安裝Mariadb
1)創建mysql用戶
]# useradd -r mysql
2)二進制方式安裝mariadb
]# tar xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local
3)創建鏈接并更改安裝目錄權限
]# cd /usr/local ]# ln -sv mariadb-5.5.46-linux-x86_64/ mysql ]# cd mysql ]# chown -R root.mysql ./*
4)創建數據文件目錄并賦權
]# mkdir -p /mydata/data ]# chown -R mysql.mysql /mydata/data/
5)創建mariadb配置文件
]# cd /usr/local/mysql/support-files ]# cp my-large.cnf /etc/my.cnf
6)在配置文件中添加參數:
]# vim /etc/my.cnf [mysqld] . . . datadir = /mydata/data innodb_file_per_table = ON skip_name_resolve = ON
7)創建啟動腳本
]# cd /usr/local/mysql/support-files ]# cp mysql.server /etc/rc.d/init.d/mysqld ]# chmod +x /etc/rc.d/init.d/mysqld
8)初始化數據庫
]# cd /usr/local/mysql/ ]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
9)啟動mariadb
]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS!
編譯安裝Apache:
1)安裝依賴包
]# yum groupinstall "Development Tools" "Server Platform Development" -y ]# yum install pcre-devel apr-devel apr-util-devel openssl-devel -y
2)編譯安裝apache
]# tar xf httpd-2.4.9.tar.bz2 ]# cd httpd-2.4.9 ]# ./configure --prefix=/usr/local/apache -sysconfdir=/etc/httpd \ --enable-so --enable-ssl --enable-rewrite --with-zlib --with-pcre \ --with-apr=/usr --with-apr-util=/usr --enable-modules=most \ --enable-mpms-shared=all --with-mpm=prefork ]# make && make install
3)修改環境變量,便于httpd相關命令的使用
]# touch /etc/profile.d/httpd.sh ]# echo "export PATH=/usr/local/apache/bin:$PATH" >>/etc/profile.d/httpd.sh ]# source /etc/profile
4)啟動httpd服務
]# apachectl start ]# ps -ef|grep httpd root 35159 1 0 15:59 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 35160 35159 0 15:59 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 35161 35159 0 15:59 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 35162 35159 0 15:59 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 35163 35159 0 15:59 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 35164 35159 0 15:59 ? 00:00:00 /usr/local/apache/bin/httpd -k start root 35166 6129 0 15:59 pts/0 00:00:00 grep --color=auto httpd
編譯安裝php:
1)安裝依賴包
]# yum install libxml2-devel libmcrypt-devel bzip2-devel -y
2)編譯安裝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-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 ]# make && make install
3)生成配置文件
]# cp php.ini-production /etc/php.ini
4)修改httpd配置文件,使其能夠支持php
]# vim /etc/httpd/httpd.conf . . . <IfModule dir_module> DirectoryIndex index.php index.html </IfModule> AddType application/x-httpd-php .php
5)測試httpd與php
]# vim /usr/local/apache/htdocs/index.php <?php phpinfo() ?>
6)測試php與mariadb的聯動
]# vim /usr/local/apache/htdocs/index.php <?php $conn = mysql_connect('127.0.0.1','root',''); if ($conn) echo "OK"; else echo "Failure"; ?>
測試成功,開始安裝WordPress
1)數據庫中創建用戶
MariaDB [(none)]> create database wpdb; MariaDB [(none)]> grant all on wpdb.* to wpuser@'192.168.0.%' identified by 'magedu'; MariaDB [(none)]> flush privileges;
2)將wordpress安裝包解壓到主頁目錄下
]# cd /usr/local/apache/htdocs ]# unzip wordpress-3.3.1-zh_CN.zip
3)修改wordpress配置文件
]# cd wordpress ]# cp wp-config-sample.php wp-config.php ]# vim wp-config.php define('DB_NAME', 'wpdb'); /** MySQL 數據庫用戶名 */ define('DB_USER', 'wpuser'); /** MySQL 數據庫密碼 */ define('DB_PASSWORD', 'magedu'); /** MySQL 主機 */ define('DB_HOST', '192.168.0.111');
4)通過頁面進行安裝
5)安裝完成,打開BLOG
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)創建主頁目錄,日志目錄和主頁文件
]# mkdir -p /web/vhosts/{www1,www2} ]# mkdir -p /var/log/httpd ]# echo "www1.stuX.com" >/web/vhosts/www1/index.html ]# echo "www2.stuX.com" >/web/vhosts/www2/index.html
2)創建認證用戶
]# htpasswd -c -m /etc/httpd/.htpasswd status
3)創建虛擬主機配置文件
]# cd /etc/httpd/extra/ ]# vim vhosts.conf <VirtualHost 192.168.0.111:80> DocumentRoot "/web/vhosts/www1" #主頁目錄 <Directory "/web/vhosts/www1"> Options None AllowOverride None Require all granted </Directory> <Location /server-status> SetHandler server-status AuthType Basic #認證模塊 AuthName "Admin Realm, show something" AuthUserFile "/etc/httpd/.htpasswd" #指定認證文件 Require user status #指定認證用戶 </Location> ServerName www1.stuX.com ErrorLog "/var/log/httpd/www1.err" #錯誤日志文件 CustomLog "/var/log/httpd/www1.access" common #登錄日志文件 </VirtualHost> <VirtualHost 192.168.0.111:80> DocumentRoot "/web/vhosts/www2" <Directory "/web/vhosts/www2"> Options None AllowOverride None Require all granted </Directory> ServerName www2.stuX.com ErrorLog "/var/log/httpd/www2.err" CustomLog "/var/log/httpd/www2.access" common </VirtualHost>
4)修改httpd主配置文件,使自定義的虛擬機配置文件生效
]# cd /etc/httpd/ ]# vim httpd.conf Include /etc/httpd/extra/vhosts.conf
5)重啟httpd服務,使配置生效
]# apachectl restart
6)修改客戶端的hosts文件
192.168.0.111 www1.stuX.com www2.stuX.com
7)客戶端使用瀏覽器訪問頁面進行測試
5、為第4題中的第2個虛擬主機提供https服務,使得用戶可以通過https安全的訪問此web站點;
(1)要求使用證書認證,證書中要求使用的國家(CN)、州(HA)、城市(ZZ)和組織(MageEdu);
(2)設置部門為Ops,主機名為www2.stuX.com,郵件為admin@stuX.com;
1)CA服務器端生成私鑰
]# cd /etc/pki/CA/ ]# (umask 077;openssl genrsa -out private/cakey.pem 2048) Generating RSA private key, 2048 bit long modulus .........+++ .....................................................................+++ e is 65537 (0x10001)
2)CA服務端生成自簽證書
]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem 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]:MagEdu Organizational Unit Name (eg, section) []:ops Common Name (eg, your name or your server's hostname) []:www2.stuX.com Email Address []:admin@stuX.com
]# touch serial index.txt ]# echo 01 >serial
3)web服務器端生成私鑰
]# cd /etc/httpd/ ]# mkdir ssl ]# cd ssl/ ]# (umask 077;openssl genrsa -out httpd.key 1024) Generating RSA private key, 1024 bit long modulus ...............++++++ ..++++++ e is 65537 (0x10001)
4)web服務器端生成證書簽署請求
]# openssl req -new -key httpd.key -out httpd.csr 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]:MagEdu 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 []: An optional company name []:
5)web服務器將證書簽署請求發送給CA服務器
]# scp httpd.csr root@192.168.0.120:/tmp/
6)CA服務器簽署證書
]# openssl ca -in /tmp/httpd.csr -out certs/httpd.crt
7)CA服務器將證書發送給web服務器端
]# scp certs/httpd.crt root@192.168.0.111:/etc/httpd/ssl
8)web服務器安裝mod_ssl模塊
]# yum install mod_ssl -y
9)編輯ssl模塊配置文件
]# vim /etc/httpd/conf.d/ssl.conf DocumentRoot "/web/vhosts/www2" ServerName www2.stuX.com SSLCertificateFile /etc/httpd/ssl/httpd.crt SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
10)重啟httpd服務
]# apachectl restart
6、在LAMP架構中,請分別以php編譯成httpd模塊形式和php以fpm工作為獨立守護進程的方式來支持httpd,列出詳細的過程。
fpm方式:
1)安裝httpd,php-fpm,mariadb,php-mysql
]# yum install php-fpm httpd mariadb-server php-mysql -y
2)創建fcgi配置文件
]# cd /etc/httpd/conf.d/ ]# vim fcgi.conf ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
3)修改httpd主配置文件,指定首頁默認文件為index.php
]# vim /etc/httpd/conf/httpd.conf <IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
4)創建php測試頁面
]# cd /var/www/html/ ]# vim index.php <?php phpinfo() ?>
5)啟動http和php-fpm服務
]# systemctl reload httpd.service ]# systemctl start php-fpm.service
php以模塊方式來支持httpd的方式在上面第3題已經提供了部署過程,這里不再贅述。
原創文章,作者:N26-西安-方老喵,如若轉載,請注明出處:http://www.www58058.com/71598
非常非常棒,再接再勵。