分离式LAMP搭建及各类服务实现
每个网站都是基于一个环境,然后才能运行,而大多数情况下,应用最多的就是我们的LAMP,其中L指的是Linux,A指的是Apache的httpd,M指的是MySQL,不过我们接下来采用的MariaDB,最后P就是专门用于动态网页开发的PHP了,首先会介绍搭建LAMP的步骤,最后再介绍优化方法。拓扑图如下所示:
实验环境准备:软件包以及下载地址
Linux主机:CentOS6.5
httpd服务器:
apache:httpd-2.4.9.tar.bz2 http://olex.openlogic.com/packages/apache/2.4.9
apr:apr-1.5.0.tar.bz2 https://apr.apache.org/download.cgi
apr-util:apr-util-1.5.3.tar.bz2 https://apr.apache.org/download.cgimariadb服务器:图中的mysql服务器
MariaDB:mariadb-5.5.39.tar.gz https://downloads.mariadb.org/mariadb/5.5.39/
建议多加一块硬盘,用于存放数据库的数据
php服务器:
php:php-5.4.26.tar.bz2 http://mirrors.sohu.com/php/
xcache:xcache-3.0.3.tar.gz http://xcache.lighttpd.net/pub/Releases/3.0.3/
注意:DNS服务器也可以搭建在其中任意一台服务器上,但是生成环境下肯定是分离开的
LAMP环境搭建如下:
首先,编译时要用到gcc-c++的编译器,可以把Development Tools这个组包安上或是安装gcc-c++
1、搭建httpd的web服务器
httpd所依赖的软件为apr和apr-util,所有,我们要把这两个包装上
安装apr软件包
1
2
3
4
# tar jxf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure- -prefix=/usr/local/apr
# make && make install
中间如果遇到问题建议google搜索
安装apr-util的软件包
1
2
3
4
# tar jxf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure- -prefix=/usr/local/apr-util- -with-apr=/usr/local/apr/ #这个要将前面的apr一起编译进去
# make && make install
安装httpd软件包,要求原有的rpm包安装的httpd服务已经停止
1
2
# tar jxf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
我们可以使用下面的命令,查看configure的参数选项
1
2
3
4
5
6
7
# ./configure –help
[iyunv@localhost
httpd-2.4.9]# ./configure--prefix=/usr/local/apache--sysconfdir=/etc/httpd2
--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- -enable-so:允许动态加载模块(prefork,worker,event)
--with-mpm=event:这里使用的event时间模块
以下是我出现的错误及解决方法:
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
这是由于缺少pcre的开发包所导致的错误
解决办法,安装pcre的开发包
1
# yum install -y pcre-devel
没有问题出现后,进行编译、安装
1
# make && make install
然后,将命令加入到PATH环境变量中去
1
2
3
# vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH #加入这行
# source /etc/profile.d/httpd.sh #重读一下这个文件
将man手册页加入到系统中
1
2
# vim /etc/man.config
MANPATH /usr/local/apache/man
时间有些长,耐心等待就好,httpd的编译结束,启动服务
1
# apachectl start
服务启动后,我们在浏览器里输入IP地址进行验证
2、搭建MariaDB的数据库服务器,我们这里采用二进制的安装包安装
进程运行的用户,如果权限过大,万一服务被劫持,就会给服务器和机房内的其他服务器造成严重的威胁,所有,mariadb服务运行于mysql用户之下
创建mysql的用户和组:
1
2
# groupadd -r mysql
# useradd -g mysql -r mysql
将安装包解压至/usr/local目录下
1
# tar zxf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/
为mariadb服务器提供一个存放数据的目录,用另一个磁盘,用逻辑卷的形式挂载后利用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# fdisk /dev/sdb #格式成LVM的格式
# fdisk -l /dev/sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xca6e1b9a
Device Boot Start End Blocks IdSystem
/dev/sdb1 1 2610 20964793+8eLinux LVM将上面的磁盘做成逻辑卷:
# pvcreate /dev/sdb1
dev_is_mpath: failed to get device for 8:17
Physical volume "/dev/sdb1" successfully created
# vgcreate myvg /dev/sdb1
Volume group "myvg" successfully created
# lvcreate -L 15G -n mylv myvg
Logical volume "mylv" created
# lvs
LV VG Attr LSizePool Origin Data%Move Log Cpy%Sync Convert
mylv myvg -wi-a—– 15.00g
将逻辑卷格式化后,进行挂载使用:
1
# mke2fs -t ext4 -b 2048 -m 3 -L MYSQL /dev/myvg/mylv
创建挂载的目录:/MySQL,使其开机后都自动挂载
1
2
3
4
# mkdir /MySQL
# vim /etc/fstab
LABEL=MYSQL /MySQL ext4 defaults 0 0
# mount –a #让fstab下的挂载选项生效
可以使用mount命令查看,是否已经挂载成功
对解压后的mariadb的文件夹进行如下操作
1
2
# cd /usr/local/
# ln -sv mariadb-5.5.36-linux-x86_64/ mysql #做一个软链接
给mariadb服务提供配置文件和启动脚本
1
2
3
4
5
6
7
8
9
# cd mysql/support-files/
# mkdir /etc/mysql #存放配置文件的目录
# cp my-large.cnf /etc/mysql/my.cnf #配置文件
# mkdir /MySQL/data #提供数据存放目录
# vim /etc/mysql/my.cnf #编辑配置文件,加入如下配置,在mysqld中
datadir = /MySQL/data #指定数据存放的位置# cp mysql.server /etc/rc.d/init.d/mysqld #提供启动脚本
# chkconfig –add mysqld #加入开机启动选项
# chkconfig –list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
初始化mariadb服务
1
# ./scripts/mysql_install_db –user=mysql –datadir=/MySQL/data/
启动mariadb服务
1
2
# service mysqld start
Starting MySQL…
查看3306端口是否在监听
1
# ss -tnlp
3、以fpm的形式安装php服务器
安装php的软件包
1
2
3
4
5
6
7
# tar jxf php-5.4.26.tar.bz2
[iyunv@localhost
php-5.4.26]# ./configure--prefix=/usr/local/php- -with-mysql=mysqlnd--with-openssl- -with-mysqli=mysqlnd-
-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--enable-maintainer-zts
这里用了mysqlnd,表明的是本机就不需要先安装MySQL或MySQL开发包了
由于上面的apache编译时采用的event模式,所以,编译php时,这个选项一定要加:--enable-maintainer-zts
出现的错误如下:
configure: error: xml2-config not found. Please check your libxml2 installation.
这是libxml2的开发包没有安装,解决方法如下:
1
# yum install -y libxml2-devel
configure: error: Please reinstall the BZip2 distribution
是由于bzip2的开发包没有安装,安装即可
1
# yum install -y bzip2-devel
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
这是因为libmcrypt的开发库没有,安装就行了
1
# yum install -y libmcrypt-devel
进行编译安装
1
# make && make install
接下来为php-fpm提供配置文件
1
# cp php.ini-production /etc/php.ini
提供php-fpm的启动脚本,并设为开机启动选项
1
2
3
4
5
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod a+x /etc/rc.d/init.d/php-fpm #添加执行权限
# chkconfig –add php-fpm
# chkconfig –list php-fpm
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
为php-fpm提供一个配置文件
1
2
# cd /usr/local/php/etc/
# mv php-fpm.conf.default php-fpm.conf #更改名称,作为php-fpm的配置文件
配置php-fpm的配置文件
1
2
3
4
5
6
7
8
9
10
11
# vim php-fpm.conf #更改内容如下所示
pid = /usr/local/php/var/run/php-fpm.pid
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
listen =172.16.0.0:9000 #监听172.16这个段地址的9000端口
pm.max_children = 20 #每个进程启动多少个线程
pm.start_servers = 4 #启动的进程数目
pm.min_spare_servers = 2 #最小的空闲进程数
pm.max_spare_servers = 6 #最大的空闲进程数
其他的配置默认不变
接下来,我们就可以启动php-fpm服务了
1
# service php-fpm start
查看服务是否已经启动:
1
# ss –tlnp #9000的端口是否监听
4、安装DNS服务器,这里,我就在httpd的服务器上进行的
安装DNS软件,即bind
1
# yum install bind
我们下面要用到的域名有:
wp.test.com
pma.test.com
dpl.test.com
编辑DNS的主配置文件,更改如下:
1
2
3
4
5
6
7
8
9
10
# vim /etc/named.conf
options {
directory "/var/named";
recursion yes;
};
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.zones";
然后再辅配置文件下,加入test.com这个域
1
2
3
4
5
# vim /etc/named.rfc1912.zones
zone "test.com" IN {
type master;
file "test.com.zone";
};
新建区域文件,如下所示,在/var/named目录下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$TTL 3600
$ORIGINtest.com.
@ IN SOA ns.test.com. admin.test.com. (
2014081301
2H
10M
5D
1D
)
IN NS ns
ns IN A 172.16.30.30
wp IN A 172.16.30.30
pma IN A 172.16.30.30
dpl IN A 172.16.30.30
将客户端的DNS服务器选项指定到上方的DNS服务器的地址
实验测试要求:
提供wordpress博客服务,要求基于用户名认证(用户名luffy,密码luffy),是虚拟主机搭建
wordpress-3.3.1-zh_CN.zip
提供phpMyadmin的数据库页面管理工具,是基于https的,要有证书认证
phpMyAdmin-3.5.1.tar.bz2
提供drupal的服务,要求提供查看服务器情况,即server-status
准备工作:
在php的服务器上,创建/web/wp,/web/pma,和/web/dpl三个目录
为了保持一致,我们在httpd的服务也创建上面的三个目录,具体步骤略。
1、基于虚拟主机,搭建wordpress,实现验证功能
编辑httpd服务器里的httpd配置文件
先将DocumentRoot注释,并且启用模块,并做如下更改
1
2
3
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
加载httpd-vhosts.conf这个配置文件
1
Include /etc/httpd2/extra/httpd-vhosts.conf
更改:识别index.php作为主页
1
2
3
DirectoryIndex index.html index.php
添加:使其能解析php
1
2
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
之后编辑extra下的httpd-vhosts.conf,编辑更改如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DocumentRoot "/web/wp"
ServerName wp.test.com
ProxyRequests Off
ProxyPassMatch ^/(.*.php)$ fcgi://172.16.30.40:9000/web/wp/$1
Options none
AllowOverride AuthConfig
AuthType Basic
AuthName "WordPress authorization"
AuthUserFile /etc/httpd2/.htpasswd
Require user luffy
然后,先在php的服务器上存放wordpress的程序包,等程序运行一段时间后,
拷贝到httpd的服务器上,这样是为了数据的一致性,安装时,请求的是php文件,
php服务器与数据库进行交互,之后才能从php服务器拷贝过去
同时,要在mariadb数据库服务器里添加一个库,名为wpdb,操作如下
1
2
3
4
5
6
MariaDB [(none)]> CREATE DATABASE wpdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> GRANT ALL ON wpdb.* TO wpadmin@localhost IDENTIFIED BY '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
然后解压wordpress安装包,将wp-config-sample.php重命名为wp-config.php
# vim wp-config.php
更改如下图所示:而且要php服务器和httpd服务器的wp-config.php配置一致
重启httpd服务器后,在浏览器上进行验证:
1
# apachectl stop && apachectl start
然后,访问wordpress的主页,在浏览器输入http://wp.test.com
基于openssl建立https的web服务器,对phpmyadmin程序进行加密
基于openssl的网页加密,要有证书才行,这里,我们先自建CA,签发证书
建立CA前的准备
1
2
3
4
# cd /etc/pki/CA/ #CA的目录
# touch index.txt
# touch serial
# echo 01 > serial
建立CA,自签证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# (umask 077; openssl genrsa -out private/key.pem 2048)
# openssl req -new -x509 -key key.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
—–
Country Name (2 letter code) :CN
State or Province Name (full name) []:HA
Locality Name (eg, city) :ZZ
Organization Name (eg, company) :wlzx
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:pma.test.com
Email Address []:admin@test.com
为httpd服务器生成密钥,生成证书:
# cd /usr/local/apache/
# mkdir ssl
# cd ssl/
# (umask 077;openssl genrsa -out httpd.key 2048) #生成公钥
# openssl req -new -key httpd.key -out httpd.csr #生成请求
其他的与上面几乎一致,下面如下,直接回车,不要输入任何信息
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
# openssl ca -in httpd.csr -out httpd.crt -days 365 #CA签署证书
编辑httpd程序的主配置文件,启用以下两个注释
1
2
3
4
# vim /etc/httpd2/httpd.conf
LoadModule ssl_module modules/mod_ssl.so #加载ssl的模块
Include /etc/httpd2/extra/httpd-ssl.conf #加载ssl的配置文件
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
配置ssl的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# vim /etc/httpd2/extra/httpd-ssl.conf #修改如下所示
DocumentRoot "/web/pma"
ServerName pma.test.com:443
ProxyRequests Off
ProxyPassMatch ^/(.*.php)$ fcgi://172.16.30.40:9000/web/pma/$1
Options none
AllowOverride none
Require all granted
SSLCertificateFile "/usr/local/apache/ssl/httpd.crt"
SSLCertificateKeyFile "/usr/local/apache/ssl/httpd.key"
然后,在httpd服务器和php服务器里的/web/pma目录中都拷贝phpmyadmin的软件
都解压出来,放在pma目录下
1
2
3
4
5
tar jxf phpMyAdmin-3.5.1.tar.bz2
# cd phpMyAdmin-3.5.1-all-languages/
# mv * ./..
# cd ..
# rm -rf phpMyAdmin-3.5.1-all-languages/
编辑phpmyadmin的配置文件,使其连上mariadb服务器,只需要改php服务上的
1
2
3
# cp config.sample.inc.php config.inc.php
# vim config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.30.50'; #将localhost改成mariadb的服务器地址
完成后,重启httpd服务,在浏览器里测试输入:https://pma.test.com
我们要先修改phpmyadmin这个程序的配置文件,使其可以跟mariadb数据库交互
1
2
3
# cp config.sample.inc.php config.inc.php
# vim config.inc.php #更改如下
$cfg['Servers'][$i]['host'] = '172.16.30.50';
我们可以通过ftp把证书下载下来,导入到本机,关闭浏览器后,再次访问
上面输入用户名密码之后,就能登入到mariadb的数据库服务器上了
提供drupal的服务,另附查看httpd服务器状态功能
编辑httpd服务的虚拟主机的配置文件,添加如下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DocumentRoot "/web/dpl"
ServerName dpl.test.com
ProxyRequests Off
ProxyPassMatch ^/(.*.php)$ fcgi://172.16.30.40:9000/web/dpl/$1
Options none
AllowOverride none
Require all granted
SetHandler server-status
Require all granted
将drupal的程序包分别下载到php服务器和httpd服务器的/web/dpl目录下
然后解压缩,将程序包的所有内容放到dpl目录下,参考上方对phpmyadmin的操作
更改drupal连接mariadb的配置文件,如下所示
1
# vimsites/default/default.settings.php
取消斜杠注释符,修改如下内容
1
2
3
4
5
6
7
8
9
10
* array(
* 'driver' => 'mysql',
* 'database' => 'dpl',
* 'username' => 'dpladmin',
* 'password' => '123',
* 'host' => '172.16.30.50',
* 'port' => 3306,
* 'prefix' => 'myprefix_',
* 'collation' => 'utf8_general_ci',
* );
这个过程并不是很难,只是加了一个Location,指定了status
重启httpd服务后,在浏览器上输入:http://dpl.test.com
然后再次输入:http://dpl.test.com/status,效果如下所示
页:
[1]