linux+httpd+php-fpm+mysqlphp
編譯安裝html
環境:mysql
iptables、selinux關閉狀態
linux
系統: CentOS release 6.7 (Final)nginx
yum源:epel,cdromweb
軟件:面試
php-5.6.17正則表達式
下載頁面:http://php.net/downloads.php算法
httpd 2.4.18sql
下載頁面:http://httpd.apache.org/download.cgi#apache24
mariadb 前面有專門編譯的了,這裏就直接yum安裝了。
編譯安裝mariadb5.5: http://fanqie.blog.51cto.com/9382669/1708239
主機:
httpd |
172.16.40.20 |
php-fpm |
172.16.40.21 |
mysql |
172.16.40.22 |
1、http
2、php
3、Mysql(rpm安裝的)
4、安裝phpMydmin和wordpress
5、爲phpMyadmin添加https。
1、http:
httpd2.4依賴於apr1.4,而咱們系統上面自帶的apr是1.3.9的版本。因此咱們要本身來編譯新版本的apr工具。
apr是httpd程序代碼跨平臺的基礎。否則還要爲各個系統上運行的httpd來編寫不一樣的庫函數。
提供了一個對httpd統一的環境,而不用再操心爲各個系統再編寫適用的底層庫。
網摘:
APR(Apache portable Run-time libraries,Apache可移植運行庫)主要爲上層的應用程序提供一個能夠跨越多操做系統平臺使用的底層支持接口庫。在早期 的Apache版本中,應用程序自己必須可以處理各類具體操做系統平臺的細節,並針對不一樣的平臺調用不一樣的處理函數。
apr下載地址(apr、apr-utils):
http://apr.apache.org/download.cgi
http://mirrors.hust.edu.cn/apache//apr/apr-1.5.2.tar.gz
http://mirrors.hust.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
這裏是新裝的系統,缺乏開發工具和開發庫。簡單點能夠直接安裝開發包組。
yum groupinstall "Development tools" #開發工具 yum groupinstall "Server Platform Development" #服務器開發庫
apr:
[root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr [root@localhost apr-1.5.2]# make [root@localhost apr-1.5.2]# make install
apr-utils:
[root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr/ --with-apr=/usr/local/apr [root@localhost apr-util-1.5.4]# make && make install
httpd:
所依賴的包。
[root@localhost httpd-2.4.18]# yum install pcre-devel openssl-devel -y
pcre 提供perl 兼容的正則表達式庫,httpd、php還有nginx中用的都是這種正則表達式。
http://baike.baidu.com/view/3963592.htm
安裝httpd:
[root@localhost httpd-2.4.18]# ./configure --prefix=/usr/local/httpd24 --sysconfdir=/etc/httpd24 --enable-modules=most --enable-so --enable-ssl --enable-mpms-shared=all --enable-cgid --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr --with-pcre --with-mpm=event [root@localhost httpd-2.4.18]# make && make install
--prefix=/usr/local/httpd24 #安裝位置 --sysconfdir=/etc/httpd24 #配置文件位置。 --enable-modules=most #要啓用的模塊,就是會編譯成模塊的, 可不是要自動掛載的。對應的還有: #--enable-mods-shared 要啓用的共享模塊。 #--enable-mods-static 要啓用的靜態模塊。 --enable-so #啓用動態裝載(DSO)功能。也就是動態裝載模塊的功能。 --enable-ssl #啓用ssl模塊。不知道在上面的most裏面有沒有包括這個,沒有嘗試過。 --enable-mpms-shared=all #各mpm功能以模塊的形式存在。能夠經過掛載不一樣的mpm模塊來使用不一樣的功能模型。prefork,event,worker --enable-cgid #cgi腳本功能,這裏要用event模型,線程模型用這個來開啓。非線程模型用--enabl-cgi來開啓。 --enable-rewrite #URL重寫的基本定義規則。能夠說就是地址重寫的功能了。 --with-pcre #使用外部的pcre庫。 --with-mpm=event #默認event模型
服務腳本咱們能夠把系統自帶的http腳本複製一份改一下就能夠,也免得麻煩本身寫了。
[root@localhost httpd24]# cp /root/httpd /etc/init.d/httpd24 #我這裏就是從別處複製來的。 [root@localhost httpd24]# chkconfig --add httpd24 #添加進chkconfig控制。 [root@localhost httpd24]# chkconfig httpd24 on #設置開機啓動 [root@localhost httpd24]# chkconfig --list httpd24 #查看狀態。 httpd24 0:off 1:off 2:on 3:on 4:on 5:on 6:off
修改一下腳本,主要改的也就幾個:
apachectl=/usr/local/httpd24/bin/apachectl httpd=/usr/local/httpd24/bin/httpd prog=httpd pidfile=/var/run/httpd24/httpd.pid #這個路徑也能夠直接指到默認的pid所在的位置,安裝目錄的logs/httpd.pid。否則就要在httpd的配置文件中修改pid文件所在位置, lockfile=/var/lock/subsys/httpd24
我這裏的設置,爲了用系統上的httpd區分開(有時候可能一些軟件依賴,httpd會自動安裝上的),因此pidfile用了httpd24的目錄。建立所必須的目錄便可。
修改pid文件位置。添加一條PidFile指令便可。
啓動:
[root@localhost httpd24]# service httpd24 start Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message [ OK ]
這個錯誤是由於沒有設置ServerName,而後反解IP地址失敗,或者反解名稱與主機名不同致使的。設置上ServerName就能夠了。如:
ServerName www.star.com
編譯安裝的2.4我的感受與rpm安裝還容易配置,配置文件很是的有條理。用什麼功能,就配置對應的配置文件就行。
虛擬主機設置:
由於在前二篇裏已經反覆寫過兩次配置過程了。這裏就簡略的寫了。
加載模塊,加載子配置文件。
[root@localhost httpd24]# pwd /etc/httpd24 [root@localhost httpd24]# vim extra/httpd-vhosts.conf
41 <VirtualHost *:80> 42 ServerName www.star.com 43 DocumentRoot "/web/vhosts/www" 44 CustomLog "/var/log/httpd24/www/access_log" combined 45 ErrorLog "/var/log/httpd24/www/error_log" 46 <Directory "/web/vhosts/www"> 47 Options None 48 Require all granted 49 </Directory> 50 </VirtualHost> 51 52 53 54 <VirtualHost *:80> 55 ServerName myadm.star.com 56 DocumentRoot "/web/vhosts/myadm" 57 CustomLog "/var/log/httpd24/myadm/access_log" combined 58 ErrorLog "/var/log/httpd24/myadm/error_log" 59 <Directory "/web/vhosts/myadm"> 60 Options None 61 Require all granted 62 </Directory> 63 </VirtualHost>
關閉中心中機,註釋DocumentRoot:
[root@localhost star]# vim /etc/httpd24/httpd.conf #DocumentRoot "/usr/local/httpd24/htdocs
建立所需目錄:
[root@localhost httpd24]# mkdir /web/vhosts/{www,myadm} -pv [root@localhost httpd24]# mkdir /var/log/httpd24/{www,myadm} -pv
把httpd的執行文件添加進PATH變量,也能夠用符號連接的方式:
[root@localhost star]# vim /etc/profile.d/httpd24.sh export PATH=/usr/local/httpd24/bin:$PATH [root@localhost star]# . /etc/profile.d/httpd24.sh [root@localhost star]# echo $PATH /usr/local/httpd24/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
測試並重啓httpd(重載也能夠):
[root@localhost star]# httpd -t Syntax OK [root@localhost star]# service httpd24 restart Stopping httpd: [ OK ] Starting httpd: [ OK ] [root@localhost star]#
測試頁面,內容就朋友們自定吧:
[root@localhost star]# vim /web/vhosts/www/index.html [root@localhost star]# vim /web/vhosts/myadm/index.html
本地測試能夠在hosts文件中添加主機名來完成域名解析:
windows主機在system32/driver/etc/hosts文件中修改
linux主機在/etc/hosts中。
如我這裏的:
star@sst-pp:/mnt/g/soft$ sudo vim /etc/hosts ..... 172.16.40.20 www.star.com myadm.star.com .....
測試沒有問題,html服務工做正常 。
如今咱們再添加反向代理的條目,php頁面都代理至後端的php服務器。而且在DirectoryIndex後面加入默認文檔index.php。咱們測試這裏主頁面都是php的,因此要在index.html前面。
主配置文件啓用代理模塊,有兩個, 一個是總代理模塊,一個是fcgi功能模塊。
[root@localhost httpd24]# pwd /etc/httpd24 [root@localhost httpd24]# vim httpd.conf
添加代理指令:
如今的httpd-vhosts配置文件:
AddType application/x-httpd-php .php #添加或覆蓋mime類型。 #如今.php的mime就是application/x-httpd-php。 主類型/次類型 ProxyRequests Off #關閉正向代理 <VirtualHost *:80> ServerName www.star.com DocumentRoot "/web/vhosts/www" CustomLog "/var/log/httpd24/www/access_log" combined ErrorLog "/var/log/httpd24/www/error_log" ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.40.21:9000/web/php/www/$1 #代理至 <Directory "/web/vhosts/www"> Options None Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName myadm.star.com DocumentRoot "/web/vhosts/myadm" CustomLog "/var/log/httpd24/myadm/access_log" combined ErrorLog "/var/log/httpd24/myadm/error_log" ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.40.21:9000/web/php/myadm/$1 #代理至 <Directory "/web/vhosts/myadm"> Options None Require all granted </Directory> </VirtualHost>
上面的AddType定義mime,能夠把反向代理關閉之後,在瀏覽器中打開一個.php頁面試一下。
不過由於瀏覽器沒法識別此mime,因此會下載此文件。
擴充一點:瀏覽器能作的就是隻顯示文本而已,而其它的數據都是經過mime類型來加載對應的插件或軟件來處理的。
默認文檔:
dir模塊默認都是啓用的,因此裏面的指令是生效的。
[root@localhost httpd24]# service httpd24 reload Reloading httpd: [root@localhost httpd24]#
如今默認網頁就已經不能打開了,php給轉到了後端,然後端不會返回數據。
2、php:
[root@localhost php-5.6.17]# ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=php --with-fpm-group=php --with-config-file-path=/etc/ --with-config-file-scan-dir=/etc/php.d/ --with-openssl --with-zlib --with-bz2 --with-jpeg-dir --with-png-dir --enable-mbstring --with-mcrypt --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-sockets --with-freetype-dir [root@localhost php-5.6.17]# make install
make test 實在是太耗時間了,這裏就不試了。生產環境最好仍是跑一下。
報錯信息:
configure: error: xml2-config not found. Please check your libxml2 installation. [root@localhost php-5.6.17]# yum install libxml2-devel -y
configure: error: Cannot find OpenSSL's <evp.h> [root@localhost php-5.6.17]# yum install openssl-devel -y
configure: error: Please reinstall the BZip2 distribution [root@localhost php-5.6.17]# yum install -y bzip2-devel
configure: error: mcrypt.h not found. Please reinstall libmcrypt. [root@localhost php-5.6.17]# yum install libmcrypt-devel -y
參數介紹:
--enable-fpm #啓用fpm。 不能與作爲httpd模塊的方式並存。也就是--with-apxs2 --with-fpm-user=php #指定程序運行的用戶 --with-fpm-group=php #指定程序運行的用戶組 --with-config-file-path=/etc/ #指定php編譯器環境配置文件。與php-fpm不同。php-fpm只是提供對外鏈接接口的。編譯器纔是真正來編譯php程序的。 --with-config-file-scan-dir=/etc/php.d/ #php編譯器的擴展配置文件所在目錄。如把對於xcache的配置文件放在裏面。 --with-openssl #openssl支持。 --with-zlib #zlib是提供數據壓縮用的函式庫。它不能建立gzip壓縮文件,但能夠讀取和在gzip壓縮文件中寫入數據。 --with-bz2 #bzip2庫函式。能夠透明地讀寫 bzip2(.bz2)壓縮文件。 --with-jpeg-dir #輸出圖象到瀏覽器或文件,動態繪圖功能。 --with-png-dir #也同樣。 --enable-mbstring # 多字節字符支持,如漢字。 --with-mcrypt #提供多種加密算法支持的庫 --with-mysql=mysqlnd #指定mysql鏈接器,mysqlnd是php內置的鏈接mysql系列數據庫的鏈接器。 --with-mysqli=mysqlnd #指定mysqli鏈接器 -with-pdo-mysql=mysqlnd #指定mysql-pdo鏈接器。 --enable-sockets #啓用socket通迅,BSD-socket。 也就是遠程網絡通訊了。本地文件通訊是unix-socket. --with-freetype-dir #字體引擎。
額外的參數:
--with-apxs2=FILE #編譯成httpd的模塊,指定httpd的apxs工具所在位置。若是在PATH變量中能夠找到apxs,後面的FILE能夠省略。不過請注意此apxs是不是想要運行的httpd的apxs。 --sysconfdir=DIR #這個是fpm的配置文件所在位置,通常不用配置,默認在安裝目錄下的etc下。 --with-mhas #基於離散數學原理的不可逆向的php加密方式擴展庫 --enable-zip #透明地讀寫ZIP壓縮文檔以及它們裏面的文件。如今好像不怎麼用zip壓縮了吧。
編譯核心配置選項列表
http://cn2.php.net/manual/zh/configure.about.php
FPM配置選項
http://cn2.php.net/manual/zh/install.fpm.configuration.php
針對各數據庫系統對應的擴展
http://php.net/manual/zh/refs.database.vendors.php
鏈接器mysql,mysqli,mysql-pdo介紹
http://php.net/manual/zh/mysqli.overview.php
其它信息能夠查看php手冊或google。
php手冊,能夠在右上角搜索信息。
http://php.net/manual/zh/index.php
接着來配置咱們的php。
php編譯器配置文件。
[root@localhost php-5.6.17]# cp php.ini-production /etc/php.ini [root@localhost php-5.6.17]# mkdir /etc/php.d
php-fpm服務腳本。注意這是在php代碼的目錄。
[root@localhost php-5.6.17]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@localhost php-5.6.17]# chmod +x /etc/init.d/php-fpm [root@localhost php-5.6.17]# chkconfig --add php-fpm [root@localhost php-5.6.17]# chkconfig php-fpm on [root@localhost php-5.6.17]# chkconfig --list php-fpm php-fpm 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 [root@localhost php-5.6.17]#
注意不要複製錯了,不要複製成init.d.php-fpm.in了,這個文件是php安裝以前的文件,而init.d.php-fpm是安裝之後生成的。
fpm配置文件:
[root@localhost php]# pwd /usr/local/php [root@localhost php]# ls bin etc include lib php sbin var [root@localhost php]# cd etc [root@localhost etc]# ls pear.conf php-fpm.conf.default [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
編輯配置文件:
[root@localhost etc]# vim php-fpm.conf listen = 172.16.40.21:9000 listen.allowed_clients = 172.16.40.20
注要就這兩項,其它的暫時也不用修改。
啓動php-fpm:
[root@localhost local]# service php-fpm start Starting php-fpm [28-Jan-2016 22:58:53] ERROR: [pool www] cannot get uid for user 'php' [28-Jan-2016 22:58:53] ERROR: FPM initialization failed failed
額,忘了建立php用戶了。
[root@localhost local]# useradd -r -s /sbin/nologin php [root@localhost local]# service php-fpm start Starting php-fpm done [root@localhost local]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 172.16.40.21:9000 *:*
安裝xcache。
[root@localhost star]# tar -xf xcache-3.2.0.tar.gz [root@localhost star]# cd xcache-3.2.0 [root@localhost xcache-3.2.0]# /usr/local/php/bin/p pear peardev pecl phar phar.phar php php-cgi php-config phpize [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.
phpize和php-config工具都是用來爲php安裝第三方擴展模塊用的。
缺乏autoconf. 用來生成config配置文件的。
[root@localhost xcache-3.2.0]# yum install autoconf -y [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@localhost xcache-3.2.0]# [root@localhost xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@localhost xcache-3.2.0]# make [root@localhost xcache-3.2.0]# make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
這個路徑是xcache模塊的存放路徑,若是php識別不出來,就要本身手動指定extension了,並且要完整路徑。不過如今通常均可以識別出來的。
複製xcache中的配置文件到/etc/php.d/。 用來讓php識別此擴展模塊。
[root@localhost xcache-3.2.0]# cp xcache.ini /etc/php.d/
測試:
建立php下網頁文件存放目錄。此目錄就是前面httpd用fcgi協議發過來的目錄。
[root@localhost star]# mkdir /web/php/{www,myadm} -pv mkdir: created directory `/web' mkdir: created directory `/web/php' mkdir: created directory `/web/php/www' mkdir: created directory `/web/php/myadm'
建立網頁測試:
[root@localhost star]# vim /web/php/www/index.php <h1>www.star.com</h1> <?php phpinfo(); ?> [root@localhost star]# vim /web/php/myadm/index.php <h1>myadm.star.com</h1> <?php phpinfo(); ?>
重啓php-fpm。打開網頁完成測試。
[root@localhost xcache-3.2.0]# service php-fpm restart
3、mysql:
安裝,爲wordpress建立數據庫並受權用戶。建立遠程管理用戶root。 這裏就不作其它操做了。
[root@localhost ~]# yum install mysql-server -y [root@localhost ~]# service mysqld start [root@localhost ~]# mysql mysql> CREATE DATABASE wordpress; Query OK, 1 row affected (0.00 sec) mysql> GRANT ALL ON wordpress.* TO wpuser@'172.16.40.21' IDENTIFIED BY 'abcdefg'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL ON *.* TO root@'172.16.40.21' IDENTIFIED BY 'abcdefg'; Query OK, 0 rows affected (0.00 sec)
4、安裝phpMyAdmin,WordPress。
php主機:
phpMyAdmin:
[root@localhost star]# unzip phpMyAdmin-4.5.3.1-all-languages.zip [root@localhost star]# mv phpMyAdmin-4.5.3.1-all-languages/* /web/php/myadm/ mv: overwrite `/web/php/myadm/index.php'? y
wordpress:
[root@localhost star]# unzip wordpress-4.4.1-zh_CN.zip [root@localhost star]# mv wordpress/* /web/php/www/ mv: overwrite `/web/php/www/index.php'? y [root@localhost star]# [root@localhost star]# cd /web/php/www [root@localhost www]# cp wp-config-sample.php wp-config.php [root@localhost www]# vim wp-config.php
修改上述信息爲所定義的數據庫和所受權的數據庫用戶。
phpMyAdmin:
[root@localhost www]# cd ../myadm/ [root@localhost myadm]# cp config.sample.inc.php config.inc.php [root@localhost myadm]# openssl rand -base64 15 3Wvh8YWSRv0RxKYfvinv [root@localhost myadm]# vim config.inc.php
添加隨機碼和目標數據庫。
httpd主機:
[root@localhost star]# mv phpMyAdmin-4.5.3.1-all-languages/* /web/vhosts/myadm/ [root@localhost star]# mv wordpress/* /web/vhosts/www/ mv: overwrite `/web/vhosts/www/index.php'? y [root@localhost star]#
那個php文件是剛纔我這裏測試的時候建立的。
如今咱們來測試一下,這兩個php應用能正常打開不。
注意防火牆。
都工做正常。
提示MySQL版本過低了。額。
我這裏用centos7作數據庫看一下。
修改一下phpMyAdmin配置文件對數據庫的指向。 只要修改php主機上的就能夠了。 咱們知道 httpd是不會執行php文件的。
phpMyAdmin:
$cfg['Servers'][$i]['host'] = '172.16.40.12';
久違的頁面終於出來了。
5、爲phpMyadmin添加https。
這裏就不寫怎麼創建私有CA了,前面都寫了兩遍,看的也眼花不是。只來過一下怎麼開啓吧。
httpd主機, php主機休息了。
掛載模塊,和開啓ssl的配置文件:
LoadModule ssl_module modules/mod_ssl.so LoadModule socache_shmcb_module modules/mod_socache_shmcb.so Include /etc/httpd24/extra/httpd-ssl.conf
證書和私鑰放到了配置目錄的ssl目錄下了。
[root@localhost httpd24]# ls extra httpd.conf magic mime.types original ssl [root@localhost httpd24]# ls ssl myadm.crt myadm.key [root@localhost httpd24]#
如今對於myadm.star.com虛擬主機的配置,www.star.com的虛擬主機沒有修改,這裏就不帖了。
<VirtualHost *:443> ServerName myadm.star.com DocumentRoot "/web/vhosts/myadm" CustomLog "/var/log/httpd24/myadm/access_log" combined ErrorLog "/var/log/httpd24/myadm/error_log" ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.40.21:9000/web/php/myadm/$1 SSLEngine on SSLCertificateFile "/etc/httpd24/ssl/myadm.crt" SSLCertificateKeyFile "/etc/httpd24/ssl/myadm.key" <Directory "/web/vhosts/myadm"> Options None Require all granted </Directory> </VirtualHost>
要注意SSLEngine on在如今這種狀態只能放在虛擬主機裏面,只在這個虛擬主機中啓用ssl。否則會由於另外一個虛擬主機沒有證書而報錯的。
[Fri Jan 29 11:59:27.191067 2016] [ssl:emerg] [pid 1216:tid 140707884644096] AH02572: Failed to configure at least one certificate and key for www.star.com:443 [Fri Jan 29 11:59:27.191204 2016] [ssl:emerg] [pid 1216:tid 140707884644096] SSL Library Error: error:140A80B1:SSL routines:SSL_CTX_check_private_key:no certificate assigned [Fri Jan 29 11:59:27.191212 2016] [ssl:emerg] [pid 1216:tid 140707884644096] AH02312: Fatal error initialising mod_ssl, exiting. AH00016: Configuration Failed
由於是私有CA,因此要手動的把CA的根證書導入瀏覽器。 讓瀏覽器信任由此CA所頒發的證書。
看一下如今的瀏覽器。