opijoiu 发表于 2016-1-25 08:47:02

LAMP架构之使用fpm机制编译安装

apache和php结合有三种方式。它们分别是编译php为apache的模块;使用cgi协议;使用fastcgi协议;cgi协议相比较于fastcgi,所占资源较多。故本博文仅介绍该如何快速基于fastcgi方式来构建LAMP。

一、配置前准备
[*]1、三台CentOS 6主机。主机A地址为172.16.25.67,主机B为172.16.25.68,主机C为172.16.25.69
[*]2、在两台主机配置好yum源。
[*]3、下载两个web架构包phpMyAdmin-4.4.14.1-all-languages.zip和wordpress-4.4.1-zh_CN.zip
[*]4、下载httpd-2.4以上的源码包、mariadb-5.5二进制程序包、php-5.4源码包、apr-1.4以上的版本、apr-util-1.4以上的版本、xcache源码包

[*]apr和apr-util下载

[*]httpd下载

[*]mariadb下载

[*]php下载

[*]xcache下载


[*]phpMyAdmin下载

二、配置目标
   1、主机A作为acache主机,响应客户端的请求;
   2、主机B作为mysql主机,为web架构提供数据;
   3、主机C作为php主机,专门接受从apache而来的请求处理动态资源。

三、编译安装httpd
1、在主机A下载开发包组DevelopmentTools, Server Platform Development以及开发程序包pcre-devel和openssl-devel

1
2
3
# yum groupinstall 'Development Tools' 'Server Platform Development'
# yum install pcre-devel
# yum install openssl-devel




2、解压指定源码包并编译安装

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
30
# 编译安装apr
# tar xf /usr/src/apr-1.5.0.tar.bz2 -C /usr/src/
# cd /usr/src/apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make && make install

# 编译安装apr-util
# tar xf /usr/src/apr-util-1.5.3.tar.bz2 -C /usr/src
# cd /usr/src/apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install

# 编译安装httpd
# tar xf /usr/src/httpd-2.4.10.tar.bz2 -C /usr/local
# cd /usr/local/httpd-2.4.10/
# ./configure \
> --prefix=/usr/local/httpd24 \   # 指明安装目录
> --sysconfdir=/etc/httpd24 \      # 指明配置文件安装目录
> --enable-rewrite \         # 支持url重写
> --enable-so \             # 允许动态模块装载
> --enable-ssl \             # 允许使用ssl
> --with-pcre \             # 支持pcre
> --with-zlib \             # 支持压缩
> --with-apr=/usr/local/apr \ # 指明apr(可移植运行时环境,支持不同平台)安装目录
> --with-apr-util=/usr/local/apr-util \ # 指明apr-util的安装目录
> --enable-modules=most \      # 指明要支持的模块选项为大多数
> --enable-mpms-shared=all \       # 支持装载的mpm模块
> --enable-mpm=worker         # 默认的mpm方式是worker

# make -j 4 && make install ---> -j 4 表示同时启动四个线程




3、配置httpd-2.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 此时httpd没有服务脚本,故要使用apachectl这个命令来启动,新建一个httpd24.sh写入其路径
# touch /etc/profile.d/httpd24.sh
# vim /etc/profile.d/httpd24.sh
# cat /etc/profile.d/httpd24.sh
export PATH=/usr/local/httpd24/bin:$PATH
# source /etc/profile.d/httpd24.sh

# 配置头文件
# ln -sv /usr/local/httpd24/include/ /usr/include/httpd
`/usr/include/httpd' -> `/usr/local/httpd24/include/'

# 启用mpm机制
# vim /etc/httpd24/httpd.conf
# Server-pool management (MPM specific)
Include /etc/httpd24/extra/httpd-mpm.conf---> 找到此行去掉注释
#启用fpm机制,在httpd.conf中找到这两个模块去掉注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so




4、启动服务并测试

1
2
3
4
5
6
# apachectl start
# ss -tnl
StateRecv-Q Send-Q    Local Address:Port   Peer Address:Port
LISTEN   0   128      :::111         :::*   
LISTEN   0   128         *:111            *:*   
LISTEN   0   128      :::80            :::*





三、使用通用二进制包编译安装mariadb
1、为mariadb提供一个数据目录

1
2
# mkdir -pv /mydata/data
mkdir: created directory `/mydata/data'





2、解压对应包并进行如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# tar xf /usr/src/mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local
# useradd -r mysql    ----> 添加系统用户mysql
# ln -sv /usr/local/mariadb-5.5.46-linux-x86_64/ /usr/local/mysql
`/usr/local/mysql' -> `/usr/local/mariadb-5.5.46-linux-x86_64/'
# cd /usr/local/mysql
# chown -R root.mysql ./*---> 更改目录下文件的属主和属组

# 初始化数据库
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# 提供服务脚本
# cp support-files/mysql.server /etc/init.d/mysqld
# 加入服务列表
# chkconfig --add mysqld




3、提供配置文件并更改配置文件如下,启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
# cp support-files/my-large.cnf /etc/my.cnf
# vim /etc/my.cnf
----> 在mysqld配置段添加如下三行内容
datadir = /mydata/data
innodb_file_per_table = ON
skip_name_resolve = ON
# service mysqld start
Starting MySQL               [确定]
# 查看3306端口是否启用
# ss -tnl
State    Recv-Q Send-Q   Local Address:Port      Peer Address:Port
LISTEN    0    128      *:46237             *:*   
LISTEN    0    50         *:3306             *:*




4、安全配置mariadb

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH   ---> 写入此行
# source /etc/profile.d/mysql.sh

# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command notfound

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.If you'vejust installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? Y---->更改root用户密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.This is intended only for testing,and to make the installation
go a bit smoother.You should removethem before moving into a
production environment.

Remove anonymous users? Y---->移除多余用户
... Success!

Normally, root should only be allowed to connect from 'localhost'.This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? Y----->是否禁止root用户远程登录
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.This is also intended only fortesting, and should be removed
before moving into a production environment.

Remove test database and access to it? Y --->移除数据库test
- Dropping test database...
ERROR 1HY000) at line 1: Can't drop database 'test'; database doesn'texist
... Failed!Not critical, keep moving...
- Removing privileges on testdatabase...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? Y   ---->刷新权限列表
... Success!

Cleaning up...

All done!If you've completed all of theabove steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!




四、在主机C编译安装php
1、下载所需开发包组Development Tools, Server Platform Development以及开发程序包libxml2-devel、libmcrypt-devel、openssl-devel、bzip2-devel(这些程序包若存在则不必安装)

1
2
3
# yum groupinstall 'Development Tools' 'Server Platform Development'
# yum install libxml2-devel openssl-devel libmcrypt-devel
# yum install php-mbstring    ---> 支持多字节字符的程序包




2、解压php源码包并编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# tar xf /usr/src/php-5.6.4.tar.xz -C /usr/src/
# cd /usr/src/php-5.6.4/
# ./configure \
> --prefix=/usr/local/php \# 指明php安装目录
>--sysconfdir=/etc/php \# 指明php配置文件的安装目录
> --with-openssl \       # 启用ssl
> --enable-mbstring \   # 允许多字节字符,以便支持中文
> --with-freetype-dir \    # 支持显示图形结果时显示字体
> --with-jpeg-dir \      # 支持使用jpeg格式
> --with-png-dir \       # 支持png格式
> --with-zlib \      # 支持zlib压缩
> --with-libxml-dir=/usr \   # 支持使用分析xml
> --enable-xml \      # 使用xml
> --enable-sockets \      # 支持以sockets方式通信
> --enable-fpm \      # 使用fastcgi机制
> --with-mcrypt \       # 支持加密库
> --with-bz2 \         # 支持bz2压缩
> --enable-maintainer-zts \# 支持线程模式
> --with-mysql=mysqlnd \    # 支持连接mysql数据库
> --with-pdo-mysql=mysqlnd \
> --with-mysqli=mysqlnd
# make -j 4 && make intall




注意:如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖。

3、为php提供配置文件

1
# cp /usr/src/php-5.6.4/php.ini-production /etc/php/php.ini




4、为php-fpm提供配置文件并修改几个配置选项如下

1
2
3
4
5
6
7
8
9
10
11
# cp /etc/php/php-fpm.conf{.default,}
# vim /etc/php/php-fpm.conf
listen = 172.16.25.69:9000    ----> 更改php-fpm的监听地址以便接受httpd发送来的请求
listen.owner = nobody      ----> 启用如下三项(去掉注释";")
listen.group = nobody
listen.mode = 0660
listen.allowed_clients = 172.16.25.67---> 允许httpd客户端发送请求
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35




5、为php-fpm提供服务脚本

1
2
3
4
5
6
7
8
9
# cp /usr/src/php-5.6.4/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
# service php-fpm start
Starting php-fpmdone
# ss -tnl
StateRecv-Q Send-Q   Local Address:Port      Peer Address:Port
LISTEN    0    128       *:38301            *:*   
LISTEN   0    128    172.16.25.69:9000            *:*




6、编译安装xcache为php提供加速机制

1
2
3
4
5
6
7
8
9
# tar xf /usr/src/xcache-3.2.0.tar.bz2 -C /usr/src/
# cd /usr/src/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
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make -j 4 && make install





五、使用phpMyadmin和wordpress测试
1、在主机A的httpd服务的配置文件做出如下修改

1
2
3
4
5
6
7
8
# vim /etc/httpd24/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#DocumentRoot "/usr/local/httpd24/htdocs"   ---> 注释掉此行
# Virtual hosts
Include /etc/httpd24/extra/httpd-vhosts.conf   ---> 找到此行去掉注释
Addtype application/x-httpd-php .php
Addtype application/x-httpd-php-source .phps




2、提供虚拟主机配置

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
# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/var/www/wordpress"
    ServerName www.wordpress.net
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.25.69:9000/var/www/wordpress/$1
      <Directory "/var/www/wordpress">
                Options None
                AllowOverride None
                Require all granted
      </Directory>
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot "/var/www/pma"
   ServerName www.pma.net
   ProxyRequests Off
   ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.25.69:9000/var/www/pma/$1
      <Directory "/var/www/pma">
                Options None
                AllowOverride None
                Require all granted
      </Directory>
</VirtualHost>

# mkdir /var/www/{wordpress,pma}
# httpd -t
Syntax OK   ---> 检查语法错误




3、重启httpd服务

1
# apachectl restart




4、在主机C提供对应的网页文件phpMyAdmin

1
2
3
4
5
6
7
# unzip /var/www/phpMyAdmin-4.4.14.1-all-languages.zip -d /var/www/
# mv /var/www/phpMyAdmin-4.4.14.1-all-languages /var/www/pma
# mv /var/www/pma/config.sample.inc.php /var/www/pma/config.inc.php
# openssl rand -base64 20   ----> 生产一段随机数
FDcPSg2TOTmOEJLGUwJZz3sdYgk=
$cfg['blowfish_secret'] = 'FDcPSg2TOTmOEJLGUwJZz3sdYgk'; /* YOU MUST FILL INTHIS FOR COOKIE AUTH! */
$cfg['Servers'][$i]['host'] = '172.16.25.68';   -----> 更改为mysql服务器地址




5、更改测试主机的hosts文件,本次测试是Linux主机,故在/etc/hosts文件添加如下内容

1
172.16.25.67www.pma.net




访问结果如下


注:此时若访问不了,请关闭三台服务器的防火墙即可;另外关于wordpress的安装配置使用可以参照我的另一篇博客LAMP架构之构建php为apache的模块(CentOS 7)
页: [1]
查看完整版本: LAMP架构之使用fpm机制编译安装