一、安装前说明:

(一)目的:

① 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

rpm包格式安装配置lamp使用rpm包安装的php与httpd默认是模块化连接的

rpm包格式安装配置lamp

因为是模块化连接的,所以不需要单独启动服务了,只需要启动httpd,httpd会自动加载的

rpm包格式安装配置lamp


测试httpd是否能正常使用:

[[email protected] conf]# service httpd start
Starting httpd:                                            [  OK  ]
[[email protected] 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))
[[email protected] conf]#

rpm包格式安装配置lamp


(二)、为httpd配置两个基于域名的虚拟主机,www.a.com和www.b.org

1、准备虚拟机的本地文件系统目录

[[email protected] /]# 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能发正常使用

rpm包格式安装配置lamp

rpm包格式安装配置lamp

对修改过的配置文件做语法测试并重读:

[[email protected] httpd]# httpd -t
Syntax OK
[[email protected] httpd]# service httpd reload
Reloading httpd:


rpm包格式安装配置lamp

rpm包格式安装配置lamp

rpm包格式安装配置lamp

(三)、安装mysql程序及 php-mysql程序


查询错误日志可以看到链接不成功的原因是:调用了错误的未定义的功能——mysql_connect() ,这说明还没有安装php与mysql连接的驱动程序 php-mysql。

[[email protected] 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客户端

[[email protected] 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服务器程序并启动服务:

[[email protected] 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!

rpm包格式安装配置lamp


[[email protected] httpd]# ss -tnlp | grep mysql
LISTEN     0      50                        *:3306                     *:*      users:(("mysqld",3382,10))

测试显示,mysql默认监听端口3306已处于监听状态。

因为yum安装的php是httpd的一个模块,而php-mysql是一个php联系mysql的驱动程序,要是这个驱动程序开始工作,就需要重启httpd才有效。

[[email protected] htdocs]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

rpm包格式安装配置lamp


重新测试页面,并查看错误日志,虽然网页页面还不能正常显示,但错误日志显示原因变为:不能连接到mysql服务器

rpm包格式安装配置lamp


[[email protected] 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
[[email protected] htdocs]#


(四)、给虚拟机www.a.com配置使用wordpress,给www.b.org配置使用phpadmin

1、 给虚拟机www.a.com配置使用wordpress

[[email protected] 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压缩的程序包,先解压:


rpm包格式安装配置lamp

rpm包格式安装配置lamp


建立数据库:


rpm包格式安装配置lamp

[[email protected] 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
[[email protected] wordpress]#


刷新页面,启用wordpress:


rpm包格式安装配置lamp

rpm包格式安装配置lamp




2、给www.b.org配置使用phpadmin

[[email protected] 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
[[email protected] htdocs]# ls
index.php  phpMyAdmin-4.0.5-all-languages.zip

rpm包格式安装配置lamp


安装 mbstring (多字节字符串扩展包):

[[email protected] pma]# yum install php-mbstring -y
……
Installed:
  php-mbstring.x86_64 0:5.3.3-38.el6                                                          
Complete!
[[email protected] pma]# service httpd reload
Reloading httpd:

因为这个是php的扩展,php是模块化安装,所以安装完后需要reload httpd是其生效。


rpm包格式安装配置lamp


给aunts授权,是aunts能创建数据库文件,并修改使用:

[[email protected] 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
[[email protected] pma]#


rpm包格式安装配置lamp



(五)、 为使用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)

rpm包格式安装配置lamp

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

rpm包格式安装配置lamp

2、给www.b.org发证书:

2.1、www.b.org申请证书

在证书申请的主机上进行如下步骤:

(1) 生成私钥;

rpm包格式安装配置lamp

(2) 生成证书签署请求;


注意:

① 其中的subject信息部分,要与CA的保持一致;

② Common Name要使用此主机在通信真实使用名字;

rpm包格式安装配置lamp


(3) 把请求发送给CA;

rpm包格式安装配置lamp

2.2、CA签发证书

(1) 验正请求者信息:一般都是人工校验把握,看是不是真的那台主机申请的

(2) 签署证书

# openssl ca -in /PATH/FROM/CSR_FILE -out /PATH/TO/CRT_FILE -days N

rpm包格式安装配置lamp

(3) 把签署好的证书发还给请求者

rpm包格式安装配置lamp


3、配置www.b.org 支持使用证书

3.1、安装mod_ssl模块


rpm包格式安装配置lamp

rpm包格式安装配置lamp


3.2修改配置ssl的配置文件然后将httpd 语法检查、reload生效


rpm包格式安装配置lamp


做语法检查:

[[email protected] ~]# 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
[[email protected] ~]# vim /etc/httpd/conf.d/ssl.conf
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
[[email protected] ~]# service httpd reload
Reloading httpd:


rpm包格式安装配置lamp



查询log结果是:

[[email protected] 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,原来是域名指定错误,文件系统路径也没有设置

重设如下:


rpm包格式安装配置lamp

rpm包格式安装配置lamp


注:因为设置https链接即是重新加设一个基于ip地址和443端口的安全连接,相当与重设一个新的主机了,所以 域名得指定、文件系统路径也得重新指定。