馬哥教育網絡班22期+第12周作業

week11
1、請描述一次完整的http請求處理過程;
2、httpd所支持的處理模型有哪些,他們的分別使用于哪些環境。
3、源碼編譯安裝LAMP環境(基于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);
5、為第4題中的第2個虛擬主機提供https服務,使得用戶可以通過https安全的訪問此web站點;
   (1)要求使用證書認證,證書中要求使用的國家(CN)、州(HA)、城市(ZZ)和組織(MageEdu);
   (2)設置部門為Ops,主機名為www2.stuX.com,郵件為admin@stuX.com;
6、在LAMP架構中,請分別以php編譯成httpd模塊形式和php以fpm工作為獨立守護進程的方式來支持httpd,列出詳細的過程。

題目1:
    1)建立或處理連接:客戶端發送http請求報文,服務器端接收或拒絕請求;
    2)接收請求:服務器端接收來自客戶端對某些資源的請求;
    3)處理請求:服務器端解析客戶端請求報文,獲取客戶端請求的資源及請求方法等信息;
    4)訪問資源:服務器端獲取客戶端請求的資源;
    5)構建響應報文;
    6)發送響應報文;
    7)日志記錄;
題目2:
http支持的處理模型:
    1)prefork:
    多進程模型;每個進程響應一個請求;
    一個主進程:負責生成及回收子進程;負責創建套接字;負責接收請求,并派發請求給子進程;
    多個子進程:負責處理來自主進程派發的客戶端請求;每個子進程處理一個請求;
    工作模式:服務器端會預先生成幾個空閑進程,用于響應客戶端請求;
            可以在配置文件中設置最大及最小空閑子進程數目;
    2)worker:
    多進程多線程模型;每個線程響應一個請求;
    一個主進程:負責生成及回收子進程;負責創建套接字;負責接收請求,并派發請求給子進程;
    多個子進程:每個子進程生成多個線程;
    n個線程:每個線程響應一個請求;
    并發數量:子進程數目x每個子進程所能生成的最大線程數
    3)event:
    事件驅動模型;多進程模型;每個進程響應多個請求;
    一個主進程:負責生成及回收子進程;負責創建套接字;負責接收請求,并派發請求給子進程;
    多個子進程:每個子進程基于事件驅動機制響應多個請求;

題目3:源碼編譯安裝LAMP+Wordpress:
1)準備環境
    [root@dr2 ~]# yum -y groupinstall "Development Tools" "Server Platform Development"
    [root@dr2 ~]# yum -y install openssl-devel zlib-devel libxml2-devel pcre-devel 
2)編譯安裝apr
    [root@dr2 ~]# tar xf apr-1.5.2.tar.bz2
    [root@dr2 ~]# cd apr-1.5.2
    [root@dr2 ~]# ./configure --prefix=/usr/local/apr
    [root@dr2 ~]# make && make install
3)編譯安裝apr-util
    [root@dr2 ~]# tar xf apr-util-1.5.4.tar.bz2
    [root@dr2 ~]# cd apr-util-1.5.4
    [root@dr2 ~]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    [root@dr2 ~]# make && make install
4)編譯安裝httpd
    [root@dr2 ~]# tar xf httpd-2.4.23.tar.bz2
    [root@dr2 ~]# cd httpd-2.4.23
    [root@dr2 ~]# ./configure --prefix=/usr/local/apache2 \
        --enable-http --enable-remoteip --enable-rewrite  \
        --enable-ssl --enable-unixd --enable-modules=most --with-mpm=event \
        --enable-mpms-shared=all --enable-mods-shared=most  --enable-so \
        --enable-deflate --enable-cgi --enable-proxy --enable-proxy-fcgi \
        --enable-proxy-ajp --enable-proxy-http --enable-proxy-balancer \
        --enable-cgi --enable-cgid  --enable-watchdog \
        --enable-proxy-hcheck  --with-pcre --with-apr=/usr/local/apr \
        --with-apr-util=/usr/local/apr-util/
    [root@dr2 ~]# make && make install
添加path路徑:/etc/profile.d/apache.sh
    export PATH=$PATH:/usr/local/apache2/bin
    root@dr2 ~]# source /etc/profile.d/apache.sh
添加服務啟動腳本:/usr/lib/systemd/system/httpd.service
    [Unit]
    Description=The Apache2.4 HTTP Server   
    After=network.target 
    [Service]
    Type=forking
    ExecStart=/usr/local/apache2/bin/apachectl start
    ExecReload=/usr/local/apache2/bin/apachectl restart
    ExecStop=/usr/local/apache2/bin/apachectl stop
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    
    [root@dr2 ~]# systemctl daemon-reload
    [root@dr2 ~]# systemctl enable httpd
編輯配置文件:/usr/local/apache2/conf/httpd.conf,修改如下參數:
    User apache
    Group apache
    
    [root@dr2 ~]# systemctl start httpd
5)編譯安裝MySQL-5.7.12
    [root@dr2 ~]# groupadd -r mysql
    [root@dr2 ~]# useradd -r -g mysql -s /sbin/nologin mysql
    [root@dr2 ~]# tar xf mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
    [root@dr2 ~]# cd /usr/local
    [root@dr2 ~]# ln -sv mysql-5.7.12-linux-glibc2.5-x86_64 mysql
    [root@dr2 ~]# chown -R mysql:mysql mysql/
    [root@dr2 ~]# cd mysql
    [root@dr2 ~]# mkdir -pv /data/mydata
    [root@dr2 ~]# chown -R mysql.mysql /data/mydata/
    [root@dr2 ~]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mydata/
    [root@dr2 ~]# cp support-files/mysql.server /etc/init.d/mysqld
    [root@dr2 ~]# chmod +x /etc/init.d/mysqld
    [root@dr2 ~]# chkconfig mysqld on
    [root@dr2 ~]# cp support-files/my-default.cnf /etc/my.cnf
編輯my.cnf:
    basedir=/usr/local/mysql
    datadir=/data/mydata
    innodb_file_per_table=ON
    skip_name_resolve=ON
    character_set_server=utf8
    pid_file=/var/run/mysql.pid
添加mysql PATH路徑:(/etc/profile.d/mysql.sh)
    export PATH=/usr/local/mysql/bin:$PATH
添加mysql庫文件:(/etc/ld.so.conf.d/mysql.conf )
    /usr/local/mysql/lib
添加mysql頭文件:
    [root@dr2 mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
修改root密碼:
    [root@dr2 ~]# mysqld_safe --skip-grant-tables &
    [root@dr2 ~]# mysql
    mysql> update user set authentication_string=password('redhat') where host='localhost';
    mysql> exit
    
    [root@dr2 ~]# kill %1
啟動mysqld:
    [root@dr2 ~]# service mysqld start
再次修改密碼:
    [root@dr2 ~]# mysql
    mysql> set password for root@localhost = password('redhat');
    mysql>exit
6)編譯安裝php7
準備環境:
    [root@dr2 ~]# yum -y install bzip2-devel gd-devel libmcrypt-devel
安裝php7:
    [root@dr2 ~]# tar xf php-7.0.8.tar.xz
    [root@dr2 ~]# cd php-7.0.8
    [root@dr2 php-7.0.8]# ./configure --prefix=/usr/local/php7 \
        --enable-fpm --with-fpm-user=apache --with-fpm-group=apache \
        --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
        --with-libxml-dir=/usr --enable-xml --enable-bcmath --with-gd \
        --with-jpeg-dir --with-png-dir --with-zlib --with-freetype-dir \
        --with-gettext --enable-mbstring --with-mysqli=mysqlnd \
        --with-mysql-sock=/tmp/mysql.sock --enable-mysqlnd \
        --enable-sockets --enable-zip --with-openssl \
        --with-pcre-dir --with-mcrypt --with-bz2 \
        --without-pear --disable-phar
    [root@dr2 php-7.0.8]# make && make install 
    [root@dr2 php-7.0.8]# mkdir /etc/php.d
    [root@dr2 php-7.0.8]# cp php.ini-production /etc/php.ini
    [root@dr2 php-7.0.8]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php7-fpm.service
編輯php7-fpm.service:
    [Unit]
    Description=The PHP FastCGI Process Manager
    After=syslog.target network.target
    [Service]
    Type=simple
    PIDFile=/var/run/php7-fpm.pid
    ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
    ExecReload=/bin/kill -USR2 $MAINPID
    [Install]
    WantedBy=multi-user.target
    
    [root@dr2 php-7.0.8]# systemctl daemon-reload
    [root@dr2 php-7.0.8]# systemctl enable php7-fpm
    [root@dr2 php-7.0.8]# cd /usr/local/php7/etc
    [root@dr2 etc]# mv php-fpm.conf.default php-fpm.conf
    [root@dr2 etc]# cd php-fpm.d
    [root@dr2 php-fpm.d]# mv www.conf.default www.conf
編輯www.conf,修改如下參數:
    user = apache
    group = apache
    listen.owner = apache
    listen.group = apache
    listen.mode = 0660
    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3
啟動服務:
    [root@dr2 ~]# systemctl start php7-fpm.service
查看是否正常啟動:
    [root@dr2 ~]# systemctl status php7-fpm.service
    [root@dr2 ~]# ss -ntl
7)安裝wordpress
    [root@dr2 soft]# mkdir /www
    [root@dr2 soft]# unzip wordpress-4.6.1.zip -d /www/
編輯apache配置文件,添加虛擬主機:
    [root@dr2 soft]# cd /usr/local/apache2/conf/
編輯httpd.conf:
    ServerName www.example.com:80
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
    #DocumentRoot "/usr/local/apache2/htdocs"  #注釋中心主機
    Include conf/extra/wordpress.conf
創建虛擬主機:(/usr/local/apache2/etc/conf/extra/wordpress.conf)
    <VirtualHost *:80>
        ServerAdmin root@localhost
        DocumentRoot "/www/wordpress"
        ServerName wordpress.example.com
        DirectoryIndex index.php
        ProxyRequests Off
        ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/wordpress/$1
        <Directory "/www/wordpress">
          Options none
          AllowOverride none
          Require all granted
        </Directory>
        ErrorLog "/var/log/httpd/wordpress/error.log"
        CustomLog "/var/log/httpd/wordpress/access.log" common
    </VirtualHost>
    
    [root@dr2 soft]# mkdir -pv /var/log/httpd/wordpress
    [root@dr2 soft]# chown -R apache:apache /var/log/httpd/
檢查:
    [root@dr2 soft]# httpd -t
    [root@dr2 soft]# httpd -M #查看當前apache裝載的模塊
重新啟動服務:
    [root@dr2 soft]# systemctl restart httpd.service
準備wordpress所用的數據庫:
    [root@dr2 soft]# mysql -uroot -p
    mysql> create database wpdb default charset utf8;
    Query OK, 1 row affected (0.01 sec)
    mysql> grant all on wpdb.* to wpuser@localhost identified by 'redhat';
    Query OK, 0 rows affected, 2 warnings (0.01 sec)
    mysql> grant all on wpdb.* to wpuser@'%' identified by 'redhat';
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    mysql> \q
安裝wordpress:
瀏覽器端輸入:http://IP;安裝提示安裝即可;

題目4:
編輯虛擬主機配置文件:extra/httpd-vhost1.conf
    <VirtualHost *:80>
        ServerAdmin root@localhost
        DocumentRoot "/web/vhosts/www1"
        ServerName www1.stuX.com
        <Directory "/web/vhosts/www1">
          Options none
          AllowOverride none
          Require all granted
        </Directory>
        <Location /server-status>
          SetHandler server-status
          AuthType Basic
          AuthName "Auth Aceess"
          AuthBasicProvider file
          AuthUserFile "/usr/local/apache2/conf/extra/password"
          Require valid-user
        </Location>
        ErrorLog "/var/log/httpd/www1.err"
        CustomLog "/var/log/httpd/www1.access" common
    </VirtualHost>
    <VirtualHost *:80>
        ServerAdmin root@localhost
        DocumentRoot "/web/vhosts/www2"
        ServerName www2.stuX.com
        <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>
編輯httpd.conf,加載虛擬主機配置文件:
    Include conf/extra/httpd-vhost1.conf
創建相關目錄文件:
    [root@dr2 extra]# mkdir -pv /web/vhosts/www{1,2}
在www1,www2目錄下創建index.html,區分web信息;

編輯/etc/hosts
    10.0.0.4 www1.stuX.com
    10.0.0.4 www2.stuX.com
重啟服務:
    [root@dr2 extra]# httpd -t
    [root@dr2 extra]# systemctl restart httpd.service
測試:
    [root@dr2 extra]# curl http://www1.stuX.com/
    Vhost: www1
    [root@dr2 extra]# curl http://www2.stuX.com/
    Vhosts: www2
    [root@dr2 extra]# curl -I http://www1.stuX.com/server-status
    HTTP/1.1 401 Unauthorized
    Date: Sat, 29 Oct 2016 23:24:27 GMT
    Server: Apache/2.4.23 (Unix) PHP/7.0.8
    WWW-Authenticate: Basic realm="Auth Aceess"
    Content-Type: text/html; charset=iso-8859-1
    [root@dr2 extra]# curl -I -u apache:redhat http://www1.stuX.com/server-status
    HTTP/1.1 200 OK
    Date: Sat, 29 Oct 2016 23:24:28 GMT
    Server: Apache/2.4.23 (Unix) PHP/7.0.8
    Content-Length: 4068
    Content-Type: text/html; charset=ISO-8859-1
    
題目5:
1)構建私有CA
    root@dr2 ~]# yum -y install openssl
    root@dr2 ~]# cd /etc/pki/CA/
生成私鑰:
    [root@dr2 CA]# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)
生成自簽證書并自簽:
    [root@dr2 CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem \
            -out /etc/pki/CA/cacert.pem -days 365 
為CA提供所需的文件:
    [root@dr2 CA]# touch serial index.txt
    [root@dr2 CA]# echo 01 > serial
2)構建https
    [root@dr2 CA]# cd /usr/local/apache2/conf/
    [root@dr2 conf]# mkdir ssl
    [root@dr2 conf]# cd ssl
生成http私鑰:
    [root@dr2 ssl]# (umask 077;openssl genrsa -out ./httpd.key 4096)
生成證書請求:
    [root@dr2 ssl]# openssl req -new -key ./httpd.key -out ./httpd.csr -days 365
CA簽署:
    [root@dr2 ssl]# openssl ca -in httpd.csr -out ./httpd.crt -days 365
    [root@dr2 ssl]# tree .
    .
    ├── httpd.crt
    ├── httpd.csr
    └── httpd.key
3)修改httpd.conf:
    Listen 443
    LoadModule ssl_module modules/mod_ssl.so
4)修改httpd虛擬主機配置文件:extra/httpd-vhost1.conf
    [root@dr2 ssl]# cd /usr/local/apache2/conf/extra
    [root@dr2 extra]# vim httpd-vhost1.conf
    <VirtualHost *:443>
        ServerAdmin root@localhost
        DocumentRoot "/web/vhosts/www2"
        ServerName www2.stuX.com
        SSLEngine on
        SSLCertificateFile "/usr/local/apache2/conf/ssl/httpd.crt"
        SSLCertificateKeyFile "/usr/local/apache2/conf/ssl/httpd.key"
        <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>
    
5)重啟服務
    [root@dr2 extra]# httpd -t
    [root@dr2 extra]# systemctl restart httpd.service
    
題目6:
1)php編譯成httpd模塊方式:
    [root@dr2 php-7.0.8]# ./configure --prefix=/usr/local/php7 --with-config-file-path=/etc \
        --with-config-file-scan-dir=/etc/php.d --with-libxml-dir=/usr --enable-xml \
        --enable-bcmath --with-gd --with-jpeg-dir --with-png-dir --with-zlib --with-freetype-dir \
        --with-gettext --enable-mbstring --with-mysqli=mysqlnd --with-mysql-sock=/tmp/mysql.sock \
        --enable-mysqlnd --enable-sockets --enable-zip --with-openssl --with-pcre-dir \
        --with-apxs2=/usr/local/apache2/bin/apxs --with-mcrypt --with-bz2 --without-pear --disable-phar
    
    使用--with-apxs2=/path/to/apx選項會將php作為模塊編譯進apache;
    查看apache模塊:
        [root@dr2 wordpress]# httpd -M|grep -i php
        php7_module (shared)
        
2)php以fpm工作為獨立守護進程方式:
    [root@dr2 php-7.0.8]# ./configure --prefix=/usr/local/php7 --enable-fpm --with-fpm-user=apache \
    --with-fpm-group=apache --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
    --with-libxml-dir=/usr --enable-xml --enable-bcmath --with-gd --with-jpeg-dir --with-png-dir \
    --with-zlib --with-freetype-dir --with-gettext --enable-mbstring --with-mysqli=mysqlnd \
    --with-mysql-sock=/tmp/mysql.sock --enable-mysqlnd --enable-sockets --enable-zip \
    --with-openssl --with-pcre-dir  --with-mcrypt --with-bz2 --without-pear --disable-phar
    
    使用--enable-fpm,--with-fpm-user,--with-fpm-group選項,不使用--with-apxs選項;
    如果apache使用的是event機制,則使用此項--enable-maintainer-zts;
    php7官方建議apache使用prefork模式,不使用worker模式;
    
php-fpm有2種監聽狀態:
    1)Tcp/ip socket
        默認監聽在127.0.0.1的9000端口;
    2)Unix socket
        listen = /var/run/php-fpm.sock
    
    對應的apache要加載proxy_module及proxy_fcgi_module模塊;
    
php-fpm進程工作模式:
    1)static:以固定數量的子進程運行;
        pm.max_children
        
    2)dynamic:子進程數是動態改變的,類似apache的prefork模式;
        pm.max_children:最多可同時運行的子進程數量;
        pm.start_servers:啟動時子進程數量;
        pm.min_spare_servers:最小空閑子進程數量;
        pm.max_spare_servers:最大空閑子進程數量;
        
    3)ondemand:主進程啟動時不會生成子進程,只有當有請求時才會生成子進程去響應;
        pm.max_children 
        pm.process_idle_timeout:一個空閑進程被銷毀的間隔時間,單位秒;

原創文章,作者:devon,如若轉載,請注明出處:http://www.www58058.com/56401

(0)
devondevon
上一篇 2016-10-31
下一篇 2016-10-31

相關推薦

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-11-10 12:09

    過程寫的比較詳細,贊;題目中涉及的Http處理模型試用于哪些環境這類接近實戰的建議多想幾個場景會更好。加油~

欧美性久久久久