20190301 Apache與HTTP協議

Http協議
解決http協議無狀態方法
cookie 客戶端存放
session 服務端存放
http事務:一次訪問的過程
請求:request
響應:response
20190301  Apache與HTTP協議
20190301  Apache與HTTP協議
在200主機上:
[root@centos7 ~]#yum install httpd
[root@centos7 ~]#cd /var/www/html
[root@centos7 html]#echo '<h1>welcome to magedu</h1>' > index.html
[root@centos7 html]#systemctl restart httpd
20190301  Apache與HTTP協議

[root@centos7 conf.d]#curl -v 192.168.141.200   這是請求報文的頭部
* About to connect() to 192.168.141.200 port 80 (#0)
*   Trying 192.168.141.200...
* Connected to 192.168.141.200 (192.168.141.200) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.141.200
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 03 Mar 2019 03:03:39 GMT
< Server: Apache/2.4.6 (CentOS)
< Last-Modified: Sun, 03 Mar 2019 02:45:40 GMT
< ETag: "1b-58327a2b2bd1e"
< Accept-Ranges: bytes
< Content-Length: 27
< Content-Type: text/html; charset=UTF-8
<h1>welcome to magedu</h1>
* Connection #0 to host 192.168.141.200 left intact
http協議常用的狀態碼
200: 成功,請求數據通過響應報文的entity-body部分發送;OK
301: 請求的URL指向的資源已經被刪除;但在響應報文中通過首部Location指明瞭資源現在所處的新位置;Moved Permanently
302: 響應報文Location指明資源臨時新位置 Moved Temporarily
304: 客戶端發出了條件式請求,但服務器上的資源未曾發生改變,則通過響應此響應狀態碼通知客戶端;Not Modified
401: 需要輸入賬號和密碼認證方能訪問資源;Unauthorized
403: 請求被禁止;Forbidden  404: 服務器無法找到客戶端請求的資源;Not Found
500: 服務器內部錯誤;Internal Server Error
502: 代理服務器從後端服務器收到了一條僞響應,如無法連接到網關;Bad Gateway
503: 服務不可用,臨時服務器維護或過載,服務器無法處理請求    504: 網關
Web相關工具 links URL    --dump        --source
wget [option]... [URL]...    -q 靜默模式    -c 斷點續傳      -P /path 保存在指定目錄   
-O filename 保存爲指定文件名,filename 爲 – 時,發送至標準輸出         --limit-rate= 指定傳輸速率,單位K,M等
[root@centos7 html]#curl wttr.in
Weather report: Beijing, China
                                                       ┌─────────────┐                                                       
┌──────────────────────────────┬───────────────────────┤  Sun 03 Mar ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│    \  /       Partly cloudy  │    \  /       Partly cloudy  │    \  /       Partly cloudy  │    \  /       Partly cloudy  │
│  _ /"".-.     9..10 °C       │  _ /"".-.     13..14 °C      │  _ /"".-.     17 °C          │  _ /"".-.     14 °C          │
│    \_(   ).   ↙ 2 km/h       │    \_(   ).   ↖ 5 km/h       │    \_(   ).   ↑ 8-9 km/h     │    \_(   ).   ↗ 5-6 km/h     │
│    /(___(__)  20 km          │    /(___(__)  20 km          │    /(___(__)  20 km          │    /(___(__)  20 km          │
│               0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
                                                       ┌─────────────┐                                                       
┌──────────────────────────────┬───────────────────────┤  Mon 04 Mar ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│    \  /       Partly cloudy  │    \  /       Partly cloudy  │    \  /       Partly cloudy  │               Cloudy         │
│  _ /"".-.     10 °C          │  _ /"".-.     15 °C          │  _ /"".-.     16 °C          │      .--.     13..14 °C      │
│    \_(   ).   ↗ 6-8 km/h     │    \_(   ).   ↙ 6-8 km/h     │    \_(   ).   ← 5-6 km/h     │   .-(    ).   ↙ 4-6 km/h     │
│    /(___(__)  20 km          │    /(___(__)  20 km          │    /(___(__)  20 km          │  (___.__)__)  19 km          │
│               0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘

[root@centos7 html]#curl 192.168.141.200
<h1>welcome to magedu</h1>
[root@centos7 ~]#tail -f /var/log/httpd/access_log
192.168.141.150 - - [03/Mar/2019:12:00:28 +0800] "GET /hello.sh HTTP/1.1" 200 22 "-" "Wget/1.14 (linux-gnu)"
[root@centos7 html]#curl -e "http://www.baidu.com" 192.168.141.200
<h1>welcome to magedu</h1>
[root@centos7 ~]#tail -f /var/log/httpd/access_log
192.168.141.150 - - [03/Mar/2019:12:00:28 +0800] "GET /hello.sh HTTP/1.1" 200 22 "-" "Wget/1.14 (linux-gnu)"
192.168.141.200 - - [03/Mar/2019:12:17:45 +0800] "GET / HTTP/1.1" 200 27 "http://www.baidu.com" "curl/7.29.
httpd的壓力測試工具
ab, webbench, http_load, seige Jmeter 開源 Loadrunner 商業,有相關認證 tcpcopy:網易,複製生產環境中的真實請求,並將之保存 ab [OPTIONS] URL

實驗:編譯安裝httpd-2.4.38

1、下載源碼並解壓縮
[root@centos7 data]#wget http://mirrors.shu.edu.cn/apache//httpd/httpd-2.4.38.tar.bz2
--2019-03-03 12:45:15--  http://mirrors.shu.edu.cn/apache//httpd/httpd-2.4.38.tar.bz2
Resolving mirrors.shu.edu.cn (mirrors.shu.edu.cn)... 202.121.199.235
Connecting to mirrors.shu.edu.cn (mirrors.shu.edu.cn)|202.121.199.235|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7035030 (6.7M) [application/x-bzip2]
Saving to: ‘httpd-2.4.38.tar.bz2’

100%[====================================================================================>] 7,035,030   6.38MB/s   in 1.1s   

2019-03-03 12:45:16 (6.38 MB/s) - ‘httpd-2.4.38.tar.bz2’ saved [7035030/7035030]
[root@centos7 data]#wget http://mirror.bit.edu.cn/apache//apr/apr-1.6.5.tar.bz2
--2019-03-03 12:47:04--  http://mirror.bit.edu.cn/apache//apr/apr-1.6.5.tar.bz2
Resolving mirror.bit.edu.cn (mirror.bit.edu.cn)... 114.247.56.117
Connecting to mirror.bit.edu.cn (mirror.bit.edu.cn)|114.247.56.117|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 855393 (835K) [application/octet-stream]
Saving to: ‘apr-1.6.5.tar.bz2’

100%[====================================================================================>] 855,393     --.-K/s   in 0.1s    

2019-03-03 12:47:04 (6.43 MB/s) - ‘apr-1.6.5.tar.bz2’ saved [855393/855393]
[root@centos7 data]#wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
--2019-03-03 12:48:00--  http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
Resolving mirror.bit.edu.cn (mirror.bit.edu.cn)... 114.247.56.117
Connecting to mirror.bit.edu.cn (mirror.bit.edu.cn)|114.247.56.117|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 428595 (419K) [application/octet-stream]
Saving to: ‘apr-util-1.6.1.tar.bz2’

100%[====================================================================================>] 428,595     --.-K/s   in 0.1s    
2019-03-03 12:48:00 (3.48 MB/s) - ‘apr-util-1.6.1.tar.bz2’ saved [428595/428595]
tar xvf apr-1.6.5.tar.bz2
tar xvf apr-util-1.6.1.tar.bz2
tar xvf httpd-2.4.38.tar.bz2
[root@centos7 data]#ls
apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.38.tar.bz2
[root@centos7 data]#ls
apr-1.6.5  apr-1.6.5.tar.bz2  apr-util-1.6.1  apr-util-1.6.1.tar.bz2  httpd-2.4.38  httpd-2.4.38.tar.bz2
2、 將三個源碼合併
[root@centos7 data]#mv apr-1.6.5 httpd-2.4.38/srclib/apr
[root@centos7 data]#mv apr-util-1.6.1 httpd-2.4.38/srclib/apr-util
[root@centos7 data]#ls httpd-2.4.38/srclib/  
   apr  apr-util  Makefile.in
3、 安裝相關包   yum install gcc gcc-c++ glibc glibc-devel pcre-devel openssl-devel expat-devel
4、編譯安裝[root@centos7 httpd-2.4.38]#1、 cd httpd-2.4.38/
./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
2、make && make install
configure: summary of build options: 
    Server Version: 2.4.38
    Install prefix: /app/httpd24
    C compiler:     gcc -std=gnu99
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E
此時,爲了避免衝突,我們把本機yum安裝的httpd卸載,不然都走80端口會衝突。
[root@centos7 httpd-2.4.38]#cd /app/
[root@centos7 app]#ls
httpd24
[root@centos7 app]#cd httpd24/
[root@centos7 httpd24]#ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules
[root@centos7 httpd24]#ls bin/
ab         apr-1-config  apxs      dbmmanage  envvars-std  htcacheclean  htdigest  httpd:主配置文件     logresolve
apachectl:系統啓動文件  apu-1-config  checkgid  envvars    fcgistarter  htdbm         htpasswd  httxt2dbm  rotatelogs
5、[root@centos7 httpd24]#vim /etc/profile.d/env.sh  將變量寫入文件
PS1="\[\e[1;32m\][\u@\h \W]\\$\[\e[0m\]"
PATH=/app/httpd24/bin:$PATH   
[root@centos7 httpd24]#. /etc/profile.d/env.sh
[root@centos7 httpd24]#echo $PATH
/app/httpd24/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
6、嘗試啓動:[root@centos7 httpd24]#apachectl
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using centos7.localdomain. Set the 'ServerName' directive globally to suppress this message
[root@centos7 ~]#ss -ntl
State       Recv-Q Send-Q                  Local Address:Port                                 Peer Address:Port              
LISTEN      0      128                                :::80                                             :::*         

7、因爲配置已經寫入文件中
[root@centos7 ~]#cd /app/httpd24/htdocs/
[root@centos7 htdocs]#ls
index.html
[root@centos7 htdocs]#cat index.html
<html><body><h1>It works!</h1></body></html>
![](https://s1.51cto.com/images/blog/201903/05/db9f3c29a2299c616f19b902da639107.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
8、此時在進程中顯示是demon服務,我們要把它改爲Apache服務:修改文件
[root@centos7 httpd24]#ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules
[root@centos7 httpd24]#cd conf
[root@centos7 conf]#ls
extra  httpd.conf  magic  mime.types  original
[root@centos7 conf]#vim httpd.conf
User apache         
Group apache  
[root@centos7 conf]#apachectl restart
[root@centos7 conf]#ps aux
apache    58161  0.0  0.1 101620  1924 ?        S    14:33   0:00 /app/httpd24/bin/httpd
apache    58162  0.0  0.1 101620  1924 ?        S    14:33   0:00 /app/httpd24/bin/httpd
apache    58163  0.0  0.1 101620  1924 ?        S    14:33   0:00 /app/httpd24/bin/httpd
apache    58164  0.0  0.1 101620  1924 ?        S    14:33   0:00 /app/httpd24/bin/httpd
apache    58165  0.0  0.1 101620  1924 ?        S    14:33   0:00 /app/httpd24/bin/httpd
9、[root@centos7 conf]#cd /etc/init.d/
[root@centos7 init.d]#ls
functions  netconsole  network  README
[root@centos7 init.d]#vim /etc/rc.local
/app/httpd24/bin/apachectl     
[root@centos7 init.d]#ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 500 Mar  3 14:37 /etc/rc.d/rc.local
[root@centos7 init.d]#chmod +x /etc/rc.d/rc.local
[root@centos7 init.d]#reboot
[root@centos7 ~]#ss -ntl
State       Recv-Q Send-Q                  Local Address:Port                                 Peer Address:Port              
LISTEN      0      128                                :::80                                             :::* 

實驗:編譯實現基於FASTCGI的LAMP的wordpress

準備環境,兩臺主機,100做httpd+php,150做mariadb
1、將安裝包rz到主機:[root@100 ~]#ls
httpd-2.4.37.tar.bz2       apr-1.6.5.tar.bz2       mariadb-10.2.15-linux-x86_64.tar.gz       apr-util-1.6.1.tar.bz2  php-7.3.2.tar.xz                    wordpress-5.0-zh_CN.zip
[root@100 ~]#tar xf apr-1.6.5.tar.bz2
[root@100 ~]#tar xf httpd-2.4.37.tar.bz2
[root@100 ~]#tar xf php-7.3.2.tar.xz
[root@100 ~]#tar xf apr-util-1.6.1.tar.bz2
2、100主機上:mv apr-1.6.5 httpd-2.4.37/srclib/apr
mv apr-util-1.6.1  httpd-2.4.37/srclib/apr-util
3、一、cd httpd-2.4.37/
二、yum install gcc glibc  pcre-devel openssl-devel expat-devel libxml2-devel bzip2-devel libmcrypt-devel
 三、./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork --with-included-apr
四、make -j 4 && make install
4、[root@100 httpd-2.4.37]#echo 'PATH=/app/httpd24/bin:$PATH' > /etc/profile.d/lamp.sh
[root@100 httpd-2.4.37]#. /etc/profile.d/lamp.sh
5、useradd -r  -s /sbin/nologin apache
[root@centos7 httpd-2.4.37]#vim /app/httpd24/conf/httpd.conf   此項修改不是必須,主要爲了方便查看
User apache
Group apache
6、[root@100 httpd-2.4.37]#apachectl
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 0.0.0.100. Set the 'ServerName' directive globally to suppress this message
[root@100 httpd-2.4.37]#ss -ntl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128       :::80                    :::*          
7、現在我們去centos6上測試:[root@centos6 ~]#curl 192.168.141.100
<html><body><h1>It works!</h1></body></html>   此時說明Apache已經安裝成功。
8、150主機上:tar xvf mariadb-10.2.15-linux-x86_64.tar.gz -C /usr/local/
[root@centos7 ~]#cd /usr/local
[root@centos7 local]#ls
bin  games    lib    libexec                       sbin   src
etc  include  lib64  mariadb-10.2.15-linux-x86_64  share
[root@centos7 local]#ln -s mariadb-10.2.15-linux-x86_64/ mysql
[root@centos7 local]#chown -R root.root  /usr/local/mysql/
[root@centos7 local]#ls
bin  games    lib    libexec  mysql  share        etc  include  lib64  mariadb-10.2.15-linux-x86_64  sbin   src
[root@centos7 local]#cd mysql/
[root@centos7 mysql]#ls
bin                 data               lib         README-wsrep  support-files
COPYING             EXCEPTIONS-CLIENT  man         scripts
COPYING.thirdparty  include            mysql-test  share
CREDITS             INSTALL-BINARY     README.md   sql-bench
[root@centos7 mysql]#mkdir /etc/mysql/
[root@centos7 mysql]#cp support-files/my-huge.cnf  /etc/mysql/my.cnf
9、[root@centos7 mysql]#vim /etc/mysql/my.cnf
[mysqld]
datadir=/data/mysql
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
10、[root@centos7 mysql]#chown mysql.mysql /data/mysql
[root@centos7 mysql]#ll /data/mysql
total 0
[root@centos7 mysql]#ls
bin                 data               lib         README-wsrep  support-files
COPYING             EXCEPTIONS-CLIENT  man         scripts
COPYING.thirdparty  include            mysql-test  share
CREDITS             INSTALL-BINARY     README.md   sql-bench
[root@centos7 mysql]#scripts/mysql_install_db  --datadir=/data/mysql --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
2019-03-04 17:09:17 140578725427008 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
2019-03-04 17:09:17 140578724841216 [Warning] Failed to load slave replication state from table mysql.gtid_slave_pos: 1146: Table 'mysql.gtid_slave_pos' doesn't exist
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'./bin/mysqladmin' -u root password 'new-password'
'./bin/mysqladmin' -u root -h centos7.localdomain password 'new-password'
Alternatively you can run:
'./bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '.' ; ./bin/mysqld_safe --datadir='/data/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd './mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
[root@centos7 mysql]#ls -l /data/mysql/
total 110660
-rw-rw----. 1 mysql mysql    16384 Mar  4 17:09 aria_log.00000001
-rw-rw----. 1 mysql mysql       52 Mar  4 17:09 aria_log_control
-rw-rw----. 1 mysql mysql      938 Mar  4 17:09 ib_buffer_pool
-rw-rw----. 1 mysql mysql 12582912 Mar  4 17:09 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Mar  4 17:09 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Mar  4 17:09 ib_logfile1
drwx------. 2 mysql root      4096 Mar  4 17:09 mysql
-rw-rw----. 1 mysql mysql    29009 Mar  4 17:09 mysql-bin.000001
-rw-rw----. 1 mysql mysql       19 Mar  4 17:09 mysql-bin.index
-rw-rw----. 1 mysql mysql        7 Mar  4 17:09 mysql-bin.state
drwx------. 2 mysql mysql       20 Mar  4 17:09 performance_schema
drwx------. 2 mysql root         6 Mar  4 17:09 test
11、[root@centos7 mysql]#cp support-files/mysql.server /etc/init.d/mysqld
[root@centos7 mysql]#ll /etc/init.d/mysqld
-rwxr-xr-x. 1 root root 12273 Mar  4 17:12 /etc/init.d/mysqld
[root@centos7 mysql]#chkconfig --add mysqld
ch[root@centos7 mysql]#chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off  三個文件已正常可使用
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@centos7 mysql]#service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]
12、[root@centos7 mysql]#echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7 mysql]#. /etc/profile.d/mysql.sh
[root@centos7 mysql]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.15-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wpdb;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> grant all on wpdb.* to wpuser@'192.168.141.%' identified by '123gxy';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit

13、[root@centos7 data]#unzip wordpress-5.0-zh_CN.zip
[root@centos7 data]#ls
mysql  php-7.3.2  wordpress  wordpress-5.0-zh_CN.zip
[root@centos7 data]#cp -R wordpress /app/httpd24/htdocs/
[root@centos7 data]#cd wordpress/
[root@centos7 wordpress]#ls
index.php        wp-blog-header.php    wp-includes        wp-settings.php
license.txt      wp-comments-post.php  wp-links-opml.php  wp-signup.php
readme.html      wp-config-sample.php  wp-load.php        wp-trackback.php
wp-activate.php  wp-content            wp-login.php       xmlrpc.php
wp-admin         wp-cron.php           wp-mail.php
[root@centos7 wordpress]#cp wp-config-sample.php wp-config.php
[root@centos7 wordpress]#vim wp-config.php
// ** MySQL 設置 - 具體信息來自您正在使用的主機 ** //
/** WordPress數據庫的名稱 */
define('DB_NAME', 'wpdb');

/** MySQL數據庫用戶名 */
define('DB_USER', 'wpuser');

/** MySQL數據庫密碼 */
define('DB_PASSWORD', '123gxy');

/** MySQL主機 */
define('DB_HOST', '192.168.141.150');
14、[root@centos7 ~]#vim /app/httpd24/conf/httpd.conf 把該文件下的2行取消註釋,激活
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
<IfModule dir_module>
  DirectoryIndex index.php index.html
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
AddType application/x-httpd-php  .php
AddType application/x-httpd-php-source  .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1   這四行加入
[root@centos7 htdocs]#apachectl restart

15、我們配置php[root@centos7 wordpress]#cd /data/php-7.3.2/
[root@centos7 php-7.3.2]#cp php.ini-production /etc/php.ini
[root@centos7 php-7.3.2]#chmod +x /etc/init.d/php-fpm
[root@centos7 php-7.3.2]#chkconfig --add php-fpm
[root@centos7 php-7.3.2]#chkconfig --list
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
php-fpm            0:off    1:off    2:on    3:on    4:on    5:on    6:off
16、[root@centos7 php-7.3.2]#ls /app/php/
bin  etc  include  lib  php  sbin  var  這是編譯好的文件
[root@centos7 php-7.3.2]#cd /app/php/etc
[root@centos7 etc]#ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@centos7 etc]#cp php-fpm.conf.default php-fpm.conf
[root@centos7 etc]#cp  php-fpm.d/www.conf.default php-fpm.d/www.conf
啓動服務:[root@centos7 etc]#service php-fpm start
Starting php-fpm  done
[root@centos7 etc]#ss -ntl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128      127.0.0.1:9000                         *:*         
現在我們去訪問頁面:
![](https://s1.51cto.com/images/blog/201903/05/8fe088338f5638d5994a2c0a4da738b2.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

實驗:編譯實現基於FASTCGI的LAMP的wordpress

基於模塊方式安裝使用php
1、yum install php
[root@centos7 ~]#cd /var/www/html
[root@centos7 html]#ls
admin  a.txt  hello.sh  index.html  setcookie.php  test.html
[root@centos7 html]#vim index.php
<?php
echo date("Y/m/d H:i:s");
phpinfo();
?>  下圖爲動態頁面:
![](https://s1.51cto.com/images/blog/201903/05/6f0e0f0b1aef141b47245c70a3033ac1.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@centos7 html]#yum install php-mysql  要連數據庫就要下載該包
3、yum install mariadb-server
[root@centos7 html]#systemctl restart mariadb
4、[root@centos7 html]#mysql_secure_installation  安全加固腳本
5、[root@centos7 html]#vim mysql.php
<?php
try {
$user='root';
$pass='123gxy';
$dbh= new PDO('mysql:host=localhost;dbname=mysql', $user, $pass);                                                                         
foreach($dbh->query('SELECT user,host from user') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException$e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
[root@centos7 html]#systemctl restart mariadb
6、[root@centos7 ~]#mysql -uroot -p123gxy
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)
![](https://s1.51cto.com/images/blog/201903/05/615bb9af29b8d06743de48dde7808bef.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
此時這個實驗就做完了。

實驗:php使用PDO擴展連接數據庫

將壓縮包傳到主機並壓縮:
tar xvf phpMyAdmin-4.0.10.20-all-languages.tar.xz
[root@centos7 ~]#cd phpMyAdmin-4.0.10.20-all-languages
[root@centos7 phpMyAdmin-4.0.10.20-all-languages]#pwd
/root/phpMyAdmin-4.0.10.20-all-languages
[root@centos7 phpMyAdmin-4.0.10.20-all-languages]#cd ..
[root@centos7 ~]#ls
!               hellodb_innodb.sql    phpMyAdmin-4.0.10.20-all-languages
anaconda-ks.cfg  initial-setup-ks.cfg  phpMyAdmin-4.0.10.20-all-languages.tar.xz
[root@centos7 ~]#mv  phpMyAdmin-4.0.10.20-all-languages /var/www/html/pma
[root@centos7 ~]#cd /var/www/html/pma
[root@centos7 pma]#yum install php-mbstring  安裝這個輔助工具
[root@centos7 pma]#cp config.sample.inc.php config.inc.php
[root@centos7 pma]#vim config.inc.php  此項是修改默認密碼,基於安全。
$cfg['blowfish_secret'] = '123gxy'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */     
[root@centos7 pma]#systemctl restart httpd
![](https://s1.51cto.com/images/blog/201903/05/e7f723aac55dd421a028d0976e5dbd76.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
登陸用戶及密碼即可進入圖形化界面:
![](https://s1.51cto.com/images/blog/201903/05/ba92983d81820468fe5d8d09d47aa5d3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
這時我們已經創建了用戶如下:
![](https://s1.51cto.com/images/blog/201903/05/56ed479904f9184459fc063fb38b454f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@centos7 ~]#mysql -uwpuser -p123gxy -h192.168.141.200.
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress          |該用戶已經被創建成功。
+--------------------+
2 rows in set (0.00 sec)     該實驗完成。在網站上訪問及在mysql數據庫查詢結果一致。

實驗:部署wordpress

將下載的軟件包傳到主機並解壓
tar xvf wordpress-5.0.3-zh_CN.tar.gz
[root@centos7 ~]#cd wordpress/
[root@centos7 wordpress]#ls
index.php    wp-activate.php     wp-comments-post.php  wp-cron.php        wp-load.php   wp-settings.php   xmlrpc.php
license.txt  wp-admin            wp-config-sample.php  wp-includes        wp-login.php  wp-signup.php
readme.html  wp-blog-header.php  wp-content            wp-links-opml.php  wp-mail.php   wp-trackback.php
[root@centos7 wordpress]#cp wp-config-sample.php wp-config.php
[root@centos7 wordpress]#vim wp-config.php
/** WordPress數據庫的名稱 */
define('DB_NAME', 'wordpress');

/** MySQL數據庫用戶名 */
define('DB_USER', 'wpuser');

/** MySQL數據庫密碼 */
define('DB_PASSWORD', '123gxy');

/** MySQL主機 */
define('DB_HOST', '192.168.141.200');
[root@centos7 ~]#mv wordpress /var/www/html/
[root@centos7 ~]#cd /var/www/html/
[root@centos7 html]#ls
admin   pma  setcookie.php   wordpress
下面我們去網站登錄:
![](https://s1.51cto.com/images/blog/201903/05/becbf2770db9d8360808b53978549105.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
成功登陸後的後臺頁面:我們就可以自建博客了:
![](https://s1.51cto.com/images/blog/201903/05/4c1f46bffbd03a52a6078ed3d33ea04f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

實驗:源碼編譯php.fpm

1、[root@centos7 ~]#ls
httpd-2.4.37.tar.bz2     apr-util-1.6.1.tar.bz2     apr-1.6.5.tar.bz2
[root@centos7 ~]#mv httpd-2.4.38.tar.bz2 apr-* /data/
[root@centos7 ~]#cd /data/
[root@centos7 data]#ls
apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.38.tar.bz2
tar xvf xxxx 將其三個包解壓:
root@centos7 data]#ls
apr-1.6.5          apr-util-1.6.1          httpd-2.4.38      apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.38.tar.bz2
[root@centos7 data]#mv apr-1.6.5 httpd-2.4.38/srclib/apr
[root@centos7 data]#mv apr-util-1.6.1 httpd-2.4.38/srclib/apr-util
2、[root@centos7 httpd-2.4.38]#1、yum install gcc glibc  pcre-devel openssl-devel expat-devel   安裝必要包
[root@centos7 httpd-2.4.38]#./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
2、[root@centos7 data]#make && make install
3、[root@centos7 httpd-2.4.38]#cd /app/httpd24/
[root@centos7 httpd24]#ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules
[root@centos7 httpd24]#cd bin
[root@centos7 bin]#ls
ab            apu-1-config  dbmmanage    fcgistarter   htdigest  httxt2dbm
apachectl     apxs          envvars      htcacheclean  htpasswd  logresolve
apr-1-config  checkgid      envvars-std  htdbm         httpd     rotatelogs
[root@centos7 bin]#./apachectl start  開啓服務
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using centos7.localdomain. Set the 'ServerName' directive globally to suppress this message
httpd (pid 6865) already running
[root@centos7 bin]#ss -ntl
State      Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
LISTEN     0      128                     :::80                                  :::*                 
4、現在去另一主機上curl一下:[root@centos7 ~]#curl 192.168.141.200
<html><body><h1>It works!</h1></body></html>
5、 yum install php-fpm php-mysql mariadb-server  安裝必要的包
6、一、[root@centos7 bin]#vim /app/httpd24/conf/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so     這2項取消註釋,使之生效。

user apache
Group apache

</IfModule>
AddType application/x-httpd-php  .php
AddType application/x-httpd-php-source  .phps
ProxyRequests Off
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/run/php-fpm.sock|fcgi://localhost/app/httpd24/htdocs/"     這是指定套接字的路徑

二、[root@centos7 bin]# vim /etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
listen=/var/run/php-fpm.sock

listen.mode = 0666      

三、[root@centos7 bin]#ll /var/run/
srw-rw-rw-.  1 root           root              0 Mar  5 21:50 php-fpm.sock   權限是066

四、root@centos7 bin]#cd /app/httpd24/htdocs/
[root@centos7 htdocs]#ls
index.html
[root@centos7 htdocs]#vim index.php
<?php
phpinfo();
?>              
7、[root@centos7 bin]#./apachectl start

8、現在我們去訪問網頁:
![](https://s1.51cto.com/images/blog/201903/05/80a7fdb78f14afa13ea63d0b99a29e71.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章