LAMP架構(一)

LAMP架構介紹

Linux:操作系統

Apache(httpd):提供web服務的軟件

MySQL:存儲數據的,不能存圖片和文件什麼的,

PHP:腳本語言,和shell類似但更復雜,做網址爲主

httpd、MySQL和PHP可以在一臺機器,也可以分開(httpd和PHP一定要在一起)

MySQL/MariaDB介紹

Mariadb是MySQL的一個分支,官網https://mariadb.com/最新版本10.2

MySQL是一個關係型數據庫,由mysql ab公司開發,mysql在2008年被sun公司收購(10億刀),2009年sun公司被oracle公司收購(74億刀)

MySQL官網https://www.mysql.com 最新版本5.7GA/8.0DMR

MySQL5.6變化比較大,5.7性能上有很大提升

MariaDB主要由SkySQL公司(現更名爲MariaDB公司)維護,SkySQL公司由MySQL原作者帶領大部分原班人馬創立.

Mariadb5.5版本對應MySQL的5.5,10.0對應MySQL5.6

Community 社區版本,Enterprise 企業版,GA(Generally Available)指通用版本,在生產環境中用的,DMR(Development Milestone Release)開發里程碑發佈版,RC(Release Candidate)發行候選版本,Beta開放測試版本,Alpha內部測試版本

MySQL安裝

MySQL的幾個常用安裝包:rpm、源碼、二進制免編譯

二進制免編譯:在Linux系統上做了編譯,再把編譯好的文件打包壓縮發佈,不用配置和編譯,直接拿來用;和rpm包類似,但優於rpm包可以自定義安裝目錄,rpm只能固定安裝目錄。

追求極致性能的話,最好自己編譯。

1、下載mysql的包,下到之前約定的目錄cd /usr/local/src/,軟件包區分平臺(64位和32位)

  [root@zyshanlinux-001 ~]# cd /usr/local/src
  [root@zyshanlinux-001 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  --2018-06-25 22:01:08--  http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  正在解析主機 mirrors.sohu.com (mirrors.sohu.com)... 221.236.12.140
  正在連接 mirrors.sohu.com (mirrors.sohu.com)|221.236.12.140|:80... 已連接。
  已發出 HTTP 請求,正在等待迴應... 200 OK
  長度:314581668 (300M) [application/octet-stream]
  正在保存至: “mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz”
  
  100%[============================================>] 314,581,668  375KB/s 用時 17m 30s
  
  2018-06-25 22:18:38 (293 KB/s) - 已保存 “mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz” [314581668/314581668])

2、下載後解壓

  [root@zyshanlinux-001 src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  mysql-5.6.35-linux-glibc2.5-x86_64/README
  mysql-5.6.35-linux-glibc2.5-x86_64/data/test/db.opt

3、解壓後把mysql包改名放到/usr/local/目錄下

  [root@zyshanlinux-001 src]# ls /usr/local/mysql
  ls: 無法訪問/usr/local/mysql: 沒有那個文件或目錄
  [root@zyshanlinux-001 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
  [root@zyshanlinux-001 src]# ls /usr/local/mysql
  bin      data  include  man         README   share      support-files
  COPYING  docs  lib      mysql-test  scripts  sql-bench

4、並跳轉到改名字後的目錄下

  [root@zyshanlinux-001 src]# cd !$
  cd /usr/local/mysql
  [root@zyshanlinux-001 mysql]# ls
  bin      data  include  man         README   share      support-files
  COPYING  docs  lib      mysql-test  scripts  sql-bench

5、創建mysql用戶,新建data目錄

  [root@zyshanlinux-001 mysql]# useradd mysql
  [root@zyshanlinux-001 mysql]# mkdir /data/
  [root@zyshanlinux-001 mysql]# ls /data/
  [root@zyshanlinux-001 mysql]# 

6、指定用戶爲mysql,指定datadir(mysql數據存放目錄),它會在目錄data中創建mysql目錄,但必須先有data目錄(即父目錄),整個過程是初始化

  [root@zyshanlinux-001 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
  Data::Dumper

告警:少了模塊,模塊叫Dumper

解決思路:少了包或者模塊,去安裝包,不知道名字就去模糊搜索

  [root@zyshanlinux-001 mysql]# yum list |grep perl |grep -i dumper
  perl-Data-Dumper.x86_64                  2.145-3.el7                   base     
  perl-Data-Dumper-Concise.noarch          2.020-6.el7                   epel     
  perl-Data-Dumper-Names.noarch            0.03-17.el7                   epel     
  perl-XML-Dumper.noarch                   0.81-17.el7                   base     
  [root@zyshanlinux-001 mysql]# 

搜索到的結果有4個包,可以考慮逐個嘗試有可能解決,但我知道是第一個。直接安裝,再執行告警前的命令

  [root@zyshanlinux-001 mysql]# yum install -y perl-Data-Dumper
  [root@zyshanlinux-001 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

怎麼知道執行的過程是否對?

第一種方法:執行反饋中有2個OK

  OK
  
  Filling help tables...2018-06-25 23:03:58 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
  2018-06-25 23:03:58 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
  2018-06-25 23:03:58 0 [Note] ./bin/mysqld (mysqld 5.6.35) starting as process 2965 ...
  2018-06-25 23:03:58 2965 [Note] InnoDB: Using atomics to ref count buffer pool pages
  2018-06-25 23:03:58 2965 [Note] InnoDB: The InnoDB memory heap is disabled
  2018-06-25 23:03:58 2965 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
  2018-06-25 23:03:58 2965 [Note] InnoDB: Memory barrier is not used
  2018-06-25 23:03:58 2965 [Note] InnoDB: Compressed tables use zlib 1.2.3
  2018-06-25 23:03:58 2965 [Note] InnoDB: Using Linux native AIO
  2018-06-25 23:03:58 2965 [Note] InnoDB: Using CPU crc32 instructions
  2018-06-25 23:03:58 2965 [Note] InnoDB: Initializing buffer pool, size = 128.0M
  2018-06-25 23:03:58 2965 [Note] InnoDB: Completed initialization of buffer pool
  2018-06-25 23:03:58 2965 [Note] InnoDB: Highest supported file format is Barracuda.
  2018-06-25 23:03:58 2965 [Note] InnoDB: 128 rollback segment(s) are active.
  2018-06-25 23:03:58 2965 [Note] InnoDB: Waiting for purge to start
  2018-06-25 23:03:58 2965 [Note] InnoDB: 5.6.35 started; log sequence number 1625977
  2018-06-25 23:03:58 2965 [Note] Binlog end
  2018-06-25 23:03:58 2965 [Note] InnoDB: FTS optimize thread exiting.
  2018-06-25 23:03:58 2965 [Note] InnoDB: Starting shutdown...
  2018-06-25 23:04:00 2965 [Note] InnoDB: Shutdown completed; log sequence number 1625987
  OK

第二種方法:echo $?反饋是0,要記住echo $?是檢驗上一條命令是否對的,所以在執行完上條命令後不要執行任何其他命令,馬上執行echo $?

  [root@zyshanlinux-001 mysql]# echo $?
  [root@zyshanlinux-001 mysql]# 0

7、拷貝配置文件和啓動腳本

  [root@zyshanlinux-001 mysql]# ls support-files/my-default.cnf
  support-files/my-default.cnf                  ##配置文件路徑##

可以用命令cp support-files/my-default.cnf /etc/my.cnf

拷貝配置文件到/etc/my.cnf

  [root@zyshanlinux-001 mysql]# ls /etc/my.cnf  ##其實系統該目錄下已經有了my.cnf的##
  /etc/my.cnf
  [root@zyshanlinux-001 mysql]# rpm -qf /etc/my.cnf  ##查看是哪個rpm包安裝的,是mariadb##
  mariadb-libs-5.5.56-2.el7.x86_64

也可以選用系統原有的,但需要該一下my.cnf文件內容,把下面

  [root@zyshanlinux-001 mysql]# vi /etc/my.cnf
  [root@zyshanlinux-001 mysql]# vim !$
  vim /etc/my.cnf
  
  [mysqld]
  datadir=/var/lib/mysql
  socket=/var/lib/mysql/mysql.sock
  # Disabling symbolic-links is recommended to prevent assorted security risks
  symbolic-links=0
  # Settings user and group are ignored when systemd is used.
  # If you need to run mysqld under a different user or group,
  # customize your systemd unit file for mariadb according to the
  # instructions in http://fedoraproject.org/wiki/Systemd
  
  [mysqld_safe]
  log-error=/var/log/mariadb/mariadb.log
  pid-file=/var/run/mariadb/mariadb.pid
  
  #
  # include all files from the config directory
  #
  !includedir /etc/my.cnf.d

改爲

  [mysqld]
  datadir=/data/mysql  ##改路徑##
  socket=/tmp/mysql.sock  ##改路徑##
  # Disabling symbolic-links is recommended to prevent assorted security risks
  symbolic-links=0
  # Settings user and group are ignored when systemd is used.
  # If you need to run mysqld under a different user or group,
  # customize your systemd unit file for mariadb according to the
  # instructions in http://fedoraproject.org/wiki/Systemd
  
  [mysqld_safe]
  #log-error=/var/log/mariadb/mariadb.log  ##註釋掉##
  #pid-file=/var/run/mariadb/mariadb.pid  ##註釋掉##
  
  #
  # include all files from the config directory
  #
  #!includedir /etc/my.cnf.d  ##註釋掉##

把啓動腳本放到/etc/init.d/裏面去

  [root@zyshanlinux-001 mysql]# ls /etc/init.d/mysqld
  ls: 無法訪問/etc/init.d/mysqld: 沒有那個文件或目錄
  [root@zyshanlinux-001 mysql]# ls /etc/init.d/
  123  functions  netconsole  network  README
  [root@zyshanlinux-001 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  [root@zyshanlinux-001 mysql]# ls /etc/init.d/mysqld
  /etc/init.d/mysqld 

還要做一下編輯

  [root@zyshanlinux-001 mysql]# vi !$
  vi /etc/init.d/mysqld
  ##在編輯中把下面的路徑加上##
  basedir=/usr/local/mysql  ##指定mysql的程序目錄
  datadir=/data/mysql  ##定義在data目錄下

做權限的變更

  ##默認權限是755,就不用修改##
  [root@zyshanlinux-001 mysql]# ls -l /etc/init.d/mysqld
  -rwxr-xr-x 1 root root 10902 6月  26 20:20 /etc/init.d/mysqld
  ##如果不是需要更改權限##
  [root@zyshanlinux-001 mysql]# chmod 755

如果需要開機啓動,需要添加到系統服務列表

  [root@zyshanlinux-001 mysql]# chkconfig --list
  
  注:該輸出結果只顯示 SysV 服務,並不包含
  原生 systemd 服務。SysV 配置數據
  可能被原生 systemd 配置覆蓋。 
  
        要列出 systemd 服務,請執行 'systemctl list-unit-files'。
        查看在具體 target 啓用的服務請執行
        'systemctl list-dependencies [target]'。
  
  netconsole      0:關 1:關 2:關 3:關 4:關 5:關 6:關
  network         0:關 1:關 2:開 3:開 4:開 5:開 6:關
  [root@zyshanlinux-001 mysql]# chkconfig --add mysqld  ##添加到系統服務列表
  [root@zyshanlinux-001 mysql]# chkconfig --list
  
  注:該輸出結果只顯示 SysV 服務,並不包含
  原生 systemd 服務。SysV 配置數據
  可能被原生 systemd 配置覆蓋。 
  
        要列出 systemd 服務,請執行 'systemctl list-unit-files'。
        查看在具體 target 啓用的服務請執行
        'systemctl list-dependencies [target]'。
  
  mysqld          0:關 1:關 2:開 3:開 4:開 5:開 6:關
  netconsole      0:關 1:關 2:關 3:關 4:關 5:關 6:關
  network         0:關 1:關 2:開 3:開 4:開 5:開 6:關

也可以用命令啓動

  [root@zyshanlinux-001 mysql]# /etc/init.d/mysqld start
  ##也可以##
  [root@zyshanlinux-001 mysql]# service mysqld start
  Starting MySQL.Logging to '/data/mysql/zyshanlinux-001.err'.
  ....... SUCCESS! ##啓動成功

查看進程

  [root@zyshanlinux-001 ~]# ps aux |grep mysql

查看端口:3306(2186/mysqld)

  [root@zyshanlinux-001 ~]# netstat -lntp

場景:沒有啓動腳本模板,可以用命令行啓動

--defaults-file=/etc/my.cnf配置文件放在前面

$丟到後臺運行去

[root@zyshanlinux-001 mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &[1] 2315[root@zyshanlinux-001 mysql]# 180626 20:41:32 mysqld_safe Logging to '/data/mysql/zyshanlinux-001.err'.180626 20:41:32 mysqld_safe Starting mysqld daemon with databases from /data/mysql

命令行無法關閉mysql服務,必須殺死

[root@zyshanlinux-001 mysql]# killall mysqld

經驗:

killall優於kill,以爲mysql在讀寫的時候,用killall是先停止讀寫,等待緩存裏面的數據讀寫完成再停止服務:而kill則會導致數據丟失,因爲它是直接停止服務,甚至會損害表。只能慢慢的等。

mysql有2個常用的引擎:innodb(存儲空間大)、myisam(存儲空間小)

MariaDB安裝

1、把Mariadb包下到/usr/local/src/目錄下

  [root@zyshanlinux-001 src]# wget https://downloads.mariadb.com/MariaDB/mariadb-10.2.6/bintar-linux-glibc_214-x86_64/mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

2、解壓包

  tar zxvf mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

3、把解壓的包放到/usr/local/改名爲mariadb

  [root@zyshanlinux-001 src]# mv mariadb-10.2.6-linux-glibc_214-x86_64 /usr/local/mariadb

4、跳轉到該目錄

  [root@zyshanlinux-001 src]# cd /usr/local/mariadb

5、創建用戶,數據目錄,初始化

  ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mariadb/ --datadir=/data/mariadb

6、確認初始化命令正確

  [root@zyshanlinux-001 mariadb]# echo $?
  0

7、配置文件,分別按照內存大小選擇不同的配置文件,由於試驗內存不大隻有2G,選用my-small.cnf配置文件

  [root@zyshanlinux-001 mariadb]# cd /usr/local/mariadb/
  [root@zyshanlinux-001 mariadb]# ls
  bin                 data               include         mysql-test    share
  COPYING             DESTINATION        INSTALL-BINARY  README.md     sql-bench
  COPYING.thirdparty  docs               lib             README-wsrep  support-files
  CREDITS             EXCEPTIONS-CLIENT  man             scripts
  [root@zyshanlinux-001 mariadb]# ls support-files/
  binary-configure  my-innodb-heavy-4G.cnf  my-small.cnf         mysql.server  wsrep_notify
  magic             my-large.cnf            mysqld_multi.server  policy
  my-huge.cnf       my-medium.cnf           mysql-log-rotate     wsrep.cnf

8、拷貝配置文件模板和啓動腳本,配置文件基本不用改,啓動腳本需要編輯一下

  [root@zyshanlinux-001 mariadb]# cp support-files/my-small.cnf /usr/local/mariadb/my.cnf
  [root@zyshanlinux-001 mariadb]# cp support-files/mysql.server /etc/init.d/mariadb
  [root@zyshanlinux-001 mariadb]# vim !$
  vim /etc/init.d/mariadb  ##編輯,需要修改2個地方
  
  basedir=
  datadir=
  ##改爲##
  basedir=/usr/local/mariadb
  datadir=/data/mariadb
  conf=$basedir/my.cnf  ##增加一行,指定配置文件所在的路徑
  
  $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
  ##改爲##
  $bindir/mysqld_safe --defaults-file="$conf" --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &

9、由於mariadb和mysql所監聽的端口是一致的,所以2個服務只能運行一個,在運行mariadb之前,需要檢測mysql是否有運行,有運行結束掉mysql

  [root@zyshanlinux-001 mariadb]# ps aux |grep mysql
  root      3013  0.0  0.0  11816  1604 pts/0    S    20:50   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zyshanlinux-001.pid
  mysql     3149  0.1 24.3 973096 456232 pts/0   Sl   20:50   0:11 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/zyshanlinux-001.err --pid-file=/data/mysql/zyshanlinux-001.pid --socket=/tmp/mysql.sock
  root      3920  0.0  0.0 112720   972 pts/0    R+   22:56   0:00 grep --color=auto mysql
  [root@zyshanlinux-001 mariadb]# service mysqld stop
  Shutting down MySQL.. SUCCESS! 
  [root@zyshanlinux-001 mariadb]# ps aux |grep mysql
  root      3947  0.0  0.0 112720   972 pts/0    R+   22:56   0:00 grep --color=auto mysql

10、運行mariadb服務,用ps確認mariadb確實是啓動了

[root@zyshanlinux-001 ~]# service mariadb start
Starting mariadb (via systemctl):                          [  確定  ]
[root@zyshanlinux-001 ~]# ps aux |grep mariadb
root      1961  0.1  0.0 115432  1756 ?        S    07:36   0:00 /bin/sh /usr/local/mariadb/bin/mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf --datadir=/data/mariadb --pid-file=/data/mariadb/zyshanlinux-001.pid
mysql     2080  2.5  3.0 1125076 56564 ?       Sl   07:36   0:00 /usr/local/mariadb/bin/mysqld --defaults-file=/usr/local/mariadb/my.cnf --basedir=/usr/local/mariadb --datadir=/data/mariadb --plugin-dir=/usr/local/mariadb/lib/plugin --user=mysql --log-error=/data/mariadb/zyshanlinux-001.err --pid-file=/data/mariadb/zyshanlinux-001.pid --socket=/tmp/mysql.sock --port=3306
root      2119  0.0  0.0 112720   972 pts/0    R+   07:36   0:00 grep --color=auto mariadb

查看端口,端口也沒問題

[root@zyshanlinux-001 mariadb]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1091/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1312/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1091/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1312/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      4150/mysqld 

11、mariadb和mysql的區別,兩者安裝都差不多,但在mariadb安裝的時候單獨定義配置文件所在的路徑,目的是爲了和mysql不產生衝突;假如只安裝了mariadb,把my.cnf就放在etc下,啓動腳本就不需要去定義conf和變量了。

[root@zyshanlinux-001 mariadb]# ls /etc/my.cnf/etc/my.cnf[root@zyshanlinux-001 mariadb]# ps aux |grep mysqlroot 4034 0.0 0.0 115432 1744 ? S 22:59 0:00 /bin/sh /usr/local/mariadb/bin/mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf --datadir=/data/mysql --pid-file=/data/mysql/zyshanlinux-001.pidmysql 4150 0.6 3.5 1125176 66680 ? Sl 22:59 0:04 /usr/local/mariadb/bin/mysqld --defaults-file=/usr/local/mariadb/my.cnf --basedir=/usr/local/mariadb --datadir=/data/mysql --plugin-dir=/usr/local/mariadb/lib/plugin --user=mysql --log-error=/data/mysql/zyshanlinux-001.err --pid-file=/data/mysql/zyshanlinux-001.pid --socket=/tmp/mysql.sock --port=3306root 4250 0.0 0.0 112720 968 pts/0 R+ 23:09 0:00 grep --color=auto mysql[root@zyshanlinux-001 mariadb]# ps aux |grep mariadbroot 4034 0.0 0.0 115432 1744 ? S 22:59 0:00 /bin/sh /usr/local/mariadb/bin/mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf --datadir=/data/mysql --pid-file=/data/mysql/zyshanlinux-001.pidmysql 4150 0.4 3.5 1125176 66680 ? Sl 22:59 0:04 /usr/local/mariadb/bin/mysqld --defaults-file=/usr/local/mariadb/my.cnf --basedir=/usr/local/mariadb --datadir=/data/mysql --plugin-dir=/usr/local/mariadb/lib/plugin --user=mysql --log-error=/data/mysql/zyshanlinux-001.err --pid-file=/data/mysql/zyshanlinux-001.pid --socket=/tmp/mysql.sock --port=3306root 4271 0.0 0.0 112720 968 pts/0 R+ 23:14 0:00 grep --color=auto mariadb[root@zyshanlinux-001 mariadb]# /etc/init.d/mariadb stopStopping mariadb (via systemctl): [ 確定 ][root@zyshanlinux-001 mariadb]# ps aux |grep mariadbroot 4315 0.0 0.0 112720 972 pts/0 R+ 23:14 0:00 grep --color=auto mariadb[root@zyshanlinux-001 mariadb]# ps aux |grep mysqlroot 4317 0.0 0.0 112720 968 pts/0 R+ 23:14 0:00 grep --color=auto mysql[root@zyshanlinux-001 mariadb]# vim /usr/local/mariadb/my.cnf

##編輯my.cnf,把

[mysqld]port = 3306socket = /tmp/mysql.sock

改爲

[mysqld]datadir = /data/mariadb ##指定數據目錄port = 3306socket = /tmp/mysql.sock

[root@zyshanlinux-001 mariadb]# /etc/init.d/mariadb startStarting mariadb (via systemctl): [ 確定 ][root@zyshanlinux-001 mariadb]# ps aux |grep mariadbroot 4357 0.0 0.0 115432 1752 ? S 23:16 0:00 /bin/sh /usr/local/mariadb/bin/mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf --datadir=/data/mariadb --pid-file=/data/mariadb/zyshanlinux-001.pidmysql 4476 0.9 3.2 1125076 61580 ? Sl 23:16 0:00 /usr/local/mariadb/bin/mysqld --defaults-file=/usr/local/mariadb/my.cnf --basedir=/usr/local/mariadb --datadir=/data/mariadb --plugin-dir=/usr/local/mariadb/lib/plugin --user=mysql --log-error=/data/mariadb/zyshanlinux-001.err --pid-file=/data/mariadb/zyshanlinux-001.pid --socket=/tmp/mysql.sock --port=3306root 4512 0.0 0.0 112720 972 pts/0 R+ 23:16 0:00 grep --color=auto mariadb

Apache安裝

Apache是一個基金會的名字,httpd纔是我們要安裝的軟件包,早期它的名字就叫apache

Apache官網www.apache.org

1、下載Apache包和apr和apr-util

[root@zyshanlinux-001 src]# wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.33.tar.gz
[root@zyshanlinux-001 src]# wget http://mirrors.cnnic.cn/apache/apr/apr-1.6.3.tar.gz
[root@zyshanlinux-001 src]# wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.bz2

httpd2.2和httpd2.4的安裝不太一樣有區別,涉及到依賴的軟件apr,apr和apr-util是一個通用的函數庫,它讓httpd可以不關心底層的操作系統平臺,可以很方便地移植(從linux移植到windows);httpd2.2和httpd2.4依賴的apr版本也不一樣,系統自帶的apr和我們要安裝的2.4是不匹配的,所以無法使用yum安裝的apr,需要自己編譯

2、解壓

[root@zyshanlinux-001 src]# tar zxvf httpd-2.4.33.tar.gz
[root@zyshanlinux-001 src]# tar zxvf apr-1.6.3.tar.gz
[root@zyshanlinux-001 src]# tar jxvf apr-util-1.6.1.tar.bz2

3、安裝apr-1.6.3

[root@zyshanlinux-001 apr-1.6.3]# cd apr-1.6.3
[root@zyshanlinux-001 apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@zyshanlinux-001 apr-1.6.3]# echo $?
0
[root@zyshanlinux-001 apr-1.6.3]# make && make install
[root@zyshanlinux-001 apr-1.6.3]# ls /usr/local/apr/
bin  build-1  include  lib

4、安裝apr-util-1.6.1

[root@zyshanlinux-001 apr-1.6.3]# cd /usr/local/src/apr-util-1.5.4
[root@zyshanlinux-001 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr                                                               ##依賴apr##
[root@zyshanlinux-001 apr-util-1.6.1]# make && make install
 [root@zyshanlinux-001 apr-util-1.6.1]# ls /usr/local/apr-util/
bin  include  lib

5、安裝httpd-2.4.33

[root@zyshanlinux-001 apr-util-1.6.1]# cd ..
[root@zyshanlinux-001 src]# cd /usr/local/src/httpd-2.4.33
                                                                     ##起名2.4與2.2區分##
[root@zyshanlinux-001 httpd-2.4.33]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
##依賴apr##                                          ##動態擴展##       ##支持哪些模塊,大多數
...
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/                   ##報錯##
[root@zyshanlinux-001 httpd-2.4.33]# yum list |grep pcre  ##解決思路,查一下yum list
pcre.x86_64                              8.32-17.el7                   @anaconda
ghc-pcre-light.x86_64                    0.4-13.el7                    epel     
ghc-pcre-light-devel.x86_64              0.4-13.el7                    epel     
mingw32-pcre.noarch                      8.38-1.el7                    epel     
mingw32-pcre-static.noarch               8.38-1.el7                    epel     
mingw64-pcre.noarch                      8.38-1.el7                    epel     
mingw64-pcre-static.noarch               8.38-1.el7                    epel     
pcre.i686                                8.32-17.el7                   base     
pcre-devel.i686                          8.32-17.el7                   base     
pcre-devel.x86_64                        8.32-17.el7                   base     
pcre-static.i686                         8.32-17.el7                   base     
pcre-static.x86_64                       8.32-17.el7                   base     
pcre-tools.x86_64                        8.32-17.el7                   base     
pcre2.i686                               10.23-2.el7                   base     
pcre2.x86_64                             10.23-2.el7                   base     
pcre2-devel.i686                         10.23-2.el7                   base     
pcre2-devel.x86_64                       10.23-2.el7                   base     
pcre2-static.i686                        10.23-2.el7                   base     
pcre2-static.x86_64                      10.23-2.el7                   base     
pcre2-tools.x86_64                       10.23-2.el7                   base     
pcre2-utf16.i686                         10.23-2.el7                   base     
pcre2-utf16.x86_64                       10.23-2.el7                   base     
pcre2-utf32.i686                         10.23-2.el7                   base     
pcre2-utf32.x86_64                       10.23-2.el7                   base
[root@zyshanlinux-001 httpd-2.4.33]# yum install -y pcre-devel  ##安裝解決庫問題
##遇到這種輸出錯誤,仔細看看,一般都可查到或猜到答案##
[root@zyshanlinux-001 httpd-2.4.33]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
configure: summary of build options:

    Server Version: 2.4.33
    Install prefix: /usr/local/apache2.4
    C compiler:     gcc -std=gnu99
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

[root@zyshanlinux-001 httpd-2.4.33]# echo $?
0
[root@zyshanlinux-001 httpd-2.4.33]# make && make install
[root@zyshanlinux-001 httpd-2.4.33]# cd /usr/local/apache2.4/
[root@zyshanlinux-001 apache2.4]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
[root@zyshanlinux-001 apache2.4]# ls -l bin/httpd
-rwxr-xr-x 1 root root 2348432 6月  27 08:44 bin/httpd  ##核心二進制文件
[root@zyshanlinux-001 apache2.4]# du -sh !$
du -sh bin/httpd
2.3M	bin/httpd
[root@zyshanlinux-001 apache2.4]# ls conf/  ##配置文件
extra  httpd.conf  magic  mime.types  original
[root@zyshanlinux-001 apache2.4]# ls htdocs/
index.html  ##默認訪問網站
[root@zyshanlinux-001 apache2.4]# ls logs/  ##訪問和錯誤日誌
[root@zyshanlinux-001 apache2.4]# ls man  ##幫助文檔
man1  man8
[root@zyshanlinux-001 apache2.4]# ls modules/  ##加載的模塊放置處,每個模塊代表一種功能
[root@zyshanlinux-001 apache2.4]# du -sh modules/
6.4M	modules/
##查看模塊##
[root@zyshanlinux-001 apache2.4]# /usr/local/apache2.4/bin/apachectl -M
##或者##
[root@zyshanlinux-001 apache2.4]# /usr/local/apache2.4/bin/httpd -M
##這裏並不是報錯,只是要你定義下'ServerName'##
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::8fc3:bbdf:ba89:22a7. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_event_module (static)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)

上面括號裏面表示:shared動態是一個擴展的文件、static靜態直接把模塊編譯進了主二進制文件/vin/httpd

運行httpd服務,不用開機啓動,直接命令啓動就好

[root@zyshanlinux-001 apache2.4]# /usr/local/apache2.4/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::8fc3:bbdf:ba89:22a7. Set the 'ServerName' directive globally to suppress this message
[root@zyshanlinux-001 apache2.4]# ps aux |grep httpd
root     33892  0.0  0.1  97644  2536 ?        Ss   09:03   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   33893  0.7  0.2 384472  4428 ?        Sl   09:03   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   33894  0.0  0.2 384472  4428 ?        Sl   09:03   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon   33895  0.0  0.2 384472  4428 ?        Sl   09:03   0:00 /usr/local/apache2.4/bin/httpd -k start
root     33978  0.0  0.0 112720   968 pts/0    S+   09:04   0:00 grep --color=auto httpd
[root@zyshanlinux-001 apache2.4]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1090/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1441/master         
tcp6       0      0 :::80                   :::*                    LISTEN      33892/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      1090/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1441/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2080/mysqld

httpd默認監聽80端口,mysqld默認監聽3306端口,25是發郵件的,22是遠程登錄的

安裝PHP5

PHP官網www.php.net

當前主流版本爲5.6/7.1

1、進入約定下載目錄

[root@zyshanlinux-001 ~]# cd /usr/local/src/
[root@zyshanlinux-001 src]#

2、下載PHP5包,解壓,進入解壓目錄

[root@zyshanlinux-001 src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
[root@zyshanlinux-001 src]# tar zxf php-5.6.30.tar.gz
[root@zyshanlinux-001 src]# cd php-5.6.30

3、配置,初始化;把PHP放到最後安裝是由於,需要用到apache的工具,所以要先安裝apache,安裝PHP的時候才能定義apache的工具

[root@zyshanlinux-001 php-5.6.30]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs##apache的工具路徑## --with-config-file-path=/usr/local/php/etc##PHP的配置文件##  --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config##這3個是mysql和PHP的驅動## --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif##配置PHP需要的模塊##

4、錯誤解決,第一次安裝PHP有許多庫是缺失的,根據錯誤一一解決,庫都是devel結尾的

configure: error: xml2-config not found. Please check your libxml2 installation.
[root@zyshanlinux-001 php-5.6.30]# yum install -y libxml2-devel

configure: error: Cannot find OpenSSL's <evp.h>
[root@zyshanlinux-001 php-5.6.30]# yum install -y openssl-devel

configure: error: Please reinstall the BZip2 distribution
[root@zyshanlinux-001 php-5.6.30]# yum install -y bzip2-devel

configure: error: libjpeg.h not found.
##需要更換源,原本的源裏面沒有這個軟件包##
[root@zyshanlinux-001 php-5.6.30]# yum install -y libjpeg-devel

configure: error: png.h not found.
[root@zyshanlinux-001 php-5.6.30]# yum install -y libpng-devel

configure: error: freetype-config not found.
[root@zyshanlinux-001 php-5.6.30]# yum install -y freetype-devel

configure: error: mcrypt.h not found. Please reinstall libmcrypt.
##如果之前沒有安裝過epel這個擴展源,因爲mcrypt這個庫實在epel這個擴展源裏面##
[root@zyshanlinux-001 php-5.6.30]# yum install epel-release
[root@zyshanlinux-001 php-5.6.30]# yum install -y libmcrypt-devel

5、安裝成功

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
[root@zyshanlinux-001 php-5.6.30]# echo $?
0

6、make && make install

  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/src/php-5.6.30/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

[root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php/
bin  etc  include  lib  php
[root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php/bin/  ##核心文件
pear  peardev  pecl  phar  phar.phar  php  php-cgi  php-config  phpize
[root@zyshanlinux-001 php-5.6.30]# du -sh /usr/local/php/bin/php
36M	/usr/local/php/bin/php
##apache和PHP是通過下面的.so連接起來的##
[root@zyshanlinux-001 php-5.6.30]# du -sh /usr/local/apache2.4/modules/libphp5.so
37M	/usr/local/apache2.4/modules/libphp5.so

查看PHP加載的模塊

[root@zyshanlinux-001 php-5.6.30]# /usr/local/php/bin/php -m
[PHP Modules]
bz2
Core
ctype
date
dom
ereg
exif
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mcrypt
mysql
mysqli
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

PHP是通過apache加載,以模塊的形式來運行

[root@zyshanlinux-001 php-5.6.30]# /usr/local/apache2.4/bin/httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::8fc3:bbdf:ba89:22a7. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_event_module (static)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)
 php5_module (shared)  ##非常重要的,缺失apache就支持PHP了##

配置文件[root@zyshanlinux-001 php-5.6.30]# vi /usr/local/apache2.4/conf/httpd.conf

隨用隨取,只要去掉#號就可以

LoadModule dir_module modules/mod_dir.so
#LoadModule actions_module modules/mod_actions.so
#LoadModule speling_module modules/mod_speling.so
#LoadModule userdir_module modules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
#LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module        modules/libphp5.so  ##取用的模塊

用命令/usr/local/php/bin/php -i |less可以查看PHP的一些信息

[root@zyshanlinux-001 php-5.6.30]# /usr/local/php/bin/php -i |less



phpinfo()
PHP Version => 5.6.30

System => Linux zyshanlinux-001 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64
Build Date => Jun 27 2018 10:35:47
Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-apxs2=/usr/local/apache2.4/bin/apxs' '--with-config-file-path=/usr/local/php/etc' '--with-mysql=/usr/local/mysql' '--with-pdo-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-bz2' '--with-openssl' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-mbstring' '--enable-sockets' '--enable-exif'
Server API => Command Line Interface
Virtual Directory Support => enabled
Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => (none)
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20131106
PHP Extension => 20131226
Zend Extension => 220131226
Zend Extension Build => API220131226,TS
PHP Extension Build => API20131226,TS
Debug Build => no
Thread Safety => enabled
Zend Signal Handling => disabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => disabled

Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar

複製參考配置文件到/usr/local/php/etc/php.ini,因爲前面初始化的時候有定義了路徑

[root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php/etc
pear.conf
[root@zyshanlinux-001 php-5.6.30]# cp php.ini-production  /usr/local/php/etc/php.ini
[root@zyshanlinux-001 php-5.6.30]# /usr/local/php/bin/php -i |less
Loaded Configuration File => /usr/local/php/etc/php.ini  ##會在配置文件上增加這一行##

安裝PHP7

1、安裝php-7.1.6步驟與上面類似,定義的目錄名字和配置文件路徑不一樣,沒有--with-mysql反而是--with-mysqli

[root@zyshanlinux-001 ~]# cd /usr/local/src
[root@zyshanlinux-001 php-7.1.6]# wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
[root@zyshanlinux-001 php-7.1.6]# tar jxvf php-7.1.6.tar.bz2
[root@zyshanlinux-001 php-7.1.6]# cd php-7.1.6
[root@zyshanlinux-001 php-7.1.6]# ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc  --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
[root@zyshanlinux-001 php-7.1.6]# make && make install
[root@zyshanlinux-001 php-7.1.6]# echo $?
0
[root@zyshanlinux-001 php-7.1.6]# ls /usr/local/apache2.4/modules/libphp7.so
/usr/local/apache2.4/modules/libphp7.so
[root@zyshanlinux-001 php-7.1.6]# du -sh !$
du -sh /usr/local/apache2.4/modules/libphp7.so
37M	/usr/local/apache2.4/modules/libphp7.so
[root@zyshanlinux-001 php-7.1.6]# /usr/local/php7/bin/php -m  ##查看模塊,和5基本一致
[root@zyshanlinux-001 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -M  ##apache加載2個PHP
...
 php5_module (shared)
 php7_module (shared)

可以運行2個php(5和7),但apache必須指定好。

選擇用哪個PHP,在配置文件上修改

[root@zyshanlinux-001 php-7.1.6]# vim /usr/local/apache2.4/conf/httpd.conf
##不想用哪個就在那行前面加#註釋掉哪個##
LoadModule php5_module        modules/libphp5.so
LoadModule php7_module        modules/libphp7.so

Apache和PHP結合

httpd主配置文件/usr/local/apache2.4/conf/httpd.conf

vim /usr/local/apache2.4/conf/httpd.conf //修改以下4個地方

1、ServerName,去掉#號

##告警,沒報錯##
[root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::8fc3:bbdf:ba89:22a7. Set the 'ServerName' directive globally to suppress this message
##更改配置文件可以去掉這段告警##
[root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
——————————————————————————
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80  ##把#號去掉##
——————————————————————————————————

##雖然沒告警了,但出現了一個錯誤,是由於配置文件中用了2個PHP(5和7)產生衝突導致的##
[root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl restart
httpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79: 51484 段錯誤               $HTTPD -k $ARGV
[root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf ##繼續修改配置文件
LoadModule php5_module        modules/libphp5.so
#LoadModule php7_module        modules/libphp7.so  ##註釋掉PHP7##
##最後沒有出現告警和報錯了##

2、Require all denied改爲Require all granted,爲了防止打開虛擬機配置的時候出現403或在Forbidden,正常的狀態都是200

首先連一下服務器

無法訪問,用CMD連接看網絡通不通:ping 192.168.106.128

如果telnet顯示不是內部或外部命令,也不是可運行的程序。就需要去win系統去打開telnet功能

只打開客戶端,不要打開服務端,否則它會監聽23端口,比較危險。

已經打開的話,就會顯示80端口連接失敗,的提示。也可以在linux中用命令查看一下打開的端口,80端口也是沒打開的

[root@zyshanlinux-001 ~]# iptables -nvL  ##查看打開的端口
[root@zyshanlinux-001 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT  ##單獨用一個命令打開80端口

連接瀏覽器,就會顯示工作

連接window的cmd終端,通的狀態

退出該連接狀態:Ctrl+]退出,再quit退出telnet

更改配置文件

當Require all denied時,顯示

[root@zyshanlinux-001 ~]# !vi
vim /usr/local/apache2.4/conf/httpd.conf
[root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl -t  ##-t檢查語法是否錯誤,非常有用##
Syntax OK
##修改了配置文件,需要重新加載配置文件,但不會重啓服務,最多就加載不成功,並不會殺死進程。##
[root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl graceful

當Require all granted,就顯示

<Directory />
    AllowOverride none
    Require all granted  ##把denied改爲granted##
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/usr/local/apache2.4/htdocs"
<Directory "/usr/local/apache2.4/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted  ##把denied改爲granted##

3、AddType application/x-httpd-php .php要支持PHP,所以要增加一個和PHP相關的配置,不然PHP沒法解析

[root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf

4、DirectoryIndex index.html 後面加上 index.php

[root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
[root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl -t  ##改完記得檢查語法##
Syntax OK
[root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl graceful  ##加載配置##

/usr/local/apache2.4/bin/apachectl start //啓動服務

netstat -lntp

curl localhost

增加文件是不用加載配置的

[root@zyshanlinux-001 ~]# vi /usr/local/apache2.4/htdocs/1.php  ##在1.php中加上一下內容##
<?php
phpinfo();
?>

PHP可以解析,就會顯示如下畫面

故意把AddType application/x-httpd-php .php從/usr/local/apache2.4/conf/httpd.conf註釋掉,就會顯示原始代碼。

如果顯示原始代碼,PHP無法解析,解決思路步驟:

1、模塊是否存在

  [root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl -M
  Loaded Modules:
   core_module (static)
   so_module (static)
   http_module (static)
   mpm_event_module (static)
   authn_file_module (shared)
   authn_core_module (shared)
   authz_host_module (shared)
   authz_groupfile_module (shared)
   authz_user_module (shared)
   authz_core_module (shared)
   access_compat_module (shared)
   auth_basic_module (shared)
   reqtimeout_module (shared)
   filter_module (shared)
   mime_module (shared)
   log_config_module (shared)
   env_module (shared)
   headers_module (shared)
   setenvif_module (shared)
   version_module (shared)
   unixd_module (shared)
   status_module (shared)
   autoindex_module (shared)
   dir_module (shared)
   alias_module (shared)
   php5_module (shared)  ##是否有php5這個模塊

2、有模塊,但沒加載

  [root@zyshanlinux-001 ~]# ls /usr/local/apache2.4/modules/libphp5.so
  /usr/local/apache2.4/modules/libphp5.so  ##是否有這個文件,否則加載不了

3、有文件,沒有顯示,在配置有是否有這兩個配置

  [root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf

4、配置文件裏是否有AddType application/x-httpd-php .php,是否加了但是寫錯了的

      [root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
      #
      AddType application/x-compress .Z
      AddType application/x-gzip .gz .tgz
      AddType application/x-httpd-php .php  ##加的##

寫錯了,可以用語法檢查

  /usr/local/apache2.4/bin/apachectl -t //測試語法

5、index.php加不加不怎麼影響,

  [root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
  #
  <IfModule dir_module>
      DirectoryIndex index.html index.php

在瀏覽器上輸入IP,實際上訪問的是index.html

  [root@zyshanlinux-001 ~]# ls /usr/local/apache2.4/htdocs/
  1.php  index.html

真正的網址是www.baidu.com/index.html加了index.html就可以省略不寫,因爲我們提前定義了一個index.html

前面用的php5,現在不用php5用php7,在配置文件下注釋掉php5,取消php7的註釋,連接瀏覽器成功使用php7

  [root@zyshanlinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
  #LoadModule php5_module        modules/libphp5.so
  LoadModule php7_module        modules/libphp7.so
  ##用Ctrl+r可以輕鬆的把歷史命令提現出來用,很方便##
  (reverse-i-search)`-t': /usr/local/apache2.4/bin/apachectl ^C
  (reverse-i-search)`gra': /usr/local/apache2.4/bin/apachectl ^Caceful
  [root@zyshanlinux-001 ~]#
  [root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl -t
  Syntax OK
  [root@zyshanlinux-001 ~]# /usr/local/apache2.4/bin/apachectl graceful

拓展練習:

Discuz建站教程:[1]本地安裝discuz網站

https://jingyan.baidu.com/article/b87fe19eb57ff252183568d9.html

MySQL創建用戶與授權

https://www.cnblogs.com/sos-blue/p/6852945.html

mysql5.5源碼編譯安裝 http://www.aminglinux.com/bbs/thread-1059-1-1.htmlmysql5.7二進制包安裝(變化較大) http://www.apelearn.com/bbs/thread-10105-1-1.htmlapache dso https://yq.aliyun.com/articles/6298apache apxs http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/programs/apxs.htmlapache工作模式 http://www.cnblogs.com/fnng/archive/2012/11/20/2779977.htmlphp中mysql,mysqli,mysqlnd,pdo到底是什麼 http://blog.csdn.net/u013785951/article/details/60876816查看編譯參數 http://ask.apelearn.com/question/1295


發佈了57 篇原創文章 · 獲贊 22 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章