馬哥教育網絡班22期+第十二周課程練習

1、請描述一次完整的http請求處理過程。
    (1) 客戶端與服務端通過TCP三次握手建立或處理連接:接收請求或拒絕請求
    (2) 接收請求:接收來自于網絡上的主機請求報文中對某特定資源的一次請求的過程
    (3) 處理請求:對請求報文進行解析,獲取客戶端請求的資源及請求方法等相關信息
    (4) 訪問資源:獲取請求報文中請求的資源
    (5) 構建響應報文
    (6) 發送響應報文
    (7) 記錄日志
2、httpd所支持的處理模型有哪些,他們的分別使用于哪些環境。
prefork:多進程模型,每個進程響應一個請求
    一個主進程:負責生成子進程及回收子進程;負責創建套接字;負責接收請求,并將其派發給某子進程進行處理
    n個子進程:每個子進程處理一個請求
    工作模型:會預先生成幾個空閑進程,隨時等待用于響應用戶請求;最大空閑和最小空閑
worker:多進程多線程模型,每線程處理一個用戶請求
    一個主進程:負責生成子進程;負責創建套接字;負責接收請求,并將其派發給某子進程進行處理
    多個子進程:每個子進程負責生成多個線程
    每個線程:負責響應用戶請求
    并發響應數量:m*n
        m:子進程數量
        n:每個子進程所能創建的最大線程數量
event:事件驅動模型,多進程模型,每個進程響應多個請求
    一個主進程:負責生成子進程;負責創建套接字;負責接收請求,并將其派發給某子進程進行處理
    子進程:基于事件驅動機制直接響應多個請求
3、源碼編譯安裝LAMP環境(基于wordpress程序),并寫出詳細的安裝、配置、測試過程。

源碼編譯安裝LAMP環境準備:
httpd-2.4:prefork模型
mairadb-5.5:通用二進制格式(php5需要依賴于mariadb,所以得先裝mariadb)
php-5.4:編譯為httpd的modules

注意:首先配置好EPEL源
安裝編譯環境:
# yum -y groupinstall "Development Tools" "Server Platform Development"

(1)安裝httpd
# yum -y install pcre-devel apr-devel apr-util-devel openssl-devel
# tar xf httpd-2.4.23.tar.bz2
# cd httpd-2.4.23
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --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
# vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache24/bin:$PATH
# . /etc/profile.d/httpd.sh
# ln -sv /usr/local/apache24/include /usr/include/httpd
# apachectl start

(2)安裝mairadb通用二進制格式
# useradd -r mysql
# mkdir -pv /mydata/data
# chown -R mysql:mysql /mydata/data
# tar xf mariadb-5.5.54-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-5.5.54-linux-x86_64 mysql
# cd /usr/local/mysql
# chown -R root:mysql ./*
# cp support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# mkdir /var/log/mariadb
# chown mysql:mysql /var/log/mariadb
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf,在[mysqld]段添加如下三個選項:
    datadir=/mydata/data
    innodb_file_per_table = ON
    skip_name_resolve = ON
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
測試配置文件語法:
# service mysqld configtest
# service mysqld start
# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
# ldconfig
# ln -sv /usr/local/mysql/include /usr/include/mysql

(3)安裝php
# yum -y install libxml2-devel libmcrypt-devel bzip2-devel
# tar xf php-5.4.45.tar.gz
# cd php-5.4.45
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-mbstring --with-png-dir 
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-apxs2=/usr/local/apache/bin/apxs
# make && make install

# cp php-5.4.45/php.ini-production /etc/php.ini
# cp /etc/apache/httpd.conf{,.backup}
# vim /etc/apache/httpd.conf,在相應位置添加如下兩個選項:
    AddType application/x-httpd-php .php
    DirectoryIndex index.php index.html
測試httpd配置文件語法:
# apachectl -t
# apachectl restart

(4)部署wordpress
# tar xf wordpress-4.7-zh_CN.tar.gz -C /usr/local/apache/htdocs
# chmod -R 777 /usr/local/apache/htdocs/wordpress
創建wordpress數據庫、用戶名及密碼
# mysql
mysql> CREATE DATABASE wordpress;
mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY 'wordpress';
mysql> FLUSH PRIVILEGES;

清除防火墻規則:
# iptables -F
在瀏覽器進行訪問及部署wordpress:
http://192.168.0.200/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)。

# mkdir -pv /web/vhosts/{www1,www2}
# mkdir /var/log/httpd
# echo "<h1>www1.stuX.com</h1>" > /web/vhosts/www1/index.html
# echo "<h1>www2.stuX.com</h1>" > /web/vhosts/www2/index.html
# vim /etc/apache/httpd.conf
注釋中心主機 #DocumentRoot "/usr/local/apache/htdocs"
引用虛擬主機配置文件:Include /etc/apache/extra/httpd-vhosts.conf
# vim /etc/apache/extra/httpd-vhosts.conf
注釋默認配置,添加如下兩個虛擬主機:
<VirtualHost *:80>
    ServerName www1.stuX.com
    DocumentRoot "/web/vhosts/www1"
    ErrorLog "/var/log/httpd/www1.err"
    CustomLog "/var/log/httpd/www1.access" combined
    <Directory "/web/vhosts/www1">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
    <Location /server-status>
        SetHandler server-status
        AuthType Basic
        AuthName "server-status"
        AuthUserFile "/etc/apache/.status_pwd"
        Require valid-user
    </Location>
</VirtualHost>

<VirtualHost *:80>
    ServerName www2.stuX.com
    DocumentRoot "/web/vhosts/www2"
    ErrorLog "/var/log/httpd/www2.err"
    CustomLog "/var/log/httpd/www2.access" combined
    <Directory "/web/vhosts/www2">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
# htpasswd -c /etc/apache/.status_pwd status
# apachectl -t
# apachectl restart

測試主機:
# vim /etc/hosts
192.168.20.200 www1.stuX.com www2.stuX.com
在瀏覽器進行訪問測試:
http://www1.stuX.com
http://www2.stuX.com

5、為第4題中的第2個虛擬主機提供https服務,使得用戶可以通過https安全的訪問此web站點:
   (1)要求使用證書認證,證書中要求使用的國家(CN)、州(HA)、城市(ZZ)和組織(MageEdu);
   (2)設置部門為Ops,主機名為www2.stuX.com,郵件為admin@stuX.com。

構建私有CA頒發SSL證書
# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)
# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650
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
# mkdir -pv /etc/pki/CA/{certs,crl,newcerts}
# touch /etc/pki/CA/{serial,index.txt}
# echo 01 > /etc/pki/CA/serial

在請求主機生成私鑰,并向CA申請簽署證書
# (umask 077; openssl genrsa -out /etc/apache/ssl/httpd.key 2048)
# openssl req -new -key /etc/apache/ssl/httpd.key -out /etc/apache/ssl/httpd.csr -days 365
CA簽署證書
# openssl ca -in /etc/apache/ssl/httpd.csr -out /etc/pki/CA/certs/httpd.crt
# cp /etc/pki/CA/certs/httpd.crt /etc/apache/ssl/

# vim /etc/apache/httpd.conf
引用SSL配置文件:Include /etc/apache/extra/httpd-ssl.conf
加載如下模塊:
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
# vim /etc/apache/extra/httpd-ssl.conf
<VirtualHost _default_:443>
DocumentRoot "/web/vhosts/www2"
ServerName www2.stuX.com
ErrorLog "/var/log/httpd/www2.ssl.err"
TransferLog "/var/log/httpd/www2.ssl.access"
<Directory "/web/vhosts/www2">
    Options None
    AllowOverride None
    Require all granted
</Directory>
SSLEngine on
SSLCertificateFile "/etc/apache/ssl/httpd.crt"
SSLCertificateKeyFile "/etc/apache/ssl/httpd.key"
</VirtualHost>
# apachectl -t
# apachectl restart

在瀏覽器進行訪問測試:
https://www2.stuX.com

6、在LAMP架構中,請分別以php編譯成httpd模塊形式和php以fpm工作為獨立守護進程的方式來支持httpd,列出詳細的過程。

(1)php編譯成httpd模塊的形式在第3題中已實現。

(2)php編譯成以fpm形式工作為獨立守護進程的詳細步驟如下:
注意:首先配置好EPEL源
安裝編譯環境:
# yum -y groupinstall "Development Tools" "Server Platform Development"

編譯安裝php-fpm:
# yum -y install libxml2-devel libmcrypt-devel bzip2-devel
# tar xf php-5.4.45.tar.gz
# cd php-5.4.45
# ./configure --prefix=/usr/local/phpfpm --with-mysql=/usr/local/mysql --with-openssl --enable-mbstring --with-png-dir 
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm
# make && make install

# cp php-5.4.45/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# chkconfig --add php-fpm
# cp /usr/local/phpfpm/etc/php-fpm.conf.default /usr/local/phpfpm/etc/php-fpm.conf
# vim /usr/local/phpfpm/etc/php-fpm.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
# service php-fpm start
# ss -tnl
若tcp的9000端口處于監聽狀態,表明php-fpm啟動成功。

# vim /etc/apache/httpd.conf
    DirectoryIndex index.php index.html
加載如下模塊:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# vim /etc/apache/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ServerName 192.168.0.200
    DocumentRoot "/usr/local/apache/htdocs"
    ErrorLog "/usr/local/apache/logs/httpd_fpm.error"
    CustomLog "/usr/local/apache/logs/httpd_fpm.access" combined
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1
    <Directory "/usr/local/apache/htdocs">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
# apachectl -t
# apachectl restart

在瀏覽器進行訪問測試:
http://192.168.0.200/phpinfo.php
若Server API為FPM/FastCGI,表明php-fpm安裝成功。

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

(0)
蘿卜蘿卜
上一篇 2016-12-29
下一篇 2016-12-30

相關推薦

  • 用戶和組相關配置文件

    1. /etc/passwd文件詳解 輸入vi /etc/passwd 可以查看此文件的內容 [root@localhost ~]# vi /etc/passwdroot:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/…

    Linux干貨 2016-10-23
  • CentOS上配置rsyslog客戶端用以遠程記錄日志

    rsyslog是一個開源工具,被廣泛用于Linux系統以通過TCP/UDP協議轉發或接收日志消息。rsyslog守護進程可以被配置成兩種環境,一種是配置成日志收集服務器,rsyslog進程可以從網絡中收集其它主機上的日志數據,這些主機會將日志配置為發送到另外的遠程服務器。rsyslog的另外一個用法,就是可以配置為客戶端,用來過濾和發送內部日志消息到本地文件…

    Linux干貨 2015-02-14
  • Bash編程之流程控制

    Bash作為一種過程式編程語言,擁有一套流程控制體系,可完成選擇執行,循環執行功能。下面分別介紹if/else,case,select,for,while/until等語法的使用。

    Linux干貨 2016-08-21
  • yum軟件管理使用詳解

    yum介紹 yum客戶端的使用 配置文件項作說明: 案例repo配置文件。 創建yum源命令(yum-config-manager) yum list相關命令 yum安裝 yum更新和升級 yum查找和顯示 yum刪除程序 查看倉庫 實戰演練本地yum倉庫(光盤) YUM內置變量 創建YUM倉庫(http) yum介紹 yum命令是在Fedora和RedHa…

    Linux干貨 2016-09-06
  • shell腳本實現MD5破解的相關小計

    分享編寫shell腳本暴力破解md5的方法 方法一 單個破解法(以10位數為例) #!/bin/bash read -p “please input strings:”string for n in {0..33000} do Md5=`echo $n |md5sum|cut -c 1-10`      …

    Linux干貨 2017-03-26
  • 程序包的編譯安裝

    程序包的編譯安裝 之所以需要安裝編譯程序包,是為了能及時更新程序包,制作好的rpm包,版本一般都有點老了,所以編譯安裝是必報的,而且我們可以自己定義安裝路徑,想卸載直接刪除就KO了; 在centos7.3環境下安裝apache http服務: 1.首先獲取最新的apache源碼包下載到/root目錄下; 2.檢查安裝環境,沒有就安裝環境:   記住…

    Linux干貨 2017-03-09

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-01-04 16:13

    寫的很棒,可以把tcp的四次斷開也寫上,如果能畫個圖說明一下是最好的

欧美性久久久久