设为首页 收藏本站
查看: 3790|回复: 0

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

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-25 08:47:02 | 显示全部楼层 |阅读模式
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
[iyunv@A ~]# yum groupinstall 'Development Tools' 'Server Platform Development'
[iyunv@A ~]# yum install pcre-devel
[iyunv@A ~]# 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
[iyunv@A ~]# tar xf /usr/src/apr-1.5.0.tar.bz2 -C /usr/src/
[iyunv@A ~]# cd /usr/src/apr-1.5.0
[iyunv@A apr-1.5.0]# ./configure --prefix=/usr/local/apr
[iyunv@A apr-1.5.0]# make && make install

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

# 编译安装httpd
[iyunv@A ~]# tar xf /usr/src/httpd-2.4.10.tar.bz2 -C /usr/local
[iyunv@A ~]# cd /usr/local/httpd-2.4.10/
[iyunv@A 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

[iyunv@A httpd-2.4.10]# 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写入其路径
[iyunv@A ~]# touch /etc/profile.d/httpd24.sh
[iyunv@A ~]# vim /etc/profile.d/httpd24.sh
[iyunv@A ~]# cat /etc/profile.d/httpd24.sh
export PATH=/usr/local/httpd24/bin:$PATH
[iyunv@A ~]# source /etc/profile.d/httpd24.sh

# 配置头文件
[iyunv@A ~]# ln -sv /usr/local/httpd24/include/ /usr/include/httpd
`/usr/include/httpd' -> `/usr/local/httpd24/include/'

# 启用mpm机制
[iyunv@A ~]# 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
[iyunv@A ~]# apachectl start
[iyunv@A ~]# ss -tnl
State  Recv-Q Send-Q    Local Address:Port     Peer Address:Port
LISTEN   0   128        :::111           :::*     
LISTEN   0   128         *:111            *:*     
LISTEN   0   128        :::80            :::*



wKiom1ag5iqiG-qBAABPrGKsViM787.jpg
三、使用通用二进制包编译安装mariadb
1、为mariadb提供一个数据目录
1
2
[iyunv@B ~]# mkdir -pv /mydata/data
mkdir: created directory `/mydata/data'




2、解压对应包并进行如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@B ~]# tar xf /usr/src/mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local
[iyunv@B ~]# useradd -r mysql    ----> 添加系统用户mysql
[iyunv@B ~]# 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/'
[iyunv@B ~]# cd /usr/local/mysql
[iyunv@B mysql]# chown -R root.mysql ./*  ---> 更改目录下文件的属主和属组

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



3、提供配置文件并更改配置文件如下,启动服务
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@B mysql]# cp support-files/my-large.cnf /etc/my.cnf
[iyunv@B ~]# vim /etc/my.cnf
[mysqld]  ----> 在mysqld配置段添加如下三行内容
datadir = /mydata/data
innodb_file_per_table = ON
skip_name_resolve = ON
[iyunv@B ~]# service mysqld start
Starting MySQL                 [确定]
# 查看3306端口是否启用
[iyunv@B ~]# 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
[iyunv@B ~]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH   ---> 写入此行
[iyunv@B ~]# source /etc/profile.d/mysql.sh

[iyunv@B ~]# 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/n] 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/n] 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/n] 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/n] 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/n] 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
[iyunv@C ~]# yum groupinstall 'Development Tools' 'Server Platform Development'
[iyunv@C ~]# yum install libxml2-devel openssl-devel libmcrypt-devel
[iyunv@C ~]# 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
[iyunv@C ~]# tar xf /usr/src/php-5.6.4.tar.xz -C /usr/src/
[iyunv@C ~]# cd /usr/src/php-5.6.4/
[iyunv@C 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
[iyunv@C ~]# make -j 4 && make intall



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

3、为php提供配置文件
1
[iyunv@C ~]# 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
[iyunv@C ~]# cp /etc/php/php-fpm.conf{.default,}
[iyunv@C ~]# 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
[iyunv@C ~]# cp /usr/src/php-5.6.4/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[iyunv@C ~]# chmod +x /etc/rc.d/init.d/php-fpm
[iyunv@C ~]# chkconfig --add php-fpm
[iyunv@C ~]# service php-fpm start
Starting php-fpm  done
[iyunv@C ~]# ss -tnl
State  Recv-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
[iyunv@C ~]# tar xf /usr/src/xcache-3.2.0.tar.bz2 -C /usr/src/
[iyunv@C ~]# cd /usr/src/xcache-3.2.0
[iyunv@C 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
[iyunv@C xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[iyunv@C xcache-3.2.0]# make -j 4 && make install




五、使用phpMyadmin和wordpress测试
1、在主机A的httpd服务的配置文件做出如下修改
1
2
3
4
5
6
7
8
[iyunv@A ~]# 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
[iyunv@A ~]# 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>

[iyunv@A ~]# mkdir /var/www/{wordpress,pma}
[iyunv@A ~]# httpd -t
Syntax OK   ---> 检查语法错误



3、重启httpd服务
1
[iyunv@A ~]# apachectl restart



4、在主机C提供对应的网页文件phpMyAdmin
1
2
3
4
5
6
7
[iyunv@C ~]# unzip /var/www/phpMyAdmin-4.4.14.1-all-languages.zip -d /var/www/
[iyunv@C ~]# mv /var/www/phpMyAdmin-4.4.14.1-all-languages /var/www/pma
[iyunv@C ~]# mv /var/www/pma/config.sample.inc.php /var/www/pma/config.inc.php
[iyunv@C pma]# 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.67  www.pma.net



访问结果如下
wKiom1ako1_ggv-9AADuvahwHlA141.jpg
wKioL1ako6fSFR2aAAHpQe1jY8A666.jpg

注:此时若访问不了,请关闭三台服务器的防火墙即可;另外关于wordpress的安装配置使用可以参照我的另一篇博客LAMP架构之构建php为apache的模块(CentOS 7)


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-169028-1-1.html 上篇帖子: LAMP编译安装,并搭建discuz 下篇帖子: LAMP架构之以模块方式让php和httpd搭档工作 linux 根目录 空间
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表