httpd學習前知識必備:
I/O類型
-
同步和異步 synchronous asynchronous
關注的是消息通知機制,如何通知調用者,站在被調用者的角度
同步:調用發出后不會立即返回,一旦返回即是最終結果
異步:調用發出后會立即返回消息,但不是最終結果,被調用者通過狀態,通知機制,或回調函數處理結果
-
阻塞和非阻塞 block nonblock
關注的是調用者等待調用結果時的狀態,站在調用者角度
阻塞: 調用結果返回之前,調用者會被掛起,調用者只有在得到返回結果后才能繼續
非阻塞: 調用結果返回之前,調用者不會被掛起,調用不會阻塞調用者,調用者不斷的檢查調用結果
I/O模型
-
阻塞式blocking
磁盤數據到內核內存blocking;數據從內核內存到進程內存blocking,進程不能響應其他請求(進程被掛起)
-
非阻塞nonblocking
磁盤數據到內核內存nonblocking;數據從內核內存到進程內存blocking,忙等待狀態,進程不能響應其他請求
-
IO復用IO multiplexing
假如進程需要網絡IO,磁盤IO ,進程阻塞到內核的select,epoll等函數上,沒有被掛起,因此進程也可也處理網絡IO或其他IO
-
事件驅動signal driven IO
磁盤數據到內核內存nonblocking;但是有通知機制,數據從內核內存到進程內存blocking,進程可以響應其他請求
水平觸發(多次通知),邊緣觸發(只通知一次,當進程沒有收到時,之后進程可以調用相應的函數回調得到通知結果)
-
asynchronous IO AIO
進程進行IO調用,內核有通知機制,之后內核負責磁盤數據到內核內存,數據從內核內存到進程內存,進程能響應其他請求
1、請描述一次完整的http請求處理過程;
-
建立或處理連接:接收請求或拒絕請求
-
接收請求:
-
處理請求:對請求報文進行解析,并獲取請求的資源及請求方法等相關信息
-
訪問資源:獲取請求報文中請求的資源
-
構建響應報文:
-
發送響應報文:
-
記錄日志
2、httpd所支持的處理模型有哪些,他們的分別使用于哪些環境。
-
prefork 多進程模型,主進程,生成多個子進程,每個子進程響應一個請求,當沒有用戶請求時,也
預留幾個子進程等待用戶請求
-
work 多線程模型,主進程,生成多個子進程,每個子進程負責產生多個線程,每個線程處理一個請求
-
event 事件驅動模型,主進程,生成多個子進程,每個子進程直接響應用戶多個請求
3、源碼編譯安裝LAMP環境(基于wordpress程序),并寫出詳細的安裝、配置、測試過程。
-
apache配置
[root@centos apr-1.5.0]# ./configure --prefix=/usr/local/apr [root@centos apr-util-1.5.2]# ./configure --prefix=/usr/local/apr_utils --with-apr=/usr/local/apr/ [root@centos httpd-2.4.9]# yum install *pcre* -y [root@centos httpd-2.4.9]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-defalte --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr_utils --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork 修改httpd的主配置文件,添加ServerName fqdn 或 ip PidFile "/var/run/httpd.pid" , 其中網站根目錄為 DocumentRoot "/usr/local/apache/htdocs",默認啟用了中心主機 [root@centos ~]# cat /usr/local/apache/htdocs/index.html <html><body><h1>It works!</h1></body></html> [root@centos ~]# curl 192.168.40.128 <html><body><h1>It works!</h1></body></html>
-
Mysql安裝和配置
[root@centos local]# groupadd -r mysql [root@centos local]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql [root@centos ~]# ln -sv /usr/local/mysql-5.6.13 /usr/local/mysql [root@centos mysql]# yum install cmake [root@centos mysql]# cmake . [root@centos mysql]# make && make install [root@centos scripts]# chmod +x mysql_install_db.sh [root@centos scripts]# ./mysql_install_db.sh --user=mysql --basedir=/usr/local/mysql --datadir=/mydata [root@centos support-files]# vim /etc/my.cnf [root@centos support-files]# service mysql start Starting MySQL. [ OK ] [root@centos support-files]# cat /etc/my.cnf basedir =/usr/local/mysql datadir =/mydata port =3306 # server_id = ..... # socket = ..... -------------------- mysql> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql> grant all on wordpress.* to root@127.0.0.1 identified by "centos6"; Query OK, 0 rows affected (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
-
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-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 # make # make test # make intall # cp php.ini-production /etc/php.ini # vim /etc/httpd/httpd.conf AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps DirectoryIndex index.php index.html [root@centos ~]# tar zxf wordpress-4.5.3-zh_CN.tar.gz /test/
-
測試
[root@centos test]# mv wordpress /usr/local/apache/htdocs/
之后在瀏覽器中輸入http://web/wordpress 按照導向安裝完成即可,重點編輯wp-config.php文件來定義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);
禁用中心主機 [root@centos ~]# vim /etc/httpd24/httpd.conf #DocumentRoot "/usr/local/apache/htdocs" Include /etc/httpd24/extra/httpd-vhosts.conf 配置虛擬主機 [root@centos ~]# vim /etc/httpd24/extra/httpd-vhosts.conf <VirtualHost 192.168.40.128:80> DocumentRoot "/web/vhost/www1/" ServerName www1.test.com ErrorLog "/var/log/httpd/www1.err" CustomLog "/var/log/httpd/www1.access" common <Directory "/web/vhost/www1"> AllowOverride None Options None Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.40.128:80> DocumentRoot "/web/vhost/www2/" ServerName www2.stuX.com ErrorLog "/var/log/httpd/www2.err" CustomLog "/var/log/httpd/www2.access" common <Directory "/web/vhost/www2/"> AllowOverride None Options None Require all granted </Directory> </VirtualHost> [root@centos ~]# vim /etc/hosts 127.0.0.1 www1.stuX.com www2.stuX.com [root@centos ~]# mkdir -p /web/vhost/{www1,www2} && echo "www1.site" > /web/vhost/www1/index.html && echo "www2.site" > /web/vhost/www2/index.html [root@centos ~]# /usr/local/apache/bin/apachectl restart [root@centos ~]# curl www1.stuX.com www1.site [root@centos ~]# curl www2.stuX.com www2.site 更改第二個虛擬主機為下面所示 <VirtualHost 192.168.40.128:80> DocumentRoot "/web/vhost/www2/" ServerName www2.stuX.com ErrorLog "/var/log/httpd/www2.err" CustomLog "/var/log/httpd/www2.access" common <Directory "/web/vhost/www2/"> AllowOverride None Options None Require all granted </Directory> <Location /server-status> AuthType Basic AuthName "Only for Admin" AuthUserFile "/usr/local/apache/.htpasswd" Require valid-user SetHandler server-status AllowOverride None Options None </Location> </VirtualHost> [root@centos bin]# ./htpasswd -m -c /usr/local/apache/.htpasswd admin New password: Re-type new password: Adding password for user admin [root@centos bin]# curl www2.stuX.com/server-status <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> </body></html>
5、為第4題中的第2個虛擬主機提供https服務,使得用戶可以通過https安全的訪問此web站點;
(1)要求使用證書認證,證書中要求使用的國家(CN)、州(HA)、城市(ZZ)和組織(MageEdu);
(2)設置部門為Ops,主機名為www2.stuX.com,郵件為admin@stuX.com;
[root@centos CA]# touch index.txt serial [root@centos CA]# echo 01 > serial [root@centos CA]# (umask 077;openssl genrsa -out ./private/cakey.pem 1024) Generating RSA private key, 1024 bit long modulus .......................................................++++++ .......++++++ e is 65537 (0x10001) [root@centos CA]# openssl req -new -x509 -key ./private/cakey.pem -out ./cacert.pem [root@centos CA]# cd /usr/local/apache/ [root@centos apache]# mkdir ssl [root@centos apache]# (umask 077;openssl genrsa -out ./ssl/http.key 1024) [root@centos CA]# cd /usr/local/apache/ [root@centos apache]# mkdir ssl [root@centos apache]# (umask 077;openssl genrsa -out ./ssl/http.key 1024) Generating RSA private key, 1024 bit long modulus ..................................++++++ .......................................++++++ e is 65537 (0x10001) [root@centos apache]# openssl req -new -key ./ssl/http.key -out ./ssl/http.crst -days 3600 [root@centos apache]# openssl ca -in ./ssl/http.crst -out ./ssl/http.crt -days 3600 [root@centos ~]# cd /usr/local/apache/ssl/ [root@centos ssl]# ls http.crst http.crt http.key 之后修改/etc/httpd24/httpd.conf LoadModule ssl_module modules/mod_ssl.so Include /etc/httpd24/extra/httpd-ssl.conf 然后編輯/etc/httpd24/extra/httpd-ssl.conf如下 <VirtualHost _default_:443> DocumentRoot "/web/vhost/www2/" ServerName www2.stuX.com ErrorLog "/var/log/httpd/www2.err" CustomLog "/var/log/httpd/www2.access" common <Directory "/web/vhost/www2/"> AllowOverride None Options None Require all granted </Directory> SSLCertificateFile "/usr/local/apache/ssl/http.crt" SSLCertificateKeyFile "/usr/local/apache/ssl/http.key" </VirtualHost>
測試
6、在LAMP架構中,請分別以php編譯成httpd模塊形式和php以fpm工作為獨立守護進程的方式來支持httpd,列出詳細的過程。
-
把php編譯成httpd以fpm工作為獨立守護進程的方式來支持httpd,需要在上例編譯http的基礎上修改和配置php編譯時要加上–enable-fpm選項
[root@centos ~]# cd /usr/local/php-5.6.24/sapi/fpm/ [root@centos fpm]# ls config.m4 init.d.php-fpm.in php-fpm.8 php-fpm.service tests CREDITS LICENSE php-fpm.8.in php-fpm.service.in fpm Makefile.frag php-fpm.conf status.html init.d.php-fpm php-fpm php-fpm.conf.in status.html.in [root@centos fpm]# cp init.d.php-fpm /etc/init.d/php-fpm [root@centos php-5.6.24]# chkconfig --add php-fpm [root@centos php-5.6.24]# chkconfig php-fpm on [root@centos fpm]# cd /usr/local/php/etc/ [root@centos etc]# ls pear.conf php-fpm.conf.default [root@centos etc]# mv php-fpm.conf.default php-fpm.conf [root@centos etc]# service php-fpm start Starting php-fpm done [root@centos etc]# ss -antl LISTEN 0 128 127.0.0.1:9000 *:* [root@centos htdocs]# cp test.php /web/vhost/www2/ #LoadModule php5_module modules/libphp5.so LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 在上面的例子的www2.stuX.虛擬主機站點添加下面兩行 ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/vhost/www2/$1
-
測試
-
總結 關于http,php,mysql編譯選項,由于篇幅有限請多–help了解
原創文章,作者:Snoo,如若轉載,請注明出處:http://www.www58058.com/33680
寫的很好,排版還可以在漂亮一點,加油