一、安裝前說明:
(一)目的:
① rpm包格式安裝配置lamp;
② 在httpd上提供兩個虛擬主機,一個用於wordpress,一個用於phpMyAdmin,爲後一個提供ssl訪問方式。
(二)涉及的程序包:
① httpd-2.2.15-39.el6.centos.x86_64
提供httpd服務,創建虛擬機
② mysql-5.1.73-3.el6_5.x86_64
提供mysql數據庫服務
③ php-5.3.3-38.el6.x86_64
提供php服務,運行動態內容
④ mod_ssl-2.2.15-39.el6.centos.x86_64
提供ssl服務,爲網頁提供https訪問服務
(三)安裝順序
前兩個必須是httpd和mysql,最後再安裝php。只要安裝了httpd,提供了好了虛擬機,mod_ssl可以隨時安裝。
二、實操
(一)、安裝httpd和php
因爲是模塊化連接的,所以不需要單獨啓動服務了,只需要啓動httpd,httpd會自動加載的
測試httpd是否能正常使用:
[root@aunt-s conf]# service httpd start Starting httpd: [ OK ] [root@aunt-s conf]# ss -tnlp | grep httpd LISTEN 0 128 :::80 :::* users:(("httpd",2899,4),("httpd",2901,4),("httpd",2902,4),("httpd",2903,4),("httpd",2904,4),("httpd",2905,4),("httpd",2906,4),("httpd",2907,4),("httpd",2908,4)) [root@aunt-s conf]#
(二)、爲httpd配置兩個基於域名的虛擬主機,www.a.com和www.b.org
1、準備虛擬機的本地文件系統目錄
[root@aunt-s /]# mkdir /vhost/{a.com,b.org}/htdocs -p
2、修改配置文件,創建2個虛擬機:
DocumentRoot "/var/www/html"
——在這一行前面加#號註釋掉
KeepAlive Off 改爲 KeepAlive On
在文件的末尾加上兩個虛擬機的設置:
NameVirtualHost 172.16.20.110:80 <VirtualHost 172.16.20.110:80> ServerAdmin [email protected] DocumentRoot /vhost/a.com/htdocs/ ServerName www.a.com ErrorLog /var/log/httpd/a.com-error_log CustomLog /var/log/httpd/a.com-access_log common </VirtualHost> <VirtualHost 172.16.20.110:80> ServerAdmin [email protected] DocumentRoot /vhost/b.org/htdocs ServerName www.b.org ErrorLog /var/log/httpd/b.org-error_log CustomLog /var/log/httpd/b.org-access_log common </VirtualHost>
修改 windows系統hosts文件,添加下面兩行,讓瀏覽器能直接打開網頁:
172.16.20.110 www.a.com a.com
172.16.20.110 www.b.org b.org
創建index.php,測試php能發正常使用
對修改過的配置文件做語法測試並重讀:
[root@aunt-s httpd]# httpd -t Syntax OK [root@aunt-s httpd]# service httpd reload Reloading httpd:
(三)、安裝mysql程序及 php-mysql程序
查詢錯誤日誌可以看到鏈接不成功的原因是:調用了錯誤的未定義的功能——mysql_connect() ,這說明還沒有安裝php與mysql連接的驅動程序 php-mysql。
[root@aunt-s httpd]# tail /var/log/httpd/a.com-error_log [Fri May 01 15:31:57 2015] [error] [client 172.16.250.148] PHP Fatal error: Call to undefined function mysql_connect() in /vhost/a.com/htdocs/index.php on line 3
mysql是C/S架構的,需要安裝服務器端和客戶端纔可以應用該服務的。yum安裝mysql-server時會自動安裝mysql客戶端
[root@aunt-s httpd]# yum list all mysql* Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Installed Packages mysql-libs.x86_64 5.1.73-3.el6_5 @CentOS/6.6 Available Packages mysql.x86_64 5.1.73-3.el6_5 DVD1 (客戶端程序包) mysql-server.x86_64 5.1.73-3.el6_5 DVD1 (服務器端程序包) (注:其餘的都刪除了)
安裝mysql服務器程序並啓動服務:
[root@aunt-s httpd]# yum install mysql-server -y ………… Total 43 MB/s | 9.6 MB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : mysql-5.1.73-3.el6_5.x86_64 1/3 Installing : perl-DBD-MySQL-4.013-3.el6.x86_64 2/3 Installing : mysql-server-5.1.73-3.el6_5.x86_64 3/3 Verifying : perl-DBD-MySQL-4.013-3.el6.x86_64 1/3 Verifying : mysql-server-5.1.73-3.el6_5.x86_64 2/3 Verifying : mysql-5.1.73-3.el6_5.x86_64 3/3 Installed: mysql-server.x86_64 0:5.1.73-3.el6_5 Dependency Installed: mysql.x86_64 0:5.1.73-3.el6_5 perl-DBD-MySQL.x86_64 0:4.013-3.el6 Complete!
[root@aunt-s httpd]# ss -tnlp | grep mysql LISTEN 0 50 *:3306 *:* users:(("mysqld",3382,10))
測試顯示,mysql默認監聽端口3306已處於監聽狀態。
因爲yum安裝的php是httpd的一個模塊,而php-mysql是一個php聯繫mysql的驅動程序,要是這個驅動程序開始工作,就需要重啓httpd纔有效。
[root@aunt-s htdocs]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
重新測試頁面,並查看錯誤日誌,雖然網頁頁面還不能正常顯示,但錯誤日誌顯示原因變爲:不能連接到mysql服務器
[root@aunt-s htdocs]# tail /var/log/httpd/a.com-error_log [Fri May 01 16:10:24 2015] [error] [client 172.16.250.148] PHP Warning: mysql_connect(): Can't connect to MySQL server on '172.0.0.1' (4) in /vhost/a.com/htdocs/index.php on line 3 [Fri May 01 16:10:24 2015] [error] [client 172.16.250.148] PHP Warning: mysql_close() expects parameter 1 to be resource, boolean given in /vhost/a.com/htdocs/index.php on line 8 [root@aunt-s htdocs]#
(四)、給虛擬機www.a.com配置使用wordpress,給www.b.org配置使用phpadmin
1、 給虛擬機www.a.com配置使用wordpress
[root@aunt-s htdocs]# lftp 172.16.0.1:/pub/Sources/5.i386/new_lamp lftp 172.16.0.1:/pub/Sources/5.i386/new_lamp> get wordpress-3.3.1-zh_CN.zip
這時一個zip壓縮的程序包,先解壓:
建立數據庫:
[root@aunt-s wordpress]# mysql …… mysql> CREATE DATABASE wpdb_aunts; Query OK, 1 row affected (0.00 sec) (這個是創建數據庫,名字叫wpdb_aunts) mysql> GRANT ALL ON wpdb_aunts.* TO 'aunts'@'172.16.20.110' IDENTIFIED BY 'aunts' ; Query OK, 0 rows affected (0.00 sec) (這個是授權數據庫wpdb_aunts下的所有文件都允許 主機 172.16.20.110上的aunts用戶憑密碼aunts使用) mysql> GRANT ALL ON wpdb_aunts.* TO 'aunts'@'localhost' IDENTIFIED BY 'aunts' ; Query OK, 0 rows affected (0.00 sec) (這是爲了防止系統反解地址而再次授權的) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) (讓數據庫重讀數據,是上面的設置生效) mysql> quit Bye [root@aunt-s wordpress]#
刷新頁面,啓用wordpress:
2、給www.b.org配置使用phpadmin
[root@aunt-s htdocs]# lftp lftp 172.16.0.1:/pub/Sources/sources/php> get phpMyAdmin-4.0.5-all-languages.zip 8004371 bytes transferred lftp 172.16.0.1:/pub/Sources/sources/php> bye [root@aunt-s htdocs]# ls index.php phpMyAdmin-4.0.5-all-languages.zip
安裝 mbstring (多字節字符串擴展包):
[root@aunt-s pma]# yum install php-mbstring -y …… Installed: php-mbstring.x86_64 0:5.3.3-38.el6 Complete! [root@aunt-s pma]# service httpd reload Reloading httpd:
因爲這個是php的擴展,php是模塊化安裝,所以安裝完後需要reload httpd是其生效。
給aunts授權,是aunts能創建數據庫文件,並修改使用:
[root@aunt-s pma]# mysql mysql> grant all on *.* to 'aunts'@'172.16.20.110'identified by 'aunts'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> grant all on *.* to 'aunts'@'localhost' identified by 'aunts'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@aunt-s pma]#
(五)、 爲使用phpadmin的虛擬機ww.b.org配置使用ssl訪問方式
新建了一臺虛擬機(ip:172.16.20.150)作爲發證主機,下面開始建立私有ca併發證給上面的www.b.org(ip:172.16.20.110)發證,配置ssl訪問方式
1、 創建一個私有ca
1.1、生成私鑰文件: /etc/pki/CA/private/cakey.pem
# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
1.2、生成自簽證書(實際上是給自己生成一個公鑰)
# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days #
-new: 生成新的證書籤署請求;
-key:私鑰文件路徑,用於提取公鑰;
-days N: 證書有效時長,單位爲“天”;
-out:輸出文件保存位置;
-x509:直接輸出自簽署的證書文件,通常只有構建CA時才這麼用;
1.3、提供輔助文件
# touch /etc/pki/CA/index.txt
# echo 01 > /etc/pki/CA/serial
2、給www.b.org發證書:
2.1、www.b.org申請證書
在證書申請的主機上進行如下步驟:
(1) 生成私鑰;
(2) 生成證書籤署請求;
注意:
① 其中的subject信息部分,要與CA的保持一致;
② Common Name要使用此主機在通信真實使用名字;
(3) 把請求發送給CA;
2.2、CA簽發證書
(1) 驗正請求者信息:一般都是人工校驗把握,看是不是真的那臺主機申請的
(2) 簽署證書
# openssl ca -in /PATH/FROM/CSR_FILE -out /PATH/TO/CRT_FILE -days N
(3) 把簽署好的證書發還給請求者
3、配置www.b.org 支持使用證書
3.1、安裝mod_ssl模塊
3.2修改配置ssl的配置文件然後將httpd 語法檢查、reload生效
做語法檢查:
[root@aunt-s ~]# httpd -t Syntax error on line 116 of /etc/httpd/conf.d/ssl.conf: SSLCertificateKeyFile: file '/etc/http/ssl/httpd.key' does not exist or is empty [root@aunt-s ~]# vim /etc/httpd/conf.d/ssl.conf SSLCertificateKeyFile /etc/httpd/ssl/httpd.key [root@aunt-s ~]# service httpd reload Reloading httpd:
查詢log結果是:
[root@aunt-s httpd]# tail /var/log/httpd/b.org-error_log [Fri May 01 21:21:28 2015] [warn] RSA server certificate CommonName (CN) `www.b.org' does NOT match server name!? [Fri May 01 21:21:28 2015] [warn] RSA server certificate CommonName (CN) `www.b.org' does NOT match server name!?
查看/etc/httpd/conf.d/ssl.conf,原來是域名指定錯誤,文件系統路徑也沒有設置
重設如下:
注:因爲設置https鏈接即是重新加設一個基於ip地址和443端口的安全連接,相當與重設一個新的主機了,所以 域名得指定、文件系統路徑也得重新指定。