两种方式编译php

在LAMP架构中,请分别以php编译成httpd模块形式和php以fpm工作为独立守护进程的方式来支持httpd,列出详细的过程。

php编译成httpd模块形式:

以centos7.2环境安装httpd2.4.9,php5.4.26,通用二进制mariadb5.5.46,具体安装如下:

(1).编译安装httpd2.4.9(以prefork模式运行)

安装httpd-2.4,依赖于apr-1.4+, apr-util-1.4+, [apr-iconv],  apr: apache portableruntime

首先安装开发环境包组:Development Tools, Server Platform Development    开发程序包:pcre-devel

[[email protected] ~]# yum groupinstall 服务器平台开发    开发工具

[[email protected] dylan]# tar -xjvfapr-1.5.0.tar.bz2

[[email protected] dylan]# cd apr-1.5.0

[[email protected] apr-1.5.0]#./configure  --prefix=/usr/local/apr

[[email protected] apr-1.5.0]# make &&make install                                ###安装apr-1.5.0

[[email protected] dylan]# tar -xjvfapr-util-1.5.3.tar.bz2

[[email protected] dylan]# cd apr-util-1.5.3

[[email protected] apr-util-1.5.3]# ./configure  --prefix=/usr/local/apr-util  --with-apr=/usr/local/apr

[[email protected] apr-util-1.5.3]# make&& make install                              ###安装apr-util-1.5.3

###解压httpd

[[email protected] dylan]# tar -xjvfhttpd-2.4.9.tar.bz2

[[email protected] dylan]# cd httpd-2.4.9

###安装必备包

[[email protected] httpd-2.4.9]# yum installopenssl openssl-devel pcre pcre-devel -y

[[email protected] httpd-2.4.9]# ./configure--prefix=/usr/local/apache24 --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=prefork

[[email protected] httpd-2.4.9]# make&& make install                             ###安装httpd-2.4.9

###编译安装完成后把apachectl命令路径加入PATH中

[[email protected] httpd-2.4.9]# vim/etc/profile.d/httpd.sh

export PATH=/usr/local/apache24/bin:$PATH                                               ###添加环境变量

[[email protected] httpd-2.4.9]# ./etc/profile.d/httpd.sh

[[email protected] apache24]# ln -sv/usr/local/apache24/include/ /usr/include/httpd            ###输出httpd头文件至系统头文件路径/usr/include;

"/usr/include/httpd" ->"/usr/local/apache24/include/"

[[email protected] httpd-2.4.9]# apachectlstart

AH00558: httpd: Could not reliablydetermine the server's fully qualified domain name, usi

ng localhost.localdomain. Set the'ServerName' directive globally to suppress this message

[[email protected] httpd-2.4.9]# vim/etc/httpd24/httpd.conf                                      ###编辑配置文件

# If your host doesn't have a registeredDNS name, enter its IP address here.

#

#ServerName www.example.com:80

ServerName localhost:80                                                                                   ###添加ServerName即可

[[email protected] httpd-2.4.9]# apachectlrestart                                   ###重启服务

[[email protected] httpd-2.4.9]# ss -tnl              

测试


(2).通用二进制格式安装mariadb-5.5.46-linux-x86_64.tar.gz

[[email protected] ~]# groupadd -r -g 306mysql

[[email protected] ~]# useradd -r -g 306 -u306 mysql

[[email protected] dylan]# tar xfmariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/

[[email protected] dylan]# cd /usr/local/

[[email protected] local]# ln -svmariadb-5.5.46-linux-x86_64/ mysql

[[email protected] local]# cd mysql

[[email protected] mysql]# chown -R root.mysql./*

[[email protected] mysql]# mkdir -pv/mydata/data

mkdir: 已创建目录"/mydata"

mkdir: 已创建目录"/mydata/data"

[[email protected] mysql]# chown -Rmysql.mysql /mydata/data/

[[email protected] mysql]# mkdir /etc/mysql

[[email protected] mysql]# cp support-files/my-large.cnf/etc/mysql/my.cnf      ###复制配置文件

[[email protected] mysql]# vim/etc/mysql/my.cnf                                                    ###编辑配置文件

thread_concurrency = 8

datadir = /mydata/data

innodb_file_per_table = ON

skip_name_resolve = ON

[[email protected] mysql]# cpsupport-files/mysql.server /etc/rc.d/init.d/mysqld   ###复制服务启动配置文件

[[email protected] mysql]# ls -l/etc/rc.d/init.d/mysqld

-rwxr-xr-x. 1 root root 12196 6月  20 22:28 /etc/rc.d/init.d/mysqld

[[email protected] mysql]# chkconfig --addmysqld

[[email protected] mysql]#scripts/mysql_install_db  --user=mysql--datadir=/mydata/data         ###创建数据

[[email protected] mysql]# service mysqldstart

Starting MySQL. ERROR!                                                                          ###启动错误

[[email protected] mysql]# vim/etc/selinux/config

SELINUX=disabled

[[email protected]ost mysql]# getenforce

Enforcing

[[email protected] mysql]# setenforce 0

[[email protected] mysql]# getenforce

Permissive

[[email protected] rc.d]# vim /etc/my.cnf

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log                                    ###错误日志路径

pid-file=/var/run/mariadb/mariadb.pid

###查看/var/log/下并没有mariadb目录,于是有三种方法解决:

1.mv /etc/my.cnf  /etc/my.cnf.bak                                              ###使/etc/my.cnf失效

2.vim /etc/my.cnf  

#log-error=/var/log/mariadb/mariadb.log                                  ###注销掉

3.mkdir /var/log/mariadb                                                           ###创建mariadb目录

[[email protected] mysql]# mkdir/var/log/mariadb           ###此处使用第三种方法

[[email protected] mysql]# service mysqldstart

Starting MySQL... SUCCESS!                                                          ###成功启动

###修改环境变量

[[email protected] rc.d]# vim/etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

[[email protected] rc.d]# source/etc/profile.d/mysql.sh          ###重读此配置文件

###导出头文件

[[email protected] rc.d]# ln -sv/usr/local/mysql/include/ /usr/include/mysqld

"/usr/include/mysqld" ->"/usr/local/mysql/include/"

###导出库文件

[[email protected] ld.so.conf.d]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib

[[email protected] ld.so.conf.d]# ldconfig                                       ###系统重载入系统库

[[email protected] ld.so.conf.d]# ldconfig -p|grep mysql  ###查看库文件读取

         libmysqld.so.18(libc6,x86-64) => /usr/local/mysql/lib/libmysqld.so.18

         libmysqld.so(libc6,x86-64) => /usr/local/mysql/lib/libmysqld.so

         libmysqlclient.so.18(libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.18

         libmysqlclient.so.18(libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.18

         libmysqlclient.so(libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so    

###mysql安全加固

[[email protected] bin]# mysql_secure_installation                            ###运行此命令进行设置

[[email protected] mysql]# mysql -pxiaozhang

MariaDB [(none)]> grant all on *.* [email protected]"192.168.%.%" identified by "test";###授权test用户

MariaDB [(none)]> flush privileges;

 

(3).编译安装php-5.4.26.tar.bz2,编译为httpd模块

[[email protected] dylan]# yum installlibxml2-devel libmcrypt-devel bzip2-devel -y

[[email protected] dylan]#  tar xf php-5.4.26.tar.bz2

[[email protected] dylan]# cd php-5.4.26

[[email protected] 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-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib--with-libxml-dir=/usr --enable-xml --enable-sockets--with-apxs2=/usr/local/apache24/bin/apxs --with-mcrypt--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2

[[email protected] php-5.4.26]# make&& make install

[[email protected] php-5.4.26]# cpphp.ini-production /etc/php.ini           ###复制php配置文件

[[email protected] php-5.4.26]# cd/etc/httpd24

[[email protected] httpd24]# cphttpd.conf{,.backup}

###使httpd能够识别php动态资源并能够提交给httpd的php模块(引擎),需要编辑httpd的配置文件

[[email protected] httpd24]# vim httpd.conf

AddType application/x-compress .Z

AddType application/x-gzip .gz .tgz

AddType application/x-httpd-php .php                                ###添加此项,识别以php结尾的文件

<IfModule dir_module>

         DirectoryIndex  index.php index.html                                                                             ###DirectoryIndex添加index.php,可识别此类为主页

</IfModule>

[[email protected] httpd24]# vim /usr/local/apache24/htdocs/index.php

<?php

       phpinfo();

?>

[[email protected] httpd24]# apachectl -t

Syntax OK

[[email protected] httpd24]# apachectl restart

测试

 

php以fpm工作为独立守护进程的方式来支持httpd

安装httpd与mariadb与前面相同,此次重新编译安装php以fpm工作为独立守护进程的方式来支持httpd

[[email protected] dylan]# yum installlibxml2-devel libmcrypt-devel bzip2-devel -y

[[email protected] dylan]#  tar xf php-5.4.26.tar.bz2

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

###编译时使用 --enable-fpm

[[email protected] php-5.4.26]# make&& make install

[[email protected] php-5.4.26]# cp php.ini-production/etc/php.ini      ###复制php配置文件

[[email protected] php-5.4.26]# cp/usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

###为php-fpm提供配置文件

[[email protected] php-5.4.26]# vim/usr/local/php/etc/php-fpm.conf     ###配置fpm相关选项,并启用pid文件

pid = /usr/local/var/run/php-fpm.pid

###为php-fpm提供服务脚本,并将其添加至服务列表

[[email protected] php-5.4.26]# cp/tmp/dylan/php-5.4.26/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

[[email protected] php-5.4.26]# chmod +x/etc/rc.d/init.d/php-fpm                           ###增加执行权限

[[email protected] php-5.4.26]# chkconfig--add php-fpm                                      ###添加服务

[[email protected] php-5.4.26]# chkconfig  php-fpm on                                                 ###服务启动

[[email protected] /]# service php-fpm start

[[email protected] /]# ps aux |grep php-fpm                                                                       ###验证是否启动

root    20948  0.0  0.3 158924 3876 ?        Ss   21:52  0:00 php-fpm: master process (

/usr/local/php/etc/php-fpm.conf)nobody   20949 0.0  0.4 161008  4780 ?       S    21:52   0:00 php-fpm: pool www

nobody  20950  0.0  0.5 161008 5648 ?        S    21:52  0:00 php-fpm: pool www

root    21196  0.0  0.0 112664  976 pts/0    R+   22:14  0:00 grep --color=auto php-fp

[email protected] /]# netstat -tnlp |grepphp-fpm                                                   ###查看监听端口

tcp       0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      20948/php-

fpm: mast

配置httpd

[[email protected] /]# vim/etc/httpd24/httpd.conf

LoadModule proxy_module modules/mod_proxy.so                            ###启用这两个模块

LoadModule proxy_fcgi_modulemodules/mod_proxy_fcgi.so

AddType application/x-httpd-php .php                                                            ###增加识别php

AddType application/x-httpd-php-source.phps

DirectoryIndex index.php  index.html                                                           ###支持php格式主页面

Include /etc/httpd24/extra/httpd-fcgi.conf                                                   ###增加配置文件

[[email protected] /]# vim/etc/httpd24/extra/httpd-fcgi.conf                                    

DirectoryIndex index.php

ProxyRequests Off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache24/htdocs/$1  

[[email protected] /]# httpd -t

Syntax OK

[[email protected] /]# apachectl restart

[[email protected] /]# service php-fpm restart

测试

[[email protected] /]# vim/usr/local/apache24/htdocs/index.php

<?php

       phpinfo();

?>


测试与数据库连接

[[email protected] /]# vim/usr/local/apache24/htdocs/index.php

<?php

 

       $conn = mysql_connect( '192.168.0.104','test','test');

       if ($conn)

                echo "OK";

       else

                echo "Failure";

?>


[[email protected] /]# service mysqld stop                                              ###关闭mysql测试

Shutting down MySQL...... SUCCESS!


测试成功。