在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
[root@localhost ~]# yum groupinstall 服务器平台开发 开发工具
[root@localhost dylan]# tar -xjvfapr-1.5.0.tar.bz2
[root@localhost dylan]# cd apr-1.5.0
[root@localhost apr-1.5.0]#./configure --prefix=/usr/local/apr
[root@localhost apr-1.5.0]# make &&make install ###安装apr-1.5.0
[root@localhost dylan]# tar -xjvfapr-util-1.5.3.tar.bz2
[root@localhost dylan]# cd apr-util-1.5.3
[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.5.3]# make&& make install ###安装apr-util-1.5.3
###解压httpd
[root@localhost dylan]# tar -xjvfhttpd-2.4.9.tar.bz2
[root@localhost dylan]# cd httpd-2.4.9
###安装必备包
[root@localhost httpd-2.4.9]# yum installopenssl openssl-devel pcre pcre-devel -y
[root@localhost 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
[root@localhost httpd-2.4.9]# make&& make install ###安装httpd-2.4.9
###编译安装完成后把apachectl命令路径加入PATH中
[root@localhost httpd-2.4.9]# vim/etc/profile.d/httpd.sh
export PATH=/usr/local/apache24/bin:$PATH ###添加环境变量
[root@localhost httpd-2.4.9]# ./etc/profile.d/httpd.sh
[root@localhost apache24]# ln -sv/usr/local/apache24/include/ /usr/include/httpd ###输出httpd头文件至系统头文件路径/usr/include;
"/usr/include/httpd" ->"/usr/local/apache24/include/"
[root@localhost 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
[root@localhost 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即可
[root@localhost httpd-2.4.9]# apachectlrestart ###重启服务
[root@localhost httpd-2.4.9]# ss -tnl
测试
(2).通用二进制格式安装mariadb-5.5.46-linux-x86_64.tar.gz
[root@localhost ~]# groupadd -r -g 306mysql
[root@localhost ~]# useradd -r -g 306 -u306 mysql
[root@localhost dylan]# tar xfmariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/
[root@localhost dylan]# cd /usr/local/
[root@localhost local]# ln -svmariadb-5.5.46-linux-x86_64/ mysql
[root@localhost local]# cd mysql
[root@localhost mysql]# chown -R root.mysql./*
[root@localhost mysql]# mkdir -pv/mydata/data
mkdir: 已创建目录"/mydata"
mkdir: 已创建目录"/mydata/data"
[root@localhost mysql]# chown -Rmysql.mysql /mydata/data/
[root@localhost mysql]# mkdir /etc/mysql
[root@localhost mysql]# cp support-files/my-large.cnf/etc/mysql/my.cnf ###复制配置文件
[root@localhost mysql]# vim/etc/mysql/my.cnf ###编辑配置文件
thread_concurrency = 8
datadir = /mydata/data
innodb_file_per_table = ON
skip_name_resolve = ON
[root@localhost mysql]# cpsupport-files/mysql.server /etc/rc.d/init.d/mysqld ###复制服务启动配置文件
[root@localhost 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
[root@localhost mysql]# chkconfig --addmysqld
[root@localhost mysql]#scripts/mysql_install_db --user=mysql--datadir=/mydata/data ###创建数据
[root@localhost mysql]# service mysqldstart
Starting MySQL. ERROR! ###启动错误
[root@localhost mysql]# vim/etc/selinux/config
SELINUX=disabled
[root@localhost mysql]# getenforce
Enforcing
[root@localhost mysql]# setenforce 0
[root@localhost mysql]# getenforce
Permissive
[root@localhost 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目录
[root@localhost mysql]# mkdir/var/log/mariadb ###此处使用第三种方法
[root@localhost mysql]# service mysqldstart
Starting MySQL... SUCCESS! ###成功启动
###修改环境变量
[root@localhost rc.d]# vim/etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost rc.d]# source/etc/profile.d/mysql.sh ###重读此配置文件
###导出头文件
[root@localhost rc.d]# ln -sv/usr/local/mysql/include/ /usr/include/mysqld
"/usr/include/mysqld" ->"/usr/local/mysql/include/"
###导出库文件
[root@localhost ld.so.conf.d]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost ld.so.conf.d]# ldconfig ###系统重载入系统库
[root@localhost 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安全加固
[root@localhost bin]# mysql_secure_installation ###运行此命令进行设置
[root@localhost mysql]# mysql -pxiaozhang
MariaDB [(none)]> grant all on *.* totest@"192.168.%.%" identified by "test";###授权test用户
MariaDB [(none)]> flush privileges;
(3).编译安装php-5.4.26.tar.bz2,编译为httpd模块
[root@localhost dylan]# yum installlibxml2-devel libmcrypt-devel bzip2-devel -y
[root@localhost dylan]# tar xf php-5.4.26.tar.bz2
[root@localhost dylan]# cd php-5.4.26
[root@localhost 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
[root@localhost php-5.4.26]# make&& make install
[root@localhost php-5.4.26]# cpphp.ini-production /etc/php.ini ###复制php配置文件
[root@localhost php-5.4.26]# cd/etc/httpd24
[root@localhost httpd24]# cphttpd.conf{,.backup}
###使httpd能够识别php动态资源并能够提交给httpd的php模块(引擎),需要编辑httpd的配置文件
[root@localhost httpd24]# vim httpd.conf
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php ###添加此项,识别以php结尾的文件
DirectoryIndex index.php index.html ###DirectoryIndex添加index.php,可识别此类为主页
[root@localhost httpd24]# vim /usr/local/apache24/htdocs/index.php
phpinfo();
?>
[root@localhost httpd24]# apachectl -t
Syntax OK
[root@localhost httpd24]# apachectl restart
测试
php以fpm工作为独立守护进程的方式来支持httpd
安装httpd与mariadb与前面相同,此次重新编译安装php以fpm工作为独立守护进程的方式来支持httpd
[root@localhost dylan]# yum installlibxml2-devel libmcrypt-devel bzip2-devel -y
[root@localhost dylan]# tar xf php-5.4.26.tar.bz2
[root@localhost 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
[root@localhost php-5.4.26]# make&& make install
[root@localhost php-5.4.26]# cp php.ini-production/etc/php.ini ###复制php配置文件
[root@localhost php-5.4.26]# cp/usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
###为php-fpm提供配置文件
[root@localhost php-5.4.26]# vim/usr/local/php/etc/php-fpm.conf ###配置fpm相关选项,并启用pid文件
pid = /usr/local/var/run/php-fpm.pid
###为php-fpm提供服务脚本,并将其添加至服务列表
[root@localhost php-5.4.26]# cp/tmp/dylan/php-5.4.26/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@localhost php-5.4.26]# chmod +x/etc/rc.d/init.d/php-fpm ###增加执行权限
[root@localhost php-5.4.26]# chkconfig--add php-fpm ###添加服务
[root@localhost php-5.4.26]# chkconfig php-fpm on ###服务启动
[root@localhost /]# service php-fpm start
[root@localhost /]# 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
root@localhost /]# netstat -tnlp |grepphp-fpm ###查看监听端口
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 20948/php-
fpm: mast
配置httpd
[root@localhost /]# 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 ###增加配置文件
[root@localhost /]# 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
[root@localhost /]# httpd -t
Syntax OK
[root@localhost /]# apachectl restart
[root@localhost /]# service php-fpm restart
测试
[root@localhost /]# vim/usr/local/apache24/htdocs/index.php
phpinfo();
?>
测试与数据库连接
[root@localhost /]# vim/usr/local/apache24/htdocs/index.php
$conn = mysql_connect( '192.168.0.104','test','test');
if ($conn)
echo "OK";
else
echo "Failure";
?>
[root@localhost /]# service mysqld stop ###关闭mysql测试
Shutting down MySQL...... SUCCESS!
测试成功。