Linux服务--CentOS6实现LAMP(源码安装)
实验要求:
安装php时实现php模块嵌入到httpd中和实现fpm两种方式。在fpm下,提供两个虚拟主机:
分别用于实现PHPMyadmin和WordPress,其中PhpMyAdmin提供ssl.
实验环境:
CentOS系统一台(IP:172.16.99.4),所需的httpd,mariadb,php,PhpAdmin,WordPress的源码包。
实验步骤:
安装顺序:httpd-->mariadb-->php.
安装前的准备工作:
1.安装开发环境:
本地yum安装开发环境:
yum groupinstall “Development Tools” “Server Platform Development”
2.关闭防火墙或者编写好相应的防火墙规则,关闭Selinux.
一.安装LAMP:实现php以模块嵌入到httpd中
1.编译安装httpd:
1.安装所以来的软件包pcre-devel软件包,本地镜像自带,所以yum安装即可。
2.下载apr和apr-util源码包,并编译安装。
APR(Apache portable Run-time libraries,Apache可移植运行库)的目的如其名称一样,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。在早期 的Apache版本中,应用程序本身必须能够处理各种具体操作系统平台的细节,并针对不同的平台调用不同的处理函数。
随着Apache的进一步开 发,Apache组织决定将这些通用的函数独立出来并发展成为一个新的项目。这样,APR的开发就从Apache中独立出来,Apache仅仅是使用 APR而已。目前APR主要还是由Apache使用,不过由于APR的较好的移植性,因此一些需要进行移植的C程序也开始使用APR,开源项目比如 Flood loader tester(http://httpd.apache.org/test/flood/,该项目用于服务器压力测试,不仅仅适用于Apache)
编译安装:apr-1.5.0.tar.bz2 ,apr-util-1.5.3.tar.bz2
# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make && make install
# tar xf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
3.编译安装httpd-2.4.12.tar.bz2
# tar xf httpd-2.4.12.tar.bz2
# cd httpd-2.4.12
# ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
# make && make install
4.安装完成后,进行相应的配置。
a.提供系统服务:
将/usr/local/httpd/bin目录下的apachectl文件复制到/etc/init.d/下,并命名为httpd:
# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
修改httpd文件:在文件前部分添加如下几行:
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.
添加系统服务:
# chkconfig --add httpd
b.添加PATH变量:
在目录/etc/profile.d/目录下创建一文件httpd.sh,内容如下:
export PATH=/usr/local/httpd/bin/:$PATH
3.安装配置完成,启动服务测试:
# service httpd start
2.编译安装Mariadb
1.准备数据存放的文件系统,使用的逻辑卷:
创建一目录用于挂在创建的逻辑卷:# mkdir -pv /mydata/data
创建逻辑卷并格式化挂在:
先分区,改变分区ID为8e
创建物理卷: # pvcreate /dev/sda5
创建卷组: # vgcreate myvg /dev/sda5
创建逻辑卷:# lvcreate -n mydata -L 5G myvg
格式化:mkfs -t ext4 /dev/mapper/myvg-mydata
挂在:mount /dev/mapper/myvg-mydata /mydata/data
永久挂在:编写文件/etc/fstab 添加一行:
/dev/mapper/myvg-mydate /mydata/data ext4 defaults 0 0
2.创建用户和组,使服务安全运行:
创建组:# groupadd -g 3333 mysql
创建用户:# useradd -g 3333 -u 3333 - r -s /sbin/nologin -M -d /mydata/data mysql
3.使用通用二进制格式安装 mariadb-5.5.43-linux-x86_64.tar.gz
# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mariadb-5.5.43-linux-x86_64 mysql
# cd mysql
进入mysql 目录之后,修改该目录下所有文件和目录的属主和属组:
# chown -R mysql.mysql .
初始化数据库:
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
再修改该目录下文件和目录的属主:# chown -R root .
4.进行后续配置:
a.提供系统服务:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
# chkconfig --add mysqld
# chkconfig mysqld on
b.提供配置文件,并在配置文件中添加几项:
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
修改配置文件:
datadir = /mydata/data
innodb_file_per_table = on
skip_name_resolve = on
c.修改PATH变量,直接可以使用mysql的相关工具:
在/etc/profile.d/目录下创建文件mysqld.sh:
export PATH=/usr/local/mysql/bin:$PATH
d.输出mysql的头文件至系统头文件路径:创建链接实现:
# ln -sv /usr/local/mysql/include /usr/include/mysql
e.输出mysql的库文件给系统库查找路径:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
让系统重新载入系统库:
# ldconfig
5.测试:
3.编译安装PHP
此次编辑php是把php以模块的形式编辑到httpd中,下文将介绍使用fpm编辑php,并进行先关的配置,安装WordPress和phpMyAdmin.
编译安装php-5.6.4.tar.xz
1.解决依赖关系:
# yum -y install bzip2-devel libmcrypt-devel libxml2-devel
2.编译安装php-5.6.4.tar.xz
# tar xf php-5.6.4.tar.xz
# cd php-5.6.4
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
说明:
1、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
2、如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
#./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
# make &&make install
3. 为服务提供配置文件:
# cp php.ini-production /etc/php.ini
4.编辑httpd的配置文件,是apache支持php
# vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
5.测试:
在index.php页面文件中添加如下内容:
<?php
$link=mysql_connect('127.0.0.1','root','');
if ($link)
echo "OK";
else
echo "Failure";
phpinfo();
?>
4.LAMP到此安装结束,只是没有进行相应的配置。我们先对lamp进行一下压力测试,查看服务器的负载能力。
# ab -c 200 -n 1000 http://172.16.99.4/index.php
Total transferred: 72493898 bytes
HTML transferred: 72313898 bytes
Requests per second: 24.05 [#/sec] (mean)
Time per request: 8316.375 [ms] (mean)
Time per request: 41.582 [ms] (mean, across all concurrent requests)
Transfer rate: 1702.54 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 31 79.8 1 800
Processing: 92 7224 10262.6 1290 41014
Waiting: 85 7129 10161.5 1275 41013
Total: 111 7255 10267.4 1364 41032
发现每秒处理24.05个请求:Requests per second: 24.05 [#/sec] (mean)
接下来安装Xcahe,再来进行压力测试,来查看结果:
5.安装xcache ,为php加速:
1、安装
# tar xf xcache-3.2.0.tar.gz
# cd xcache-3.2.0
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
编译完出现:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
2、编辑php.ini,整合php和xcache:
首先将xcache提供的样例配置导入php.ini
# mkdir /etc/php.d
将源码包解压的目录下的xcache.ini复制到/etc/php.d
# cp xcache.ini /etc/php.d
接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
6.重新启动httpd服务和mariadb服务进行压力测试:
]# ab -c 200 -n 1000 http://172.16.99.4/index.php
Total transferred: 76241920 bytes
HTML transferred: 76061920 bytes
Requests per second: 562.74 [#/sec] (mean)
Time per request: 355.406 [ms] (mean)
Time per request: 1.777 [ms] (mean, across all concurrent requests)
Transfer rate: 41898.61 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 32 28.8 27 100
Processing: 15 140 137.7 118 1201
Waiting: 2 101 137.0 75 1200
Total: 22 172 137.1 144 1274
查看结果Requests per second: 562.74 [#/sec] (mean),明显比上次快了几倍!
二.安装LAMP,实现php以fpm方式与Apache互联
httpd,mariadb的安装与前一部分相同,这里只需安装php就行。
1.编译安装 php-5.6.4.tar.xz
# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
make && make install
这里把--with-apxs2=/usr/local/apache/bin/apxs 改成--enable-fpm ,即,就是在编辑php时,添加fpm模块。
说明:如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
为php提供配置文件:#cp php.ini-production /etc/php.ini
2.配置php-fpm:
安装完php之后,会在php-5.6.4目录下的有一个目录提供了php-fpm的服务脚本。
sapi/fpm/init.d.php-fpm
1.将php-fpm添加至服务列表:
php-5.6.4]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
]# chkconfig --add php-fpm
]# chkconfig --level php-fpm
2.给php-fpm提供配置文件:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
3.配置php-fpm的配置文件,修改内容:
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php5/var/run/php-fpm.pid
4.启动php-fpm服务,查看是否成功:
# service php-fpm start
3.配置httpd:添加支持fcig模块和配置虚拟主机,支持fcgi:
1.让httpd能识别php文件,
# vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
2.添加fcgi模块
在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
3.配置支持fcgi的虚拟主机
在配置文件中,取消对#Include /etc/httpd24/extra/httpd-vhosts.conf的注释,使httpd支持虚拟主机。
编辑虚拟主机的配置文件:
# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost wordp.zkchang.com:80>
DocumentRoot "/web/vhosts/wordp/"
ServerName wordp.zkchang.com
ErrorLog "/var/log/v_wp_error_log"
CustomLog "/var/log/v_wp_access_log" common
ProxyRequests off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/vhosts/wordp/$1
<Directory "/web/vhosts/wordp/">
Options None
Allowoverride None
Require all denied
Require ip 172.16.0.0/16
</Directory>
</VirtualHost>
<VirtualHost phpma.zkchang.com:80>
DocumentRoot "/web/vhosts/pma"
ServerName phpma.zkcang.com
ErrorLog "/var/log/v_pma_ror_log"
CustomLog "/var/log/v_pma_access_log" common
ProxyRequests off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/vhosts/pma/$1
<Directory "/web/vhosts/pma/">
Options None
Allowoverride None
Require all denied
Require ip 172.16.0.0/16
</Directory>
</VirtualHost>
4.分别在不同主机的站点目录下创建测试网页文件,进行测试:
# vim /web/vhosts/wordp/index.php
<?php
echo "This is a wordprocess page"
?>
# vim /web/vhosts/pma/index.php
<?php
$link = mysql_connect("127.0.0.1","root","");
if ($link)
echo "OK";
else
echo "Worry!";
mysql_close();
phpinfo();
?>
测试结果截图:
5.对fcgi模式下进行压力测试,并与php以模块化嵌入httpd的进行比较:
# ab -c 200 -n 1000 http://phpma.zkchang.com/index.php
Total transferred: 66731883 bytes
HTML transferred: 66531883 bytes
Requests per second: 637.04 [#/sec] (mean)
Time per request: 313.951 [ms] (mean)
Time per request: 1.570 [ms] (mean, across all concurrent requests)
Transfer rate: 41514.64 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 13.3 0 42
Processing: 30 143 125.8 137 1077
Waiting: 26 142 125.7 136 1077
Total: 51 150 123.1 138 1077
6.安装Xcache,为php进行加速:
# tar xf xcache-3.2.0.tar.bz2
# cd xcache-3.2.0
xcache-3.2.0]# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
编译完出现:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
编辑php.ini,整合php和xcache:
首先将xcache提供的样例配置导入php.ini
# mkdir /etc/php.d
将源码包解压的目录下的xcache.ini复制到/etc/php.d
# cp xcache.ini /etc/php.d
接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
再次进行压力测试,进行比较:
# ab -c 200 -n 1000 http://phpma.zkchang.com/index.php
Total transferred: 66731876 bytes
HTML transferred: 66531876 bytes
Requests per second: 1322.20 [#/sec] (mean)
Time per request: 151.263 [ms] (mean)
Time per request: 0.756 [ms] (mean, across all concurrent requests)
Transfer rate: 86165.07 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 4.6 0 14
Processing: 13 135 36.6 145 351
Waiting: 13 134 36.8 144 350
Total: 19 137 34.8 146 365
三.分别在两个虚拟主机上安装phpmyadmin. WordPress,并实现通过ssl访问phpmyadmin。
1.解压安装phpmyadmin,并实现ssl:
1.提供phpmyadmin:
]# unzip phpMyAdmin-4.4.14.1-all-languages.zip
]# cd phpMyAdmin-4.4.14.1-all-languages
]# cp -a phpMyAdmin-4.4.14.1-all-languages /web/vhosts/pma/pma
]# cp /web/vhost/pma/pma/config.sample.inc.php{,.bak}
]# mv /web/vhost/pma/pma/config.sample.inc.php config.inc.php
# vim /web/vhost/pma/pma/config.inc.php
$cfg['blowfish_secret'] = 'asdfghjkl'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH!
打开网页测试:
2.给mysql的管理员用户设置密码:
MariaDB [(none)]> SET PASSWORD FOR 'root'@'127.0.0.1'=PASSWORD('zkchang');
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('zkchang');
设置完密码之后就可以登录phpmyadmin
3.启动ssl模块,支持https协议。
修改httpd主配置文件httpd.conf:把:
#Include /etc/httpd24/extra/httpd-ssl.conf 取消注释
a.为httpd服务器生成证书:
由于在主机172.16.99.2上已经创建了CA的私有CA,在此就不在创建证书颁发机构的私有证书,只需在httpd服务器上创建私钥和证书请求,然后把证书请求发给172.16.99.2,让CA颁发机构给服务器颁发证书即可。
1.在服务器172.16.99.4上创建私钥和证书请求,并把证书请求发送给CA颁发机构:
创建私钥:]# (umask 077;openssl genrsa -out /etc/httpd24/ssl/httpd.key 2048)
创建证书请求:
]# openssl req -new -key /etc/httpd24/ssl/httpd.key -out /etc/httpd24/ssl/httpdpma.csr -days 365
发给CA颁发机构:
]# scp /etc/httpd24/ssl/httpdpma.csr [email protected]:/tmp/
2.在CA颁发机构172.16.99.2上为服务器颁发证书,并把证书发送给服务器:
签发证书:
CA]# openssl ca -in /tmp/httpdpma.csr -out /etc/pki/CA/certs/httpdpma.crt -days 365
发给服务器:
# scp /etc/pki/CA/certs/httpdpma.crt [email protected]:/etc/httpd24/ssl
b.编辑/etc/httpd24/extra/httpd-ssl.conf 文件,修改一下内容:
<VirtualHost _default_:443>
DocumentRoot "/web/vhosts/pma/"
ServerName phpma.zkchang.com:443
#ServerAdmin [email protected]
ErrorLog "/var/log/ssl_v_error_log"
TransferLog "/var/log/ssl_v_access_log"
<Directory "/web/vhosts/pma/">
Options None
Allowoverride None
Require all denied
Require ip 172.16.0.0/16
</Directory>
SSLCertificateFile "/etc/httpd24/ssl/httpdpma.crt"
SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key
c.在httpd配置文件中启动相应的ssl模块
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule ssl_module modules/mod_ssl.so
d.将CA颁发机构的CA证书,导入到浏览器中进行测试:
2.解压安装WordPress:
1.解压安装:
]# unzip wordpress-4.3.1-zh_CN.zip
]# cp -a wordpress /web/vhosts/wordp/wordp
]#cd /web/vhosts/wordp/wordp/
]# mv wp-config-sample.php wp-config.php
]# vim wp-config.php
2.创建数据库,创建用户和密码并授权:
MariaDB [(none)]> CREATE DATABASE wordpdb;
MariaDB [(none)]> CREATE USER 'wordpuser'@'127.0.0.1' IDENTIFIED BY 'wordpuser';
MariaDB [(none)]> CREATE USER 'wordpuser'@'localhost' IDENTIFIED BY 'wordpuser';
MariaDB [(none)]> GRANT ALL ON wordpdb.* TO 'wordpuser'@'127.0.0.1';
MariaDB [(none)]> GRANT ALL ON wordpdb.* TO 'wordpuser'@'localhost';
3.网页打开测试:
4.对一个动态网页做压力测试:
# ab -c 200 -n 1000 http://wordp.zkchang.com/wordp/index.php
Requests per second: 16.49 [#/sec] (mean)
Time per request: 12132.181 [ms] (mean)
Time per request: 60.661 [ms] (mean, across all concurrent requests)
Transfer rate: 5.35 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 59 168.4 0 1430
Processing: 271 10657 6602.9 9321 50520
Waiting: 271 10379 6469.7 9191 49844
Total: 275 10716 6585.6 9404 50520
转载于:https://blog.51cto.com/zkchang/1697968